Debian Codegraph @

src/test/modules/test_custom_rmgrs/t/001_basic.pl

raw
1 # Copyright (c) 2021-2025, PostgreSQL Global Development Group
2
3 use strict;
4 use warnings FATAL => 'all';
5
6 use PostgreSQL::Test::Cluster;
7 use PostgreSQL::Test::Utils;
8 use Test::More;
9
10 my $node = PostgreSQL::Test::Cluster->new('main');
11
12 $node->init;
13 $node->append_conf(
14 'postgresql.conf', q{
15 wal_level = 'replica'
16 max_wal_senders = 4
17 shared_preload_libraries = 'test_custom_rmgrs'
18 });
19 $node->start;
20
21 # setup
22 $node->safe_psql('postgres', 'CREATE EXTENSION test_custom_rmgrs');
23
24 # pg_walinspect is required only for verifying test_custom_rmgrs output.
25 # test_custom_rmgrs doesn't use/depend on it internally.
26 $node->safe_psql('postgres', 'CREATE EXTENSION pg_walinspect');
27
28 # make sure checkpoints don't interfere with the test.
29 my $start_lsn = $node->safe_psql('postgres',
30 qq[SELECT lsn FROM pg_create_physical_replication_slot('regress_test_slot1', true, false);]
31 );
32
33 # write and save the WAL record's returned end LSN for verifying it later
34 my $record_end_lsn = $node->safe_psql('postgres',
35 'SELECT * FROM test_custom_rmgrs_insert_wal_record(\'payload123\')');
36
37 # ensure the WAL is written and flushed to disk
38 $node->safe_psql('postgres', 'SELECT pg_switch_wal()');
39
40 my $end_lsn =
41 $node->safe_psql('postgres', 'SELECT pg_current_wal_flush_lsn()');
42
43 # check if our custom WAL resource manager has successfully registered with the server
44 my $row_count = $node->safe_psql(
45 'postgres',
46 qq[SELECT count(*) FROM pg_get_wal_resource_managers()
47 WHERE rm_name = 'test_custom_rmgrs';]);
48 is($row_count, '1',
49 'custom WAL resource manager has successfully registered with the server'
50 );
51
52 # check if our custom WAL resource manager has successfully written a WAL record
53 my $expected =
54 qq($record_end_lsn|test_custom_rmgrs|TEST_CUSTOM_RMGRS_MESSAGE|0|payload (10 bytes): payload123);
55 my $result = $node->safe_psql(
56 'postgres',
57 qq[SELECT end_lsn, resource_manager, record_type, fpi_length, description FROM pg_get_wal_records_info('$start_lsn', '$end_lsn')
58 WHERE resource_manager = 'test_custom_rmgrs';]);
59 is($result, $expected,
60 'custom WAL resource manager has successfully written a WAL record');
61
62 $node->stop;
63 done_testing();