|
| 1 | + |
| 2 | +# Copyright (c) 2024, PostgreSQL Global Development Group |
| 3 | + |
| 4 | +use strict; |
| 5 | +use warnings; |
| 6 | +use PostgreSQL::Test::Cluster; |
| 7 | +use PostgreSQL::Test::Utils; |
| 8 | +use Time::HiRes qw(usleep); |
| 9 | +use Test::More; |
| 10 | + |
| 11 | +# Test timeouts that will cause FATAL errors. This test relies on injection |
| 12 | +# points to await a timeout occurrence. Relying on sleep proved to be unstable |
| 13 | +# on buildfarm. It's difficult to rely on the NOTICE injection point because |
| 14 | +# the backend under FATAL error can behave differently. |
| 15 | + |
| 16 | +if ($ENV{enable_injection_points} ne 'yes') |
| 17 | +{ |
| 18 | + plan skip_all => 'Injection points not supported by this build'; |
| 19 | +} |
| 20 | + |
| 21 | +# Node initialization |
| 22 | +my $node = PostgreSQL::Test::Cluster->new('master'); |
| 23 | +$node->init(); |
| 24 | +$node->start; |
| 25 | +$node->safe_psql('postgres', 'CREATE EXTENSION injection_points;'); |
| 26 | + |
| 27 | +# |
| 28 | +# 1. Test of the transaction timeout |
| 29 | +# |
| 30 | + |
| 31 | +$node->safe_psql('postgres', |
| 32 | + "SELECT injection_points_attach('transaction-timeout', 'wait');"); |
| 33 | + |
| 34 | +my $psql_session = $node->background_psql('postgres'); |
| 35 | + |
| 36 | +# The following query will generate a stream of SELECT 1 queries. This is done |
| 37 | +# so to exercise transaction timeout in the presence of short queries. |
| 38 | +$psql_session->query_until( |
| 39 | + qr/starting_bg_psql/, q( |
| 40 | + \echo starting_bg_psql |
| 41 | + SET transaction_timeout to '10ms'; |
| 42 | + BEGIN; |
| 43 | + SELECT 1 \watch 0.001 |
| 44 | + \q |
| 45 | +)); |
| 46 | + |
| 47 | +# Wait until the backend is in the timeout injection point. Will get an error |
| 48 | +# here if anything goes wrong. |
| 49 | +$node->wait_for_event('client backend', 'transaction-timeout'); |
| 50 | + |
| 51 | +my $log_offset = -s $node->logfile; |
| 52 | + |
| 53 | +# Remove the injection point. |
| 54 | +$node->safe_psql('postgres', |
| 55 | + "SELECT injection_points_wakeup('transaction-timeout');"); |
| 56 | + |
| 57 | +# Check that the timeout was logged. |
| 58 | +$node->wait_for_log('terminating connection due to transaction timeout', |
| 59 | + $log_offset); |
| 60 | + |
| 61 | +# If we send \q with $psql_session->quit it can get to pump already closed. |
| 62 | +# So \q is in initial script, here we only finish IPC::Run. |
| 63 | +$psql_session->{run}->finish; |
| 64 | + |
| 65 | + |
| 66 | +# |
| 67 | +# 2. Test of the sidle in transaction timeout |
| 68 | +# |
| 69 | + |
| 70 | +$node->safe_psql('postgres', |
| 71 | + "SELECT injection_points_attach('idle-in-transaction-session-timeout', 'wait');" |
| 72 | +); |
| 73 | + |
| 74 | +# We begin a transaction and the hand on the line |
| 75 | +$psql_session = $node->background_psql('postgres'); |
| 76 | +$psql_session->query_until( |
| 77 | + qr/starting_bg_psql/, q( |
| 78 | + \echo starting_bg_psql |
| 79 | + SET idle_in_transaction_session_timeout to '10ms'; |
| 80 | + BEGIN; |
| 81 | +)); |
| 82 | + |
| 83 | +# Wait until the backend is in the timeout injection point. |
| 84 | +$node->wait_for_event('client backend', |
| 85 | + 'idle-in-transaction-session-timeout'); |
| 86 | + |
| 87 | +$log_offset = -s $node->logfile; |
| 88 | + |
| 89 | +# Remove the injection point. |
| 90 | +$node->safe_psql('postgres', |
| 91 | + "SELECT injection_points_wakeup('idle-in-transaction-session-timeout');"); |
| 92 | + |
| 93 | +# Check that the timeout was logged. |
| 94 | +$node->wait_for_log( |
| 95 | + 'terminating connection due to idle-in-transaction timeout', $log_offset); |
| 96 | + |
| 97 | +ok($psql_session->quit); |
| 98 | + |
| 99 | + |
| 100 | +# |
| 101 | +# 3. Test of the idle session timeout |
| 102 | +# |
| 103 | +$node->safe_psql('postgres', |
| 104 | + "SELECT injection_points_attach('idle-session-timeout', 'wait');"); |
| 105 | + |
| 106 | +# We just initialize the GUC and wait. No transaction is required. |
| 107 | +$psql_session = $node->background_psql('postgres'); |
| 108 | +$psql_session->query_until( |
| 109 | + qr/starting_bg_psql/, q( |
| 110 | + \echo starting_bg_psql |
| 111 | + SET idle_session_timeout to '10ms'; |
| 112 | +)); |
| 113 | + |
| 114 | +# Wait until the backend is in the timeout injection point. |
| 115 | +$node->wait_for_event('client backend', 'idle-session-timeout'); |
| 116 | + |
| 117 | +$log_offset = -s $node->logfile; |
| 118 | + |
| 119 | +# Remove the injection point. |
| 120 | +$node->safe_psql('postgres', |
| 121 | + "SELECT injection_points_wakeup('idle-session-timeout');"); |
| 122 | + |
| 123 | +# Check that the timeout was logged. |
| 124 | +$node->wait_for_log('terminating connection due to idle-session timeout', |
| 125 | + $log_offset); |
| 126 | + |
| 127 | +ok($psql_session->quit); |
| 128 | + |
| 129 | +done_testing(); |
0 commit comments