From f31bcb42199116cfdbb8be86ed4de0c07c82a576 Mon Sep 17 00:00:00 2001 From: Mehmet YILMAZ Date: Tue, 29 Jul 2025 15:52:36 +0300 Subject: [PATCH] PG18 - Assert("HaveRegisteredOrActiveSnapshot() fix for cluster creation (#8073) fixes #8072 fixes #8055 https://github.com/postgres/postgres/commit/706054b11b959c865c0c7935c34d92370d7168d4 before fix when try to create cluster with assert on `citus_dev make test1 --destroy` ``` TRAP: failed Assert("HaveRegisteredOrActiveSnapshot()"), File: "heapam.c", Line: 232, PID: 75572 postgres: citus citus [local] SELECT(ExceptionalCondition+0x6e)[0x5585e16123e6] postgres: citus citus [local] SELECT(heap_insert+0x220)[0x5585e10709af] postgres: citus citus [local] SELECT(simple_heap_insert+0x33)[0x5585e1071a20] postgres: citus citus [local] SELECT(CatalogTupleInsert+0x32)[0x5585e1135843] /home/citus/.pgenv/pgsql-18beta2/lib/citus.so(+0x11e0aa)[0x7fa26f1ca0aa] /home/citus/.pgenv/pgsql-18beta2/lib/citus.so(+0x11b607)[0x7fa26f1c7607] /home/citus/.pgenv/pgsql-18beta2/lib/citus.so(+0x11bf25)[0x7fa26f1c7f25] /home/citus/.pgenv/pgsql-18beta2/lib/citus.so(+0x11d4e2)[0x7fa26f1c94e2] postgres: citus citus [local] SELECT(+0x1c267d)[0x5585e10e967d] postgres: citus citus [local] SELECT(+0x1c6ba0)[0x5585e10edba0] postgres: citus citus [local] SELECT(+0x1c7b80)[0x5585e10eeb80] postgres: citus citus [local] SELECT(CommitTransactionCommand+0xd)[0x5585e10eef0a] postgres: citus citus [local] SELECT(+0x575b3d)[0x5585e149cb3d] postgres: citus citus [local] SELECT(+0x5788ce)[0x5585e149f8ce] postgres: citus citus [local] SELECT(PostgresMain+0xae7)[0x5585e14a2088] postgres: citus citus [local] SELECT(BackendMain+0x51)[0x5585e149ab36] postgres: citus citus [local] SELECT(postmaster_child_launch+0x101)[0x5585e13d6b32] postgres: citus citus [local] SELECT(+0x4b273f)[0x5585e13d973f] postgres: citus citus [local] SELECT(+0x4b49f3)[0x5585e13db9f3] postgres: citus citus [local] SELECT(PostmasterMain+0x1089)[0x5585e13dcee2] postgres: citus citus [local] SELECT(main+0x1d7)[0x5585e12e3428] /lib/x86_64-linux-gnu/libc.so.6(+0x29d90)[0x7fa271421d90] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x80)[0x7fa271421e40] ``` --- .../distributed/metadata/node_metadata.c | 2 +- .../transaction/transaction_recovery.c | 2 +- src/include/pg_version_compat.h | 20 +++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/backend/distributed/metadata/node_metadata.c b/src/backend/distributed/metadata/node_metadata.c index 6c7a98587..2412a88a2 100644 --- a/src/backend/distributed/metadata/node_metadata.c +++ b/src/backend/distributed/metadata/node_metadata.c @@ -2930,7 +2930,7 @@ InsertNodeRow(int nodeid, char *nodeName, int32 nodePort, NodeMetadata *nodeMeta TupleDesc tupleDescriptor = RelationGetDescr(pgDistNode); HeapTuple heapTuple = heap_form_tuple(tupleDescriptor, values, isNulls); - CatalogTupleInsert(pgDistNode, heapTuple); + CATALOG_INSERT_WITH_SNAPSHOT(pgDistNode, heapTuple); CitusInvalidateRelcacheByRelid(DistNodeRelationId()); diff --git a/src/backend/distributed/transaction/transaction_recovery.c b/src/backend/distributed/transaction/transaction_recovery.c index fb5509def..a4ad3e094 100644 --- a/src/backend/distributed/transaction/transaction_recovery.c +++ b/src/backend/distributed/transaction/transaction_recovery.c @@ -106,7 +106,7 @@ LogTransactionRecord(int32 groupId, char *transactionName, FullTransactionId out TupleDesc tupleDescriptor = RelationGetDescr(pgDistTransaction); HeapTuple heapTuple = heap_form_tuple(tupleDescriptor, values, isNulls); - CatalogTupleInsert(pgDistTransaction, heapTuple); + CATALOG_INSERT_WITH_SNAPSHOT(pgDistTransaction, heapTuple); CommandCounterIncrement(); diff --git a/src/include/pg_version_compat.h b/src/include/pg_version_compat.h index 385aecd38..997ad4b58 100644 --- a/src/include/pg_version_compat.h +++ b/src/include/pg_version_compat.h @@ -13,6 +13,10 @@ #include "pg_version_constants.h" +/* we need these for PG-18’s PushActiveSnapshot/PopActiveSnapshot APIs */ +#include "access/xact.h" +#include "utils/snapmgr.h" + #if PG_VERSION_NUM >= PG_VERSION_18 #define create_foreignscan_path_compat(a, b, c, d, e, f, g, h, i, j, k) \ create_foreignscan_path( \ @@ -36,6 +40,14 @@ /* PG-18 unified row-compare operator codes under COMPARE_* */ #define ROWCOMPARE_NE COMPARE_NE +#define CATALOG_INSERT_WITH_SNAPSHOT(rel, tup) \ + do { \ + Snapshot __snap = GetTransactionSnapshot(); \ + PushActiveSnapshot(__snap); \ + CatalogTupleInsert((rel), (tup)); \ + PopActiveSnapshot(); \ + } while (0) + #elif PG_VERSION_NUM >= PG_VERSION_17 #define create_foreignscan_path_compat(a, b, c, d, e, f, g, h, i, j, k) \ create_foreignscan_path( \ @@ -43,6 +55,10 @@ (e), (f), \ (g), (h), (i), (j), (k) \ ) + +/* no-op wrapper on older PGs */ +#define CATALOG_INSERT_WITH_SNAPSHOT(rel, tup) \ + CatalogTupleInsert((rel), (tup)) #endif #if PG_VERSION_NUM >= PG_VERSION_17 @@ -453,6 +469,10 @@ getStxstattarget_compat(HeapTuple tup) k) create_foreignscan_path(a, b, c, d, e, f, g, h, \ i, k) +/* no-op wrapper on older PGs */ +#define CATALOG_INSERT_WITH_SNAPSHOT(rel, tup) \ + CatalogTupleInsert((rel), (tup)) + #define getProcNo_compat(a) (a->pgprocno) #define getLxid_compat(a) (a->lxid)