From 9fb17dba049717318683040b37a853efded43e32 Mon Sep 17 00:00:00 2001 From: Konstantin Knizhnik Date: Fri, 5 Jul 2024 14:21:25 +0300 Subject: [PATCH] Add neon.max_vacuum_defer_cleanup_age to restrict bloat of master in case of using hot_standby_feedback --- pgxn/neon/walproposer_pg.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pgxn/neon/walproposer_pg.c b/pgxn/neon/walproposer_pg.c index 944b316344..2f9534a8c6 100644 --- a/pgxn/neon/walproposer_pg.c +++ b/pgxn/neon/walproposer_pg.c @@ -63,6 +63,8 @@ char *wal_acceptors_list = ""; int wal_acceptor_reconnect_timeout = 1000; int wal_acceptor_connection_timeout = 10000; +int max_vacuum_defer_cleanup_age = 0; + /* Set to true in the walproposer bgw. */ static bool am_walproposer; static WalproposerShmemState *walprop_shared; @@ -218,6 +220,16 @@ nwp_register_gucs(void) PGC_SIGHUP, GUC_UNIT_MS, NULL, NULL, NULL); + + DefineCustomIntVariable( + "neon.max_vacuum_defer_cleanup_age", + "Restrict oldest xmin pinned by hot standby feedback to prevent bloating of master", + NULL, + &max_vacuum_defer_cleanup_age, + 0, 0, INT_MAX, + PGC_SIGHUP, + 0, + NULL, NULL, NULL); } /* @@ -1855,6 +1867,7 @@ walprop_pg_process_safekeeper_feedback(WalProposer *wp, Safekeeper *sk) FullTransactionId xmin = hsFeedback.xmin; FullTransactionId catalog_xmin = hsFeedback.catalog_xmin; FullTransactionId next_xid = ReadNextFullTransactionId(); + /* * Page server is updating nextXid in checkpoint each 1024 transactions, * so feedback xmin can be actually larger then nextXid and @@ -1863,8 +1876,14 @@ walprop_pg_process_safekeeper_feedback(WalProposer *wp, Safekeeper *sk) */ if (FullTransactionIdPrecedes(next_xid, xmin)) xmin = next_xid; + else if (max_vacuum_defer_cleanup_age != 0 && xmin.value < next_xid.value - max_vacuum_defer_cleanup_age) + xmin.value = next_xid.value - max_vacuum_defer_cleanup_age; + if (FullTransactionIdPrecedes(next_xid, catalog_xmin)) catalog_xmin = next_xid; + else if (max_vacuum_defer_cleanup_age != 0 && catalog_xmin.value < next_xid.value - max_vacuum_defer_cleanup_age) + catalog_xmin.value = next_xid.value - max_vacuum_defer_cleanup_age; + agg_hs_feedback = hsFeedback; elog(DEBUG2, "ProcessStandbyHSFeedback(xmin=%d, catalog_xmin=%d", XidFromFullTransactionId(hsFeedback.xmin), XidFromFullTransactionId(hsFeedback.catalog_xmin)); ProcessStandbyHSFeedback(hsFeedback.ts,