1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# Copyright (c) 2021-2022, PostgreSQL Global Development Group
# Tests that standbys:
# - drop stats for objects when the those records are replayed
# - persist stats across graceful restarts
# - discard stats after immediate / crash restarts
use strict;
use warnings;
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
my $node_primary = PostgreSQL::Test::Cluster->new('primary');
$node_primary->init(allows_streaming => 1);
$node_primary->append_conf('postgresql.conf', "track_functions = 'all'");
$node_primary->start;
my $backup_name = 'my_backup';
$node_primary->backup($backup_name);
my $node_standby = PostgreSQL::Test::Cluster->new('standby');
$node_standby->init_from_backup($node_primary, $backup_name,
has_streaming => 1);
$node_standby->start;
## Test that stats are cleaned up on standby after dropping table or function
my $sect = 'initial';
my ($dboid, $tableoid, $funcoid) =
populate_standby_stats('postgres', 'public');
test_standby_func_tab_stats_status('postgres',
$dboid, $tableoid, $funcoid, 't');
drop_table_by_oid('postgres', $tableoid);
drop_function_by_oid('postgres', $funcoid);
$sect = 'post drop';
my $primary_lsn = $node_primary->lsn('flush');
$node_primary->wait_for_catchup($node_standby, 'replay', $primary_lsn);
test_standby_func_tab_stats_status('postgres',
$dboid, $tableoid, $funcoid, 'f');
## Test that stats are cleaned up on standby after dropping indirectly
$sect = "schema creation";
$node_primary->safe_psql('postgres', "CREATE SCHEMA drop_schema_test1");
$primary_lsn = $node_primary->lsn('flush');
$node_primary->wait_for_catchup($node_standby, 'replay', $primary_lsn);
($dboid, $tableoid, $funcoid) =
populate_standby_stats('postgres', 'drop_schema_test1');
test_standby_func_tab_stats_status('postgres',
$dboid, $tableoid, $funcoid, 't');
$node_primary->safe_psql('postgres', "DROP SCHEMA drop_schema_test1 CASCADE");
$sect = "post schema drop";
$primary_lsn = $node_primary->lsn('flush');
$node_primary->wait_for_catchup($node_standby, 'replay', $primary_lsn);
# verify table and function stats removed from standby
test_standby_func_tab_stats_status('postgres',
$dboid, $tableoid, $funcoid, 'f');
## Test that stats are cleaned up on standby after dropping database
$sect = "createdb";
$node_primary->safe_psql('postgres', "CREATE DATABASE test");
$primary_lsn = $node_primary->lsn('flush');
$node_primary->wait_for_catchup($node_standby, 'replay', $primary_lsn);
($dboid, $tableoid, $funcoid) = populate_standby_stats('test', 'public');
# verify stats are present
test_standby_func_tab_stats_status('test', $dboid, $tableoid, $funcoid, 't');
test_standby_db_stats_status('test', $dboid, 't');
$node_primary->safe_psql('postgres', "DROP DATABASE test");
$sect = "post dropdb";
$primary_lsn = $node_primary->lsn('flush');
$node_primary->wait_for_catchup($node_standby, 'replay', $primary_lsn);
# Test that the stats were cleaned up on standby
# Note that this connects to 'postgres' but provides the dboid of dropped db
# 'test' which we acquired previously
test_standby_func_tab_stats_status('postgres',
$dboid, $tableoid, $funcoid, 'f');
test_standby_db_stats_status('postgres', $dboid, 'f');
## verify that stats persist across graceful restarts on a replica
# NB: Can't test database stats, they're immediately repopulated when
# reconnecting...
$sect = "pre restart";
($dboid, $tableoid, $funcoid) = populate_standby_stats('postgres', 'public');
test_standby_func_tab_stats_status('postgres',
$dboid, $tableoid, $funcoid, 't');
$node_standby->restart();
$sect = "post non-immediate";
test_standby_func_tab_stats_status('postgres',
$dboid, $tableoid, $funcoid, 't');
# but gone after an immediate restart
$node_standby->stop('immediate');
$node_standby->start();
$sect = "post immediate restart";
test_standby_func_tab_stats_status('postgres',
$dboid, $tableoid, $funcoid, 'f');
done_testing();
sub populate_standby_stats
{
my ($connect_db, $schema) = @_;
# create objects on primary
$node_primary->safe_psql($connect_db,
"CREATE TABLE $schema.drop_tab_test1 AS SELECT generate_series(1,100) AS a"
);
$node_primary->safe_psql($connect_db,
"CREATE FUNCTION $schema.drop_func_test1() RETURNS VOID AS 'select 2;' LANGUAGE SQL IMMUTABLE"
);
my $primary_lsn = $node_primary->lsn('flush');
$node_primary->wait_for_catchup($node_standby, 'replay', $primary_lsn);
# collect object oids
my $dboid = $node_standby->safe_psql($connect_db,
"SELECT oid FROM pg_database WHERE datname = '$connect_db'");
my $tableoid = $node_standby->safe_psql($connect_db,
"SELECT '$schema.drop_tab_test1'::regclass::oid");
my $funcoid = $node_standby->safe_psql($connect_db,
"SELECT '$schema.drop_func_test1()'::regprocedure::oid");
# generate stats on standby
$node_standby->safe_psql($connect_db,
"SELECT * FROM $schema.drop_tab_test1");
$node_standby->safe_psql($connect_db, "SELECT $schema.drop_func_test1()");
return ($dboid, $tableoid, $funcoid);
}
sub drop_function_by_oid
{
my ($connect_db, $funcoid) = @_;
# Get function name from returned oid
my $func_name = $node_primary->safe_psql($connect_db,
"SELECT '$funcoid'::regprocedure");
$node_primary->safe_psql($connect_db, "DROP FUNCTION $func_name");
}
sub drop_table_by_oid
{
my ($connect_db, $tableoid) = @_;
# Get table name from returned oid
my $table_name =
$node_primary->safe_psql($connect_db, "SELECT '$tableoid'::regclass");
$node_primary->safe_psql($connect_db, "DROP TABLE $table_name");
}
sub test_standby_func_tab_stats_status
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($connect_db, $dboid, $tableoid, $funcoid, $present) = @_;
my %expected = (rel => $present, func => $present);
my %stats;
$stats{rel} = $node_standby->safe_psql($connect_db,
"SELECT pg_stat_have_stats('relation', $dboid, $tableoid)");
$stats{func} = $node_standby->safe_psql($connect_db,
"SELECT pg_stat_have_stats('function', $dboid, $funcoid)");
is_deeply(\%stats, \%expected, "$sect: standby stats as expected");
return;
}
sub test_standby_db_stats_status
{
local $Test::Builder::Level = $Test::Builder::Level + 1;
my ($connect_db, $dboid, $present) = @_;
is( $node_standby->safe_psql(
$connect_db, "SELECT pg_stat_have_stats('database', $dboid, 0)"),
$present,
"$sect: standby db stats as expected");
}
|