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.96) (envelope-from ) id 1x4HHe-0072rt-0t for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Sep 2026 12:20:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x4HGc-00EYZM-36 for pgsql-hackers@arkaria.postgresql.org; Wed, 09 Sep 2026 12:19:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x4HGc-00EYZ8-0y for pgsql-hackers@lists.postgresql.org; Wed, 09 Sep 2026 12:19:06 +0000 Received: from fhigh-a4-smtp.messagingengine.com ([103.168.172.155]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x4HGa-00000004ldv-0TVB for pgsql-hackers@lists.postgresql.org; Wed, 09 Sep 2026 12:19:05 +0000 Received: from phl-compute-02.internal (phl-compute-02.internal [10.202.2.42]) by mailfhigh.phl.internal (Postfix) with ESMTP id 7C96114000A7; Wed, 9 Sep 2026 08:19:02 -0400 (EDT) Received: from phl-imap-02 ([10.202.2.81]) by phl-compute-02.internal (MEProxy); Wed, 09 Sep 2026 08:19:02 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=burd.me; h=cc:cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1788956342; x=1789042742; bh=lPDZ7YkvHhslWwlY/MA0rtBfhhiBZz1CwiSFOQw1wPU=; b= V8wc00A6K8h6QRK5BmjUplUo05yidHPmlGC5UA9U9tiGG8Q45PdfNJFeIM3nd1nb xfMxo4m550+/qUeB7zbWHKZXBcb2ijJ5wO84rv/F/qgpRFC8vc5qvZ2xbwutP8Jo 2feJYEc1I8nHimCUhR4hl6FOJfIm3tSMGnyLYOP5dqlonWC8oq/fParU62BPPxeb 1cMFTHA3U3Vq9choQaSa6LQemdb22jeEETyyTSlC4/IoD1sWdQSHlmT7S/7m8Zt6 x2atkeCX9r9GO4tY0qlAMyBzdOX0RNj9stFk/+W1uMKpBHC+n5Ah35xhotgznMG1 Y3LIbx2gdTR03zTenvPWXQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1788956342; x= 1789042742; bh=lPDZ7YkvHhslWwlY/MA0rtBfhhiBZz1CwiSFOQw1wPU=; b=k n6k+pfChL+xSNphEKXVU6P0+1FWF6pnhYHmlvQYFnfsW1+wJvf/iFzg9IsF4NyqN PwQwd3UMDriHGf/vFmenYB0uZYCTePKKEM3tnsIFU2zMOD3fUG6OVMZ0u5Kzs4dI Q1S94fQF7KrP8CVWHspt80g3Yqo1Hbivs0c0kdnTEDnqe5HVcabm5Ejfa3l+h8/a 8o0QylYYLXw/G71rUvWsmngSmnZ5Yl43Vquz/wL6xfiN/5lDIIYAFdZoUDtpj8Nz MgggQxFyMe2b4VK7Ep7W8SOvxTAY05X/1+ENXcotOQYFIYit67XGVQR093U9VHDN waNVpGadQFLnp73Oso/gg== X-ME-Sender: X-ME-Proxy-Cause: dmFkZTGE73ODiYRolAxLKgQ7phWqz/I1/BiiyUODevzQmO2AyC8TvM5kM4TQtp+wUTTjwK 9JB3y9LyZ8EbUlYOgVe3IHU6mra2o0sI8Qlzxc2y6g1AkN7J4+XgZP5eUyYlDeo1/aKsxE wMnngSSYTMH7tuagGiKHFyXDcM9FG7LcAiW7BbNVkAQv5yOf+1RWzG2NUlv782i/gPDL31 0SwNNfqDDe34hWC6VALZJ5Y9nR0j/GG49WZqSLypdJ14lHhHaIaC5Auh6EbgpnSjQwjGDi 0V/ZItZt3BSzjM+Z9sgS3V3LkevMbDOSv1g+f0Ebxsvmzb9LeC88d+mxPVsvIsDxsZBsF7 LSA8iw098qzvF4fR9ErpLxSF7jN5LXyGpkn5eF+aCc1fHPdPXT+w10jnT7rWdpzZcgV+dm yPPOUms0mMKi/h6JGvZwfEusSaogapsVF5nixxk3UydICEkb5XUcByLZ0Qdr3OgSbPQeYA wFOz1XtmQM0SUiKt8MDuWz4CSgfgSQCVHH6Zp1gE0VtGpN+6OK7400CZ01j3yX9bbxii4L FBiLK/2CR2k7swAau4YgA+CVB6UuRUCAPqUleEyNgYMmQ5736Qcsf/mS3RrTqXuxDsEit6 OdSSmdyQMP/RwfkyI0JaS67a+qPmaqbkLKcRsNMNP/a9GhiyP7pq8MeTTA6w X-ME-Proxy: Feedback-ID: i675e48f3:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 14E14700065; Wed, 9 Sep 2026 08:19:02 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface MIME-Version: 1.0 X-ThreadId: ASzvD0DbL1UJ Date: Wed, 09 Sep 2026 08:18:40 -0400 From: "Greg Burd" To: "Michael Paquier" Cc: "PostgreSQL Hackers" , "Andres Freund" Message-Id: <06d82634-1eef-4a55-b8e0-9a49bdfbc679@app.fastmail.com> In-Reply-To: References: <0498c10f-839b-4f68-9994-c29b454e55a4@app.fastmail.com> Subject: Re: ExecForceStoreHeapTuple() loses tts_tid, so ORDER BY-op index scans project an invalid ctid Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Sep 8, 2026, at 8:03 PM, Michael Paquier wrote: > On Tue, Sep 08, 2026 at 01:28:22PM -0400, Greg Burd wrote: >> ExecForceStoreHeapTuple() does not set slot->tts_tid when the target >> slot is a TTS_IS_BUFFERTUPLE slot. Any plan that re-stores a heap tup= le >> through it and then projects ctid therefore gets (4294967295,0) inste= ad >> of the row's real heap TID. > > Oops. Yeah, oops indeed. :) > My question would be what kind of testing you have done to spot that.. Honestly, not by a test. I am working on a new index AM for vector similarity search [1] adding a 1-bit encoding with rerank. To do that rerank it sets xs_recheckorderby =3D true, because its distances are quantised and only the executor's exact re-check can order the top-k correctly. That routes tuples through nodeIndexscan.c's reorder queue. One of our documented recipes harvests ctid from a scan and chains it downstream; that quietly started matching nothing, with the ctids coming back as (4294967295,0). My first assumption was that I'd broken something, so I went looking for the boundary: - xs_heaptid was correct and every real column was right; only the projected ctid was wrong, which pointed at the slot, not the AM. - ExecForceStoreHeapTuple's TTS_IS_BUFFERTUPLE branch calls ExecClearTuple (hence ItemPointerSetInvalid on tts_tid) and never restores it, while the sibling tts_heap_store_tuple does; and slot_getsysattr answers SelfItemPointerAttributeNumber straight out of tts_tid. - Then I reproduced it with core GiST and nothing else loaded, which is the version in the patch. To confirm the fix I built stock 18.4 with only that hunk applied and ran a test: unpatched patched ctid self-join, expect 5 1 5 UPDATE ... WHERE ctid, expect 5 1 5 sentinel ctids at LIMIT 50 49/50 0/50 The UPDATE line is the one that bothers me most. No error, it just affects the wrong number of rows. As for why the tree doesn't catch it, AFAICT nothing in core projects ctid from an ORDER BY-op index scan. The GiST kNN tests check ordering and results, which are fine here the row data is never wrong. That's what v1 adds a regress case for, and I checked it fails (ctid_matches =3D 1) without the hunk and passes (5) with it, so it gates the fix rather than just recording current output. One detail that probably explains the longevity, IndexNextWithReorder only queues a tuple when the AM's advertised ORDER BY value doesn't compare equal to the recomputed one. GiST's bounding-box distance is sometimes exact, so some rows keep their real ctid. Note the 49 of 50 above rather than 50. My index AM can't usefully bound its distance and advertises -inf, so every tuple goes through the queue and every one shows the sentinel. Partial in core, total out here. >> ExecClearTuple() reaches tts_buffer_heap_clear(), which does >> ItemPointerSetInvalid(&slot->tts_tid). The tuple is then copied in, b= ut >> tts_tid is left invalid. The sibling path, ExecStoreHeapTuple() -> >> tts_heap_store_tuple() =E2=80=94 *does* slot->tts_tid =3D tuple->t_se= lf, so this >> reads as a plain asymmetry rather than an intentional choice. > > That's strange. Once thing that I can see why scanning this file is > the same code pattern in tts_buffer_heap_copyslot(), where a slot is > similarly cleared in a copy-paste fashion. Agreed. > Andres? > -- > Michael best, thanks for looking, -greg [1] https://codeberg.org/gregburd/pg_turbovec https://github.com/gburd/p= g_turbovec