[PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

Started by Virender Singla3 months ago25 messageshackers
Jump to latest
#1Virender Singla
virender.cse@gmail.com

Hi hackers,

We encountered an issue where queries using the index scan reorder queue
(e.g., KNN GiST scans with lossy distance recheck) lose the tuple's
physical ItemPointer (ctid) in the returned slot.

When combined with row locking (e.g., SELECT ... FOR UPDATE), the invalid
TID (4294967295, 0) is passed to heap_lock_tuple(). Because InvalidBlockNumber
equals P_NEW, ReadBuffer() extends the relation on disk before aborting with
"attempted to lock invisible tuple", leaving an orphaned uninitialized block
that fails subsequent sequential scans with "ERROR: invalid page in block N".

This affects all supported versions (PG 14 through master).

=== Cause ===

When tuples are popped from node->iss_ReorderQueue, nodeIndexscan.c calls:
ExecForceStoreHeapTuple(tuple, slot, true);

In src/backend/executor/execTuples.c (TTS_IS_BUFFERTUPLE branch),
ExecClearTuple() resets slot->tts_tid to InvalidItemPointer, but
slot->tts_tid is never updated with tuple->t_self.

=== Reproducer ===

CREATE TABLE t (id int PRIMARY KEY, v circle);
INSERT INTO t SELECT i, circle(point(i, i), 0.5) FROM generate_series(1, 100) i;
CREATE INDEX ON t USING gist (v circle_ops);

-- 1. Emits invalid ctid (4294967295, 0):
SELECT ctid, id FROM t ORDER BY v <-> point(50, 50) LIMIT 1;

-- 2. Fails with "attempted to lock invisible tuple" and extends table on disk:
BEGIN;
SELECT id FROM t ORDER BY v <-> point(50, 50) LIMIT 1 FOR UPDATE;
ROLLBACK;

=== Fix ===

Assign `slot->tts_tid = tuple->t_self;` in the TTS_IS_BUFFERTUPLE branch of
ExecForceStoreHeapTuple(), matching the behavior of
tts_buffer_heap_store_tuple().

Attached is a patch against master including regression test coverage
in gist.sql.

Thanks,
Virender

Attachments:

t253628_1
fix_exec_force_store_heap_tuple_tid.patchapplication/x-patch; name=fix_exec_force_store_heap_tuple_tid.patchDownload+40-1
#2Virender Singla
virender.cse@gmail.com
In reply to: Virender Singla (#1)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

Just a quick ping on this patch.
It appears the `tts_tid` assignment was inadvertently omitted from the
BufferHeapTupleTableSlot path in ExecForceStoreHeapTuple() when the
function was first introduced during the PG12 TupleTableSlot
refactoring [1]https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=4da597edf1b.
This was subsequently exposed when the GiST index scan reorder queue
was updated to route popped tuples through this same function [2]https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=b8b94ea129f. The
relevant code hasn't changed since those commits.

Thanks,
Virender
[1]: https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=4da597edf1b
[2]: https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=b8b94ea129f

#3Greg Burd
greg@burd.me
In reply to: Virender Singla (#2)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

On Sep 14, 2026, at 8:10 AM, Virender Singla <virender.cse@gmail.com> wrote:

Just a quick ping on this patch.
It appears the `tts_tid` assignment was inadvertently omitted from the
BufferHeapTupleTableSlot path in ExecForceStoreHeapTuple() when the
function was first introduced during the PG12 TupleTableSlot
refactoring [1].

I ran into this myself and posted a patch [1]https://pg.ddx.io/m/pgsql-hackers/0498c10f-839b-4f68-9994-c29b454e55a4@app.fastmail.com/ as well. I had not noticed your
patch. Looks like both patches are essentially identical with different tests
we could combine those into one and then possibly get the attention of a
committer.

This was subsequently exposed when the GiST index scan reorder queue
was updated to route popped tuples through this same function [2]. The
relevant code hasn't changed since those commits.

Thanks,
Virender
[1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=4da597edf1b
[2] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=b8b94ea129f

I agree, it's a bug/oversight that has lingered since v12 and should be fixed.

best.

-greg

[1]: https://pg.ddx.io/m/pgsql-hackers/0498c10f-839b-4f68-9994-c29b454e55a4@app.fastmail.com/

#4Dilip Kumar
dilipbalaut@gmail.com
In reply to: Greg Burd (#3)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

On Mon, Sep 14, 2026 at 5:47 PM Burd, Greg <greg@burd.me> wrote:

On Sep 14, 2026, at 8:10 AM, Virender Singla <virender.cse@gmail.com> wrote:

Just a quick ping on this patch.
It appears the `tts_tid` assignment was inadvertently omitted from the
BufferHeapTupleTableSlot path in ExecForceStoreHeapTuple() when the
function was first introduced during the PG12 TupleTableSlot
refactoring [1].

I ran into this myself and posted a patch [1] as well. I had not noticed your
patch. Looks like both patches are essentially identical with different tests
we could combine those into one and then possibly get the attention of a
committer.

This was subsequently exposed when the GiST index scan reorder queue
was updated to route popped tuples through this same function [2]. The
relevant code hasn't changed since those commits.

Thanks,
Virender
[1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=4da597edf1b
[2] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=b8b94ea129f

I agree, it's a bug/oversight that has lingered since v12 and should be fixed.

+1 this seems like and issue, we can compare this with
ExecStoreHeapTuple() which restore back the ctid so I think here it
seems like and oversight and attached patch seems to be doing right
thing.

--
Regards,
Dilip Kumar
Google

#5Greg Burd
greg@burd.me
In reply to: Dilip Kumar (#4)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

On Mon, Sep 14, 2026, at 8:31 AM, Dilip Kumar wrote:

On Mon, Sep 14, 2026 at 5:47 PM Burd, Greg <greg@burd.me> wrote:

On Sep 14, 2026, at 8:10 AM, Virender Singla <virender.cse@gmail.com> wrote:

Just a quick ping on this patch.
It appears the `tts_tid` assignment was inadvertently omitted from the
BufferHeapTupleTableSlot path in ExecForceStoreHeapTuple() when the
function was first introduced during the PG12 TupleTableSlot
refactoring [1].

I ran into this myself and posted a patch [1] as well. I had not noticed your
patch. Looks like both patches are essentially identical with different tests
we could combine those into one and then possibly get the attention of a
committer.

This was subsequently exposed when the GiST index scan reorder queue
was updated to route popped tuples through this same function [2]. The
relevant code hasn't changed since those commits.

Thanks,
Virender
[1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=4da597edf1b
[2] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=b8b94ea129f

I agree, it's a bug/oversight that has lingered since v12 and should be fixed.

+1 this seems like and issue, we can compare this with
ExecStoreHeapTuple() which restore back the ctid so I think here it
seems like and oversight and attached patch seems to be doing right
thing.

Hey Dilip, thanks for chiming in.

--
Regards,
Dilip Kumar
Google

Virender,

I've re-worked the tests to include your two checks, and found something
about them worth flagging: at LIMIT 1 neither one fails on unpatched code.

The first tuple out of IndexNextWithReorder() is the one whose advertised
distance compared equal to the recomputed one, so was_exact is true and it
never enters the reorder queue. It keeps its real ctid. Only the tuples
behind it get requeued through ExecForceStoreHeapTuple(). I built your
test against a tree with the execTuples.c hunk reverted and valid_tid
comes back 't', and the FOR UPDATE succeeds. Raise the same table to
LIMIT 5 and 4 of 5 rows are (4294967295,0) and the FOR UPDATE dies. Your
checks are right, it's just the LIMIT.

So v2 keeps both of your assertions at LIMIT 5, on the polygon fixture
from my patch, plus an EXPLAIN to pin the plan (worth having, since your
block landed after the reset enable_seqscan at the end of gist.sql). A/B
with only the execTuples.c hunk reverted:

unpatched patched
sentinel ctids at LIMIT 5 4 0
ctid self-join, expect 5 1 5
ORDER BY ... LIMIT 5 FOR UPDATE assert 5

I dropped the separate circle_ops table. Unpatched it fails at LIMIT 5
just like the polygon one (19 of 20 sentinels), same reorderqueue_pop
path, so it wasn't buying coverage. The polygon case is only a stronger
trigger in degree.

The FOR UPDATE case is important. I reproduced the assert
(ItemPointerIsValid in itemptr.h). The non-assert consequence, P_NEW
extending the relation and leaving a block that later breaks seqscans,
I've taken from your mail rather than tested and credited as such.
It's the strongest argument for back-patching, since it turns a
wrong-answer bug into on-disk damage.

One refinement on provenance. 4da597edf1b did create the function without
the assignment, but tts_tid didn't exist yet. The field arrives in
b8d71745eac, which sets it in both tts_heap_store_tuple and
tts_buffer_heap_store_tuple and misses this branch, and it only becomes
observable at ff11e7f4b9a, which made tts_buffer_heap_clear invalidate
tts_tid. Before that ExecClearTuple left it alone, so the slot kept a
stale value instead of the sentinel. Then b8b94ea129f made it reachable,
as you say, by dropping iss_ReorderQueueSlot, which was TTSOpsHeapTuple
and took the correct branch. All in the v12 cycle, so your conclusion
holds, just a different commit as the origin.

On that basis I've also switched to your Backpatch-through: 14 rather
than the 13 I'd written, since 13 is out of support.

On author attribution, you first since your post predates mine. Dilip,
thanks for the review.

v3 attached,

-greg

Attachments:

t253628_5
v3-0001-Restore-slot-tts_tid-in-ExecForceStoreHeapTuple-s.patchtext/x-patch; name="=?UTF-8?Q?v3-0001-Restore-slot-tts=5Ftid-in-ExecForceStoreHeapTuple-s.pa?= =?UTF-8?Q?tch?="Download+100-3
#6Andres Freund
andres@anarazel.de
In reply to: Greg Burd (#5)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

Hi,

On 2026-09-01 11:48:43 +0530, Virender Singla wrote:

-- 2. Fails with "attempted to lock invisible tuple" and extends table on disk:

I was a bit shocked about the extends-table-on-disk issue at first, thinking
it'd lead to trying to extend the relation to an absurd size. But it's "just"
the invalid block number being interpreted as a request to extend the relation
by one block (because our historical relation extension path was to call
ReadBuffer() with P_NEW, which is InvalidBlockNumber). So this isn't *too*
bad.

I think we, separately from the fix to main tts_tid, should also add an error
path against trying to lock an invalid tid. This should have never gotten
anywhere close to a ReadBuffer() IMO.

So v2 keeps both of your assertions at LIMIT 5, on the polygon fixture
from my patch, plus an EXPLAIN to pin the plan (worth having, since your
block landed after the reset enable_seqscan at the end of gist.sql).

Yep, I think verifying plans in stuff like this is a good practice.

One refinement on provenance. 4da597edf1b did create the function without
the assignment, but tts_tid didn't exist yet. The field arrives in
b8d71745eac, which sets it in both tts_heap_store_tuple and
tts_buffer_heap_store_tuple and misses this branch, and it only becomes
observable at ff11e7f4b9a, which made tts_buffer_heap_clear invalidate
tts_tid. Before that ExecClearTuple left it alone, so the slot kept a
stale value instead of the sentinel.

Which is *way* worse.

+create table gist_knn_ctid (id int, p polygon);
+insert into gist_knn_ctid
+select i, ('((' || i*10 || ',0),(' || (i*10+9) || ',9),('
+               || (i*10+9) || ',0))')::polygon

Ick, that's hard to read. Maybe a format() or such would make it easier?

+from generate_series(1,20) i;
+create index gist_knn_ctid_idx on gist_knn_ctid using gist (p);
+vacuum analyze gist_knn_ctid;
+explain (costs off)
+select ctid, id from gist_knn_ctid order by p <-> point(100,4) limit 5;
+                        QUERY PLAN                         
+-----------------------------------------------------------
+ Limit
+   ->  Index Scan using gist_knn_ctid_idx on gist_knn_ctid
+         Order By: (p <-> '(100,4)'::point)
+(3 rows)
+
+-- no row may report the invalid-tid sentinel
+select count(*) as invalid_ctids
+from (select ctid from gist_knn_ctid order by p <-> point(100,4) limit 5) s
+where ctid = '(4294967295,0)'::tid;

Is that actually reliable? In theory the filter could get pushed down
(although unlikely due to the limit and the complexity safely pushing down
below that would require), and you don't have an explain plan guarding it.

I'd probably put the query results in a temp table and then query that table
in the verifications.

+ invalid_ctids 
+---------------
+             0
+(1 row)
+
+-- every row must be findable by the ctid it reported
+select count(*) as ctid_matches
+from (select ctid, id from gist_knn_ctid order by p <-> point(100,4) limit 5) s
+     join gist_knn_ctid t on t.ctid = s.ctid and t.id = s.id;
+ ctid_matches 
+--------------
+            5
+(1 row)

I'd make this a NOT EXISTS() or such, a 0-rows-not-found is easier to verify.

Greetings,

Andres Freund

#7Greg Burd
greg@burd.me
In reply to: Andres Freund (#6)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

Hello Andres,

Thanks for the review. Attached is v4.

0001 is the fix, plus the regression test. One line: restore tts_tid from
the tuple in the TTS_IS_BUFFERTUPLE branch of ExecForceStoreHeapTuple(),
which is what both tts_heap_store_tuple() and tts_buffer_heap_store_tuple()
already do. This is the piece that wants backpatching.

0002 is the assertion you suggested, in IndexNextWithReorder() only.

0003 is the invalid-TID error path you asked for, at the table AM boundary.

More detail on each, and the review points, below.

I think we, separately from the fix to main tts_tid, should also add an error
path against trying to lock an invalid tid. This should have never gotten
anywhere close to a ReadBuffer() IMO.

That is 0003. I put the check in table_tuple_lock() rather than
heap_lock_tuple(), because heap_lock_tuple() is only reachable through the
AM callback in heapam_handler.c, so one check at the boundary covers every
AM and rejects the TID before any AM code runs.

I tested with 0001 reverted and only 0003 in place, the FOR UPDATE case gives

ERROR: cannot lock tuple with invalid TID (4294967295,0) in relation "tri"

and pg_relation_size() reads 385024 bytes, 47 blocks, both before and after,
so no extension, and the backend stays up. Remove 0003 from the same
assert-enabled build and it crashes instead, tripping
ItemPointerIsValid() inside ItemPointerGetBlockNumber() called from
heap_lock_tuple(). I did not rebuild without assertions, so the production
extension behaviour is still Virender's report rather than mine; the commit
message says as much.

I wonder if we ought to have an assertion for the two tids being the same
that, perhaps only on master?
I don't think we can do that in general, there are legitimate cases of those
differing due to HOT IIRC. But in the reorder case I don't think that
difference exists [...] so I think we should just assert it there.

0002 is what I think you're asking for, in IndexNextWithReorder() only.

My first attempt asserted the general invariant in slot_getsysattr(), for any
heap or buffer-heap slot holding a tuple, and it passed the whole suite under
cassert with nothing firing. So whatever divergence exists is not exercised
anywhere in the tests. That does not make the general version safe, and I have
not tried to argue for it here, but if someone revisits this later that is the
starting point.

One trap worth recording for whoever writes this next. The obvious form of
the assertion is wrong:

ExecForceStoreHeapTuple(tuple, slot, true);
Assert(ItemPointerEquals(&slot->tts_tid, &tuple->t_self)); /* wrong */

shouldFree is true here, so the store pfrees the tuple and t_self is read
from freed memory. It shows up as a TID of (0,32639), which is 0x7F7F, the
clobber pattern. 0002 copies the TID out before the store. It also compares
with the NoCheck accessors, because ItemPointerEquals() asserts validity
internally and would otherwise trip on that rather than on the thing we
actually want to catch.

Ick, that's hard to read. Maybe a format() or such would make it easier?

Better?

select i, format('((%s,0),(%s,9),(%s,0))', i * 10, i * 10 + 9, i * 10 + 9)::polygon

I'd probably put the query results in a temp table and then query that table
in the verifications.

Done. The ordered result goes into a temp table first and the checks run
against that, so there is no qual left for the planner to push into the
ordered scan. The EXPLAIN stays, to pin the plan that produces the temp
table. I also added an explicit set/reset of enable_seqscan, since the block
lands after gist.sql's final reset and would otherwise not be guaranteed an
index scan.

I'd make this a NOT EXISTS() or such, a 0-rows-not-found is easier to verify.

Done, so both checks now read zero when correct:

select count(*) as invalid_ctids from gist_knn_ctid_res
where c = '(4294967295,0)'::tid;

select count(*) as rows_not_found
from gist_knn_ctid_res r
where not exists (select 1 from gist_knn_ctid t
where t.ctid = r.c and t.id = r.id);

The test does gate the fix, which I checked at 0001 alone rather than with
the assertion masking it: reverting the one line takes invalid_ctids from 0
to 4 and rows_not_found from 0 to 4, and the FOR UPDATE aborts. Four of five
rather than five is the was_exact fast path returning the first tuple
without queueing, which is why the comment warns against checking at
LIMIT 1.

Which is *way* worse.

Agreed, and that is in 0001's commit message now: the omission dates to
b8d71745eac, which added tts_tid and set it in both store callbacks while
missing this branch, and it only became observable at ff11e7f4b9a, which
made tts_buffer_heap_clear() invalidate tts_tid. Before that the slot kept a
stale TID, which is worse than a recognisable sentinel and would have been
much harder to spot.

One loose end, for Michael's earlier question about tts_buffer_heap_copyslot()
having the same clear-then-copy shape. It does, and its copy branch also
never restores tts_tid, while the branch below it goes through
tts_buffer_heap_store_tuple() and does. I wrote the fix, then instrumented
that branch to see whether it is reachable with a source tuple that has a
valid t_self, and got zero hits across the regress and recovery suites. So I
have left it alone rather than ship a change I cannot demonstrate. If anyone
can construct a case that reaches it, it should be fixed the same way.

The whole series builds clean with no warnings, each commit builds on its
own, and the full suite is green under cassert here on macOS/arm64: 389
passed, 0 failed, 23 skipped, where the skips want ssl/ldap/xid_wraparound
setup I do not have.

best.

-greg

Attachments:

t253628_7
v4-0001-Restore-tts_tid-in-ExecForceStoreHeapTuple.patchtext/x-patch; name="=?UTF-8?Q?v4-0001-Restore-tts=5Ftid-in-ExecForceStoreHeapTuple.patch?="Download+114-1
v4-0002-Assert-the-reorder-queue-keeps-a-tuple-s-TID-in-t.patchtext/x-patch; name="=?UTF-8?Q?v4-0002-Assert-the-reorder-queue-keeps-a-tuple-s-TID-in-t.patc?= =?UTF-8?Q?h?="Download+21-1
v4-0003-Reject-an-invalid-TID-in-table_tuple_lock.patchtext/x-patch; name="=?UTF-8?Q?v4-0003-Reject-an-invalid-TID-in-table=5Ftuple=5Flock.patch?="Download+13-1
#8Virender Singla
virender.cse@gmail.com
In reply to: Greg Burd (#7)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

Hi Greg,

Thank you for driving this forward and taking care of the refinements!
I applied the v4 patchset locally and verified that it correctly fixes
the original issue and passes all tests.

Thanks,
Virender

#9Greg Burd
greg@burd.me
In reply to: Virender Singla (#8)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

On Tue, Sep 15, 2026, at 5:42 AM, Virender Singla wrote:

Hi Greg,

Thank you for driving this forward and taking care of the refinements!

Hey Virender, I'm happy to help!

I applied the v4 patchset locally and verified that it correctly fixes
the original issue and passes all tests.

Thanks for the review.

Thanks,
Virender

best.

-greg

#10Nikolay Samokhvalov
nik@postgres.ai
In reply to: Greg Burd (#7)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

On Sep 14, 2026, Greg Burd <greg@burd.me> wrote:

Done, so both checks now read zero when correct:

My AI harness noticed that v4-0001 still has the old ctid_matches join
returning 5, in both gist.sql and gist.out. It looks like the email and
attachment got out of sync.

That is 0003. I put the check in table_tuple_lock() rather than
heap_lock_tuple(), because heap_lock_tuple() is only reachable through
the AM callback in heapam_handler.c, so one check at the boundary covers
every AM and rejects the TID before any AM code runs.

This catches the reported (InvalidBlockNumber, 0), but
ItemPointerIsValid() only checks for a non-NULL pointer and ip_posid != 0.
For example, (InvalidBlockNumber, 1) still reaches the AM.

If the intended protection is specifically against passing P_NEW to
heap's ReadBuffer(), should this check be heap-side? A stronger generic
check would need a clearly stated table-AM invariant; the moved-partitions
marker is also an InvalidBlockNumber encoding with a nonzero offset.

Thanks,
Nik

#11Greg Burd
greg@burd.me
In reply to: Nikolay Samokhvalov (#10)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

On Monday, September 21st, 2026 at 12:24 AM, Nikolay Samokhvalov <nik@postgres.ai> wrote:

Hi Nik,

Thanks for reviewing, both of your points are correct. Thanks, and sorry for
the slow reply.

My AI harness noticed that v4-0001 still has the old ctid_matches join
returning 5, in both gist.sql and gist.out. It looks like the email and
attachment got out of sync.

Right, and the email was the thing that was wrong. I did the format() and
temp-table changes, wrote up all three, and never actually made the
NOT EXISTS change before generating the patch. Fixed in v5-0001:

select count(*) as ctid_not_found
from gist_knn_ctid_res r
where not exists (select 1 from gist_knn_ctid t
where t.ctid = r.c and t.id = r.id);

So both checks now read zero when correct, which is what I claimed last
time.

This catches the reported (InvalidBlockNumber, 0), but
ItemPointerIsValid() only checks for a non-NULL pointer and ip_posid != 0.
For example, (InvalidBlockNumber, 1) still reaches the AM.

Yes. ItemPointerIsValid() is just

return pointer && pointer->ip_posid != 0;

So my check tested the offset and called it a block-number guard. Anything
with InvalidBlockNumber and a nonzero offset went straight through to
ReadBuffer() as P_NEW, which is exactly the case I said I was preventing.

If the intended protection is specifically against passing P_NEW to
heap's ReadBuffer(), should this check be heap-side? A stronger generic
check would need a clearly stated table-AM invariant; the moved-partitions
marker is also an InvalidBlockNumber encoding with a nonzero offset.

Agreed on both halves, and I've moved it. v5-0003 checks the block number
in heapam_tuple_lock() instead of table_tuple_lock(). That covers every
caller of the heap AM's lock callback, since heap_lock_tuple() is only
reachable through it, and it leaves other AMs to state their own rules
rather than my inventing an invariant for them.

Your moved-partitions point turned out to matter in a second way I hadn't
anticipated. MovedPartitionsBlockNumber is InvalidBlockNumber with offset
0xfffd, so it has to be allowed through to the existing report in
heapam_tuple_lock()'s retry loop. But the obvious spelling,

if (ItemPointerGetBlockNumberNoCheck(tid) == InvalidBlockNumber &&
!ItemPointerIndicatesMovedPartitions(tid))

crashes before it can reject anything: ItemPointerIndicatesMovedPartitions()
reads the offset through the checked accessor, which asserts on offset 0,
which is the case we're here to catch. The offset has to be tested first.
I only found that because the assert fired; reading the code I had
convinced myself it was fine.

v5 attached, rebased on master (6a93535798a).

0001 the fix, plus the regression test. This is the piece that wants
backpatching.
0002 the reorder-case assertion Andres asked for. master only.
0003 the invalid-TID rejection, now heap-side. master only.

On backpatching 0001: it applies as-is to 18, 17, 16 and 15. On 14 the C
hunk applies (with an offset) but the test hunks do not, because gist.sql
has grown since, the block ahead of the insertion point in master does not
exist on 14. The test block itself is self-contained, its own table, its
own set/reset of enable_seqscan, its own cleanup, no dependency on
gist_tbl, so appending it to the end of 14's gist.sql and gist.out is all
that is needed.

I built that assembled 14 tree with --enable-cassert to check I wasn't
just asserting this. The gist test passes with the fix, and with only the
execTuples.c hunk reverted it fails the way it should:
invalid_ctids 0 -> 4
ctid_not_found 0 -> 4
FOR UPDATE 5 rows -> assert

So the test gates the fix on 14 as well, it just needs placing by hand.
Happy to send a separate 14-specific 0001 if that's easier for whoever
commits it.

Verified on an assert-enabled build:

- 0003 alone, with 0001 and 0002 reverted: the FOR UPDATE case gives
ERROR: cannot lock tuple with invalid TID (4294967295,0) in relation
"tri", pg_relation_size() is 8192 before and after, and the backend
stays up. Without 0003 the same build asserts in
ItemPointerGetOffsetNumber() under heapam_tuple_lock().
- (4294967295,65533), the moved-partitions encoding, still reaches its
own path rather than the new error.
- All three applied: regression suite green, 239 tests.

I have still not built without assertions, so the production
relation-extension behavior remains Virender's report rather than
something I've reproduced. The commit message says so.

One loose end: there's no commitfest entry for this thread. I'll create
one so it doesn't get lost, unless Virender would rather do it as the
original reporter.

best.

-greg

Attachments:

t253628_11
v5-0003-Reject-an-invalid-TID-before-heap_lock_tuple-reads-i.patchtext/x-patch; name=v5-0003-Reject-an-invalid-TID-before-heap_lock_tuple-reads-i.patchDownload+21-1
v5-0001-Restore-tts_tid-in-ExecForceStoreHeapTuple.patchtext/x-patch; name=v5-0001-Restore-tts_tid-in-ExecForceStoreHeapTuple.patchDownload+116-1
v5-0002-Assert-the-reorder-queue-keeps-a-tuple-s-TID-in-the-.patchtext/x-patch; name=v5-0002-Assert-the-reorder-queue-keeps-a-tuple-s-TID-in-the-.patchDownload+21-1
#12Virender Singla
virender.cse@gmail.com
In reply to: Greg Burd (#11)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

One loose end: there's no commitfest entry for this thread. I'll create
one so it doesn't get lost, unless Virender would rather do it as the
original reporter.

Thanks Greg!!
Here is the commitfest entry: https://commitfest.postgresql.org/patch/7371/

Regards,
Virender

#13Andres Freund
andres@anarazel.de
In reply to: Greg Burd (#11)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

Hi,

On 2026-09-29 16:31:39 +0000, Greg Burd wrote:

From 057189b13e905377766f0e6c667c23849241708c Mon Sep 17 00:00:00 2001
From: Greg Burd <greg@burd.me>
Date: Mon, 14 Sep 2026 12:58:22 -0400
Subject: [PATCH v5 1/3] Restore tts_tid in ExecForceStoreHeapTuple()

The TTS_IS_BUFFERTUPLE branch calls ExecClearTuple(), which resets
tts_tid via tts_buffer_heap_clear(), then copies the tuple in without
restoring tts_tid. Both tts_heap_store_tuple() and
tts_buffer_heap_store_tuple() assign slot->tts_tid = tuple->t_self, so
this reads as an omission rather than an intentional choice.

It is user-visible because slot_getsysattr() answers
SelfItemPointerAttributeNumber straight out of tts_tid. The reorder
queue in nodeIndexscan.c reaches this path for any index AM that sets
xs_recheckorderby, so an ORDER BY-op scan over such an AM projects
(4294967295,0) as ctid. Feeding that sentinel to heap_lock_tuple()
extends the relation, because InvalidBlockNumber equals P_NEW, leaving
an uninitialized block that later breaks sequential scans.

The bug dates to b8d71745eac, which added tts_tid and set it in both
store callbacks while missing this branch. It only became observable
at ff11e7f4b9a, which made tts_buffer_heap_clear() invalidate tts_tid;
before that the slot retained a stale TID instead of the sentinel.

The test uses thin diagonal triangles so that poly_ops' bounding-box
distance is strictly below the true distance, which forces the requeue
path, and materializes the ordered result before checking it so that
the planner cannot push the checks' quals into the scan.

Reported-by: Virender Singla
Reported-by: Greg Burd

We normally include email addresses as well. Also would be good to add
Discussion links for the (at least?) two threads. Adding Reviewed-By also
helps eventual committers.

@@ -1768,6 +1768,12 @@ ExecForceStoreHeapTuple(HeapTuple tuple,
slot->tts_flags |= TTS_FLAG_SHOULDFREE;
MemoryContextSwitchTo(oldContext);

+		/*
+		 * ExecClearTuple() above reset tts_tid, so restore it from the tuple
+		 * we just stored, the same way the tts_*_store_tuple() callbacks do.
+		 */
+		slot->tts_tid = tuple->t_self;
+
if (shouldFree)
pfree(tuple);
}

So one thing that's weird is that this does not modify the subsequent else
block:

else
{
ExecClearTuple(slot);
heap_deform_tuple(tuple, slot->tts_tupleDescriptor,
slot->tts_values, slot->tts_isnull);
ExecStoreVirtualTuple(slot);

if (shouldFree)
{
ExecMaterializeSlot(slot);
pfree(tuple);
}
}

If we decide to maintain the tid in ExecForceStoreHeapTuple() - I suspect the
right call - we really should actually do it consistently.

The lock path itself can't be reached with a virtual slot (c.f. assertion in
heapam_tuple_lock()), but it might be possible to have a virtual slot in the
path towards it. But even if it can't be today, it'll likely be soon.

From 7ed9738b2a8d2471673fbff88418fcfe411842f2 Mon Sep 17 00:00:00 2001
From: Greg Burd <greg@burd.me>
Date: Mon, 14 Sep 2026 12:58:42 -0400
Subject: [PATCH v5 2/3] Assert the reorder queue keeps a tuple's TID in the
slot

IndexNextWithReorder() re-stores a queued tuple with
ExecForceStoreHeapTuple(), and slot_getsysattr() answers
SelfItemPointerAttributeNumber out of tts_tid alone, so a slot that
loses the TID silently projects a different ctid than the row it
returned. Assert that the slot advertises the TID the tuple was
fetched from.

The invariant does not hold for slots in general, since HOT can
legitimately make tts_tid and the stored tuple's t_self differ, so the
check is confined to this path, where a divergence changes query
results.

The TID is captured before the store, which frees the tuple, and the
comparison uses the NoCheck accessors so that the sentinel trips this
assertion rather than the validity check inside ItemPointerEquals().

Maybe I'm slow, but who cares whether an earlier assert trips it? I think
flagging that the ctid is invalid, rather than unequal, seems perfectly fine?
Certainly doesn't seem worth to me to quadruple the size of the assertion for.

From 1431683a3df66187dac74ededaf613f3d4ecaea6 Mon Sep 17 00:00:00 2001
From: Greg Burd <greg@burd.me>
Date: Mon, 14 Sep 2026 13:01:10 -0400
Subject: [PATCH v5 3/3] Reject an invalid TID before heap_lock_tuple() reads
it

An invalid TID reaching heap_lock_tuple() is handed to ReadBuffer() as
InvalidBlockNumber, which is P_NEW, so the relation is extended by a
block before the lock attempt fails. The uninitialized block is left
behind and later breaks sequential scans with "invalid page in block".
A caller that gets this far with such a TID has a bug, so fail cleanly
instead.

An earlier version of this check sat in table_tuple_lock(), but that was
both too weak and in the wrong place. ItemPointerIsValid() only tests
for a non-NULL pointer and a nonzero offset, so a TID like
(InvalidBlockNumber, 1) passed it and still reached ReadBuffer(). And
the hazard being guarded against is specific to heap's use of P_NEW,
not a documented table AM invariant, so the generic layer is not the
right place to enforce it. Checking the block number in
heapam_tuple_lock() covers every caller of the heap AM's lock callback
and leaves other AMs to state their own rules.

I don't think I buy the "specific to heap's use of P_NEW", given that

a) Stuff like ItemPointerGetBlockNumber() asserts out if P_NEW is used
b) P_NEW is a ReadBuffer() thing, not a heap thing

The moved-partitions marker also encodes InvalidBlockNumber, with
offset MovedPartitionsOffsetNumber, and is a legitimate value that the
retry loop in heapam_tuple_lock() reports on its own terms, so it is
allowed through.

I'm not sure I buy that? I don't think it'd be valid to call
heapam_tuple_lock() with something indicating a moved partition? That's
different than reaching that during the retry loop!

(not yet done thinking about this, but have to run into a meeting, and this
seemed enough)

Greetings,

Andres Freund

#14Andres Freund
andres@anarazel.de
In reply to: Andres Freund (#13)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

Hi,

On 2026-10-01 10:16:05 -0400, Andres Freund wrote:

On 2026-09-29 16:31:39 +0000, Greg Burd wrote:

@@ -1768,6 +1768,12 @@ ExecForceStoreHeapTuple(HeapTuple tuple,
slot->tts_flags |= TTS_FLAG_SHOULDFREE;
MemoryContextSwitchTo(oldContext);

+		/*
+		 * ExecClearTuple() above reset tts_tid, so restore it from the tuple
+		 * we just stored, the same way the tts_*_store_tuple() callbacks do.
+		 */
+		slot->tts_tid = tuple->t_self;
+
if (shouldFree)
pfree(tuple);
}

So one thing that's weird is that this does not modify the subsequent else
block:

else
{
ExecClearTuple(slot);
heap_deform_tuple(tuple, slot->tts_tupleDescriptor,
slot->tts_values, slot->tts_isnull);
ExecStoreVirtualTuple(slot);

if (shouldFree)
{
ExecMaterializeSlot(slot);
pfree(tuple);
}
}

If we decide to maintain the tid in ExecForceStoreHeapTuple() - I suspect the
right call - we really should actually do it consistently.

None of the copyslots maintains it properly either. Some just don't set it,
others will *sometimes* infer it from the source tuple
(e.g. tts_buffer_heap_copyslot, if the source slot is a buffer slot). Nor is
tts_tableOid consistently maintained as part of a copy.

That seems to currently not really be observable, because afaict we always
push down the projection of ctid, tableoid accesses down to the scan level,
where the slot is populated "originally" (if you assume nodeModifyTable.c is a
scan node, at least).

Which I guess is an argument to not consider that aspect a bug.

The lock path itself can't be reached with a virtual slot (c.f. assertion in
heapam_tuple_lock()), but it might be possible to have a virtual slot in the
path towards it. But even if it can't be today, it'll likely be soon.

AI did find a path, see attached tid2.sql, note the wrong tid for
INSERT INTO parent_rows VALUES (2) RETURNING a, ctid;

Seems we should just move
slot->tts_tid = tuple->t_self;
to the end of ExecForceStoreHeapTuple()?

After looking at tid2.sql I played around trying to see whether it'd make it
possible to get an invalid tid for an update and was rather horrified to find
out that cross-partition updates involving ctids just silently cause data to
be inconsistent with the partition constraints :(.

Greetings,

Andres Freund

Attachments:

tid2.sqlapplication/sqlDownload
#15Greg Burd
greg@burd.me
In reply to: Andres Freund (#14)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

On Thursday, October 1st, 2026 at 12:58 PM, Andres Freund <andres@anarazel.de> wrote:

Hi,

Hello Andres, thanks for looking into this.

We normally include email addresses as well. Also would be good to add
Discussion links for the (at least?) two threads. Adding Reviewed-By also
helps eventual committers.

Yep, done, addresses on everything, both Discussion links, and Reviewed-by for
you, Dilip, Nik and Virender in v7 attached. What's the convention when someone
both reports an issue and co-authors the patch? I added myself and Virender
as reporters, but maybe we're also co-authors too? Still learning this... :)

I'm dropping the attempts at adding asserts in other paths mainly because I'm
not sure of the value but also because while testing I ran into b8b94ea129f:

For amcanreorderby scans the nodeIndexscan.c's reorder queue holds
heap tuples, but the underlying table likely does not.

For such an AM the scan slot isn't a buffer slot, so reorderqueue_push() goes
ExecCopySlotHeapTuple() -> tts_virtual_copy_heap_tuple() -> heap_form_tuple(),
which does ItemPointerSetInvalid(&tuple->t_self). The queued tuple therefore
has no TID to begin with. 0001 stores that invalid TID and the assertion then
fires on a slot that is behaving correctly. heap_copytuple() preserves t_self,
which is why heap never hits this.

On 2026-10-01 10:16:05 -0400, Andres Freund wrote:

On 2026-09-29 16:31:39 +0000, Greg Burd wrote:

@@ -1768,6 +1768,12 @@ ExecForceStoreHeapTuple(HeapTuple tuple,
slot->tts_flags |= TTS_FLAG_SHOULDFREE;
MemoryContextSwitchTo(oldContext);

+		/*
+		 * ExecClearTuple() above reset tts_tid, so restore it from the tuple
+		 * we just stored, the same way the tts_*_store_tuple() callbacks do.
+		 */
+		slot->tts_tid = tuple->t_self;
+
if (shouldFree)
pfree(tuple);
}

So one thing that's weird is that this does not modify the subsequent else
block:

else
{
ExecClearTuple(slot);
heap_deform_tuple(tuple, slot->tts_tupleDescriptor,
slot->tts_values, slot->tts_isnull);
ExecStoreVirtualTuple(slot);

if (shouldFree)
{
ExecMaterializeSlot(slot);
pfree(tuple);
}
}

If we decide to maintain the tid in ExecForceStoreHeapTuple() - I suspect the
right call - we really should actually do it consistently.

None of the copyslots maintains it properly either. Some just don't set it,
others will *sometimes* infer it from the source tuple
(e.g. tts_buffer_heap_copyslot, if the source slot is a buffer slot). Nor is
tts_tableOid consistently maintained as part of a copy.

That seems to currently not really be observable, because afaict we always
push down the projection of ctid, tableoid accesses down to the scan level,
where the slot is populated "originally" (if you assume nodeModifyTable.c is a
scan node, at least).

Which I guess is an argument to not consider that aspect a bug.

Agreed.

The lock path itself can't be reached with a virtual slot (c.f. assertion in
heapam_tuple_lock()), but it might be possible to have a virtual slot in the
path towards it. But even if it can't be today, it'll likely be soon.

AI did find a path, see attached tid2.sql, note the wrong tid for
INSERT INTO parent_rows VALUES (2) RETURNING a, ctid;

Seems we should just move
slot->tts_tid = tuple->t_self;
to the end of ExecForceStoreHeapTuple()?

I took a copy of the TID at function start and use that later to avoid a case
where the tuple might have bee free'd.

After looking at tid2.sql I played around trying to see whether it'd make it
possible to get an invalid tid for an update and was rather horrified to find
out that cross-partition updates involving ctids just silently cause data to
be inconsistent with the partition constraints :(.

Yikes, good find that sounds worse than anything in this thread. The
postgres_fdw side looks like a separate patch, and I don't think it
should hold up the backpatchable fix. I'll take a look at it
separately if nobody else is already on it and start a new thread.

Greetings,

Andres Freund

v7/0001 the fix, now maintaining the TID for all slot types, plus the
regression test. This is the piece that wants backpatching.

On backpatching 0001: applies as-is to 18, 17, 16 and 15. On 14 the C
hunk applies with an offset but the test hunks don't, gist.sql has grown
since. The test block is self-contained, so appending it to the end of
14's gist.sql and gist.out is enough IMO.

best.

-greg

Attachments:

t253628_15
v7-0001-Maintain-tts_tid-in-ExecForceStoreHeapTuple.patchtext/x-patch; name=v7-0001-Maintain-tts_tid-in-ExecForceStoreHeapTuple.patchDownload+86-1
#16Greg Burd
greg@burd.me
In reply to: Greg Burd (#15)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

Hello.

v8 attached, code/fix is unchanged from v7 but just some cleanup of things
I'd overlooked or misunderstood so a new commit message and some additional
tests.

First, the tid2.sql case *is* fixed by this patch. I said otherwise, and
I'm sorry for the confusion. I'd tested against the wrong build, and then
misread EXPLAIN VERBOSE: the routed INSERT shows no "Remote SQL ...
RETURNING", which I took to mean ctid wasn't being requested. That remote
statement is built at execution time in postgresBeginForeignInsert(),
which passes ri_returningList through to deparseReturningList(), so ctid
is requested and make_tuple_from_result_row() does fill in t_self. What
lost it was the else (virtual-slot) branch of ExecForceStoreHeapTuple()
not setting tts_tid, which is exactly what moving the assignment to the
end (as you had suggested Andres) fixes.

Testing against a fresh build:

INSERT INTO parent_rows VALUES (2) RETURNING a, ctid;
v8: 2 | (0,2)fixes
execTuples.c reverted: 2 | (4294967295,0)

v8 drops the paragraph in v7's commit message that claimed this was a
separate postgres_fdw problem, and adds your case as a regression test
in postgres_fdw.sql, next to the existing insert tuple routing tests.
It inserts into itrtest and checks every returned ctid against
loct1/loct2, so as not to depend on page layout.

While writing it I found that only one of the two partitions in that
test shows the bug: remp1, whose columns match the parent, gets a virtual
result slot. remp2 declares (b, a), so its insert goes through a tuple
conversion map and a heap slot, which was already correct. The test
therefore reports one missing ctid rather than two without the fix.

Second, in my last mail I answered your cross-partition update comment
with "The postgres_fdw side looks like a separate patch". That attached
the FDW case to the wrong issue. As far as I can tell they're unrelated:
the FDW case was this bug, while

cross-partition updates involving ctids just silently cause data to be
inconsistent with the partition constraints :(.

is something else. I tried a few obvious shapes, a ctid-keyed UPDATE that
moves the row, a ctid-only predicate that matches (0,1) in two
partitions, and a ctid taken from a subquery against another partition,
and they all came out consistent with the partition constraints. So I
haven't found your case. Could you share what you ran? I'm happy to work
on it in a new thread.

On attribution, I'll answer my own question from last time, I've used
Co-authored-by: me and Virender, and Reported-by: you for the FDW
path. Tell me if you'd rather it were different.

Backpatching: v8 applies as-is to master and 15 through 18. On 14 the
execTuples.c hunk applies, but both test hunks need placing by hand
because gist.sql and postgres_fdw.sql have grown since. Both blocks are
self-contained and go in unchanged. I built 14 with --enable-cassert and
the placed postgres_fdw test passes with the fix and fails without it
(ctid_not_found 0 -> 1), the same as on master.

Apologies for rushing out the last email.

best.

-greg

Attachments:

t253628_16
v8-0001-Maintain-tts_tid-in-ExecForceStoreHeapTuple.patchtext/x-patch; name=v8-0001-Maintain-tts_tid-in-ExecForceStoreHeapTuple.patchDownload+121-1
#17shihao zhong
zhong950419@gmail.com
In reply to: Greg Burd (#16)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

Hi Greg,

The thread only tested the FOR UPDATE case with asserts, so I tried
it on a normal build. After the FOR UPDATE and a restart, the table
cannot be read or vacuumed:

create table tri (id int, p polygon);
insert into tri select i, format('((%s,0),(%s,9),(%s,0))',
i * 10, i * 10 + 9, i * 10 + 9)::polygon
from generate_series(1, 20) i;
create index on tri using gist (p);
vacuum analyze tri;
set enable_seqscan = off;
begin;
select id from tri order by p <-> point(100,4) limit 5 for update;
rollback;
checkpoint;

Then restart the server and run

select count(*) from tri;
ERROR: invalid page in block 1 of relation "base/5/16384"

With v8 the FOR UPDATE returns 5 rows and the count is 20. I think
it is ready for committer.

Thanks,
Shihao

#18Greg Burd
greg@burd.me
In reply to: shihao zhong (#17)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

On Sun, 18 Oct 2026 23:55:36 -0400, shihao zhong <zhong950419@gmail.com> wrote:

Hi Greg,

The thread only tested the FOR UPDATE case with asserts, so I tried
it on a normal build. After the FOR UPDATE and a restart, the table
cannot be read or vacuumed:

create table tri (id int, p polygon);
insert into tri select i, format('((%s,0),(%s,9),(%s,0))',
i * 10, i * 10 + 9, i * 10 + 9)::polygon
from generate_series(1, 20) i;
create index on tri using gist (p);
vacuum analyze tri;
set enable_seqscan = off;
begin;
select id from tri order by p <-> point(100,4) limit 5 for update;
rollback;
checkpoint;

Then restart the server and run

select count(*) from tri;
ERROR: invalid page in block 1 of relation "base/5/16384"

With v8 the FOR UPDATE returns 5 rows and the count is 20. I think
it is ready for committer.

Thanks,
Shihao

Hi Shihao,

Thanks for testing this. v9 is attached. The code is unchanged from v8.
The commit message now describes the damage as above, and I've added you
as Tested-by.

There are two patches this time:

v9-0001 master, and applies as-is to REL_18 down to REL_15
v9-REL_14-0001 the same fix, with both test blocks placed where 14's
gist.sql and postgres_fdw.sql need them

For each of the six branches, without the fix the new tests fail on
exactly their own hunk (gist ctid_not_found 0 -> 4, postgres_fdw 0 -> 1)
and nothing else, and with the fix they pass. On master the full meson
suite passes, 359 tests and none failing.

best.

-greg

Attachments:

t253628_18
v9-REL_14-0001-Maintain-tts_tid-in-ExecForceStoreHeapTuple.patch_text/plain; charset=us-ascii; name=v9-REL_14-0001-Maintain-tts_tid-in-ExecForceStoreHeapTuple.patch_Download+121-1
v9-0001-Maintain-tts_tid-in-ExecForceStoreHeapTuple.patchtext/plain; charset=us-ascii; name=v9-0001-Maintain-tts_tid-in-ExecForceStoreHeapTuple.patchDownload+121-1
#19Nathan Bossart
nathandbossart@gmail.com
In reply to: Greg Burd (#18)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

Here is what I have staged for commit, which I'm planning to do early next
week barring additional feedback or objections. I've left out the tests
and extra checks for now. Those might be worth adding, but I'd like to
make sure we at least get the main fix into November's releases.

--
nathan

Attachments:

t253628_19
v10-0001-Fix-missing-tts_tid-assignment-in-ExecForceStore.patchtext/plain; charset=us-asciiDownload+9-1
#20Greg Burd
greg@burd.me
In reply to: Nathan Bossart (#19)
Re: [PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple

On Friday, October 30th, 2026 at 12:11 PM, Nathan Bossart <nathandbossart@gmail.com> wrote:

Here is what I have staged for commit, which I'm planning to do early next
week barring additional feedback or objections. I've left out the tests
and extra checks for now. Those might be worth adding, but I'd like to
make sure we at least get the main fix into November's releases.

--
nathan

Thanks Nathan, +1.

-greg

#21Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Nathan Bossart (#19)
#22Greg Burd
greg@burd.me
In reply to: Heikki Linnakangas (#21)
#23Nathan Bossart
nathandbossart@gmail.com
In reply to: Greg Burd (#22)
#24Nathan Bossart
nathandbossart@gmail.com
In reply to: Nathan Bossart (#23)
#25Greg Burd
greg@burd.me
In reply to: Nathan Bossart (#24)