Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
 
# Copyright (c) 2026, PostgreSQL Global Development Group
 
# Test for the lock statistics and log_lock_waits
#
# This test creates multiple locking situations when a session (s2) has to
# wait on a lock for longer than deadlock_timeout. The first tests each test a
# dedicated lock type.
# The last one checks that log_lock_waits has no impact on the statistics
# counters.
#
# This test also checks that log_lock_waits messages are emitted both when
# a wait occurs and when the lock is acquired, and that the "still waiting for"
# message is logged exactly once per wait, even if the backend wakes due
# to signals.
 
use strict;
use warnings FATAL => 'all';
 
use PostgreSQL::Test::Cluster;
use PostgreSQL::Test::Utils;
use Test::More;
 
plan skip_all => 'Injection points not supported by this build'
  unless $ENV{enable_injection_points} eq 'yes';
 
my $deadlock_timeout = 10;
my $s1;
my $s2;
my $node;
 
# Setup the 2 sessions
sub setup_sessions
{
    $s1 = $node->background_psql('postgres');
    $s2 = $node->background_psql('postgres');
 
    # Setup injection points for the waiting session
    $s2->query_until(
        qr/attaching_injection_point/, q[
            \echo attaching_injection_point
            SELECT injection_points_attach('deadlock-timeout-fired', 'wait');
        ]);
}
 
# Fetch waits and wait_time from pg_stat_lock for a given lock type
# until they reached expected values: at least one wait and waiting longer
# than the deadlock_timeout.
sub wait_for_pg_stat_lock
{
    my ($node, $lock_type) = @_;
 
    $node->poll_query_until(
        'postgres', qq[
        SELECT waits > 0 AND wait_time >= $deadlock_timeout
        FROM pg_stat_lock
        WHERE locktype = '$lock_type';
    ]) or die "Timed out waiting for pg_stat_lock for $lock_type";
}
 
# Convenience wrapper to wait for a point, then detach it.
sub wait_and_detach
{
    my ($node, $point_name) = @_;
 
    $node->wait_for_event('client backend', $point_name);
    $node->safe_psql(
        'postgres', qq[
SELECT injection_points_detach('$point_name');
SELECT injection_points_wakeup('$point_name');
]);
}
 
# Node initialization
$node = PostgreSQL::Test::Cluster->new('node');
$node->init();
$node->append_conf('postgresql.conf',
    "deadlock_timeout = ${deadlock_timeout}ms");
$node->start();
 
# Check if the extension injection_points is available
plan skip_all => 'Extension injection_points not installed'
  unless $node->check_extension('injection_points');
 
$node->safe_psql('postgres', 'CREATE EXTENSION injection_points;');
 
$node->safe_psql(
    'postgres', q[
CREATE TABLE test_stat_tab(key text not null, value int);
INSERT INTO test_stat_tab(key, value) VALUES('k0', 1);
]);
 
############################################################################
 
####### Relation lock
 
setup_sessions();
 
my $log_offset = -s $node->logfile;
 
$s1->query_safe(
    q[
SELECT pg_stat_reset_shared('lock');
BEGIN;
LOCK TABLE test_stat_tab;
]);
 
# s2 setup
$s2->query_safe(
    q[
BEGIN;
SELECT pg_stat_force_next_flush();
]);
# s2 blocks on LOCK.
$s2->query_until(
    qr/lock_s2/, q[
\echo lock_s2
LOCK TABLE test_stat_tab;
]);
 
wait_and_detach($node, 'deadlock-timeout-fired');
 
# Check that log_lock_waits message is emitted during a lock wait.
$node->wait_for_log(qr/still waiting for AccessExclusiveLock on relation/,
    $log_offset);
 
# Wake the backend waiting on the lock and confirm it woke by calling
# pg_log_backend_memory_contexts() and checking for the logged memory
# contexts. This is necessary to test later that the "still waiting for"
# message is logged exactly once per wait, even if the backend wakes
# during the wait.
$node->safe_psql(
    'postgres', q[SELECT pg_log_backend_memory_contexts(pid)
    FROM pg_locks WHERE locktype = 'relation' AND
    relation = 'test_stat_tab'::regclass AND NOT granted;]);
$node->wait_for_log(qr/logging memory contexts/, $log_offset);
 
# deadlock_timeout fired, now commit in s1 and s2
$s1->query_safe(q(COMMIT));
$s2->query_safe(q(COMMIT));
 
# check that pg_stat_lock has been updated
wait_for_pg_stat_lock($node, 'relation');
ok(1, "Lock stats ok for relation");
 
# Check that log_lock_waits message is emitted when the lock is acquired
# after waiting.
$node->wait_for_log(qr/acquired AccessExclusiveLock on relation/,
    $log_offset);
 
