pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #19778: Empty tsquery cannot roundtrip through binary input
3+ messages / 3 participants
[nested] [flat]

Commits from this thread:
  2026-10-22 0d0d2a13e8 6c4172b34d e05b7e2a45 774c47de1b 187051f85c 264c441c4f 3c5a4a867f  Accept empty tsquery in binary input.
* BUG #19778: Empty tsquery cannot roundtrip through binary input
@ 2026-10-20 19:24  PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 3+ messages in thread

From: PG Bug reporting form @ 2026-10-20 19:24 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: roji@roji.org

The following bug has been logged on the website:

Bug reference:      19778
Logged by:          Shay Rojansky
Email address:      roji@roji.org
PostgreSQL version: 18.4
Operating system:   macOS Tahoe 26.7.1
Description:        

It appears that an empty binary tsquery cannot be written to PostgreSQL.
Writing a non-empty binary tsquery works, as well as writing an empty text
tsquery.

Minimal repro:

#include <libpq-fe.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void require_row(PGresult *result)
{
    if (PQresultStatus(result) != PGRES_TUPLES_OK || PQntuples(result) != 1)
    {
        fprintf(stderr, "Unexpected result: %s",
PQresultErrorMessage(result));
        exit(1);
    }
}

int main(int argc, char **argv)
{
    PGconn *conn = PQconnectdb(argc > 1 ? argv[1] : "dbname=postgres");
    if (PQstatus(conn) != CONNECTION_OK)
    {
        fprintf(stderr, "%s", PQerrorMessage(conn));
        PQfinish(conn);
        return 1;
    }
    printf("libpq=%d, PostgreSQL=%d\n", PQlibVersion(),
PQserverVersion(conn));

    /* Obtain both binary values from PostgreSQL, without constructing
either. */
    PGresult *values = PQexecParams(
        conn, "SELECT ''::tsquery, 'foo'::tsquery",
        0, NULL, NULL, NULL, NULL, 1);
    require_row(values);
    const Oid types[] = {PQftype(values, 0)};
    const char *params[] = {PQgetvalue(values, 0, 1)};
    int lengths[] = {PQgetlength(values, 0, 1)};
    int formats[] = {1};

    PGresult *result = PQexecParams(conn, "SELECT $1", 1, types, params,
lengths, formats, 0);
    require_row(result);
    if (PQgetisnull(result, 0, 0) || strcmp(PQgetvalue(result, 0, 0),
"'foo'") != 0)
        return 1;
    printf("Binary non-empty tsquery roundtrip: %s\n", PQgetvalue(result, 0,
0));
    PQclear(result);

    params[0] = "";
    lengths[0] = 0;
    formats[0] = 0;
    result = PQexecParams(conn, "SELECT $1", 1, types, params, lengths,
formats, 0);
    require_row(result);
    if (PQgetisnull(result, 0, 0) || PQgetlength(result, 0, 0) != 0)
        return 1;
    puts("Text empty tsquery roundtrip: OK");
    PQclear(result);

    params[0] = PQgetvalue(values, 0, 0);
    lengths[0] = PQgetlength(values, 0, 0);
    formats[0] = 1;
    printf("Server-produced empty tsquery: type=%u, format=%d, length=%d,
hex=",
           types[0], PQfformat(values, 0), lengths[0]);
    for (int i = 0; i < lengths[0]; i++)
        printf("%02x", (unsigned char)params[0][i]);
    putchar('\n');

    /* Send the server's bytes back unchanged, with the server's type OID.
*/
    result = PQexecParams(conn, "SELECT $1", 1, types, params, lengths,
formats, 0);
    const char *state = PQresultErrorField(result, PG_DIAG_SQLSTATE);
    const char *message = PQresultErrorField(result,
PG_DIAG_MESSAGE_PRIMARY);
    const char *routine = PQresultErrorField(result,
PG_DIAG_SOURCE_FUNCTION);
    int reproduced = PQresultStatus(result) == PGRES_FATAL_ERROR
        && state && strcmp(state, "XX000") == 0
        && message && strcmp(message, "malformed tsquery: operand not
found") == 0
        && routine && strcmp(routine, "findoprnd_recurse") == 0;
    if (reproduced)
        printf("REPRODUCED: %s: %s (%s)\n", state, message, routine);
    else
        fprintf(stderr, "Expected server error was not reproduced: %s\n",
PQresultErrorMessage(result));

    PQclear(result);
    PQclear(values);
    PQfinish(conn);
    return reproduced ? 0 : 1;
}








^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: BUG #19778: Empty tsquery cannot roundtrip through binary input
@ 2026-10-22 16:52  Ayush Tiwari <ayushtiwari.slg01@gmail.com>
  parent: PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 3+ messages in thread

From: Ayush Tiwari @ 2026-10-22 16:52 UTC (permalink / raw)
  To: roji@roji.org; pgsql-bugs@lists.postgresql.org

Hi,

On Thu, 22 Oct 2026 at 11:58, PG Bug reporting form
<noreply@postgresql.org> wrote:
>
> The following bug has been logged on the website:
>
> Bug reference:      19778
> Logged by:          Shay Rojansky
> Email address:      roji@roji.org
> PostgreSQL version: 18.4
> Operating system:   macOS Tahoe 26.7.1
> Description:
>
> It appears that an empty binary tsquery cannot be written to PostgreSQL.
> Writing a non-empty binary tsquery works, as well as writing an empty text
> tsquery.
>
> Minimal repro:
>
> #include <libpq-fe.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> static void require_row(PGresult *result)
> {
>     if (PQresultStatus(result) != PGRES_TUPLES_OK || PQntuples(result) != 1)
>     {
>         fprintf(stderr, "Unexpected result: %s",
> PQresultErrorMessage(result));
>         exit(1);
>     }
> }
>
> int main(int argc, char **argv)
> {
>     PGconn *conn = PQconnectdb(argc > 1 ? argv[1] : "dbname=postgres");
>     if (PQstatus(conn) != CONNECTION_OK)
>     {
>         fprintf(stderr, "%s", PQerrorMessage(conn));
>         PQfinish(conn);
>         return 1;
>     }
>     printf("libpq=%d, PostgreSQL=%d\n", PQlibVersion(),
> PQserverVersion(conn));
>
>     /* Obtain both binary values from PostgreSQL, without constructing
> either. */
>     PGresult *values = PQexecParams(
>         conn, "SELECT ''::tsquery, 'foo'::tsquery",
>         0, NULL, NULL, NULL, NULL, 1);
>     require_row(values);
>     const Oid types[] = {PQftype(values, 0)};
>     const char *params[] = {PQgetvalue(values, 0, 1)};
>     int lengths[] = {PQgetlength(values, 0, 1)};
>     int formats[] = {1};
>
>     PGresult *result = PQexecParams(conn, "SELECT $1", 1, types, params,
> lengths, formats, 0);
>     require_row(result);
>     if (PQgetisnull(result, 0, 0) || strcmp(PQgetvalue(result, 0, 0),
> "'foo'") != 0)
>         return 1;
>     printf("Binary non-empty tsquery roundtrip: %s\n", PQgetvalue(result, 0,
> 0));
>     PQclear(result);
>
>     params[0] = "";
>     lengths[0] = 0;
>     formats[0] = 0;
>     result = PQexecParams(conn, "SELECT $1", 1, types, params, lengths,
> formats, 0);
>     require_row(result);
>     if (PQgetisnull(result, 0, 0) || PQgetlength(result, 0, 0) != 0)
>         return 1;
>     puts("Text empty tsquery roundtrip: OK");
>     PQclear(result);
>
>     params[0] = PQgetvalue(values, 0, 0);
>     lengths[0] = PQgetlength(values, 0, 0);
>     formats[0] = 1;
>     printf("Server-produced empty tsquery: type=%u, format=%d, length=%d,
> hex=",
>            types[0], PQfformat(values, 0), lengths[0]);
>     for (int i = 0; i < lengths[0]; i++)
>         printf("%02x", (unsigned char)params[0][i]);
>     putchar('\n');
>
>     /* Send the server's bytes back unchanged, with the server's type OID.
> */
>     result = PQexecParams(conn, "SELECT $1", 1, types, params, lengths,
> formats, 0);
>     const char *state = PQresultErrorField(result, PG_DIAG_SQLSTATE);
>     const char *message = PQresultErrorField(result,
> PG_DIAG_MESSAGE_PRIMARY);
>     const char *routine = PQresultErrorField(result,
> PG_DIAG_SOURCE_FUNCTION);
>     int reproduced = PQresultStatus(result) == PGRES_FATAL_ERROR
>         && state && strcmp(state, "XX000") == 0
>         && message && strcmp(message, "malformed tsquery: operand not
> found") == 0
>         && routine && strcmp(routine, "findoprnd_recurse") == 0;
>     if (reproduced)
>         printf("REPRODUCED: %s: %s (%s)\n", state, message, routine);
>     else
>         fprintf(stderr, "Expected server error was not reproduced: %s\n",
> PQresultErrorMessage(result));
>
>     PQclear(result);
>     PQclear(values);
>     PQfinish(conn);
>     return reproduced ? 0 : 1;
> }

Thanks for the report, I can reproduce this on master too.

tsqueryrecv() calls findoprnd() even for an empty query, and that
fails with "malformed tsquery: operand not found". The text input
doesn't have this problem since parse_tsquery() returns early for an
empty query.

Attached is a patch that skips findoprnd() when size is 0.

IIUC, this goes back to all supported branches, and logical
replication with binary = true would hit it too. Which is why I'm
somewhat in favour of backpatching for this.

Regards,
Ayush

Attachments:

v1-0001-Accept-empty-tsquery-in-binary-input.patch (application/octet-stream, 1.4K) download From 06237e0bcc9bebf5c5e12e1eb33584ab94b5540b Mon Sep 17 00:00:00 2001 From: Ayush Tiwari <ayushtiwari.slg01@gmail.com> Date: Thu, 22 Oct 2026 22:10:20 +0530 Subject: [PATCH v1] Accept empty tsquery in binary input tsqueryrecv() calls findoprnd() even when the input has no query items, so it rejects the binary representation of an empty tsquery, which tsquerysend() produces. This breaks, for example, binary COPY of a table containing an empty tsquery. Skip the operand walk for an empty query, as parse_tsquery() does. Reported-by: Shay Rojansky <roji@roji.org> Author: Ayush Tiwari <ayushtiwari.slg01@gmail.com> Bug: #19778 Discussion: https://postgr.es/m/19778-1befc1fba593ab1b@postgresql.org --- src/backend/utils/adt/tsquery.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/backend/utils/adt/tsquery.c b/src/backend/utils/adt/tsquery.c index e4eb7cf5d43..228fa6276be 100644 --- a/src/backend/utils/adt/tsquery.c +++ b/src/backend/utils/adt/tsquery.c @@ -1283,10 +1283,13 @@ tsqueryrecv(PG_FUNCTION_ARGS) * Fill in the left-pointers. Checks that the tree is well-formed as a * side-effect. */ - findoprnd(item, size, &needcleanup); + if (size > 0) + { + findoprnd(item, size, &needcleanup); - /* Can't have found any QI_VALSTOP nodes */ - Assert(!needcleanup); + /* Can't have found any QI_VALSTOP nodes */ + Assert(!needcleanup); + } /* Copy operands to output struct */ for (uint32 i = 0; i < size; i++)
^ permalink raw reply [nested|flat] 3+ messages in thread

* Re: BUG #19778: Empty tsquery cannot roundtrip through binary input
@ 2026-10-22 17:34  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2026-10-22 17:34 UTC (permalink / raw)
  To: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; +Cc: roji@roji.org; pgsql-bugs@lists.postgresql.org

Ayush Tiwari <ayushtiwari.slg01@gmail.com> writes:
> On Thu, 22 Oct 2026 at 11:58, PG Bug reporting form
> <noreply@postgresql.org> wrote:
>> It appears that an empty binary tsquery cannot be written to PostgreSQL.

> Thanks for the report, I can reproduce this on master too.

Yeah, you can duplicate it without extra moving parts using
binary COPY out and then in.

> tsqueryrecv() calls findoprnd() even for an empty query, and that
> fails with "malformed tsquery: operand not found". The text input
> doesn't have this problem since parse_tsquery() returns early for an
> empty query.
> Attached is a patch that skips findoprnd() when size is 0.

+1, will push.

			regards, tom lane






^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2026-10-22 17:34 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-20 19:24 BUG #19778: Empty tsquery cannot roundtrip through binary input PG Bug reporting form <noreply@postgresql.org>
2026-10-22 16:52 ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-10-22 17:34   ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox