| 1 |
|
| 2 |
# Copyright (c) 2021-2025, PostgreSQL Global Development Group |
| 3 |
|
| 4 |
# Testing of commit timestamps preservation across restarts |
| 5 |
use strict; |
| 6 |
use warnings FATAL => 'all'; |
| 7 |
use PostgreSQL::Test::Cluster; |
| 8 |
use PostgreSQL::Test::Utils; |
| 9 |
use Test::More; |
| 10 |
|
| 11 |
my $node_primary = PostgreSQL::Test::Cluster->new('primary'); |
| 12 |
$node_primary->init(allows_streaming => 1); |
| 13 |
$node_primary->append_conf('postgresql.conf', 'track_commit_timestamp = on'); |
| 14 |
$node_primary->start; |
| 15 |
|
| 16 |
my ($ret, $stdout, $stderr); |
| 17 |
|
| 18 |
($ret, $stdout, $stderr) = |
| 19 |
$node_primary->psql('postgres', qq[SELECT pg_xact_commit_timestamp('0');]); |
| 20 |
is($ret, 3, 'getting ts of InvalidTransactionId reports error'); |
| 21 |
like( |
| 22 |
$stderr, |
| 23 |
qr/cannot retrieve commit timestamp for transaction/, |
| 24 |
'expected error from InvalidTransactionId'); |
| 25 |
|
| 26 |
($ret, $stdout, $stderr) = |
| 27 |
$node_primary->psql('postgres', qq[SELECT pg_xact_commit_timestamp('1');]); |
| 28 |
is($ret, 0, 'getting ts of BootstrapTransactionId succeeds'); |
| 29 |
is($stdout, '', 'timestamp of BootstrapTransactionId is null'); |
| 30 |
|
| 31 |
($ret, $stdout, $stderr) = |
| 32 |
$node_primary->psql('postgres', qq[SELECT pg_xact_commit_timestamp('2');]); |
| 33 |
is($ret, 0, 'getting ts of FrozenTransactionId succeeds'); |
| 34 |
is($stdout, '', 'timestamp of FrozenTransactionId is null'); |
| 35 |
|
| 36 |
# Since FirstNormalTransactionId will've occurred during initdb, long before we |
| 37 |
# enabled commit timestamps, it'll be null since we have no cts data for it but |
| 38 |
# cts are enabled. |
| 39 |
is( $node_primary->safe_psql( |
| 40 |
'postgres', qq[SELECT pg_xact_commit_timestamp('3');]), |
| 41 |
'', |
| 42 |
'committs for FirstNormalTransactionId is null'); |
| 43 |
|
| 44 |
$node_primary->safe_psql('postgres', |
| 45 |
qq[CREATE TABLE committs_test(x integer, y timestamp with time zone);]); |
| 46 |
|
| 47 |
my $xid = $node_primary->safe_psql( |
| 48 |
'postgres', qq[ |
| 49 |
BEGIN; |
| 50 |
INSERT INTO committs_test(x, y) VALUES (1, current_timestamp); |
| 51 |
SELECT pg_current_xact_id()::xid; |
| 52 |
COMMIT; |
| 53 |
]); |
| 54 |
|
| 55 |
my $before_restart_ts = $node_primary->safe_psql('postgres', |
| 56 |
qq[SELECT pg_xact_commit_timestamp('$xid');]); |
| 57 |
ok($before_restart_ts ne '' && $before_restart_ts ne 'null', |
| 58 |
'commit timestamp recorded'); |
| 59 |
|
| 60 |
$node_primary->stop('immediate'); |
| 61 |
$node_primary->start; |
| 62 |
|
| 63 |
my $after_crash_ts = $node_primary->safe_psql('postgres', |
| 64 |
qq[SELECT pg_xact_commit_timestamp('$xid');]); |
| 65 |
is($after_crash_ts, $before_restart_ts, |
| 66 |
'timestamps before and after crash are equal'); |
| 67 |
|
| 68 |
$node_primary->stop('fast'); |
| 69 |
$node_primary->start; |
| 70 |
|
| 71 |
my $after_restart_ts = $node_primary->safe_psql('postgres', |
| 72 |
qq[SELECT pg_xact_commit_timestamp('$xid');]); |
| 73 |
is($after_restart_ts, $before_restart_ts, |
| 74 |
'timestamps before and after restart are equal'); |
| 75 |
|
| 76 |
# Now disable commit timestamps |
| 77 |
$node_primary->append_conf('postgresql.conf', 'track_commit_timestamp = off'); |
| 78 |
$node_primary->stop('fast'); |
| 79 |
|
| 80 |
# Start the server, which generates a XLOG_PARAMETER_CHANGE record where |
| 81 |
# the parameter change is registered. |
| 82 |
$node_primary->start; |
| 83 |
|
| 84 |
# Now restart again the server so as no XLOG_PARAMETER_CHANGE record are |
| 85 |
# replayed with the follow-up immediate shutdown. |
| 86 |
$node_primary->restart; |
| 87 |
|
| 88 |
# Move commit timestamps across page boundaries. Things should still |
| 89 |
# be able to work across restarts with those transactions committed while |
| 90 |
# track_commit_timestamp is disabled. |
| 91 |
$node_primary->safe_psql( |
| 92 |
'postgres', |
| 93 |
qq(CREATE PROCEDURE consume_xid(cnt int) |
| 94 |
AS \$\$ |
| 95 |
DECLARE |
| 96 |
i int; |
| 97 |
BEGIN |
| 98 |
FOR i in 1..cnt LOOP |
| 99 |
EXECUTE 'SELECT pg_current_xact_id()'; |
| 100 |
COMMIT; |
| 101 |
END LOOP; |
| 102 |
END; |
| 103 |
\$\$ |
| 104 |
LANGUAGE plpgsql; |
| 105 |
)); |
| 106 |
$node_primary->safe_psql('postgres', 'CALL consume_xid(2000)'); |
| 107 |
|
| 108 |
($ret, $stdout, $stderr) = $node_primary->psql('postgres', |
| 109 |
qq[SELECT pg_xact_commit_timestamp('$xid');]); |
| 110 |
is($ret, 3, 'no commit timestamp from enable tx when cts disabled'); |
| 111 |
like( |
| 112 |
$stderr, |
| 113 |
qr/could not get commit timestamp data/, |
| 114 |
'expected error from enabled tx when committs disabled'); |
| 115 |
|
| 116 |
# Do a tx while cts disabled |
| 117 |
my $xid_disabled = $node_primary->safe_psql( |
| 118 |
'postgres', qq[ |
| 119 |
BEGIN; |
| 120 |
INSERT INTO committs_test(x, y) VALUES (2, current_timestamp); |
| 121 |
SELECT pg_current_xact_id(); |
| 122 |
COMMIT; |
| 123 |
]); |
| 124 |
|
| 125 |
# Should be inaccessible |
| 126 |
($ret, $stdout, $stderr) = $node_primary->psql('postgres', |
| 127 |
qq[SELECT pg_xact_commit_timestamp('$xid_disabled');]); |
| 128 |
is($ret, 3, 'no commit timestamp when disabled'); |
| 129 |
like( |
| 130 |
$stderr, |
| 131 |
qr/could not get commit timestamp data/, |
| 132 |
'expected error from disabled tx when committs disabled'); |
| 133 |
|
| 134 |
# Re-enable, restart and ensure we can still get the old timestamps |
| 135 |
$node_primary->append_conf('postgresql.conf', 'track_commit_timestamp = on'); |
| 136 |
|
| 137 |
# An immediate shutdown is used here. At next startup recovery will |
| 138 |
# replay transactions which committed when track_commit_timestamp was |
| 139 |
# disabled, and the facility should be able to work properly. |
| 140 |
$node_primary->stop('immediate'); |
| 141 |
$node_primary->start; |
| 142 |
|
| 143 |
my $after_enable_ts = $node_primary->safe_psql('postgres', |
| 144 |
qq[SELECT pg_xact_commit_timestamp('$xid');]); |
| 145 |
is($after_enable_ts, '', 'timestamp of enabled tx null after re-enable'); |
| 146 |
|
| 147 |
my $after_enable_disabled_ts = $node_primary->safe_psql('postgres', |
| 148 |
qq[SELECT pg_xact_commit_timestamp('$xid_disabled');]); |
| 149 |
is($after_enable_disabled_ts, '', |
| 150 |
'timestamp of disabled tx null after re-enable'); |
| 151 |
|
| 152 |
$node_primary->stop; |
| 153 |
|
| 154 |
done_testing(); |