TRUNCATE of a user_catalog_table discards rows that logical decoding still needs: an output plugin reading the table through the historic snapshot sees it empty when decoding transactions that committed before the TRUNCATE ============================================================================================================================================================================================================================== AI-generated bug report from the AI Annex Report ID: 01a22d4e-441e-7636-9c82-83b1b1b4c9cd Filed: 2026-11-30 16:33:53 UTC Severity: 45/100 Blamed commit: 66abc2608c7c00fcd449e00a9e23f13f02e65d04 ## Summary Output plugins may read tables marked user_catalog_table = true through the historic (decoding) snapshot. Rewrites of such tables are rejected ("cannot rewrite table ... used as a catalog table") because historic access relies on the current relfilenode still containing the older row versions. TRUNCATE is not rejected. It switches the table to a new, empty relfilenode. When a transaction that committed before the TRUNCATE is decoded afterwards, which is the normal case because decoding lags, the plugin's systable scan returns no rows. The historic snapshot would show three rows. VACUUM FULL and CLUSTER keep the recently dead rows and give the right answer. Only TRUNCATE loses them. ## Reproduction Settings: wal_level = logical. The plugin library must be listed in output_plugin_libraries. Minimal output plugin (uctcount.c, built with PGXS, MODULES = uctcount). For each change to table "t" it counts the rows of user catalog table "uct" with a systable scan: #include "postgres.h" #include "access/genam.h" #include "access/table.h" #include "catalog/namespace.h" #include "replication/logical.h" #include "replication/output_plugin.h" #include "utils/rel.h" PG_MODULE_MAGIC; static void startup(LogicalDecodingContext *ctx, OutputPluginOptions *opt, bool is_init) { opt->output_type = OUTPUT_PLUGIN_TEXTUAL_OUTPUT; } static void begin(LogicalDecodingContext *ctx, ReorderBufferTXN *txn) { } static void commit(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, XLogRecPtr lsn) { } static void change(LogicalDecodingContext *ctx, ReorderBufferTXN *txn, Relation rel, ReorderBufferChange *chg) { Relation uct; SysScanDesc scan; int n = 0; if (strcmp(RelationGetRelationName(rel), "t") != 0) return; uct = table_open(RelnameGetRelid("uct"), AccessShareLock); scan = systable_beginscan(uct, InvalidOid, false, NULL, 0, NULL); while (systable_getnext(scan) != NULL) n++; systable_endscan(scan); table_close(uct, AccessShareLock); OutputPluginPrepareWrite(ctx, true); appendStringInfo(ctx->out, "uct rows seen: %d", n); OutputPluginWrite(ctx, true); } void _PG_output_plugin_init(OutputPluginCallbacks *cb) { cb->startup_cb = startup; cb->begin_cb = begin; cb->change_cb = change; cb->commit_cb = commit; } SQL: CREATE TABLE uct(k int) WITH (user_catalog_table = true); CREATE TABLE t(id int); INSERT INTO uct SELECT generate_series(1, 3); SELECT 'init' FROM pg_create_logical_replication_slot('s', 'uctcount'); -- control: decoded before the TRUNCATE INSERT INTO t VALUES (1); SELECT data FROM pg_logical_slot_get_changes('s', NULL, NULL); -- uct rows seen: 3 -- committed while uct has three rows, decoded after TRUNCATE INSERT INTO t VALUES (2); TRUNCATE uct; SELECT data FROM pg_logical_slot_get_changes('s', NULL, NULL); -- expected: uct rows seen: 3 -- actual: uct rows seen: 0 -- comparison: VACUUM FULL keeps the old rows INSERT INTO uct SELECT generate_series(1, 3); INSERT INTO t VALUES (3); VACUUM FULL uct; SELECT data FROM pg_logical_slot_get_changes('s', NULL, NULL); -- uct rows seen: 3 ## Analysis RelationInitPhysicalAddr() (relcache.c) always uses the current relfilenode when a historic snapshot is active and the relation is accessible in logical decoding. Its comment says: "The new file will still contain older rows so lookups in them will work correctly", and that rewrites "are prevented both on catalog tables and on user tables declared as additional catalog tables". Commit 66abc2608c7c added that prevention for ALTER TABLE rewrites (the RelationIsUsedAsCatalogTable() check in ATRewriteTables()). TRUNCATE (ExecuteTruncate / truncate_check_rel) has no equivalent check. For system catalogs, TRUNCATE is rejected as a system catalog operation. For user catalog tables it is allowed. It installs an empty relfilenode, so every older row version that a not-yet-decoded transaction's historic snapshot should see is gone. ## Notes Tested on master e6671a7489e933d5e8a5e0380a9e4fb0602d9141 with a randomized oracle (each change records the table contents seen by its own transaction; the plugin compares them with a historic snapshot scan). The oracle was clean for DML, savepoint rollbacks, spilled transactions, VACUUM FULL and CLUSTER on the user catalog table. Every mismatch without option toggling minimized to an earlier transaction followed by a committed TRUNCATE. Related variant: ALTER TABLE ... SET (user_catalog_table = false) followed by a rewriting ALTER TABLE (ALTER COLUMN TYPE ... USING, ADD COLUMN with a volatile default) gives the same symptom for transactions decoded later. The rewrite inserts tuples with a new xmin, and the historic relcache still treats the table as a catalog and reads the current file. That needs the option to be turned off first, so it may be considered user error. It shares the current-relfilenode assumption. Branches: likely REL_14_STABLE through master (the ATRewriteTables() check from 66abc2608c7c is present on all of them and TRUNCATE has no equivalent; reproduced on master only).