agora inbox for [email protected]help / color / mirror / Atom feed
[PATCH v48 6/7] Error out any process that would block at REPACK 128+ messages / 2 participants [nested] [flat]
* [PATCH v48 6/7] Error out any process that would block at REPACK @ 2026-03-25 19:35 Álvaro Herrera <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Álvaro Herrera @ 2026-03-25 19:35 UTC (permalink / raw) Any process waiting on REPACK to release its lock would actually cause it to deadlock when it tries to upgrade its lock to AEL, losing all work done to that point. We avoid this by teaching the deadlock detector to raise an error when this condition is detected. --- src/backend/commands/repack.c | 52 ++++++++---- src/backend/storage/lmgr/deadlock.c | 15 ++++ src/include/storage/proc.h | 6 +- src/test/modules/injection_points/Makefile | 1 + .../expected/repack_deadlock.out | 63 ++++++++++++++ src/test/modules/injection_points/meson.build | 1 + .../specs/repack_deadlock.spec | 83 +++++++++++++++++++ 7 files changed, 202 insertions(+), 19 deletions(-) create mode 100644 src/test/modules/injection_points/expected/repack_deadlock.out create mode 100644 src/test/modules/injection_points/specs/repack_deadlock.spec diff --git a/src/backend/commands/repack.c b/src/backend/commands/repack.c index a514053b777..781a471fc1d 100644 --- a/src/backend/commands/repack.c +++ b/src/backend/commands/repack.c @@ -276,6 +276,16 @@ ExecRepack(ParseState *pstate, RepackStmt *stmt, bool isTopLevel) /* Determine the lock mode to use. */ lockmode = RepackLockLevel((params.options & CLUOPT_CONCURRENT) != 0); + /* + * If in concurrent mode, set the PROC_IN_CONCURRENT_REPACK flag. This + * makes the deadlock checker cause anyone that would conflict with us to + * error out. It's important to set this flag ahead of actually locking + * the relation; it won't of course affect anyone until we do have a lock + * that others can conflict with. + */ + if ((params.options & CLUOPT_CONCURRENT) != 0) + MyProc->statusFlags |= PROC_IN_CONCURRENT_REPACK; + /* * If a single relation is specified, process it and we're done ... unless * the relation is a partitioned table, in which case we fall through. @@ -476,11 +486,8 @@ RepackLockLevel(bool concurrent) * If indexOid is InvalidOid, the table will be rewritten in physical order * instead of index order. * - * Note that, in the concurrent case, the function releases the lock at some - * point, in order to get AccessExclusiveLock for the final steps (i.e. to - * swap the relation files). To make things simpler, the caller should expect - * OldHeap to be closed on return, regardless CLUOPT_CONCURRENT. (The - * AccessExclusiveLock is kept till the end of the transaction.) + * On return, OldHeap is closed but locked with AccessExclusiveLock - the lock + * will be released at end of the transaction. * * 'cmd' indicates which command is being executed, to be used for error * messages. @@ -512,10 +519,12 @@ cluster_rel(RepackCommand cmd, Relation OldHeap, Oid indexOid, /* * Make sure we're not in a transaction block. * - * The reason is that repack_setup_logical_decoding() could deadlock - * if there's an XID already assigned. It would be possible to run in - * a transaction block if we had no XID, but this restriction is - * simpler for users to understand and we don't lose anything. + * The reason is that repack_setup_logical_decoding() could wait + * indefinitely for our XID to complete. (The deadlock detector would + * not recognize it because we'd be waiting for ourselves, i.e. no + * real lock conflict.) It would be possible to run in a transaction + * block if we had no XID, but this restriction is simpler for users + * to understand and we don't lose anything. */ PreventInTransactionBlock(isTopLevel, "REPACK (CONCURRENTLY)"); @@ -998,10 +1007,8 @@ rebuild_relation(Relation OldHeap, Relation index, bool verbose, * Note that the worker has to wait for all transactions with XID * already assigned to finish. If some of those transactions is * waiting for a lock conflicting with ShareUpdateExclusiveLock on our - * table (e.g. it runs CREATE INDEX), we can end up in a deadlock. - * Not sure this risk is worth unlocking/locking the table (and its - * clustering index) and checking again if it's still eligible for - * REPACK CONCURRENTLY. + * table (e.g. it runs CREATE INDEX), it should encounter ERROR in the + * deadlock checking code. */ start_repack_decoding_worker(tableOid); @@ -3090,7 +3097,16 @@ rebuild_relation_finish_concurrent(Relation NewHeap, Relation OldHeap, LockRelationOid(OldHeap->rd_rel->reltoastrelid, AccessExclusiveLock); /* - * Tuples and pages of the old heap will be gone, but the heap will stay. + * Now that we have all access-exclusive locks on all relations, we no + * longer want other processes to error out when trying to acquire a + * conflicting lock. Therefore, unset our flag. + */ + MyProc->statusFlags &= ~PROC_IN_CONCURRENT_REPACK; + + /* + * Tuples and pages of the old heap will be gone, but the heap itself will + * stay. In order for predicate locks to continue to work, convert them + * to relation-level locks. We do this both for table and indexes. */ TransferPredicateLocksToHeapRelation(OldHeap); foreach_ptr(RelationData, index, indexrels) @@ -3288,9 +3304,11 @@ start_repack_decoding_worker(Oid relid) /* * The decoding setup must be done before the caller can have XID assigned - * for any reason, otherwise the worker might end up in a deadlock, - * waiting for the caller's transaction to end. Therefore wait here until - * the worker indicates that it has the logical decoding initialized. + * for any reason, otherwise the worker might end up waiting for the + * caller's transaction to end. (Deadlock detector does not consider this + * a conflict because the worker is in the same locking group as the + * backend that launched it.) Therefore wait here until the worker + * indicates that it has the logical decoding initialized. */ ConditionVariablePrepareToSleep(&shared->cv); for (;;) diff --git a/src/backend/storage/lmgr/deadlock.c b/src/backend/storage/lmgr/deadlock.c index b8962d875b6..c20ac682b0d 100644 --- a/src/backend/storage/lmgr/deadlock.c +++ b/src/backend/storage/lmgr/deadlock.c @@ -620,6 +620,21 @@ FindLockCycleRecurseMember(PGPROC *checkProc, proc->statusFlags & PROC_IS_AUTOVACUUM) blocking_autovacuum_proc = proc; + /* + * Similarly, if we note that we're blocked by some + * process running REPACK (CONCURRENTLY), just fail. That + * process is going to upgrade its lock at some point, and + * it would be inappropriate for any other process to + * cause that to fail. + */ + if (checkProc == MyProc && + proc->statusFlags & PROC_IN_CONCURRENT_REPACK) + ereport(ERROR, + errcode(ERRCODE_OBJECT_IN_USE), + errmsg("could not wait for concurrent REPACK"), + errdetail("Process %d waits for REPACK running on process %d", + MyProc->pid, proc->pid)); + /* We're done looking at this proclock */ break; } diff --git a/src/include/storage/proc.h b/src/include/storage/proc.h index 1dad125706e..8ad9718f3d6 100644 --- a/src/include/storage/proc.h +++ b/src/include/storage/proc.h @@ -69,10 +69,12 @@ struct XidCache #define PROC_AFFECTS_ALL_HORIZONS 0x20 /* this proc's xmin must be * included in vacuum horizons * in all databases */ +#define PROC_IN_CONCURRENT_REPACK 0x40 /* REPACK (CONCURRENTLY) */ -/* flags reset at EOXact */ +/* flags reset at EOXact. A bit of a misnomer ... */ #define PROC_VACUUM_STATE_MASK \ - (PROC_IN_VACUUM | PROC_IN_SAFE_IC | PROC_VACUUM_FOR_WRAPAROUND) + (PROC_IN_VACUUM | PROC_IN_SAFE_IC | PROC_VACUUM_FOR_WRAPAROUND | \ + PROC_IN_CONCURRENT_REPACK) /* * Xmin-related flags. Make sure any flags that affect how the process' Xmin diff --git a/src/test/modules/injection_points/Makefile b/src/test/modules/injection_points/Makefile index 2cd7d87c533..f7663859fe2 100644 --- a/src/test/modules/injection_points/Makefile +++ b/src/test/modules/injection_points/Makefile @@ -15,6 +15,7 @@ REGRESS_OPTS = --dlpath=$(top_builddir)/src/test/regress ISOLATION = basic \ inplace \ repack \ + repack_deadlock \ repack_toast \ syscache-update-pruned \ heap_lock_update diff --git a/src/test/modules/injection_points/expected/repack_deadlock.out b/src/test/modules/injection_points/expected/repack_deadlock.out new file mode 100644 index 00000000000..a86e4767536 --- /dev/null +++ b/src/test/modules/injection_points/expected/repack_deadlock.out @@ -0,0 +1,63 @@ +Parsed test spec with 2 sessions + +starting permutation: wait_before_lock add_column wakeup_before_lock check1 +injection_points_attach +----------------------- + +(1 row) + +step wait_before_lock: + REPACK (CONCURRENTLY) repack_deadlock USING INDEX repack_deadlock_pkey; + <waiting ...> +step add_column: + alter table repack_deadlock add column noise text; + <waiting ...> +step add_column: <... completed> +ERROR: could not wait for concurrent REPACK +step wakeup_before_lock: + SELECT injection_points_wakeup('repack-concurrently-before-lock'); + +injection_points_wakeup +----------------------- + +(1 row) + +step wait_before_lock: <... completed> +step check1: + INSERT INTO relfilenodes(node) + SELECT relfilenode FROM pg_class WHERE relname='repack_deadlock'; + + SELECT count(DISTINCT node) FROM relfilenodes; + + SELECT i, j FROM repack_deadlock ORDER BY i, j; + + INSERT INTO data_s1(i, j) + SELECT i, j FROM repack_deadlock; + + SELECT count(*) + FROM data_s1 d1 FULL JOIN data_s2 d2 USING (i, j) + WHERE d1.i ISNULL OR d2.i ISNULL; + +count +----- + 1 +(1 row) + +i|j +-+- +1|1 +2|2 +3|3 +4|4 +(4 rows) + +count +----- + 4 +(1 row) + +injection_points_detach +----------------------- + +(1 row) + diff --git a/src/test/modules/injection_points/meson.build b/src/test/modules/injection_points/meson.build index a414abb924b..1cd88d6db65 100644 --- a/src/test/modules/injection_points/meson.build +++ b/src/test/modules/injection_points/meson.build @@ -46,6 +46,7 @@ tests += { 'basic', 'inplace', 'repack', + 'repack_deadlock', 'repack_toast', 'syscache-update-pruned', 'heap_lock_update', diff --git a/src/test/modules/injection_points/specs/repack_deadlock.spec b/src/test/modules/injection_points/specs/repack_deadlock.spec new file mode 100644 index 00000000000..9d23a6588c2 --- /dev/null +++ b/src/test/modules/injection_points/specs/repack_deadlock.spec @@ -0,0 +1,83 @@ +# Test REPACK with a concurrent transaction that would cause a deadlock +setup +{ + CREATE EXTENSION injection_points; + + CREATE TABLE repack_deadlock(i int PRIMARY KEY, j int); + INSERT INTO repack_deadlock(i, j) VALUES (1, 1), (2, 2), (3, 3), (4, 4); + + CREATE TABLE relfilenodes(node oid); + + CREATE TABLE data_s1(i int, j int); + CREATE TABLE data_s2(i int, j int); +} + +teardown +{ + DROP TABLE repack_deadlock; + DROP EXTENSION injection_points; + + DROP TABLE relfilenodes; + DROP TABLE data_s1; + DROP TABLE data_s2; +} + +session s1 +setup +{ + SELECT injection_points_set_local(); + SELECT injection_points_attach('repack-concurrently-before-lock', 'wait'); +} +# Perform the initial load and wait for s2 to do some data changes. +step wait_before_lock +{ + REPACK (CONCURRENTLY) repack_deadlock USING INDEX repack_deadlock_pkey; +} +# Check the table from the perspective of s1. +# +# Besides the contents, we also check that relfilenode has changed. + +# Have each session write the contents into a table and use FULL JOIN to check +# if the outputs are identical. +step check1 +{ + INSERT INTO relfilenodes(node) + SELECT relfilenode FROM pg_class WHERE relname='repack_deadlock'; + + SELECT count(DISTINCT node) FROM relfilenodes; + + SELECT i, j FROM repack_deadlock ORDER BY i, j; + + INSERT INTO data_s1(i, j) + SELECT i, j FROM repack_deadlock; + + SELECT count(*) + FROM data_s1 d1 FULL JOIN data_s2 d2 USING (i, j) + WHERE d1.i ISNULL OR d2.i ISNULL; +} +teardown +{ + SELECT injection_points_detach('repack-concurrently-before-lock'); +} + +session s2 +# Change the existing data. UPDATE changes both key and non-key columns. Also +# update one row twice to test whether tuple version generated by this session +# can be found. +step add_column +{ + alter table repack_deadlock add column noise text; +} + +step wakeup_before_lock +{ + SELECT injection_points_wakeup('repack-concurrently-before-lock'); +} + +# Test if data changes introduced while one session is performing REPACK +# CONCURRENTLY find their way into the table. +permutation + wait_before_lock + add_column + wakeup_before_lock + check1 -- 2.47.3 --qfkt2ktdpcfeypib Content-Type: text/x-diff; charset=utf-8 Content-Disposition: attachment; filename="v48-0007-Teach-snapshot-builder-to-skip-transactions-runn.patch" ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-06-16 11:54 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-06-16 11:54 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index 2268cc277bc..127d4415084 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -609,11 +608,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -627,11 +623,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -705,6 +696,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -881,11 +874,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -901,8 +892,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -922,6 +912,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -932,33 +924,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2346,21 +2341,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2372,6 +2391,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2380,76 +2402,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v01-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
* [PATCH 1/8] Use tuple slot to pass tuples for rewriting. @ 2026-07-15 08:37 Antonin Houska <[email protected]> 0 siblings, 0 replies; 128+ messages in thread From: Antonin Houska @ 2026-07-15 08:37 UTC (permalink / raw) This patch tries to adopt the preferable way of handling tuples, i.e. pass the containing tuple slot rather than the actual tuple. The motivation is that heap_insert_for_repack() will need a slot in the near future, in order to call ExecInsertIndexTuples(). Also, in order to set values of dropped attributes to to NULL, it seems more compact to pass a tuple slot as a workspace than two arrays (one for values and one for nulls). --- src/backend/access/heap/heapam_handler.c | 199 +++++++++++++---------- 1 file changed, 110 insertions(+), 89 deletions(-) diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c index bf87430cf01..4db8a16f691 100644 --- a/src/backend/access/heap/heapam_handler.c +++ b/src/backend/access/heap/heapam_handler.c @@ -48,14 +48,13 @@ #include "utils/rel.h" #include "utils/tuplesort.h" -static void reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate); -static void heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull, - BulkInsertState bistate); -static HeapTuple reform_tuple(HeapTuple tuple, Relation OldHeap, - Relation NewHeap, Datum *values, bool *isnull); +static void reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate); +static void heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, + BulkInsertStateData *bistate); +static bool tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc); +static void clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform); static bool SampleHeapTupleVisible(TableScanDesc scan, Buffer buffer, HeapTuple tuple, @@ -603,11 +602,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, bool is_system_catalog; Tuplesortstate *tuplesort; TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); TupleTableSlot *slot; - int natts; - Datum *values; - bool *isnull; + TupleTableSlot *reform_slot; BufferHeapTupleTableSlot *hslot; BlockNumber prev_cblock = InvalidBlockNumber; bool concurrent = snapshot != NULL; @@ -621,11 +617,6 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, */ Assert(RelationGetTargetBlock(NewHeap) == InvalidBlockNumber); - /* Preallocate values/isnull arrays */ - natts = newTupDesc->natts; - values = palloc_array(Datum, natts); - isnull = palloc_array(bool, natts); - /* * In non-concurrent mode, initialize the rewrite operation. This is not * needed in concurrent mode. @@ -699,6 +690,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, slot = table_slot_create(OldHeap, NULL); hslot = (BufferHeapTupleTableSlot *) slot; + reform_slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsVirtual); /* * Scan through the OldHeap, either in OldIndex order or sequentially; @@ -875,11 +868,9 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, int64 ct_val[2]; if (!concurrent) - reform_and_rewrite_tuple(tuple, OldHeap, NewHeap, - values, isnull, rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* * In indexscan mode and also VACUUM FULL, report increase in @@ -895,8 +886,7 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, index_endscan(indexScan); if (tableScan != NULL) table_endscan(tableScan); - if (slot) - ExecDropSingleTupleTableSlot(slot); + ExecDropSingleTupleTableSlot(slot); /* * In scan-and-sort mode, complete the sort, then read out all live tuples @@ -916,6 +906,8 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, pgstat_progress_update_param(PROGRESS_REPACK_PHASE, PROGRESS_REPACK_PHASE_WRITE_NEW_HEAP); + slot = MakeSingleTupleTableSlot(RelationGetDescr(OldHeap), + &TTSOpsHeapTuple); for (;;) { HeapTuple tuple; @@ -926,33 +918,36 @@ heapam_relation_copy_for_cluster(Relation OldHeap, Relation NewHeap, if (tuple == NULL) break; + /* + * XXX Ideally we should use tuplesort_gettupleslot() above, but + * it retrieves minimal tuples and tuplesort_puttupleslot() cannot + * get the tuple descriptor from tuplesort created by + * tuplesort_begin_cluster(). + */ + ExecStoreHeapTuple(tuple, slot, false); + n_tuples += 1; if (!concurrent) - reform_and_rewrite_tuple(tuple, - OldHeap, NewHeap, - values, isnull, - rwstate); + reform_and_rewrite_tuple(slot, reform_slot, rwstate); else - heap_insert_for_repack(tuple, OldHeap, NewHeap, - values, isnull, bistate); + heap_insert_for_repack(NewHeap, slot, reform_slot, bistate); /* Report n_tuples */ pgstat_progress_update_param(PROGRESS_REPACK_HEAP_TUPLES_INSERTED, n_tuples); } + ExecDropSingleTupleTableSlot(slot); tuplesort_end(tuplesort); } + ExecDropSingleTupleTableSlot(reform_slot); + /* Write out any remaining tuples, and fsync if needed */ if (rwstate) end_heap_rewrite(rwstate); if (bistate) FreeBulkInsertState(bistate); - - /* Clean up */ - pfree(values); - pfree(isnull); } /* @@ -2340,21 +2335,45 @@ heapam_scan_sample_next_tuple(TableScanDesc scan, SampleScanState *scanstate, * currently only known to happen as an after-effect of ALTER TABLE * SET WITHOUT OIDS. * - * So, we must reconstruct the tuple from component Datums. + * So, we must reconstruct the tuple from component Datums. 'reform' slot + * is a workspace for this reconstruction. */ static void -reform_and_rewrite_tuple(HeapTuple tuple, - Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, RewriteState rwstate) +reform_and_rewrite_tuple(TupleTableSlot *src, TupleTableSlot *reform, + RewriteState rwstate) { - HeapTuple newtuple; + HeapTuple tuple, + newtuple; + bool shouldFree, + shouldFreeNew; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + /* + * The old tuple will not be modified, so do not request materialization. + * (A copy can be created for specific slot type though, e.g. + * TTSOpsMinimalTuple.) + */ + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + + /* No need to materialize, copy will be created anyway. */ + Assert(TTS_IS_VIRTUAL(reform)); + newtuple = ExecFetchSlotHeapTuple(reform, false, &shouldFreeNew); + } + else + { + newtuple = heap_copytuple(tuple); + shouldFreeNew = true; + } /* The heap rewrite module does the rest */ rewrite_heap_tuple(rwstate, tuple, newtuple); - heap_freetuple(newtuple); + if (shouldFree) + heap_freetuple(tuple); + if (shouldFreeNew) + heap_freetuple(newtuple); } /* @@ -2366,6 +2385,9 @@ reform_and_rewrite_tuple(HeapTuple tuple, * information). Thus we must use heap_insert() both during the * catch-up and here. * + * 'reform' is a slot to use for tuple "reforming", typically to get set + * values of dropped columns to NULL. + * * We pass the NO_LOGICAL flag to heap_insert() in order to skip logical * decoding: as soon as REPACK CONCURRENTLY swaps the relation files, it drops * this relation, so no logical replication subscription should need the data. @@ -2374,76 +2396,75 @@ reform_and_rewrite_tuple(HeapTuple tuple, * case. */ static void -heap_insert_for_repack(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull, BulkInsertState bistate) +heap_insert_for_repack(Relation rel, TupleTableSlot *src, + TupleTableSlot *reform, BulkInsertStateData *bistate) { - HeapTuple newtuple; + HeapTuple tuple; + bool shouldFree; + TupleTableSlot *slot; - newtuple = reform_tuple(tuple, OldHeap, NewHeap, values, isnull); + tuple = ExecFetchSlotHeapTuple(src, false, &shouldFree); + if (tuple_needs_reform(tuple, src->tts_tupleDescriptor)) + { + clear_dropped_attributes(tuple, reform); + slot = reform; + } + else + slot = src; - heap_insert(NewHeap, newtuple, GetCurrentCommandId(true), - HEAP_INSERT_NO_LOGICAL, bistate); + /* + * clear_dropped_attributes() should have deformed the tuple, so nothing + * should depend on it now. + */ + if (shouldFree) + heap_freetuple(tuple); - heap_freetuple(newtuple); + table_tuple_insert(rel, slot, GetCurrentCommandId(true), + TABLE_INSERT_NO_LOGICAL, bistate); } -/* - * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. - * - * Deform the given tuple, set values of dropped columns to NULL, and fill in - * any values from attmissingval; then form a new tuple and return it. If no - * attributes need to be changed, a copy of the original tuple is returned. - * Caller is responsible for freeing the returned tuple. - * - * XXX this coding assumes that both relations have the same tupledesc. - */ -static HeapTuple -reform_tuple(HeapTuple tuple, Relation OldHeap, Relation NewHeap, - Datum *values, bool *isnull) +static bool +tuple_needs_reform(HeapTuple tuple, TupleDesc tupDesc) { - TupleDesc oldTupDesc = RelationGetDescr(OldHeap); - TupleDesc newTupDesc = RelationGetDescr(NewHeap); - bool needs_reform = false; - /* * A short tuple might require values from attmissing val, so activate the * coding unconditionally in that case. The value might legitimally be * NULL otherwise, so this is slightly wasteful, but it probably beats * having to test each attribute for presence of attmissingval each time. */ - if (HeapTupleHeaderGetNatts(tuple->t_data) < newTupDesc->natts) - needs_reform = true; + if (HeapTupleHeaderGetNatts(tuple->t_data) < tupDesc->natts) + return true; - /* - * If the column has been dropped but a value is still present, we can - * optimize storage now by getting rid of it. - */ - if (!needs_reform) + /* Does it have dropped attributes? */ + for (int i = 0; i < tupDesc->natts; i++) { - for (int i = 0; i < newTupDesc->natts; i++) - { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped && - !heap_attisnull(tuple, i + 1, newTupDesc)) - { - needs_reform = true; - break; - } - } + if (TupleDescCompactAttr(tupDesc, i)->attisdropped && + !heap_attisnull(tuple, i + 1, tupDesc)) + return true; } - /* Skip work if no changes are needed */ - if (!needs_reform) - return heap_copytuple(tuple); + return false; +} - heap_deform_tuple(tuple, oldTupDesc, values, isnull); +/* + * Subroutine for reform_and_rewrite_tuple and heap_insert_for_repack. + * + * Set values of dropped columns to NULL, + */ +static void +clear_dropped_attributes(HeapTuple tuple, TupleTableSlot *reform) +{ + TupleDesc tupDesc = reform->tts_tupleDescriptor; + + /* Assuming 'reform' is virtual, this deforms the tuple. */ + Assert(TTS_IS_VIRTUAL(reform)); + ExecForceStoreHeapTuple(tuple, reform, false); - for (int i = 0; i < newTupDesc->natts; i++) + for (int i = 0; i < tupDesc->natts; i++) { - if (TupleDescCompactAttr(newTupDesc, i)->attisdropped) - isnull[i] = true; + if (TupleDescCompactAttr(tupDesc, i)->attisdropped) + reform->tts_isnull[i] = true; } - - return heap_form_tuple(newTupDesc, values, isnull); } /* -- 2.52.0 --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=v02-0002-Move-functions-to-repack.c.patch ^ permalink raw reply [nested|flat] 128+ messages in thread
end of thread, other threads:[~2026-07-15 08:37 UTC | newest] Thread overview: 128+ messages (download: mbox mbox.gz follow: Atom feed) -- links below jump to the message on this page -- 2026-03-25 19:35 [PATCH v48 6/7] Error out any process that would block at REPACK Álvaro Herrera <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-06-16 11:54 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]> 2026-07-15 08:37 [PATCH 1/8] Use tuple slot to pass tuples for rewriting. Antonin Houska <[email protected]>
This inbox is served by agora; see mirroring instructions for how to clone and mirror all data and code used for this inbox