Review
[PATCH] Corruption Issue: Fix missing tts_tid in ExecForceStoreHeapTuple
- Proposal version
- 10
- Message
- auTBvArH9r2AiEvJ@nathan
- Base commit
- 5e4966e9d23931e49f914770e663dbc316ed568f
Summary
The staged patch restores slot->tts_tid in ExecForceStoreHeapTuple() on every slot type. That is the right fix, and it is the version the discussion converged on. I applied the attached v10 patch to master at 5e4966e9d23931e49f914770e663dbc316ed568f and both reported symptoms went away.
What is wrong
ExecForceStoreHeapTuple() clears the destination slot, and every clear callback invalidates tts_tid. The heap-slot path puts the TID back inside tts_heap_store_tuple(). The buffer-heap path copies the tuple with heap_copytuple(), which keeps t_self, and the virtual path deforms the tuple into datums. Neither path assigns slot->tts_tid. slot_getsysattr() answers ctid from slot->tts_tid alone, so both paths project (4294967295,0) while the heap tuple still carries a real t_self.
Two callers make that visible.
A distance-ordered index scan that sets xs_recheckorderby pops queued tuples in nodeIndexscan.c and stores them with ExecForceStoreHeapTuple() into the scan slot. On a heap table that slot is a buffer slot. Tuples that pass through the reorder queue come back with an invalid ctid. The tuple whose index distance was already exact skips the queue and keeps its ctid, so LIMIT 1 often looks fine.
SELECT FOR UPDATE then locks that TID. ItemPointerGetBlockNumber() of the invalid TID is InvalidBlockNumber, and ReadBuffer() treats that value as P_NEW, so the relation grows by one uninitialized block. The statement errors with "attempted to lock invisible tuple". After checkpoint and restart, page verification rejects the new block and a sequential scan fails with "invalid page in block 1".
postgres_fdw hits the virtual-slot path. store_returning_result() builds a heap tuple from the remote RETURNING row, with ctid in t_self, then calls ExecForceStoreHeapTuple(). An INSERT into a foreign partition whose rowtype matches the parent returns ctid (4294967295,0) for a row that was stored under a real ctid.
The change
The patch copies tuple->t_self into a local ItemPointerData before any branch can free the tuple, then assigns slot->tts_tid after the heap, buffer, and virtual branches. The heap branch has already stored the same value. The early copy matters on the other branches, because shouldFree pfree's the tuple before the assignment. Putting the assignment in one place covers the virtual slot as well as the buffer slot. The first versions of the patch changed only the buffer branch, and a foreign-partition INSERT RETURNING ctid still lost the TID there.
The assignment matches tts_heap_store_tuple() and tts_buffer_heap_store_tuple(). For a non-heap index AM, the queued tuple often comes from heap_form_tuple() and its t_self is already invalid. The slot was cleared to that same sentinel, so those scans keep their current behavior.
Checked on master
Unpatched build, assertions off. A 20-row polygon table with a GiST index, ORDER BY p <-> point(100,4) LIMIT 5, returned one real ctid and four (4294967295,0). The same scan FOR UPDATE failed with "attempted to lock invisible tuple", and pg_relation_size() went from 8192 to 16384. After checkpoint and restart, SELECT count(*) failed with "invalid page in block 1".
A loopback foreign partition, same rowtype as the parent, INSERT ... RETURNING ctid returned (4294967295,0). The underlying local row was (0,1).
Rebuilt with v10. The GiST query returned five real ctids. FOR UPDATE returned five rows and the relation stayed 8192 bytes. A second foreign INSERT RETURNING ctid returned (0,2), matching the local table. After restart the new table still scanned. A relation extended before the fix still failed verification after restart.
The full regression suite was not run.
Held back on purpose
Tests. The v9 patch in the thread has a GiST check and a postgres_fdw RETURNING check, and both fail on a build without this assignment. They should be committed, on master and on the back branches. Shipping the code fix alone in time for the November releases is a reasonable split, as long as the tests follow. This is easy to regress: only the non-heap branches were wrong, and a LIMIT 1 query hides the GiST case.
A guard so an invalid TID cannot reach ReadBuffer() as P_NEW. The thread did not settle the place for that check. ItemPointerIsValid() rejects only a zero offset, so a TID such as (InvalidBlockNumber, 1) still extends the file. The moved-partitions marker is an InvalidBlockNumber with a different offset and has to stay reachable by its own path. The hazard is ReadBuffer()'s P_NEW, which is shared by any caller that turns a TID into a block number, so a heap-only message is a narrow net. Leaving the guard out of this back-patched fix is the right call. A separate master patch at the point where a TID becomes a block number is still worth writing.
tts_tableOid stays untouched on the buffer and virtual paths. Clear does not invalidate it. The thread's reading was that tableoid projection is pushed to the scan, so the hole is dormant. It is outside this fix.
An assertion that the slot TID equals the tuple TID belongs only on a path where both are expected to be valid, and it has to use the NoCheck accessors. ItemPointerEquals() asserts validity and would fire on the sentinel instead of on a mismatch. The patch correctly omits that assertion.
Conclusion
Commit the staged fix. The motivation is a wrong ctid and a durable extra block, the approach matches the existing store callbacks, and the implementation covers every slot type the function has. Follow it with the regression tests that are already written.
Assessment 78. Motivation, approach, and the code change are in good shape. The missing tests are a completeness gap, and the P_NEW guard is unfinished design that this commit rightly does not pretend to close.
Consensus 74. The thread ran from the original report through several patch revisions. Review comments on the virtual-slot path, the test shape, and the weak TID check were taken up. The committer posted this version for the November releases and the patch author agreed. The guard's final location was set aside rather than closed, which keeps this short of a fully settled side discussion.