pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: roji@roji.org
Subject: BUG #19778: Empty tsquery cannot roundtrip through binary input
Date: Tue, 20 Oct 2026 19:24:37 +0000
Message-ID: <19778-1befc1fba593ab1b@postgresql.org> (raw)

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;
}








view thread (3+ messages)  latest in thread

Message-ID: <19778-1befc1fba593ab1b@postgresql.org>
Permalink:  ../19778-1befc1fba593ab1b@postgresql.org/
Also on:    postgresql.org/message-id/19778-1befc1fba593ab1b@postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: noreply@postgresql.org, roji@roji.org
  Subject: Re: BUG #19778: Empty tsquery cannot roundtrip through binary input
  In-Reply-To: <19778-1befc1fba593ab1b@postgresql.org>
  References: <19778-1befc1fba593ab1b@postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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