diff --git a/src/backend/access/heap/hio.c b/src/backend/access/heap/hio.c index 6db73bf..40131ca 100644 --- a/src/backend/access/heap/hio.c +++ b/src/backend/access/heap/hio.c @@ -283,6 +283,8 @@ RelationGetBufferForTuple(Relation relation, Size len, } else if (bistate && bistate->current_buf != InvalidBuffer) targetBlock = BufferGetBlockNumber(bistate->current_buf); + else if (!use_fsm) + targetBlock = InvalidBlockNumber; else targetBlock = RelationGetTargetBlock(relation); diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 1c7eded..0d5171d 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -1169,76 +1169,62 @@ ExecuteTruncate(TruncateStmt *stmt) /* * OK, truncate each table. + * + * We used to call heap_truncate_one_rel() in some corner cases, but it + * is no longer safe to do so and that behavior is now fully removed. */ mySubid = GetCurrentSubTransactionId(); foreach(cell, rels) { Relation rel = (Relation) lfirst(cell); + Oid heap_relid; + Oid toast_relid; + MultiXactId minmulti; /* - * Normally, we need a transaction-safe truncation here. However, if - * the table was either created in the current (sub)transaction or has - * a new relfilenode in the current (sub)transaction, then we can just - * truncate it in-place, because a rollback would cause the whole - * table or the current physical file to be thrown away anyway. + * This effectively deletes all rows in the table, and may be done + * in a serializable transaction. In that case we must record a + * rw-conflict in to this transaction from each transaction + * holding a predicate lock on the table. */ - if (rel->rd_createSubid == mySubid || - rel->rd_newRelfilenodeSubid == mySubid) - { - /* Immediate, non-rollbackable truncation is OK */ - heap_truncate_one_rel(rel); - } - else - { - Oid heap_relid; - Oid toast_relid; - MultiXactId minmulti; + CheckTableForSerializableConflictIn(rel); - /* - * This effectively deletes all rows in the table, and may be done - * in a serializable transaction. In that case we must record a - * rw-conflict in to this transaction from each transaction - * holding a predicate lock on the table. - */ - CheckTableForSerializableConflictIn(rel); + minmulti = GetOldestMultiXactId(); - minmulti = GetOldestMultiXactId(); + /* + * Need the full transaction-safe pushups. + * + * Create a new empty storage file for the relation, and assign it + * as the relfilenode value. The old storage file is scheduled for + * deletion at commit. + */ + RelationSetNewRelfilenode(rel, rel->rd_rel->relpersistence, + RecentXmin, minmulti); + if (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED) + heap_create_init_fork(rel); - /* - * Need the full transaction-safe pushups. - * - * Create a new empty storage file for the relation, and assign it - * as the relfilenode value. The old storage file is scheduled for - * deletion at commit. - */ + heap_relid = RelationGetRelid(rel); + toast_relid = rel->rd_rel->reltoastrelid; + + /* + * The same for the toast table, if any. + */ + if (OidIsValid(toast_relid)) + { + rel = relation_open(toast_relid, AccessExclusiveLock); RelationSetNewRelfilenode(rel, rel->rd_rel->relpersistence, RecentXmin, minmulti); if (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED) heap_create_init_fork(rel); - - heap_relid = RelationGetRelid(rel); - toast_relid = rel->rd_rel->reltoastrelid; - - /* - * The same for the toast table, if any. - */ - if (OidIsValid(toast_relid)) - { - rel = relation_open(toast_relid, AccessExclusiveLock); - RelationSetNewRelfilenode(rel, rel->rd_rel->relpersistence, - RecentXmin, minmulti); - if (rel->rd_rel->relpersistence == RELPERSISTENCE_UNLOGGED) - heap_create_init_fork(rel); - heap_close(rel, NoLock); - } - - /* - * Reconstruct the indexes to match, and we're done. - */ - reindex_relation(heap_relid, REINDEX_REL_PROCESS_TOAST, 0); + heap_close(rel, NoLock); } + /* + * Reconstruct the indexes to match, and we're done. + */ + reindex_relation(heap_relid, REINDEX_REL_PROCESS_TOAST, 0); + pgstat_count_truncate(rel); }