# Check that the "still waiting for" message is logged exactly once per wait,
# even if the backend wakes during the wait.
my $log_contents = slurp_file($node->logfile, $log_offset);
my @still_waiting = ($log_contents =~ /still waiting for/g);
is( scalar @still_waiting,
    1,
    "still waiting logged exactly once despite wakeups from pg_log_backend_memory_contexts()"
);
 
# close sessions
$s1->quit;
$s2->quit;
 
####### transaction lock
 
setup_sessions();
 
$log_offset = -s $node->logfile;
 
$s1->query_safe(
    q[
SELECT pg_stat_reset_shared('lock');
INSERT INTO test_stat_tab(key, value) VALUES('k1', 1), ('k2', 1), ('k3', 1);
BEGIN;
UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
]);
 
# s2 setup
$s2->query_safe(
    q[
SET log_lock_waits = on;
BEGIN;
SELECT pg_stat_force_next_flush();
]);
# s2 blocks here on UPDATE
$s2->query_until(
    qr/lock_s2/, q[
\echo lock_s2
UPDATE test_stat_tab SET value = value + 1 WHERE key = 'k1';
]);
 
wait_and_detach($node, 'deadlock-timeout-fired');
 
# Check that log_lock_waits message is emitted during a lock wait.
$node->wait_for_log(qr/still waiting for ShareLock on transaction/,
    $log_offset);
 
# deadlock_timeout fired, now commit in s1 and s2
$s1->query_safe(q(COMMIT));
$s2->query_safe(q(COMMIT));
 
# check that pg_stat_lock has been updated
wait_for_pg_stat_lock($node, 'transactionid');
ok(1, "Lock stats ok for transactionid");
 
# Check that log_lock_waits message is emitted when the lock is acquired
# after waiting.
$node->wait_for_log(qr/acquired ShareLock on transaction/, $log_offset);
 
# Close sessions
$s1->quit;
$s2->quit;
 
####### advisory lock
 
setup_sessions();
 
$log_offset = -s $node->logfile;
 
$s1->query_safe(
    q[
SELECT pg_stat_reset_shared('lock');
SELECT pg_advisory_lock(1);
]);
 
# s2 setup
$s2->query_safe(
    q[
SET log_lock_waits = on;
BEGIN;
SELECT pg_stat_force_next_flush();
]);
# s2 blocks on the advisory lock.
$s2->query_until(
    qr/lock_s2/, q[
\echo lock_s2
SELECT pg_advisory_lock(1);
]);
 
wait_and_detach($node, 'deadlock-timeout-fired');
 
# Check that log_lock_waits message is emitted during a lock wait.
$node->wait_for_log(qr/still waiting for ExclusiveLock on advisory lock/,
    $log_offset);
 
# deadlock_timeout fired, now unlock and commit s2
$s1->query_safe(q(SELECT pg_advisory_unlock(1)));
$s2->query_safe(
    q[
SELECT pg_advisory_unlock(1);
COMMIT;
]);
 
# check that pg_stat_lock has been updated
wait_for_pg_stat_lock($node, 'advisory');
ok(1, "Lock stats ok for advisory");
 
# Check that log_lock_waits message is emitted when the lock is acquired
# after waiting.
$node->wait_for_log(qr/acquired ExclusiveLock on advisory lock/, $log_offset);
 
# Close sessions
$s1->quit;
$s2->quit;
 
####### Ensure log_lock_waits has no impact
 
setup_sessions();
 
$log_offset = -s $node->logfile;
 
$s1->query_safe(
    q[
SELECT pg_stat_reset_shared('lock');
BEGIN;
LOCK TABLE test_stat_tab;
]);
 
# s2 setup
$s2->query_safe(
    q[
SET log_lock_waits = off;
BEGIN;
SELECT pg_stat_force_next_flush();
]);
# s2 blocks on LOCK.
$s2->query_until(
    qr/lock_s2/, q[
\echo lock_s2
LOCK TABLE test_stat_tab;
]);
 
wait_and_detach($node, 'deadlock-timeout-fired');
 
# deadlock_timeout fired, now commit in s1 and s2
$s1->query_safe(q(COMMIT));
$s2->query_safe(q(COMMIT));
 
# check that pg_stat_lock has been updated
wait_for_pg_stat_lock($node, 'relation');
ok(1, "log_lock_waits has no impact on Lock stats");
 
# Check that no log_lock_waits messages are emitted
ok( !$node->log_contains(
        "still waiting for AccessExclusiveLock on relation", $log_offset),
    'check that no log_lock_waits message is emitted during a lock wait');
ok( !$node->log_contains(
        "acquired AccessExclusiveLock on relation", $log_offset),
    'check that no log_lock_waits message is emitted when the lock is acquired after waiting'
);
 
# close sessions
$s1->quit;
$s2->quit;
 
# cleanup
$node->safe_psql('postgres', q[DROP TABLE test_stat_tab;]);
 
done_testing();