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 1xDnjV-00000000ihP-0KXo for pgsql-bugs@arkaria.postgresql.org; Mon, 05 Oct 2026 18:48:17 +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 1xDnjU-0000000CTo1-0O4Z for pgsql-bugs@arkaria.postgresql.org; Mon, 05 Oct 2026 18:48:16 +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 1xDnjT-0000000CTnt-3azS for pgsql-bugs@lists.postgresql.org; Mon, 05 Oct 2026 18:48:15 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xDnjR-00000000Zmc-2RiY for pgsql-bugs@lists.postgresql.org; Mon, 05 Oct 2026 18:48:15 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 695ImBOg1091611; Mon, 5 Oct 2026 14:48:11 -0400 From: Tom Lane To: kehan5800@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19748: tsvectorrecv accepts duplicate lexemes and position 0 that tsvectorin rejects In-reply-to: <19748-6075f0508fa1ffc0@postgresql.org> References: <19748-6075f0508fa1ffc0@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Sun, 04 Oct 2026 05:19:27 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1091609.1791226091.1@sss.pgh.pa.us> Date: Mon, 05 Oct 2026 14:48:11 -0400 Message-ID: <1091610.1791226091@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk PG Bug reporting form writes: > 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(...) <= 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. Hmph. Making that happen would be a lot of mess, since that'd require also merging and de-duping positions for equal lexemes, and the de-dup logic used by tsvectorin() can't be re-used because it doesn't work on the same data representation. But it seems to me that tsvectorrecv() is being quite schizophrenic here: it will let you be sloppy about lexeme order, but not about duplicated or out-of-order positions. I don't see a good reason for it to be forgiving about either thing. So my proposal is to just throw an error if the lexeme sequence isn't strictly increasing, as it already does for positions. That is probably not something to back-patch, but this issue is surely not important enough to worry about back-patching. > 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. Agreed, that's an oversight. regards, tom lane