From 807fcba7b6e7c6e0f4e272f8a3eee0415b3e45fd Mon Sep 17 00:00:00 2001 From: Dmitry Fomin Date: Sat, 28 Nov 2026 15:15:29 +0100 Subject: [PATCH 3/3] v18 0003: test_wait_timing coverage for the local buffer and drain Add a test module exercising the local wait-event buffer and its safe-point drain through fake in-core consumers. The module registers dedicated custom Extension wait events and injects its synthetic waits only under those codes; every observation (consumer deliveries and buffer inspection) filters on them, so incidental real waits are invisible and the results do not depend on load. Checks that need an exact buffer state run inside a single C call, so no real wait can intervene. To stay deterministic under CLOBBER_CACHE_ALWAYS and contention, every scenario but the dedicated overflow and high-water ones uses a large capacity, and a large drain interval keeps elapsed wall time between statements from draining either; a real wait therefore cannot fill the buffer or trip the time trigger in the middle of a scenario. Where the point is that a requested drain is served by an interrupt, the CHECK_FOR_INTERRUPTS() runs in the same C call as the records, so the outcome does not hinge on when an incidental interrupt check fell. Coverage includes: records delivered with the context in force at wait start; a wait in flight across a drain; overflow accounting; the high-water and time-based drain triggers; a consumer that raises an error (the batch is discarded, the buffer is not wedged); consumers that register or unregister from their own callback; the public pgstat_report_wait_start()/pgstat_report_wait_end() inlines and pgstat_report_wait_end_abort(); and the final drain at backend exit (TAP). Auxiliary-process drain placement and fork() child initialization cannot be reached from a test module and are only code-reviewed; the module header says so. --- src/test/modules/Makefile | 1 + src/test/modules/meson.build | 1 + src/test/modules/test_wait_timing/Makefile | 24 + .../expected/test_wait_timing.out | 999 ++++++++++++++++++ src/test/modules/test_wait_timing/meson.build | 38 + .../test_wait_timing/sql/test_wait_timing.sql | 254 +++++ .../test_wait_timing/t/001_exit_drain.pl | 75 ++ .../test_wait_timing--1.0.sql | 120 +++ .../test_wait_timing/test_wait_timing.c | 763 +++++++++++++ .../test_wait_timing/test_wait_timing.control | 4 + 10 files changed, 2279 insertions(+) create mode 100644 src/test/modules/test_wait_timing/Makefile create mode 100644 src/test/modules/test_wait_timing/expected/test_wait_timing.out create mode 100644 src/test/modules/test_wait_timing/meson.build create mode 100644 src/test/modules/test_wait_timing/sql/test_wait_timing.sql create mode 100644 src/test/modules/test_wait_timing/t/001_exit_drain.pl create mode 100644 src/test/modules/test_wait_timing/test_wait_timing--1.0.sql create mode 100644 src/test/modules/test_wait_timing/test_wait_timing.c create mode 100644 src/test/modules/test_wait_timing/test_wait_timing.control diff --git a/src/test/modules/Makefile b/src/test/modules/Makefile index 6c1504ac932..60bc52c7ea7 100644 --- a/src/test/modules/Makefile +++ b/src/test/modules/Makefile @@ -55,6 +55,7 @@ SUBDIRS = \ test_slru \ test_tidstore \ test_wait_lsn \ + test_wait_timing \ unsafe_tests \ worker_spi \ xid_wraparound diff --git a/src/test/modules/meson.build b/src/test/modules/meson.build index ef3cdb8faf9..ae6963a8fab 100644 --- a/src/test/modules/meson.build +++ b/src/test/modules/meson.build @@ -57,6 +57,7 @@ subdir('test_shm_mq') subdir('test_slru') subdir('test_tidstore') subdir('test_wait_lsn') +subdir('test_wait_timing') subdir('typcache') subdir('unsafe_tests') subdir('worker_spi') diff --git a/src/test/modules/test_wait_timing/Makefile b/src/test/modules/test_wait_timing/Makefile new file mode 100644 index 00000000000..a2d624e1535 --- /dev/null +++ b/src/test/modules/test_wait_timing/Makefile @@ -0,0 +1,24 @@ +# src/test/modules/test_wait_timing/Makefile + +MODULE_big = test_wait_timing +OBJS = \ + $(WIN32RES) \ + test_wait_timing.o +PGFILEDESC = "test_wait_timing - tests for the local wait event buffer" + +EXTENSION = test_wait_timing +DATA = test_wait_timing--1.0.sql + +TAP_TESTS = 1 +REGRESS = test_wait_timing + +ifdef USE_PGXS +PG_CONFIG = pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) +else +subdir = src/test/modules/test_wait_timing +top_builddir = ../../../.. +include $(top_builddir)/src/Makefile.global +include $(top_srcdir)/contrib/contrib-global.mk +endif diff --git a/src/test/modules/test_wait_timing/expected/test_wait_timing.out b/src/test/modules/test_wait_timing/expected/test_wait_timing.out new file mode 100644 index 00000000000..020b642e42a --- /dev/null +++ b/src/test/modules/test_wait_timing/expected/test_wait_timing.out @@ -0,0 +1,999 @@ +-- Tests for the local wait event buffer and its drain. +-- +-- Once timing is enabled the backend also records its own real waits (such +-- as waiting for the next client command), whenever they happen. So this +-- test never injects raw wait event numbers and never counts raw records: +-- it injects waits under dedicated custom wait events (test_wait_timing_1 .. +-- _4, see wt_code()), which cannot collide with a core event, and the test +-- consumers and wt_buffer() ignore every other record. The results below +-- therefore do not depend on timing or load. Checks that need an exact +-- buffer state run inside one C call (see test_wait_timing.c). +-- +-- To keep the scenarios load-independent, all but the dedicated overflow and +-- high-water tests use a large capacity, so incidental real waits cannot fill +-- the buffer and provoke an unexpected drain; a large time trigger (set just +-- below) keeps the elapsed wall time between statements from draining either. +CREATE EXTENSION test_wait_timing; +CREATE FUNCTION dump_buffer() RETURNS text LANGUAGE sql AS +$$ SELECT format('allocated=%s buffered=%s draining=%s in_flight=%s', + allocated, buffered, draining, in_flight) + FROM wt_buffer() $$; +-- what consumer c has been handed +CREATE FUNCTION dump_seen(c int DEFAULT 1) RETURNS text LANGUAGE sql AS +$$ SELECT format('c%s: callbacks=%s records=%s', c, callbacks, records) + FROM wt_seen(c) $$; +CREATE FUNCTION dump_records(c int DEFAULT 1) RETURNS text LANGUAGE sql AS +$$ SELECT coalesce(string_agg(format('%s/%s/%s/%s', event, context, + duration_ok, end_ns_ordered), ' '), '(none)') + FROM wt_seen_records(c) $$; +CREATE FUNCTION dump_all_seen() RETURNS text LANGUAGE sql AS +$$ SELECT string_agg(dump_seen(c), '; ' ORDER BY c) FROM generate_series(1, 3) c $$; +-- The custom events exist, are distinct, and belong to the Extension class. +SELECT count(DISTINCT wt_code(n)) AS distinct_codes, + bool_and((wt_code(n) >> 24) = 7) AS extension_class + FROM generate_series(1, 4) n; + distinct_codes | extension_class +----------------+----------------- + 4 | t +(1 row) + +-- A huge time trigger keeps the drains below under the test's control. +SELECT wt_set_interval_ns(1000000000000000); + wt_set_interval_ns +-------------------- + +(1 row) + +-- Disabled by default: no buffer, and timed waits leave no trace. +SELECT dump_buffer(); + dump_buffer +----------------------------------------------- + allocated=f buffered=0 draining=f in_flight=f +(1 row) + +SELECT wt_record(wt_code(1), 0, 100); + wt_record +----------- + +(1 row) + +SELECT dump_seen(); + dump_seen +--------------------------- + c1: callbacks=0 records=0 +(1 row) + +-- Enable, record completed waits, drain: exactly those records arrive, each +-- with the context in force when its wait started, in order. A context +-- changed after the wait began must not affect its record. +SELECT wt_enable(2048); + wt_enable +----------- + +(1 row) + +SELECT dump_buffer(); + dump_buffer +----------------------------------------------- + allocated=t buffered=0 draining=f in_flight=f +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record(wt_code(1), 111, 1000); + wt_record +----------- + +(1 row) + +SELECT wt_record(wt_code(2), 222, 1000); + wt_record +----------- + +(1 row) + +SELECT wt_record(wt_code(3), 333, 1000); + wt_record +----------- + +(1 row) + +SELECT wt_record_ctx_change(wt_code(4), 444, 555, 1000); + wt_record_ctx_change +---------------------- + +(1 row) + +SELECT 'four buffered: ' || dump_buffer(); + ?column? +-------------------------------------------------------------- + four buffered: allocated=t buffered=4 draining=f in_flight=f +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_seen(); + dump_seen +--------------------------- + c1: callbacks=1 records=4 +(1 row) + +SELECT dump_records(); + dump_records +----------------------------------------- + 1/111/t/t 2/222/t/t 3/333/t/t 4/444/t/t +(1 row) + +SELECT dump_buffer(); + dump_buffer +----------------------------------------------- + allocated=t buffered=0 draining=f in_flight=f +(1 row) + +-- Draining an empty buffer calls no consumer. +SELECT wt_drain_empty() AS consumer_calls; + consumer_calls +---------------- + 0 +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_seen(); + dump_seen +--------------------------- + c1: callbacks=0 records=0 +(1 row) + +-- A wait in flight survives a drain: the drain leaves the in-flight state +-- alone, and the wait is recorded when it ends, with its full duration. +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT * FROM wt_inflight_drain(wt_code(1), 77, 1000); + in_flight_before | in_flight_after_drain | start_kept +------------------+-----------------------+------------ + t | t | t +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_seen(); + dump_seen +--------------------------- + c1: callbacks=1 records=1 +(1 row) + +SELECT dump_records(); + dump_records +-------------- + 1/77/t/t +(1 row) + +-- An aborted wait leaves no record, and its in-flight state is forgotten; +-- an end with no matching start is ignored. +SELECT * FROM wt_abort_check(wt_code(2), 666); + in_flight_after_begin | cleared_by_abort | records_added | clear_after_unmatched_end +-----------------------+------------------+---------------+--------------------------- + t | t | 0 | t +(1 row) + +SELECT wt_disable(); + wt_disable +------------ + +(1 row) + +-- The public reporting inlines feed the buffer too: a wait recorded through +-- pgstat_report_wait_start()/pgstat_report_wait_end() arrives like any other, +-- and pgstat_report_wait_end_abort() clears the reported event (checked inside +-- the C call) without appending a record. +SELECT wt_enable(2048); + wt_enable +----------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_public(wt_code(1), 88, 1000) AS reported_cleared; + reported_cleared +------------------ + t +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_seen(); + dump_seen +--------------------------- + c1: callbacks=1 records=1 +(1 row) + +SELECT dump_records(); + dump_records +-------------- + 1/88/t/t +(1 row) + +SELECT * FROM wt_abort_public_check(wt_code(2), 99); + reported_and_in_flight | cleared_by_abort | records_added +------------------------+------------------+--------------- + t | t | 0 +(1 row) + +SELECT wt_disable(); + wt_disable +------------ + +(1 row) + +-- Overflow: records beyond capacity are counted, and their time is kept. +-- (Real waits may add to the dropped count, hence the comparison.) +SELECT wt_enable(16); + wt_enable +----------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT 'drain_requested=' || wt_record_n(wt_code(1), 7, 20, 100); + ?column? +---------------------- + drain_requested=true +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_seen(); + dump_seen +---------------------------- + c1: callbacks=1 records=16 +(1 row) + +SELECT dropped_count >= 4 AS dropped_at_least_4, dropped_ns_positive FROM wt_seen(); + dropped_at_least_4 | dropped_ns_positive +--------------------+--------------------- + t | t +(1 row) + +SELECT wt_disable(); + wt_disable +------------ + +(1 row) + +-- High-water mark: a drain is requested only above it. CHECK_FOR_INTERRUPTS() +-- runs in the same C call as the records (wt_record_n_check), so whether the +-- pending drain is served is deterministic: below the mark nothing is drained +-- and the records stay; above it the interrupt empties the buffer. +SELECT wt_enable(16); + wt_enable +----------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_n_check(wt_code(1), 8, 11, 100) AS below_high_water_buffered; + below_high_water_buffered +--------------------------- + 11 +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_n_check(wt_code(1), 8, 13, 100) AS above_high_water_buffered; + above_high_water_buffered +--------------------------- + 0 +(1 row) + +SELECT dump_seen(); + dump_seen +---------------------------- + c1: callbacks=1 records=13 +(1 row) + +SELECT wt_disable(); + wt_disable +------------ + +(1 row) + +-- Time trigger: with a zero interval a single wait requests a drain, served +-- here by the in-call CHECK_FOR_INTERRUPTS(). +SELECT wt_enable(2048); + wt_enable +----------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_set_interval_ns(0); + wt_set_interval_ns +-------------------- + +(1 row) + +SELECT wt_record_n_check(wt_code(1), 9, 1, 100) AS buffered_after_check; + buffered_after_check +---------------------- + 0 +(1 row) + +SELECT dump_seen(); + dump_seen +--------------------------- + c1: callbacks=1 records=1 +(1 row) + +SELECT wt_set_interval_ns(1000000000000000); + wt_set_interval_ns +-------------------- + +(1 row) + +SELECT wt_disable(); + wt_disable +------------ + +(1 row) + +-- A consumer that raises ERROR must not wedge the buffer: the batch is +-- discarded, the buffer is emptied and not draining, recording works again. +SELECT wt_enable(2048); + wt_enable +----------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_set_throw(true); + wt_set_throw +-------------- + +(1 row) + +SELECT 'drain_requested=' || wt_record_n(wt_code(1), 10, 3, 100); + ?column? +----------------------- + drain_requested=false +(1 row) + +SELECT * FROM wt_drain_catch(); + errmsg | raw_count | draining +---------------------+-----------+---------- + test consumer error | 0 | f +(1 row) + +SELECT 'consumer saw the batch: ' || dump_seen(); + ?column? +--------------------------------------------------- + consumer saw the batch: c1: callbacks=1 records=3 +(1 row) + +SELECT wt_set_throw(false); + wt_set_throw +-------------- + +(1 row) + +SELECT 'drain_requested=' || wt_record_n(wt_code(2), 11, 2, 100); + ?column? +----------------------- + drain_requested=false +(1 row) + +SELECT 'recording works again: ' || dump_buffer(); + ?column? +---------------------------------------------------------------------- + recording works again: allocated=t buffered=2 draining=f in_flight=f +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_seen(); + dump_seen +--------------------------- + c1: callbacks=2 records=5 +(1 row) + +SELECT wt_disable(); + wt_disable +------------ + +(1 row) + +-- Disabling delivers what is left, then drops the buffer. +SELECT wt_enable(2048); + wt_enable +----------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT 'drain_requested=' || wt_record_n(wt_code(1), 12, 2, 100); + ?column? +----------------------- + drain_requested=false +(1 row) + +SELECT wt_disable(); + wt_disable +------------ + +(1 row) + +SELECT dump_seen(); + dump_seen +--------------------------- + c1: callbacks=1 records=2 +(1 row) + +SELECT dump_buffer(); + dump_buffer +----------------------------------------------- + allocated=f buffered=0 draining=f in_flight=f +(1 row) + +-- Several consumers: each sees every batch, in registration order. +SELECT wt_enable(2048); + wt_enable +----------- + +(1 row) + +SELECT wt_register(2); + wt_register +------------- + +(1 row) + +SELECT wt_register(3); + wt_register +------------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_n(wt_code(1), 21, 3, 100) AS drain_requested; + drain_requested +----------------- + f +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_all_seen(); + dump_all_seen +--------------------------------------------------------------------------------- + c1: callbacks=1 records=3; c2: callbacks=1 records=3; c3: callbacks=1 records=3 +(1 row) + +-- If a consumer raises an error, those after it do not see the batch, and +-- the batch is discarded: the buffer is emptied and not draining. Later +-- drains reach every consumer again. +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_n(wt_code(2), 22, 2, 100) AS drain_requested; + drain_requested +----------------- + f +(1 row) + +SELECT wt_set_throw(true, 2); + wt_set_throw +-------------- + +(1 row) + +SELECT * FROM wt_drain_catch(); + errmsg | raw_count | draining +---------------------+-----------+---------- + test consumer error | 0 | f +(1 row) + +SELECT dump_all_seen(); + dump_all_seen +--------------------------------------------------------------------------------- + c1: callbacks=1 records=2; c2: callbacks=1 records=2; c3: callbacks=0 records=0 +(1 row) + +SELECT wt_set_throw(false, 2); + wt_set_throw +-------------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_n(wt_code(3), 23, 1, 100) AS drain_requested; + drain_requested +----------------- + f +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_all_seen(); + dump_all_seen +--------------------------------------------------------------------------------- + c1: callbacks=1 records=1; c2: callbacks=1 records=1; c3: callbacks=1 records=1 +(1 row) + +SELECT wt_disable(); + wt_disable +------------ + +(1 row) + +-- A consumer unregisters itself from its callback: it still gets the batch +-- in progress (the loop works from a copy of the registry), the buffer +-- stays alive during the loop, and later batches skip it. +SELECT wt_enable(2048); + wt_enable +----------- + +(1 row) + +SELECT wt_register(2); + wt_register +------------- + +(1 row) + +SELECT wt_register(3); + wt_register +------------- + +(1 row) + +SELECT wt_set_mode(2, 'unregister_self'); + wt_set_mode +------------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_n(wt_code(1), 31, 2, 100) AS drain_requested; + drain_requested +----------------- + f +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_all_seen(); + dump_all_seen +--------------------------------------------------------------------------------- + c1: callbacks=1 records=2; c2: callbacks=1 records=2; c3: callbacks=1 records=2 +(1 row) + +SELECT bool_and(buffer_alive) AS buffer_alive_in_callbacks + FROM generate_series(1, 3) c, LATERAL wt_seen(c); + buffer_alive_in_callbacks +--------------------------- + t +(1 row) + +SELECT dump_buffer(); + dump_buffer +----------------------------------------------- + allocated=t buffered=0 draining=f in_flight=f +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_n(wt_code(1), 32, 1, 100) AS drain_requested; + drain_requested +----------------- + f +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_all_seen(); + dump_all_seen +--------------------------------------------------------------------------------- + c1: callbacks=1 records=1; c2: callbacks=0 records=0; c3: callbacks=1 records=1 +(1 row) + +SELECT wt_set_mode(2, 'none'); + wt_set_mode +------------- + +(1 row) + +SELECT wt_disable(); + wt_disable +------------ + +(1 row) + +-- The only consumer unregisters itself from its callback: the buffer is +-- still there while the callback runs, and is released once the drain is +-- over. Timing can then be enabled again, and works. +SELECT wt_enable(2048); + wt_enable +----------- + +(1 row) + +SELECT wt_set_mode(1, 'unregister_self'); + wt_set_mode +------------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_n(wt_code(1), 41, 2, 100) AS drain_requested; + drain_requested +----------------- + f +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_seen(); + dump_seen +--------------------------- + c1: callbacks=1 records=2 +(1 row) + +SELECT buffer_alive AS buffer_alive_in_callback FROM wt_seen(); + buffer_alive_in_callback +-------------------------- + t +(1 row) + +SELECT dump_buffer(); + dump_buffer +----------------------------------------------- + allocated=f buffered=0 draining=f in_flight=f +(1 row) + +SELECT wt_set_mode(1, 'none'); + wt_set_mode +------------- + +(1 row) + +SELECT wt_enable(2048); + wt_enable +----------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_n(wt_code(2), 42, 1, 100) AS drain_requested; + drain_requested +----------------- + f +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_seen(); + dump_seen +--------------------------- + c1: callbacks=1 records=1 +(1 row) + +SELECT wt_disable(); + wt_disable +------------ + +(1 row) + +-- A callback unregisters every consumer, the last one included: all of them +-- still get the batch, and the buffer is released after the loop. +SELECT wt_enable(2048); + wt_enable +----------- + +(1 row) + +SELECT wt_register(2); + wt_register +------------- + +(1 row) + +SELECT wt_register(3); + wt_register +------------- + +(1 row) + +SELECT wt_set_mode(1, 'unregister_all'); + wt_set_mode +------------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_n(wt_code(1), 51, 2, 100) AS drain_requested; + drain_requested +----------------- + f +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_all_seen(); + dump_all_seen +--------------------------------------------------------------------------------- + c1: callbacks=1 records=2; c2: callbacks=1 records=2; c3: callbacks=1 records=2 +(1 row) + +SELECT bool_and(buffer_alive) AS buffer_alive_in_callbacks + FROM generate_series(1, 3) c, LATERAL wt_seen(c); + buffer_alive_in_callbacks +--------------------------- + t +(1 row) + +SELECT dump_buffer(); + dump_buffer +----------------------------------------------- + allocated=f buffered=0 draining=f in_flight=f +(1 row) + +SELECT wt_set_mode(1, 'none'); + wt_set_mode +------------- + +(1 row) + +SELECT wt_enable(2048); + wt_enable +----------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_n(wt_code(2), 52, 1, 100) AS drain_requested; + drain_requested +----------------- + f +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_all_seen(); + dump_all_seen +--------------------------------------------------------------------------------- + c1: callbacks=1 records=1; c2: callbacks=0 records=0; c3: callbacks=0 records=0 +(1 row) + +SELECT wt_disable(); + wt_disable +------------ + +(1 row) + +-- A callback registers another consumer: it takes part from the next batch. +SELECT wt_enable(2048); + wt_enable +----------- + +(1 row) + +SELECT wt_register(2); + wt_register +------------- + +(1 row) + +SELECT wt_set_mode(1, 'register_third'); + wt_set_mode +------------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_n(wt_code(1), 61, 2, 100) AS drain_requested; + drain_requested +----------------- + f +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_all_seen(); + dump_all_seen +--------------------------------------------------------------------------------- + c1: callbacks=1 records=2; c2: callbacks=1 records=2; c3: callbacks=0 records=0 +(1 row) + +SELECT wt_set_mode(1, 'none'); + wt_set_mode +------------- + +(1 row) + +SELECT wt_reset_seen(); + wt_reset_seen +--------------- + +(1 row) + +SELECT wt_record_n(wt_code(1), 62, 1, 100) AS drain_requested; + drain_requested +----------------- + f +(1 row) + +SELECT wt_drain(); + wt_drain +---------- + +(1 row) + +SELECT dump_all_seen(); + dump_all_seen +--------------------------------------------------------------------------------- + c1: callbacks=1 records=1; c2: callbacks=1 records=1; c3: callbacks=1 records=1 +(1 row) + +SELECT wt_disable(); + wt_disable +------------ + +(1 row) + +DROP FUNCTION dump_buffer(), dump_seen(int), dump_records(int), dump_all_seen(); +DROP EXTENSION test_wait_timing; diff --git a/src/test/modules/test_wait_timing/meson.build b/src/test/modules/test_wait_timing/meson.build new file mode 100644 index 00000000000..c204bc75b65 --- /dev/null +++ b/src/test/modules/test_wait_timing/meson.build @@ -0,0 +1,38 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +test_wait_timing_sources = files( + 'test_wait_timing.c', +) + +if host_system == 'windows' + test_wait_timing_sources += rc_lib_gen.process(win32ver_rc, extra_args: [ + '--NAME', 'test_wait_timing', + '--FILEDESC', 'test_wait_timing - tests for the local wait event buffer',]) +endif + +test_wait_timing = shared_module('test_wait_timing', + test_wait_timing_sources, + kwargs: pg_test_mod_args, +) +test_install_libs += test_wait_timing + +test_install_data += files( + 'test_wait_timing.control', + 'test_wait_timing--1.0.sql', +) + +tests += { + 'name': 'test_wait_timing', + 'sd': meson.current_source_dir(), + 'bd': meson.current_build_dir(), + 'regress': { + 'sql': [ + 'test_wait_timing', + ], + }, + 'tap': { + 'tests': [ + 't/001_exit_drain.pl', + ], + }, +} diff --git a/src/test/modules/test_wait_timing/sql/test_wait_timing.sql b/src/test/modules/test_wait_timing/sql/test_wait_timing.sql new file mode 100644 index 00000000000..e866bac4c43 --- /dev/null +++ b/src/test/modules/test_wait_timing/sql/test_wait_timing.sql @@ -0,0 +1,254 @@ +-- Tests for the local wait event buffer and its drain. +-- +-- Once timing is enabled the backend also records its own real waits (such +-- as waiting for the next client command), whenever they happen. So this +-- test never injects raw wait event numbers and never counts raw records: +-- it injects waits under dedicated custom wait events (test_wait_timing_1 .. +-- _4, see wt_code()), which cannot collide with a core event, and the test +-- consumers and wt_buffer() ignore every other record. The results below +-- therefore do not depend on timing or load. Checks that need an exact +-- buffer state run inside one C call (see test_wait_timing.c). +-- +-- To keep the scenarios load-independent, all but the dedicated overflow and +-- high-water tests use a large capacity, so incidental real waits cannot fill +-- the buffer and provoke an unexpected drain; a large time trigger (set just +-- below) keeps the elapsed wall time between statements from draining either. + +CREATE EXTENSION test_wait_timing; + +CREATE FUNCTION dump_buffer() RETURNS text LANGUAGE sql AS +$$ SELECT format('allocated=%s buffered=%s draining=%s in_flight=%s', + allocated, buffered, draining, in_flight) + FROM wt_buffer() $$; + +-- what consumer c has been handed +CREATE FUNCTION dump_seen(c int DEFAULT 1) RETURNS text LANGUAGE sql AS +$$ SELECT format('c%s: callbacks=%s records=%s', c, callbacks, records) + FROM wt_seen(c) $$; + +CREATE FUNCTION dump_records(c int DEFAULT 1) RETURNS text LANGUAGE sql AS +$$ SELECT coalesce(string_agg(format('%s/%s/%s/%s', event, context, + duration_ok, end_ns_ordered), ' '), '(none)') + FROM wt_seen_records(c) $$; + +CREATE FUNCTION dump_all_seen() RETURNS text LANGUAGE sql AS +$$ SELECT string_agg(dump_seen(c), '; ' ORDER BY c) FROM generate_series(1, 3) c $$; + +-- The custom events exist, are distinct, and belong to the Extension class. +SELECT count(DISTINCT wt_code(n)) AS distinct_codes, + bool_and((wt_code(n) >> 24) = 7) AS extension_class + FROM generate_series(1, 4) n; + +-- A huge time trigger keeps the drains below under the test's control. +SELECT wt_set_interval_ns(1000000000000000); + +-- Disabled by default: no buffer, and timed waits leave no trace. +SELECT dump_buffer(); +SELECT wt_record(wt_code(1), 0, 100); +SELECT dump_seen(); + +-- Enable, record completed waits, drain: exactly those records arrive, each +-- with the context in force when its wait started, in order. A context +-- changed after the wait began must not affect its record. +SELECT wt_enable(2048); +SELECT dump_buffer(); +SELECT wt_reset_seen(); +SELECT wt_record(wt_code(1), 111, 1000); +SELECT wt_record(wt_code(2), 222, 1000); +SELECT wt_record(wt_code(3), 333, 1000); +SELECT wt_record_ctx_change(wt_code(4), 444, 555, 1000); +SELECT 'four buffered: ' || dump_buffer(); +SELECT wt_drain(); +SELECT dump_seen(); +SELECT dump_records(); +SELECT dump_buffer(); + +-- Draining an empty buffer calls no consumer. +SELECT wt_drain_empty() AS consumer_calls; +SELECT wt_reset_seen(); +SELECT wt_drain(); +SELECT dump_seen(); + +-- A wait in flight survives a drain: the drain leaves the in-flight state +-- alone, and the wait is recorded when it ends, with its full duration. +SELECT wt_reset_seen(); +SELECT * FROM wt_inflight_drain(wt_code(1), 77, 1000); +SELECT wt_drain(); +SELECT dump_seen(); +SELECT dump_records(); + +-- An aborted wait leaves no record, and its in-flight state is forgotten; +-- an end with no matching start is ignored. +SELECT * FROM wt_abort_check(wt_code(2), 666); +SELECT wt_disable(); + +-- The public reporting inlines feed the buffer too: a wait recorded through +-- pgstat_report_wait_start()/pgstat_report_wait_end() arrives like any other, +-- and pgstat_report_wait_end_abort() clears the reported event (checked inside +-- the C call) without appending a record. +SELECT wt_enable(2048); +SELECT wt_reset_seen(); +SELECT wt_record_public(wt_code(1), 88, 1000) AS reported_cleared; +SELECT wt_drain(); +SELECT dump_seen(); +SELECT dump_records(); +SELECT * FROM wt_abort_public_check(wt_code(2), 99); +SELECT wt_disable(); + +-- Overflow: records beyond capacity are counted, and their time is kept. +-- (Real waits may add to the dropped count, hence the comparison.) +SELECT wt_enable(16); +SELECT wt_reset_seen(); +SELECT 'drain_requested=' || wt_record_n(wt_code(1), 7, 20, 100); +SELECT wt_drain(); +SELECT dump_seen(); +SELECT dropped_count >= 4 AS dropped_at_least_4, dropped_ns_positive FROM wt_seen(); +SELECT wt_disable(); + +-- High-water mark: a drain is requested only above it. CHECK_FOR_INTERRUPTS() +-- runs in the same C call as the records (wt_record_n_check), so whether the +-- pending drain is served is deterministic: below the mark nothing is drained +-- and the records stay; above it the interrupt empties the buffer. +SELECT wt_enable(16); +SELECT wt_reset_seen(); +SELECT wt_record_n_check(wt_code(1), 8, 11, 100) AS below_high_water_buffered; +SELECT wt_reset_seen(); +SELECT wt_record_n_check(wt_code(1), 8, 13, 100) AS above_high_water_buffered; +SELECT dump_seen(); +SELECT wt_disable(); + +-- Time trigger: with a zero interval a single wait requests a drain, served +-- here by the in-call CHECK_FOR_INTERRUPTS(). +SELECT wt_enable(2048); +SELECT wt_reset_seen(); +SELECT wt_set_interval_ns(0); +SELECT wt_record_n_check(wt_code(1), 9, 1, 100) AS buffered_after_check; +SELECT dump_seen(); +SELECT wt_set_interval_ns(1000000000000000); +SELECT wt_disable(); + +-- A consumer that raises ERROR must not wedge the buffer: the batch is +-- discarded, the buffer is emptied and not draining, recording works again. +SELECT wt_enable(2048); +SELECT wt_reset_seen(); +SELECT wt_set_throw(true); +SELECT 'drain_requested=' || wt_record_n(wt_code(1), 10, 3, 100); +SELECT * FROM wt_drain_catch(); +SELECT 'consumer saw the batch: ' || dump_seen(); +SELECT wt_set_throw(false); +SELECT 'drain_requested=' || wt_record_n(wt_code(2), 11, 2, 100); +SELECT 'recording works again: ' || dump_buffer(); +SELECT wt_drain(); +SELECT dump_seen(); +SELECT wt_disable(); + +-- Disabling delivers what is left, then drops the buffer. +SELECT wt_enable(2048); +SELECT wt_reset_seen(); +SELECT 'drain_requested=' || wt_record_n(wt_code(1), 12, 2, 100); +SELECT wt_disable(); +SELECT dump_seen(); +SELECT dump_buffer(); + +-- Several consumers: each sees every batch, in registration order. +SELECT wt_enable(2048); +SELECT wt_register(2); +SELECT wt_register(3); +SELECT wt_reset_seen(); +SELECT wt_record_n(wt_code(1), 21, 3, 100) AS drain_requested; +SELECT wt_drain(); +SELECT dump_all_seen(); + +-- If a consumer raises an error, those after it do not see the batch, and +-- the batch is discarded: the buffer is emptied and not draining. Later +-- drains reach every consumer again. +SELECT wt_reset_seen(); +SELECT wt_record_n(wt_code(2), 22, 2, 100) AS drain_requested; +SELECT wt_set_throw(true, 2); +SELECT * FROM wt_drain_catch(); +SELECT dump_all_seen(); +SELECT wt_set_throw(false, 2); +SELECT wt_reset_seen(); +SELECT wt_record_n(wt_code(3), 23, 1, 100) AS drain_requested; +SELECT wt_drain(); +SELECT dump_all_seen(); +SELECT wt_disable(); + +-- A consumer unregisters itself from its callback: it still gets the batch +-- in progress (the loop works from a copy of the registry), the buffer +-- stays alive during the loop, and later batches skip it. +SELECT wt_enable(2048); +SELECT wt_register(2); +SELECT wt_register(3); +SELECT wt_set_mode(2, 'unregister_self'); +SELECT wt_reset_seen(); +SELECT wt_record_n(wt_code(1), 31, 2, 100) AS drain_requested; +SELECT wt_drain(); +SELECT dump_all_seen(); +SELECT bool_and(buffer_alive) AS buffer_alive_in_callbacks + FROM generate_series(1, 3) c, LATERAL wt_seen(c); +SELECT dump_buffer(); +SELECT wt_reset_seen(); +SELECT wt_record_n(wt_code(1), 32, 1, 100) AS drain_requested; +SELECT wt_drain(); +SELECT dump_all_seen(); +SELECT wt_set_mode(2, 'none'); +SELECT wt_disable(); + +-- The only consumer unregisters itself from its callback: the buffer is +-- still there while the callback runs, and is released once the drain is +-- over. Timing can then be enabled again, and works. +SELECT wt_enable(2048); +SELECT wt_set_mode(1, 'unregister_self'); +SELECT wt_reset_seen(); +SELECT wt_record_n(wt_code(1), 41, 2, 100) AS drain_requested; +SELECT wt_drain(); +SELECT dump_seen(); +SELECT buffer_alive AS buffer_alive_in_callback FROM wt_seen(); +SELECT dump_buffer(); +SELECT wt_set_mode(1, 'none'); +SELECT wt_enable(2048); +SELECT wt_reset_seen(); +SELECT wt_record_n(wt_code(2), 42, 1, 100) AS drain_requested; +SELECT wt_drain(); +SELECT dump_seen(); +SELECT wt_disable(); + +-- A callback unregisters every consumer, the last one included: all of them +-- still get the batch, and the buffer is released after the loop. +SELECT wt_enable(2048); +SELECT wt_register(2); +SELECT wt_register(3); +SELECT wt_set_mode(1, 'unregister_all'); +SELECT wt_reset_seen(); +SELECT wt_record_n(wt_code(1), 51, 2, 100) AS drain_requested; +SELECT wt_drain(); +SELECT dump_all_seen(); +SELECT bool_and(buffer_alive) AS buffer_alive_in_callbacks + FROM generate_series(1, 3) c, LATERAL wt_seen(c); +SELECT dump_buffer(); +SELECT wt_set_mode(1, 'none'); +SELECT wt_enable(2048); +SELECT wt_reset_seen(); +SELECT wt_record_n(wt_code(2), 52, 1, 100) AS drain_requested; +SELECT wt_drain(); +SELECT dump_all_seen(); +SELECT wt_disable(); + +-- A callback registers another consumer: it takes part from the next batch. +SELECT wt_enable(2048); +SELECT wt_register(2); +SELECT wt_set_mode(1, 'register_third'); +SELECT wt_reset_seen(); +SELECT wt_record_n(wt_code(1), 61, 2, 100) AS drain_requested; +SELECT wt_drain(); +SELECT dump_all_seen(); +SELECT wt_set_mode(1, 'none'); +SELECT wt_reset_seen(); +SELECT wt_record_n(wt_code(1), 62, 1, 100) AS drain_requested; +SELECT wt_drain(); +SELECT dump_all_seen(); +SELECT wt_disable(); + +DROP FUNCTION dump_buffer(), dump_seen(int), dump_records(int), dump_all_seen(); +DROP EXTENSION test_wait_timing; diff --git a/src/test/modules/test_wait_timing/t/001_exit_drain.pl b/src/test/modules/test_wait_timing/t/001_exit_drain.pl new file mode 100644 index 00000000000..9c6ea45c346 --- /dev/null +++ b/src/test/modules/test_wait_timing/t/001_exit_drain.pl @@ -0,0 +1,75 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# The final drain at backend exit. +# +# Core registers a before_shmem_exit callback, the first time timing is +# enabled, that delivers what is still buffered. A session enables timing, +# records synthetic waits under the module's dedicated custom wait events +# (every other record is ignored by the test consumer, so the count is exact +# whatever real waits happen), and disconnects without draining. The test +# consumer writes each delivery to the server log, which is where the +# final batch is looked for. + +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +my $node = PostgreSQL::Test::Cluster->new('main'); +$node->init; +$node->start; +$node->safe_psql('postgres', 'CREATE EXTENSION test_wait_timing'); + +# A huge time trigger and a large capacity: no incidental real wait can fill +# the buffer or elapse the interval, so nothing but the exit drain delivers +# the handful of synthetic waits. +my $setup = q{ +SELECT wt_set_interval_ns(1000000000000000); +SELECT wt_enable(2048); +SELECT wt_set_log(true); +}; + +# 1. Records buffered at exit are delivered. +my $offset = -s $node->logfile; +$node->safe_psql( + 'postgres', $setup . q{ +SELECT wt_record(wt_code(1), 1, 100); +SELECT wt_record(wt_code(2), 2, 100); +SELECT wt_record(wt_code(3), 3, 100); +SELECT wt_record(wt_code(4), 4, 100); +SELECT wt_record(wt_code(1), 5, 100); +}); +ok( $node->wait_for_log( + qr/test_wait_timing: consumer 1 delivered 5 records/, $offset), + 'final batch delivered at backend exit'); + +# 2. A consumer that fails in the exit drain is reported but cannot +# interfere with the exit. +$offset = -s $node->logfile; +$node->safe_psql( + 'postgres', $setup . q{ +SELECT wt_set_throw(true); +SELECT wt_record(wt_code(2), 9, 100); +SELECT wt_record(wt_code(2), 9, 100); +}); +ok( $node->wait_for_log( + qr/test_wait_timing: consumer 1 delivered 2 records/, $offset), + 'batch reached the consumer at exit'); +ok( $node->wait_for_log(qr/test consumer error/, $offset), + 'consumer error reported in the exit drain'); +is($node->safe_psql('postgres', 'SELECT 1'), + '1', 'server fine after a failing exit drain'); + +# 3. A later session works normally. +$offset = -s $node->logfile; +$node->safe_psql( + 'postgres', $setup . q{ +SELECT wt_record(wt_code(3), 1, 100); +}); +ok( $node->wait_for_log( + qr/test_wait_timing: consumer 1 delivered 1 records/, $offset), + 'a later session delivers at exit too'); + +$node->stop; +done_testing(); diff --git a/src/test/modules/test_wait_timing/test_wait_timing--1.0.sql b/src/test/modules/test_wait_timing/test_wait_timing--1.0.sql new file mode 100644 index 00000000000..3300e71c1d6 --- /dev/null +++ b/src/test/modules/test_wait_timing/test_wait_timing--1.0.sql @@ -0,0 +1,120 @@ +/* src/test/modules/test_wait_timing/test_wait_timing--1.0.sql */ + +-- complain if script is sourced in psql, rather than via CREATE EXTENSION +\echo Use "CREATE EXTENSION test_wait_timing" to load this file. \quit + +-- The wait event code of test event n (1..4), a custom Extension event named +-- test_wait_timing_. Only records carrying these codes are observed. +CREATE FUNCTION wt_code(n int) RETURNS int8 + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- Register consumer 1 and enable timing with the given capacity. +CREATE FUNCTION wt_enable(capacity int) RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- Disable timing and unregister every test consumer. +CREATE FUNCTION wt_disable() RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- Register / unregister test consumer c (1..3). +CREATE FUNCTION wt_register(c int) RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +CREATE FUNCTION wt_unregister(c int) RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- What consumer c does while it is called: 'none', 'unregister_self', +-- 'unregister_all' or 'register_third'. +CREATE FUNCTION wt_set_mode(c int, mode text) RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +CREATE FUNCTION wt_set_throw(throw bool, c int DEFAULT 1) RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +-- Make consumer c write each delivery to the server log. +CREATE FUNCTION wt_set_log(logit bool, c int DEFAULT 1) RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +CREATE FUNCTION wt_set_context(ctx int8) RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +CREATE FUNCTION wt_set_interval_ns(ns int8) RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- Run the slow paths for one wait of the given event, context and duration. +CREATE FUNCTION wt_record(event_info int8, ctx int8, duration_us int) RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- Discard the buffered records, then record n such waits; returns whether a +-- drain was requested as a result. +CREATE FUNCTION wt_record_n(event_info int8, ctx int8, n int, duration_us int) + RETURNS bool AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- Like wt_record_n(), but also run CHECK_FOR_INTERRUPTS() in the same call; +-- returns the number of test records still buffered afterwards. +CREATE FUNCTION wt_record_n_check(event_info int8, ctx int8, n int, duration_us int) + RETURNS int AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- Record one wait through the public pgstat_report_wait_start()/_end() inlines; +-- returns whether the reported wait event was cleared afterwards. +CREATE FUNCTION wt_record_public(event_info int8, ctx int8, duration_us int) + RETURNS bool AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- A wait whose context changes after it began. +CREATE FUNCTION wt_record_ctx_change(event_info int8, ctx int8, new_ctx int8, + duration_us int) + RETURNS void AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- A wait that is in flight while a drain runs. +CREATE FUNCTION wt_inflight_drain(event_info int8, ctx int8, duration_us int, + OUT in_flight_before bool, + OUT in_flight_after_drain bool, + OUT start_kept bool) + RETURNS record AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- An aborted wait, and an end without a start. +CREATE FUNCTION wt_abort_check(event_info int8, ctx int8, + OUT in_flight_after_begin bool, + OUT cleared_by_abort bool, + OUT records_added int, + OUT clear_after_unmatched_end bool) + RETURNS record AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- pgstat_report_wait_start() followed by pgstat_report_wait_end_abort(), +-- through the public API. +CREATE FUNCTION wt_abort_public_check(event_info int8, ctx int8, + OUT reported_and_in_flight bool, + OUT cleared_by_abort bool, + OUT records_added int) + RETURNS record AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- Drain explicitly / via CHECK_FOR_INTERRUPTS(). +CREATE FUNCTION wt_drain() RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +CREATE FUNCTION wt_check_interrupts() RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- Discard the buffer, drain it, and return the number of consumer calls. +CREATE FUNCTION wt_drain_empty() RETURNS int + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- Drain, catching a consumer error; also reports the raw buffer state after. +CREATE FUNCTION wt_drain_catch(OUT errmsg text, OUT raw_count int, + OUT draining bool) + RETURNS record AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- Forget what all consumers have seen so far. +CREATE FUNCTION wt_reset_seen() RETURNS void + AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- What consumer c has been handed since wt_reset_seen(), test events only. +CREATE FUNCTION wt_seen(c int DEFAULT 1, + OUT callbacks int, OUT records int, + OUT dropped_count int8, OUT dropped_ns_positive bool, + OUT buffer_alive bool) + RETURNS record AS 'MODULE_PATHNAME' LANGUAGE C STRICT; +CREATE FUNCTION wt_seen_records(c int DEFAULT 1, + OUT event int, OUT context int8, + OUT duration_ok bool, OUT end_ns_ordered bool) + RETURNS SETOF record AS 'MODULE_PATHNAME' LANGUAGE C STRICT; + +-- State of the core buffer, counting test events only. +CREATE FUNCTION wt_buffer(OUT allocated bool, OUT buffered int, OUT draining bool, + OUT drain_pending bool, OUT in_flight bool) + RETURNS record AS 'MODULE_PATHNAME' LANGUAGE C STRICT; diff --git a/src/test/modules/test_wait_timing/test_wait_timing.c b/src/test/modules/test_wait_timing/test_wait_timing.c new file mode 100644 index 00000000000..cf95b9d2be1 --- /dev/null +++ b/src/test/modules/test_wait_timing/test_wait_timing.c @@ -0,0 +1,763 @@ +/*------------------------------------------------------------------------- + * + * test_wait_timing.c + * Tests for the local wait event buffer and its safe-point drain. + * + * Fake consumers record what they are handed. Most tests call the slow paths + * (pgstat_wait_timing_begin_slow()/_end_slow()) directly, so that they do not + * depend on any real wait site; a few go through the public + * pgstat_report_wait_start()/pgstat_report_wait_end() inlines to exercise the + * timing branch that is folded into them, and through + * pgstat_report_wait_end_abort(). + * + * Determinism. Once timing is enabled, the backend also records its own, + * real waits (waiting for the next client command, LWLocks, ...). Whether + * and when those happen depends on system load, so the tests must not count + * them. The module therefore registers a few dedicated custom wait events + * (class Extension, so they cannot collide with a core event or with each + * other), injects its synthetic waits under those codes only, and makes + * every observation filter on them: the consumers discard any other record, + * and the buffer inspection functions count only test events. A stray real + * wait is thus invisible, and the expected output does not depend on load. + * Two further rules keep the remaining checks exact: + * + * - A wait that is meant to be "in flight" is begun and ended within one C + * call (together with whatever the test does in between), because the + * buffer tracks a single in-flight wait and a real wait between two SQL + * statements would replace it. + * + * - Functions that need an exact buffer state (wt_record_n, wt_drain_empty) + * discard the buffer contents first, inside the same call that then + * records, so no real wait can intervene. + * + * What is NOT covered here (see also the TAP test and the SQL test header): + * + * - Auxiliary processes (checkpointer, bgwriter, ...) have their own + * interrupt hubs, and some drain just before a long sleep; a test module + * cannot register a consumer in them, so that drain placement is only + * code-reviewed. A requested drain does NOT arm MyLatch (that would + * disturb latch-paced loops), so there is no latch behavior to test here. + * + * - pgstat_wait_event_timing_child_init() matters on a fork() build, where a + * child inherits the postmaster's live memory and must not keep its buffer, + * pending-drain flag or consumer registry; on EXEC_BACKEND the child starts + * with zeroed globals, so there is nothing to inherit and the call is just + * belt and suspenders. Either way a backend is already a postmaster child + * by the time it runs SQL, so this is only code-reviewed (see wait_event.c). + * + * Copyright (c) 2026, PostgreSQL Global Development Group + * + * IDENTIFICATION + * src/test/modules/test_wait_timing/test_wait_timing.c + * + *------------------------------------------------------------------------- + */ +#include "postgres.h" + +#include "access/htup_details.h" +#include "funcapi.h" +#include "miscadmin.h" +#include "utils/builtins.h" +#include "utils/tuplestore.h" +#include "utils/wait_event.h" +#include "utils/wait_event_timing.h" + +PG_MODULE_MAGIC; + +#define NUM_EVENTS 4 +#define NUM_CONSUMERS 3 +#define MAX_SEEN 256 + +typedef enum ConsumerMode +{ + MODE_NONE, + MODE_UNREGISTER_SELF, /* unregister this consumer from its callback */ + MODE_UNREGISTER_ALL, /* unregister every test consumer */ + MODE_REGISTER_THIRD /* register consumer 3 from the callback */ +} ConsumerMode; + +typedef struct ConsumerState +{ + int index; /* 1-based */ + bool registered; + ConsumerMode mode; + bool throws; + bool log; + int raw_callbacks; /* all calls, whatever the batch held */ + int callbacks; /* calls whose batch held test records */ + int nrecords; /* test records seen (up to MAX_SEEN) */ + WaitEventRecord seen[MAX_SEEN]; + uint64 dropped_count; + uint64 dropped_ns; + bool buffer_alive; /* buffer existed in every callback so far */ +} ConsumerState; + +static ConsumerState consumers[NUM_CONSUMERS]; +static uint32 event_codes[NUM_EVENTS]; +static bool events_registered = false; + +static void +init_consumers(void) +{ + static bool done = false; + + if (done) + return; + for (int i = 0; i < NUM_CONSUMERS; i++) + { + consumers[i].index = i + 1; + consumers[i].buffer_alive = true; + } + done = true; +} + +/* + * Register the dedicated wait events. WaitEventExtensionNew() returns the + * existing code if the name is already known, so every backend gets the same + * codes. It may take an LWLock, so call it from SQL-callable entry points + * only, never while a wait is in flight. + */ +static void +ensure_events(void) +{ + if (events_registered) + return; + for (int i = 0; i < NUM_EVENTS; i++) + { + char name[NAMEDATALEN]; + + snprintf(name, sizeof(name), "test_wait_timing_%d", i + 1); + event_codes[i] = WaitEventExtensionNew(name); + } + events_registered = true; +} + +/* 1-based index of a test event, or 0 for any other event */ +static int +test_event_index(uint32 info) +{ + if (!events_registered) + return 0; + for (int i = 0; i < NUM_EVENTS; i++) + { + if (event_codes[i] == info) + return i + 1; + } + return 0; +} + +static int +count_test_records(const WaitEventLocalBuffer *buf) +{ + int n = 0; + + if (buf == NULL) + return 0; + for (int i = 0; i < buf->count; i++) + { + if (test_event_index(buf->records[i].wait_event_info)) + n++; + } + return n; +} + +static void test_consumer(const WaitEventRecord *records, int nrecords, + uint64 dropped_count, uint64 dropped_ns, void *arg); + +static ConsumerState * +get_consumer(int c) +{ + init_consumers(); + if (c < 1 || c > NUM_CONSUMERS) + elog(ERROR, "consumer number %d out of range", c); + return &consumers[c - 1]; +} + +static void +register_consumer(ConsumerState *cs) +{ + ensure_events(); + pgstat_register_wait_event_consumer(test_consumer, cs); + cs->registered = true; +} + +static void +unregister_consumer(ConsumerState *cs) +{ + pgstat_unregister_wait_event_consumer(test_consumer, cs); + cs->registered = false; +} + +static void +test_consumer(const WaitEventRecord *records, int nrecords, + uint64 dropped_count, uint64 dropped_ns, void *arg) +{ + ConsumerState *cs = (ConsumerState *) arg; + int nkept = 0; + + cs->raw_callbacks++; + if (wait_event_report_state.buf == NULL) + cs->buffer_alive = false; + + for (int i = 0; i < nrecords; i++) + { + if (!test_event_index(records[i].wait_event_info)) + continue; + nkept++; + if (cs->nrecords < MAX_SEEN) + cs->seen[cs->nrecords++] = records[i]; + } + + /* a batch without test records is somebody else's business */ + if (nkept == 0) + return; + + cs->callbacks++; + cs->dropped_count += dropped_count; + cs->dropped_ns += dropped_ns; + + if (cs->log) + ereport(LOG, + (errmsg("test_wait_timing: consumer %d delivered %d records", + cs->index, nkept))); + + switch (cs->mode) + { + case MODE_NONE: + break; + case MODE_UNREGISTER_SELF: + unregister_consumer(cs); + break; + case MODE_UNREGISTER_ALL: + for (int i = 0; i < NUM_CONSUMERS; i++) + unregister_consumer(&consumers[i]); + break; + case MODE_REGISTER_THIRD: + register_consumer(&consumers[2]); + break; + } + + if (cs->throws) + elog(ERROR, "test consumer error"); +} + +/* ---- setup ---- */ + +PG_FUNCTION_INFO_V1(wt_code); +Datum +wt_code(PG_FUNCTION_ARGS) +{ + int n = PG_GETARG_INT32(0); + + ensure_events(); + if (n < 1 || n > NUM_EVENTS) + elog(ERROR, "test event number %d out of range", n); + PG_RETURN_INT64((int64) event_codes[n - 1]); +} + +/* Register consumer 1 and enable timing with the given capacity. */ +PG_FUNCTION_INFO_V1(wt_enable); +Datum +wt_enable(PG_FUNCTION_ARGS) +{ + register_consumer(get_consumer(1)); + pgstat_set_wait_event_timing_enabled(true, PG_GETARG_INT32(0)); + PG_RETURN_VOID(); +} + +/* Disable timing, then unregister every test consumer still registered. */ +PG_FUNCTION_INFO_V1(wt_disable); +Datum +wt_disable(PG_FUNCTION_ARGS) +{ + init_consumers(); + pgstat_set_wait_event_timing_enabled(false, 0); + for (int i = 0; i < NUM_CONSUMERS; i++) + { + if (consumers[i].registered) + unregister_consumer(&consumers[i]); + } + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(wt_register); +Datum +wt_register(PG_FUNCTION_ARGS) +{ + register_consumer(get_consumer(PG_GETARG_INT32(0))); + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(wt_unregister); +Datum +wt_unregister(PG_FUNCTION_ARGS) +{ + unregister_consumer(get_consumer(PG_GETARG_INT32(0))); + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(wt_set_mode); +Datum +wt_set_mode(PG_FUNCTION_ARGS) +{ + ConsumerState *cs = get_consumer(PG_GETARG_INT32(0)); + char *mode = text_to_cstring(PG_GETARG_TEXT_PP(1)); + + if (strcmp(mode, "none") == 0) + cs->mode = MODE_NONE; + else if (strcmp(mode, "unregister_self") == 0) + cs->mode = MODE_UNREGISTER_SELF; + else if (strcmp(mode, "unregister_all") == 0) + cs->mode = MODE_UNREGISTER_ALL; + else if (strcmp(mode, "register_third") == 0) + cs->mode = MODE_REGISTER_THIRD; + else + elog(ERROR, "unknown mode \"%s\"", mode); + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(wt_set_throw); +Datum +wt_set_throw(PG_FUNCTION_ARGS) +{ + get_consumer(PG_GETARG_INT32(1))->throws = PG_GETARG_BOOL(0); + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(wt_set_log); +Datum +wt_set_log(PG_FUNCTION_ARGS) +{ + get_consumer(PG_GETARG_INT32(1))->log = PG_GETARG_BOOL(0); + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(wt_set_context); +Datum +wt_set_context(PG_FUNCTION_ARGS) +{ + pgstat_set_wait_event_context((uint32) PG_GETARG_INT64(0)); + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(wt_set_interval_ns); +Datum +wt_set_interval_ns(PG_FUNCTION_ARGS) +{ + pgstat_set_wait_event_drain_interval(PG_GETARG_INT64(0)); + PG_RETURN_VOID(); +} + +/* ---- recording ---- */ + +/* One complete wait of the given event, context and duration. */ +PG_FUNCTION_INFO_V1(wt_record); +Datum +wt_record(PG_FUNCTION_ARGS) +{ + pgstat_set_wait_event_context((uint32) PG_GETARG_INT64(1)); + + /* call the slow paths directly, as the inline wrappers would */ + pgstat_wait_timing_begin_slow((uint32) PG_GETARG_INT64(0)); + pg_usleep(PG_GETARG_INT32(2)); + pgstat_wait_timing_end_slow(); + PG_RETURN_VOID(); +} + +/* + * Discard the buffered records and any drain request, then record n + * identical waits and report whether a drain has been requested. All in + * one call, so that no real wait can change the answer. + */ +PG_FUNCTION_INFO_V1(wt_record_n); +Datum +wt_record_n(PG_FUNCTION_ARGS) +{ + int n = PG_GETARG_INT32(2); + WaitEventLocalBuffer *buf = wait_event_report_state.buf; + + if (buf != NULL) + { + buf->count = 0; + buf->dropped_count = 0; + buf->dropped_ns = 0; + } + WaitEventDrainPending = false; + + pgstat_set_wait_event_context((uint32) PG_GETARG_INT64(1)); + for (int i = 0; i < n; i++) + { + pgstat_wait_timing_begin_slow((uint32) PG_GETARG_INT64(0)); + pg_usleep(PG_GETARG_INT32(3)); + pgstat_wait_timing_end_slow(); + } + PG_RETURN_BOOL(WaitEventDrainPending); +} + +/* + * Like wt_record_n(), but run CHECK_FOR_INTERRUPTS() in the SAME C call, right + * after recording. If the records requested a drain, the interrupt serves it + * here, deterministically: a CHECK_FOR_INTERRUPTS() that fell between two SQL + * statements could otherwise have drained already, or not yet, depending on + * load. Returns the number of test records still buffered afterwards (zero if + * the interrupt drained them). + */ +PG_FUNCTION_INFO_V1(wt_record_n_check); +Datum +wt_record_n_check(PG_FUNCTION_ARGS) +{ + int n = PG_GETARG_INT32(2); + WaitEventLocalBuffer *buf = wait_event_report_state.buf; + + if (buf != NULL) + { + buf->count = 0; + buf->dropped_count = 0; + buf->dropped_ns = 0; + } + WaitEventDrainPending = false; + + pgstat_set_wait_event_context((uint32) PG_GETARG_INT64(1)); + for (int i = 0; i < n; i++) + { + pgstat_wait_timing_begin_slow((uint32) PG_GETARG_INT64(0)); + pg_usleep(PG_GETARG_INT32(3)); + pgstat_wait_timing_end_slow(); + } + + CHECK_FOR_INTERRUPTS(); + + PG_RETURN_INT32(buf != NULL ? count_test_records(buf) : -1); +} + +/* A wait whose context is changed after it began; the record keeps the old. */ +PG_FUNCTION_INFO_V1(wt_record_ctx_change); +Datum +wt_record_ctx_change(PG_FUNCTION_ARGS) +{ + pgstat_set_wait_event_context((uint32) PG_GETARG_INT64(1)); + pgstat_wait_timing_begin_slow((uint32) PG_GETARG_INT64(0)); + pgstat_set_wait_event_context((uint32) PG_GETARG_INT64(2)); + pg_usleep(PG_GETARG_INT32(3)); + pgstat_wait_timing_end_slow(); + PG_RETURN_VOID(); +} + +/* + * A wait that is in flight while a drain runs. The drain must not touch + * the in-flight state, and the wait must be recorded when it ends. + */ +PG_FUNCTION_INFO_V1(wt_inflight_drain); +Datum +wt_inflight_drain(PG_FUNCTION_ARGS) +{ + WaitEventLocalBuffer *buf = wait_event_report_state.buf; + uint32 code = (uint32) PG_GETARG_INT64(0); + TupleDesc tupdesc; + Datum values[3]; + bool nulls[3] = {false, false, false}; + bool before; + bool after; + bool start_kept; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "return type must be a row type"); + if (buf == NULL) + elog(ERROR, "timing is not enabled"); + + pgstat_set_wait_event_context((uint32) PG_GETARG_INT64(1)); + pgstat_wait_timing_begin_slow(code); + before = buf->current_event == code; + + pgstat_drain_wait_events(); + + after = buf->current_event == code; + start_kept = !INSTR_TIME_IS_ZERO(buf->wait_start); + + pg_usleep(PG_GETARG_INT32(2)); + pgstat_wait_timing_end_slow(); + + values[0] = BoolGetDatum(before); + values[1] = BoolGetDatum(after); + values[2] = BoolGetDatum(start_kept); + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} + +/* + * An aborted wait leaves no record and clears the in-flight state; an end + * with no matching start is ignored. + */ +PG_FUNCTION_INFO_V1(wt_abort_check); +Datum +wt_abort_check(PG_FUNCTION_ARGS) +{ + WaitEventLocalBuffer *buf = wait_event_report_state.buf; + uint32 code = (uint32) PG_GETARG_INT64(0); + TupleDesc tupdesc; + Datum values[4]; + bool nulls[4] = {false, false, false, false}; + int n0; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "return type must be a row type"); + if (buf == NULL) + elog(ERROR, "timing is not enabled"); + + n0 = count_test_records(buf); + pgstat_set_wait_event_context((uint32) PG_GETARG_INT64(1)); + pgstat_wait_timing_begin_slow(code); + values[0] = BoolGetDatum(buf->current_event == code); + pgstat_report_wait_end_abort(); + values[1] = BoolGetDatum(buf->current_event == 0 && + INSTR_TIME_IS_ZERO(buf->wait_start)); + pgstat_wait_timing_end_slow(); /* no matching start */ + values[2] = Int32GetDatum(count_test_records(buf) - n0); + values[3] = BoolGetDatum(buf->current_event == 0); + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} + +/* + * Record one wait through the PUBLIC inline wrappers pgstat_report_wait_start() + * and pgstat_report_wait_end(), not the slow paths directly. This exercises + * the timing branch folded into the ordinary reporting functions. Discards + * the buffer first so the count is exact. Returns whether the reported wait + * event was cleared by the end (it always must be). + */ +PG_FUNCTION_INFO_V1(wt_record_public); +Datum +wt_record_public(PG_FUNCTION_ARGS) +{ + WaitEventLocalBuffer *buf = wait_event_report_state.buf; + uint32 code = (uint32) PG_GETARG_INT64(0); + + if (buf != NULL) + { + buf->count = 0; + buf->dropped_count = 0; + buf->dropped_ns = 0; + } + WaitEventDrainPending = false; + + pgstat_set_wait_event_context((uint32) PG_GETARG_INT64(1)); + pgstat_report_wait_start(code); + pg_usleep(PG_GETARG_INT32(2)); + pgstat_report_wait_end(); + + PG_RETURN_BOOL(*(volatile uint32 *) my_wait_event_info == 0); +} + +/* + * Drive pgstat_report_wait_start() and then pgstat_report_wait_end_abort(). + * The start both reports the event and begins timing; the abort must clear the + * reported event and forget the in-flight timing without appending a record. + * All in one C call, so no real wait can intervene. + */ +PG_FUNCTION_INFO_V1(wt_abort_public_check); +Datum +wt_abort_public_check(PG_FUNCTION_ARGS) +{ + WaitEventLocalBuffer *buf = wait_event_report_state.buf; + uint32 code = (uint32) PG_GETARG_INT64(0); + TupleDesc tupdesc; + Datum values[3]; + bool nulls[3] = {false, false, false}; + int n0; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "return type must be a row type"); + if (buf == NULL) + elog(ERROR, "timing is not enabled"); + + n0 = count_test_records(buf); + pgstat_set_wait_event_context((uint32) PG_GETARG_INT64(1)); + pgstat_report_wait_start(code); + values[0] = BoolGetDatum(*(volatile uint32 *) my_wait_event_info == code && + buf->current_event == code); + pgstat_report_wait_end_abort(); + values[1] = BoolGetDatum(*(volatile uint32 *) my_wait_event_info == 0 && + buf->current_event == 0 && + INSTR_TIME_IS_ZERO(buf->wait_start)); + values[2] = Int32GetDatum(count_test_records(buf) - n0); + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} + +/* ---- draining ---- */ + +PG_FUNCTION_INFO_V1(wt_drain); +Datum +wt_drain(PG_FUNCTION_ARGS) +{ + pgstat_drain_wait_events(); + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(wt_check_interrupts); +Datum +wt_check_interrupts(PG_FUNCTION_ARGS) +{ + CHECK_FOR_INTERRUPTS(); + PG_RETURN_VOID(); +} + +/* + * Discard the buffer contents, drain, and return how many consumer + * callbacks that provoked (none are expected: an empty buffer is not + * delivered). + */ +PG_FUNCTION_INFO_V1(wt_drain_empty); +Datum +wt_drain_empty(PG_FUNCTION_ARGS) +{ + WaitEventLocalBuffer *buf = wait_event_report_state.buf; + int before = 0; + int after = 0; + + init_consumers(); + if (buf != NULL) + { + buf->count = 0; + buf->dropped_count = 0; + buf->dropped_ns = 0; + } + for (int i = 0; i < NUM_CONSUMERS; i++) + before += consumers[i].raw_callbacks; + pgstat_drain_wait_events(); + for (int i = 0; i < NUM_CONSUMERS; i++) + after += consumers[i].raw_callbacks; + PG_RETURN_INT32(after - before); +} + +/* + * Drain, catching any error a consumer raises, and report the buffer state + * right afterwards (the raw record count, not only test records). + */ +PG_FUNCTION_INFO_V1(wt_drain_catch); +Datum +wt_drain_catch(PG_FUNCTION_ARGS) +{ + MemoryContext oldcxt = CurrentMemoryContext; + char *msg = NULL; + WaitEventLocalBuffer *buf; + TupleDesc tupdesc; + Datum values[3]; + bool nulls[3] = {false, false, false}; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "return type must be a row type"); + + PG_TRY(); + { + pgstat_drain_wait_events(); + } + PG_CATCH(); + { + ErrorData *edata; + + MemoryContextSwitchTo(oldcxt); + edata = CopyErrorData(); + FlushErrorState(); + msg = edata->message; + } + PG_END_TRY(); + + buf = wait_event_report_state.buf; + if (msg) + values[0] = CStringGetTextDatum(msg); + else + nulls[0] = true; + values[1] = Int32GetDatum(buf ? buf->count : -1); + values[2] = BoolGetDatum(buf ? buf->draining : false); + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} + +/* ---- observation ---- */ + +PG_FUNCTION_INFO_V1(wt_reset_seen); +Datum +wt_reset_seen(PG_FUNCTION_ARGS) +{ + init_consumers(); + for (int i = 0; i < NUM_CONSUMERS; i++) + { + ConsumerState *cs = &consumers[i]; + + cs->raw_callbacks = 0; + cs->callbacks = 0; + cs->nrecords = 0; + cs->dropped_count = 0; + cs->dropped_ns = 0; + cs->buffer_alive = true; + } + PG_RETURN_VOID(); +} + +PG_FUNCTION_INFO_V1(wt_seen); +Datum +wt_seen(PG_FUNCTION_ARGS) +{ + ConsumerState *cs = get_consumer(PG_GETARG_INT32(0)); + TupleDesc tupdesc; + Datum values[5]; + bool nulls[5] = {false, false, false, false, false}; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "return type must be a row type"); + + values[0] = Int32GetDatum(cs->callbacks); + values[1] = Int32GetDatum(cs->nrecords); + values[2] = Int64GetDatum((int64) cs->dropped_count); + values[3] = BoolGetDatum(cs->dropped_ns > 0); + values[4] = BoolGetDatum(cs->buffer_alive); + + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} + +PG_FUNCTION_INFO_V1(wt_seen_records); +Datum +wt_seen_records(PG_FUNCTION_ARGS) +{ + ConsumerState *cs = get_consumer(PG_GETARG_INT32(0)); + ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; + + InitMaterializedSRF(fcinfo, 0); + + for (int i = 0; i < cs->nrecords; i++) + { + Datum values[4]; + bool nulls[4] = {false, false, false, false}; + + values[0] = Int32GetDatum(test_event_index(cs->seen[i].wait_event_info)); + values[1] = Int64GetDatum((int64) cs->seen[i].context); + values[2] = BoolGetDatum(cs->seen[i].duration_ns > 0); + values[3] = BoolGetDatum(i == 0 || + cs->seen[i].end_ns >= cs->seen[i - 1].end_ns); + tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls); + } + + return (Datum) 0; +} + +/* + * State of the core buffer. Only test events are counted, so that real + * waits recorded meanwhile do not show. + */ +PG_FUNCTION_INFO_V1(wt_buffer); +Datum +wt_buffer(PG_FUNCTION_ARGS) +{ + WaitEventLocalBuffer *buf = wait_event_report_state.buf; + TupleDesc tupdesc; + Datum values[5]; + bool nulls[5] = {false, false, false, false, false}; + + if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) + elog(ERROR, "return type must be a row type"); + + values[0] = BoolGetDatum(buf != NULL); + values[1] = Int32GetDatum(count_test_records(buf)); + values[2] = BoolGetDatum(buf ? buf->draining : false); + values[3] = BoolGetDatum(WaitEventDrainPending); + values[4] = BoolGetDatum(buf ? test_event_index(buf->current_event) != 0 : false); + + PG_RETURN_DATUM(HeapTupleGetDatum(heap_form_tuple(tupdesc, values, nulls))); +} diff --git a/src/test/modules/test_wait_timing/test_wait_timing.control b/src/test/modules/test_wait_timing/test_wait_timing.control new file mode 100644 index 00000000000..dcabaaeebf1 --- /dev/null +++ b/src/test/modules/test_wait_timing/test_wait_timing.control @@ -0,0 +1,4 @@ +comment = 'Tests for the local wait event buffer' +default_version = '1.0' +module_pathname = '$libdir/test_wait_timing' +relocatable = true -- 2.49.0