From a886a43e62c4702d27e1dd26794deca1ba45b56d Mon Sep 17 00:00:00 2001 From: Sami Imseih Date: Tue, 1 Dec 2026 16:01:28 +0000 Subject: [PATCH v23 5/6] test_custom_stats: Show transactional counters in a custom stats kind To demonstrate how a custom stats kind can implement in-transaction flushes, split the variable-numbered kind's pending counts into nontransactional counters, which can be flushed during a transaction, and transactional counters, flushed only at a transaction boundary. Add test_custom_stats_var_update_txn() to bump the transactional counters, report all four counters from test_custom_stats_var_report(), and test that the transactional counters stay out of shared stats until the transaction ends. Author: Sami Imseih Reviewed-by: Bertrand Drouvot Reviewed-by: Zsolt Parragi Discussion: https://postgr.es/m/CAA5RZ0uA-4qcD3+2hjcE_-zQUBhvWf5foPM2vzYneFKrJLsBDQ@mail.gmail.com --- .../test_custom_stats/t/001_custom_stats.pl | 50 +++++-- .../test_custom_var_stats--1.0.sql | 8 ++ .../test_custom_stats/test_custom_var_stats.c | 130 +++++++++++++++--- 3 files changed, 162 insertions(+), 26 deletions(-) diff --git a/src/test/modules/test_custom_stats/t/001_custom_stats.pl b/src/test/modules/test_custom_stats/t/001_custom_stats.pl index a9e06971baf..39413c6275d 100644 --- a/src/test/modules/test_custom_stats/t/001_custom_stats.pl +++ b/src/test/modules/test_custom_stats/t/001_custom_stats.pl @@ -93,25 +93,25 @@ foreach my $stats_kind (@var_stats_kinds) $result = $node->safe_psql('postgres', q(select * from test_custom_stats_var_report('entry1'))); is( $result, - "entry1|2|Test entry 1", + "entry1|2|2|0|0|Test entry 1", "variable-sized stats of entry1 for $kind_name"); $result = $node->safe_psql('postgres', q(select * from test_custom_stats_var_report('entry2'))); is( $result, - "entry2|3|Test entry 2", + "entry2|3|3|0|0|Test entry 2", "variable-sized stats of entry2 for $kind_name"); $result = $node->safe_psql('postgres', q(select * from test_custom_stats_var_report('entry3'))); is( $result, - "entry3|2|Test entry 3", + "entry3|2|2|0|0|Test entry 3", "variable-sized stats of entry3 for $kind_name"); $result = $node->safe_psql('postgres', q(select * from test_custom_stats_var_report('entry4'))); is( $result, - "entry4|3|Test entry 4", + "entry4|3|3|0|0|Test entry 4", "variable-sized stats of entry4 for $kind_name"); # Repeated flushes in one transaction must not double count. @@ -129,11 +129,43 @@ foreach my $stats_kind (@var_stats_kinds) $result = $node->safe_psql('postgres', q(select * from test_custom_stats_var_report('xact_entry'))); is( $result, - "xact_entry|3|Test xact entry", + "xact_entry|3|3|0|0|Test xact entry", "in-transaction flush does not double-count pending stats for $kind_name"); $node->safe_psql('postgres', q(select * from test_custom_stats_var_drop('xact_entry'))); + # Transactional counters stay deferred until the transaction boundary. + $node->safe_psql('postgres', + q(select test_custom_stats_var_create('txn_entry', 'Test txn entry'))); + + my $bg = $node->background_psql('postgres'); + $bg->query_safe('BEGIN'); + $bg->query_safe(q(select test_custom_stats_var_update('txn_entry'))); + $bg->query_safe(q(select test_custom_stats_var_update_txn('txn_entry'))); + $bg->query_safe(q(select pg_stat_force_next_flush())); + + $result = $node->safe_psql('postgres', + q(select calls, calls2, calls_txn, calls_txn2 + from test_custom_stats_var_report('txn_entry'))); + is($result, "1|1|0|0", + "transactional counter deferred at an in-transaction flush for $kind_name"); + + $bg->query_safe('COMMIT'); + $bg->quit; + + # After commit the deferred counter is flushed too. + $node->poll_query_until('postgres', + q(select calls_txn = 1 and calls_txn2 = 1 + from test_custom_stats_var_report('txn_entry'))) + or die "timed out waiting for transactional counter to be flushed for $kind_name"; + $result = $node->safe_psql('postgres', + q(select calls, calls2, calls_txn, calls_txn2 + from test_custom_stats_var_report('txn_entry'))); + is($result, "1|1|1|1", + "transactional counter flushed at the transaction boundary for $kind_name"); + $node->safe_psql('postgres', + q(select test_custom_stats_var_drop('txn_entry'))); + # Test drop of variable-sized stats via pgstat_drop_entry(). $node->safe_psql('postgres', q(select * from test_custom_stats_var_drop('entry3'))); @@ -183,14 +215,14 @@ foreach my $stats_kind (@var_stats_kinds) $result = $node->safe_psql('postgres', q(select * from test_custom_stats_var_report('entry1'))); is( $result, - "entry1|2|Test entry 1", + "entry1|2|2|0|0|Test entry 1", "variable-sized stats of entry1 persist after clean restart for $kind_name" ); $result = $node->safe_psql('postgres', q(select * from test_custom_stats_var_report('entry2'))); is( $result, - "entry2|3|Test entry 2", + "entry2|3|3|0|0|Test entry 2", "variable-sized stats of entry2 persist after clean restart for $kind_name" ); @@ -199,13 +231,13 @@ foreach my $stats_kind (@var_stats_kinds) $result = $node->safe_psql('postgres', q(select * from test_custom_stats_var_report('entry1'))); is( $result, - "entry1|0|Test entry 1", + "entry1|0|0|0|0|Test entry 1", "variable-sized stats of entry1 reset for $kind_name"); $result = $node->safe_psql('postgres', q(select * from test_custom_stats_var_report('entry2'))); is( $result, - "entry2|0|Test entry 2", + "entry2|0|0|0|0|Test entry 2", "variable-sized stats of entry2 reset for $kind_name"); # Test loss after crash recovery. diff --git a/src/test/modules/test_custom_stats/test_custom_var_stats--1.0.sql b/src/test/modules/test_custom_stats/test_custom_var_stats--1.0.sql index d58a310aa14..f3249bc9168 100644 --- a/src/test/modules/test_custom_stats/test_custom_var_stats--1.0.sql +++ b/src/test/modules/test_custom_stats/test_custom_var_stats--1.0.sql @@ -13,6 +13,11 @@ RETURNS void AS 'MODULE_PATHNAME', 'test_custom_stats_var_update' LANGUAGE C STRICT PARALLEL UNSAFE; +CREATE FUNCTION test_custom_stats_var_update_txn(IN name TEXT) +RETURNS void +AS 'MODULE_PATHNAME', 'test_custom_stats_var_update_txn' +LANGUAGE C STRICT PARALLEL UNSAFE; + CREATE FUNCTION test_custom_stats_var_drop(IN name TEXT) RETURNS void AS 'MODULE_PATHNAME', 'test_custom_stats_var_drop' @@ -35,6 +40,9 @@ LANGUAGE C STRICT PARALLEL UNSAFE; CREATE FUNCTION test_custom_stats_var_report(INOUT name TEXT, OUT calls BIGINT, + OUT calls2 BIGINT, + OUT calls_txn BIGINT, + OUT calls_txn2 BIGINT, OUT description TEXT) RETURNS SETOF record AS 'MODULE_PATHNAME', 'test_custom_stats_var_report' diff --git a/src/test/modules/test_custom_stats/test_custom_var_stats.c b/src/test/modules/test_custom_stats/test_custom_var_stats.c index dcc94b6697c..b2603ecb55f 100644 --- a/src/test/modules/test_custom_stats/test_custom_var_stats.c +++ b/src/test/modules/test_custom_stats/test_custom_var_stats.c @@ -57,10 +57,36 @@ PG_MODULE_MAGIC_EXT( *-------------------------------------------------------------------------- */ -/* Backend-local pending statistics before flush to shared memory */ -typedef struct PgStat_StatCustomVarEntry +/* + * Backend-local pending statistics before flush to shared memory. + * + * nontxn flushes on any flush. txn flushes only at a transaction + * boundary. This example only defers txn; it does not discard the counts + * of an aborted transaction. + */ +typedef struct PgStat_StatCustomVarCountsNonTxn { PgStat_Counter numcalls; /* times statistic was incremented */ + PgStat_Counter numcalls2; /* second non-transactional counter */ +} PgStat_StatCustomVarCountsNonTxn; + +typedef struct PgStat_StatCustomVarCountsTxn +{ + PgStat_Counter numcalls_txn; /* flushed only at a transaction boundary */ + PgStat_Counter numcalls_txn2; /* second transactional counter */ +} PgStat_StatCustomVarCountsTxn; + +StaticAssertDecl(sizeof(PgStat_StatCustomVarCountsNonTxn) == + 2 * sizeof(PgStat_Counter), + "PgStat_StatCustomVarCountsNonTxn has padding"); +StaticAssertDecl(sizeof(PgStat_StatCustomVarCountsTxn) == + 2 * sizeof(PgStat_Counter), + "PgStat_StatCustomVarCountsTxn has padding"); + +typedef struct PgStat_StatCustomVarEntry +{ + PgStat_StatCustomVarCountsNonTxn nontxn; + PgStat_StatCustomVarCountsTxn txn; } PgStat_StatCustomVarEntry; /* Shared memory statistics entry visible to all backends */ @@ -192,8 +218,9 @@ _PG_init(void) * Called by pgstat collector to flush accumulated local statistics * to shared memory where other backends can read them. * - * Returns PGSTAT_FLUSH_LOCK_CONFLICT only if nowait=true and lock acquisition - * fails. + * nontxn always flushes. txn flushes only at a transaction boundary. + * This callback uses the clear after flush pattern instead of a separate + * flushed baseline. */ static PgStat_FlushResult test_custom_stats_var_flush_pending_cb(PgStat_EntryRef *entry_ref, bool nowait, @@ -201,15 +228,43 @@ test_custom_stats_var_flush_pending_cb(PgStat_EntryRef *entry_ref, bool nowait, { PgStat_StatCustomVarEntry *pending_entry; PgStatShared_CustomVarEntry *shared_entry; + static const PgStat_StatCustomVarCountsNonTxn zero_nontxn = {0}; + static const PgStat_StatCustomVarCountsTxn zero_txn = {0}; + bool nontxn_pending; + bool txn_pending; pending_entry = (PgStat_StatCustomVarEntry *) entry_ref->pending; shared_entry = (PgStatShared_CustomVarEntry *) entry_ref->shared_stats; + /* If nothing nontransactional can flush now, avoid taking the lock. */ + nontxn_pending = + memcmp(&pending_entry->nontxn, &zero_nontxn, + sizeof(PgStat_StatCustomVarCountsNonTxn)) != 0; + txn_pending = + memcmp(&pending_entry->txn, &zero_txn, + sizeof(PgStat_StatCustomVarCountsTxn)) != 0; + + if (!nontxn_pending) + { + if (!xact_boundary && txn_pending) + return PGSTAT_FLUSH_PARTIAL; + if (!txn_pending) + return PGSTAT_FLUSH_DONE; + } + if (!pgstat_lock_entry(entry_ref, nowait)) return PGSTAT_FLUSH_LOCK_CONFLICT; - /* Add pending counts to shared totals */ - shared_entry->stats.numcalls += pending_entry->numcalls; + /* Always flush the nontransactional counters. */ + shared_entry->stats.nontxn.numcalls += pending_entry->nontxn.numcalls; + shared_entry->stats.nontxn.numcalls2 += pending_entry->nontxn.numcalls2; + + /* Flush transactional counters only at a transaction boundary. */ + if (xact_boundary) + { + shared_entry->stats.txn.numcalls_txn += pending_entry->txn.numcalls_txn; + shared_entry->stats.txn.numcalls_txn2 += pending_entry->txn.numcalls_txn2; + } pgstat_unlock_entry(entry_ref); @@ -219,7 +274,16 @@ test_custom_stats_var_flush_pending_cb(PgStat_EntryRef *entry_ref, bool nowait, * avoid counting it again on a later flush. Nothing reads these stats * within the current transaction, so zeroing is correct. */ - memset(pending_entry, 0, sizeof(*pending_entry)); + memset(&pending_entry->nontxn, 0, sizeof(PgStat_StatCustomVarCountsNonTxn)); + + if (!xact_boundary) + { + /* Transactional counters still pending; retain the entry. */ + if (txn_pending) + return PGSTAT_FLUSH_PARTIAL; + } + else + memset(&pending_entry->txn, 0, sizeof(PgStat_StatCustomVarCountsTxn)); return PGSTAT_FLUSH_DONE; } @@ -625,8 +689,8 @@ test_custom_stats_var_create(PG_FUNCTION_ARGS) * test_custom_stats_var_update * Increment custom statistic counter * - * Increments call count in backend-local memory. Changes are flushed - * to shared memory by the statistics collector. + * Increments the non-transactional counters in backend-local memory. + * Changes are flushed to shared memory by the statistics collector. */ PG_FUNCTION_INFO_V1(test_custom_stats_var_update); Datum @@ -641,7 +705,35 @@ test_custom_stats_var_update(PG_FUNCTION_ARGS) PGSTAT_CUSTOM_VAR_STATS_IDX(stat_name), NULL); pending_entry = (PgStat_StatCustomVarEntry *) entry_ref->pending; - pending_entry->numcalls++; + pending_entry->nontxn.numcalls++; + pending_entry->nontxn.numcalls2++; + + PG_RETURN_VOID(); +} + +/* + * test_custom_stats_var_update_txn + * Increment the transactional custom statistic counter + * + * Like test_custom_stats_var_update(), but increments the transactional + * counter, whose effect is made visible in shared memory only at a transaction + * boundary. + */ +PG_FUNCTION_INFO_V1(test_custom_stats_var_update_txn); +Datum +test_custom_stats_var_update_txn(PG_FUNCTION_ARGS) +{ + char *stat_name = text_to_cstring(PG_GETARG_TEXT_PP(0)); + PgStat_EntryRef *entry_ref; + PgStat_StatCustomVarEntry *pending_entry; + + /* Get pending entry in local memory */ + entry_ref = pgstat_prep_pending_entry(PGSTAT_KIND_TEST_CUSTOM_VAR_STATS, InvalidOid, + PGSTAT_CUSTOM_VAR_STATS_IDX(stat_name), NULL); + + pending_entry = (PgStat_StatCustomVarEntry *) entry_ref->pending; + pending_entry->txn.numcalls_txn++; + pending_entry->txn.numcalls_txn2++; PG_RETURN_VOID(); } @@ -749,8 +841,9 @@ test_custom_stats_var_reset(PG_FUNCTION_ARGS) * test_custom_stats_var_report * Retrieve custom statistic values * - * Returns single row with statistic name, call count, and description if the - * statistic exists, otherwise returns no rows. + * Returns single row with statistic name, the two non-transactional counters, + * the two transactional counters, and description if the statistic exists, + * otherwise returns no rows. */ PG_FUNCTION_INFO_V1(test_custom_stats_var_report); Datum @@ -783,8 +876,8 @@ test_custom_stats_var_report(PG_FUNCTION_ARGS) if (funcctx->call_cntr < funcctx->max_calls) { - Datum values[3]; - bool nulls[3] = {false, false, false}; + Datum values[6]; + bool nulls[6] = {false, false, false, false, false, false}; HeapTuple tuple; PgStat_EntryRef *entry_ref; PgStatShared_CustomVarEntry *shared_entry; @@ -817,12 +910,15 @@ test_custom_stats_var_report(PG_FUNCTION_ARGS) } values[0] = PointerGetDatum(cstring_to_text(stat_name)); - values[1] = Int64GetDatum(stat_entry->numcalls); + values[1] = Int64GetDatum(stat_entry->nontxn.numcalls); + values[2] = Int64GetDatum(stat_entry->nontxn.numcalls2); + values[3] = Int64GetDatum(stat_entry->txn.numcalls_txn); + values[4] = Int64GetDatum(stat_entry->txn.numcalls_txn2); if (description) - values[2] = PointerGetDatum(cstring_to_text(description)); + values[5] = PointerGetDatum(cstring_to_text(description)); else - nulls[2] = true; + nulls[5] = true; tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls); SRF_RETURN_NEXT(funcctx, HeapTupleGetDatum(tuple)); -- 2.50.1