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-00000000fxz-1U3f 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-0Jif 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 1xDEdr-000000043CP-3FpG for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 05:20:07 +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 1xDEdp-00000000JQs-31dD for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 05:20:07 +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=5EFfeIGCySWRivuKpfn4vahDpXTlwf19dOeXx/KFjzc=; b=U0w56QcGg5KkMS1LWIx/+PqS6B Ep1MX7EXuBIFimrdIeHGMNr/pzaxZp2cMNOCJQ1ZMskv2XgglgWS7wbfPZdOMD50XE8jhesoq1vyu JCRTFf9zEwnR07wQ9h+LbKNF/XvRi+nz1ajGH7cKmGDUxgbcETjzQxbrLfo2pVCjOqJbVNnFbv19l CY7DTQ6hB1db2tgK6T6OBS/+rUEm24rdmoCJNkRMCQxzrTj+TfWJBr1n5yAN+zNRugBvDO4LtarIE Jo4kalVbWyIWVHHRrXdHtC8TMFJge4XAAoIj2bgKUxNwLk0yVPRKxkJpGadMvMuXyvKH8iKXswktu bg50RX2g==; 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 1xDEdo-00000000uk1-1e0V for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 05:20: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 1xDEdn-00000002SDL-1Eev for pgsql-bugs@lists.postgresql.org; Sun, 04 Oct 2026 05:20:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19748: tsvectorrecv accepts duplicate lexemes and position 0 that tsvectorin rejects 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:19:27 +0000 Message-ID: <19748-6075f0508fa1ffc0@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: 19748 Logged by: Ke Email address: kehan5800@gmail.com PostgreSQL version: 18.6 Operating system: Ubuntu 22.04.2 x86_64 Description: =20 tsvectorrecv() (src/backend/utils/adt/tsvector.c:446) has this comment (tsvector.c:552-556): * Enforce that datalen is still within MAXSTRPOS, ie the last lexeme * didn't go past that. We could allow that, since no "pos" field * overflowed, but tsvectorrecv shouldn't accept values that other * tsvector-constructing routines wouldn't. Two checks are still missing relative to tsvectorin(): 1. Duplicate lexemes are not merged. tsvectorin() runs uniqueentry() (tsvector.c:265). tsvectorrecv() detects "compareentry(...) <=3D 0" (tsvector.c:514-517), but only sorts (tsvector.c:563); equal entries stay. The result is a tsvector with repeated lexemes, which no other constructor produces, and which silently changes on a text round trip. 2. The first position is never checked. tsvectorin() rejects position 0 ("wrong position info in tsvector", tsvector_parser.c:334); the receive loop only checks that positions ascend for j > 0 (tsvector.c:540-545), so a leading position 0 is accepted. Self-contained reproduction (bash + psql; the files are COPY BINARY files with one tsvector column): psql -c "CREATE TABLE tv (v tsvector)" # one row: tsvector with 2 entries, 'a' (no positions), 'a' (no positions) printf 'PGCOPY\n\377\r\n\0\0\0\0\0\0\0\0\0\0\001\0\0\0\014\0\0\0\002a\0\0\0a\0\0\0= \377\377' > dup.bin # one row: tsvector with 1 entry, 'a' with one position, 0 printf 'PGCOPY\n\377\r\n\0\0\0\0\0\0\0\0\0\0\001\0\0\0\012\0\0\0\001a\0\0\001\0\0\= 377\377' > pos0.bin psql -c "\copy tv from 'dup.bin' with (format binary)" -- COPY 1 psql -c "\copy tv from 'pos0.bin' with (format binary)" -- COPY 1 SELECT v, length(v), pg_input_is_valid(v::text, 'tsvector') FROM tv; v | length | pg_input_is_valid ---------+--------+------------------- 'a':0 | 1 | f 'a' 'a' | 2 | t SELECT v, length(v), v::text::tsvector AS reparsed, length(v::text::tsvector), v =3D v::text::tsvector AS same FROM tv WHERE length(v) =3D 2; v | length | reparsed | length | same ---------+--------+----------+--------+------ 'a' 'a' | 2 | 'a' | 1 | f SELECT v::text::tsvector FROM tv WHERE length(v) =3D 1; ERROR: wrong position info in tsvector: "'a':0" Consequences through pg_dump (plain format, psql restore), from the attached transcript: duplicates: 'a' 'a' -> 'a', 'a':1 'a':2 -> 'a':1,2, 'a' 'a' 'b' -> 'a' 'b'; restore reports no error and length(v) changes position 0: ERROR: wrong position info in tsvector: "'a':0"; 0 of 2 rows of that table restored Expected: tsvectorrecv() rejects (or normalises, as tsvectorin() does for duplicates) values that tsvectorin() would not produce, as its own comment says it should. Actual: both are accepted; the duplicate case later changes silently on a text dump/restore, and the position-0 case makes the dump unrestorable. Suggested fix: - after the qsort in tsvectorrecv(), merge adjacent equal entries the way uniqueentry() does (merging and de-duplicating their position lists), or reject them with ERRCODE_INVALID_BINARY_REPRESENTATION; - in the position loop, also reject WEP_GETPOS(wepptr[0]) =3D=3D 0. The existing elog(ERROR, ...) calls in tsvectorrecv() could at the same time become ereport(ERROR, errcode(ERRCODE_INVALID_BINARY_REPRESENTATION), ...), since they are reachable from client input (COPY ... FORMAT binary, binary-format parameters).