Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xDjXS-00000000fy0-1Vf4 for pgsql-bugs@arkaria.postgresql.org; Mon, 05 Oct 2026 14:19:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xDjXR-0000000B584-0MLw for pgsql-bugs@arkaria.postgresql.org; Mon, 05 Oct 2026 14:19:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xDErQ-000000044X1-2yT9 for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 05:34:08 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xDErO-00000000JXN-1GUq for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 05:34:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=Ptx/IXdXMBKOxpyHrzDBEd5Q88krJzDQTxZE9F3jdGQ=; b=L4m/OFd0o3i+NNRjKEXrECuj8u 25dyucvfzs7GL1LlQ44IbR5QPil0ngiuO3ToVrZlanMbY+vncStoNc6OoB/A3RgzluhvARqSN8YVt w25CdJ4/iIwZQ26PKnynHW282pZXUrBeKXwk9uqI3FRDdQU+UIKj3GummPp/h5gtm06X/fmSGtsEk NQwjwlocjg626tnoR+ZMCYo5hoONT0gmWVbmzfzwoq/9LctDw9YdGKSbccr1A8EVZbNbKKQC/B9BC /fa+teDDyUmG1cq7XCo37n0aSvotZ5ve4bbTCbr08Jjxucjh6RwFXiaPWSCrJORA9EFZBpAl39kZV gVDxKJ3Q==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xDErM-00000000v1P-1LxW for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 05:34:04 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xDErL-00000002TCV-13o1 for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 05:34:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19750: tsquery output omits parentheses, so the text reparses to a different value To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: kehan5800@gmail.com Reply-To: kehan5800@gmail.com, pgsql-bugs@lists.postgresql.org Date: Sun, 04 Oct 2026 05:33:50 +0000 Message-ID: <19750-d4e6f6e6e93000ba@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19750 Logged by: Ke Email address: kehan5800@gmail.com PostgreSQL version: 18.6 Operating system: Ubuntu 22.04.2 x86_64 Description: =20 tsquery's output function, infix() in src/backend/utils/adt/tsquery.c, parenthesises a binary operator's operand only when the operand's operator has lower priority than the parent (or, for <->, when it is the right operand). When the same operator, & or |, is nested on the right, no parentheses are written. The parser is left-associative, so that text reads back as a different tree, and tsquery equality compares trees: =3D# select q, q::tsquery::text as printed, (q::tsquery::text::tsquery =3D q::tsquery)::int as round_trips from (values ('a & b & c'), ('a & (b & c)'), ('(a & b) & c'), ('a | (b | c)'), ('a & (b | c)'), ('a <-> (b <-> c)')) v(q); q | printed | round_trips -----------------+-------------------------+------------- a & b & c | 'a' & 'b' & 'c' | 1 a & (b & c) | 'a' & 'b' & 'c' | 0 (a & b) & c | 'a' & 'b' & 'c' | 1 a | (b | c) | 'a' | 'b' | 'c' | 0 a & (b | c) | 'a' & ( 'b' | 'c' ) | 1 a <-> (b <-> c) | 'a' <-> ( 'b' <-> 'c' ) | 1 (6 rows) Two different values print as 'a' & 'b' & 'c': =3D# select count(distinct q) from (values ('a & (b & c)'::tsquery), ('a & b & c'::tsquery)) v(q); count ------- 2 =3D# select count(distinct q::text) from (values ('a & (b & c)'::tsquery), ('a & b & c'::tsquery)) v(q); count ------- 1 You get this shape without writing parentheses. The && and || operators, which applications use to build a query from parts, produce it: =3D# select ('a'::tsquery && ('b & c')::tsquery)::text as printed, (('a'::tsquery && ('b & c')::tsquery)::text::tsquery =3D ('a'::tsquery && ('b & c')::tsquery))::int as round_tri= ps; printed | round_trips -----------------+------------- 'a' & 'b' & 'c' | 0 A text dump/restore, which goes through tsqueryout/tsqueryin, therefore changes the stored value: =3D# create table tq (q tsquery); =3D# insert into tq values ('a'::tsquery && ('b & c')::tsquery); =3D# select (select q from tq) =3D (select q::text::tsquery from tq); ?column? ---------- f Expected: either the output is 'a' & ( 'b' & 'c' ), which reads back to the same value (as is already done for <->), or the two values compare equal. round_trips should be 1 in every row above. The two trees match the same documents, since & and | are associative, so @@ results do not change. What changes is the value itself. Equality, ordering, GROUP BY, DISTINCT and a unique index on a tsquery column all treat the restored value differently from the original. Suggested fix: in infix(), also parenthesise the right operand of & and | when it has the same priority as the parent. That is already done for OP_PHRASE through the rightPhraseOp argument ("phrase operator depends on order"). Generalising that, so a right operand is parenthesised whenever its priority is <=3D the parent's (not only for OP_PHRASE), would do it. The cost is extra parentheses in the output for right-nested & and | only; left-nested and flat chains print the same as today. Reproduced on master 20devel @ 45277ca0d1cb, 18.6 and 17.11.