aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Korotkov <akorotkov@postgresql.org>2024-03-14 13:10:21 +0200
committerAlexander Korotkov <akorotkov@postgresql.org>2024-03-14 13:12:15 +0200
commiteeefd4280f6e5167d70efabb89586b7d38922d95 (patch)
tree25f6bdce86953c94e52b90cc367d15896c966483
parente85662df44ff47acdf5d2d413339445d60a9c30c (diff)
downloadpostgresql-eeefd4280f6e5167d70efabb89586b7d38922d95.tar.gz
postgresql-eeefd4280f6e5167d70efabb89586b7d38922d95.zip
Add TAP tests for timeouts
This commit adds new tests to verify that transaction_timeout, idle_session_timeout, and idle_in_transaction_session_timeout work as expected. We introduce new injection points in before throwing a timeout FATAL error and check these injection points are reached. Discussion: https://postgr.es/m/CAAhFRxiQsRs2Eq5kCo9nXE3HTugsAAJdSQSmxncivebAxdmBjQ%40mail.gmail.com Author: Andrey Borodin Reviewed-by: Alexander Korotkov
-rw-r--r--src/backend/tcop/postgres.c10
-rw-r--r--src/test/modules/test_misc/Makefile4
-rw-r--r--src/test/modules/test_misc/meson.build4
-rw-r--r--src/test/modules/test_misc/t/005_timeouts.pl129
4 files changed, 147 insertions, 0 deletions
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 6b7903314ab..7ac623019bc 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -72,6 +72,7 @@
#include "tcop/tcopprot.h"
#include "tcop/utility.h"
#include "utils/guc_hooks.h"
+#include "utils/injection_point.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/ps_status.h"
@@ -3411,9 +3412,12 @@ ProcessInterrupts(void)
* interrupt.
*/
if (IdleInTransactionSessionTimeout > 0)
+ {
+ INJECTION_POINT("idle-in-transaction-session-timeout");
ereport(FATAL,
(errcode(ERRCODE_IDLE_IN_TRANSACTION_SESSION_TIMEOUT),
errmsg("terminating connection due to idle-in-transaction timeout")));
+ }
else
IdleInTransactionSessionTimeoutPending = false;
}
@@ -3422,9 +3426,12 @@ ProcessInterrupts(void)
{
/* As above, ignore the signal if the GUC has been reset to zero. */
if (TransactionTimeout > 0)
+ {
+ INJECTION_POINT("transaction-timeout");
ereport(FATAL,
(errcode(ERRCODE_TRANSACTION_TIMEOUT),
errmsg("terminating connection due to transaction timeout")));
+ }
else
TransactionTimeoutPending = false;
}
@@ -3433,9 +3440,12 @@ ProcessInterrupts(void)
{
/* As above, ignore the signal if the GUC has been reset to zero. */
if (IdleSessionTimeout > 0)
+ {
+ INJECTION_POINT("idle-session-timeout");
ereport(FATAL,
(errcode(ERRCODE_IDLE_SESSION_TIMEOUT),
errmsg("terminating connection due to idle-session timeout")));
+ }
else
IdleSessionTimeoutPending = false;
}
diff --git a/src/test/modules/test_misc/Makefile b/src/test/modules/test_misc/Makefile
index 39c6c2014a0..a958d156f47 100644
--- a/src/test/modules/test_misc/Makefile
+++ b/src/test/modules/test_misc/Makefile
@@ -2,6 +2,10 @@
TAP_TESTS = 1
+EXTRA_INSTALL=src/test/modules/injection_points
+
+export enable_injection_points enable_injection_points
+
ifdef USE_PGXS
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
diff --git a/src/test/modules/test_misc/meson.build b/src/test/modules/test_misc/meson.build
index 964d95db263..df2913e8938 100644
--- a/src/test/modules/test_misc/meson.build
+++ b/src/test/modules/test_misc/meson.build
@@ -5,11 +5,15 @@ tests += {
'sd': meson.current_source_dir(),
'bd': meson.current_build_dir(),
'tap': {
+ 'env': {
+ 'enable_injection_points': get_option('injection_points') ? 'yes' : 'no',
+ },
'tests': [
't/001_constraint_validation.pl',
't/002_tablespace.pl',
't/003_check_guc.pl',
't/004_io_direct.pl',
+ 't/005_timeouts.pl'
],
},
}
diff --git a/src/test/modules/test_misc/t/005_timeouts.pl b/src/test/modules/test_misc/t/005_timeouts.pl
new file mode 100644
index 00000000000..e67b3e694b9
--- /dev/null
+++ b/src/test/modules/test_misc/t/005_timeouts.pl
@@ -0,0 +1,129 @@
+
+# Copyright (c) 2024, PostgreSQL Global Development Group
+
+use strict;
+use warnings;
+use PostgreSQL::Test::Cluster;
+use PostgreSQL::Test::Utils;
+use Time::HiRes qw(usleep);
+use Test::More;
+
+# Test timeouts that will cause FATAL errors. This test relies on injection
+# points to await a timeout occurrence. Relying on sleep proved to be unstable
+# on buildfarm. It's difficult to rely on the NOTICE injection point because
+# the backend under FATAL error can behave differently.
+
+if ($ENV{enable_injection_points} ne 'yes')
+{
+ plan skip_all => 'Injection points not supported by this build';
+}
+
+# Node initialization
+my $node = PostgreSQL::Test::Cluster->new('master');
+$node->init();
+$node->start;
+$node->safe_psql('postgres', 'CREATE EXTENSION injection_points;');
+
+#
+# 1. Test of the transaction timeout
+#
+
+$node->safe_psql('postgres',
+ "SELECT injection_points_attach('transaction-timeout', 'wait');");
+
+my $psql_session = $node->background_psql('postgres');
+
+# The following query will generate a stream of SELECT 1 queries. This is done
+# so to exercise transaction timeout in the presence of short queries.
+$psql_session->query_until(
+ qr/starting_bg_psql/, q(
+ \echo starting_bg_psql
+ SET transaction_timeout to '10ms';
+ BEGIN;
+ SELECT 1 \watch 0.001
+ \q
+));
+
+# Wait until the backend is in the timeout injection point. Will get an error
+# here if anything goes wrong.
+$node->wait_for_event('client backend', 'transaction-timeout');
+
+my $log_offset = -s $node->logfile;
+
+# Remove the injection point.
+$node->safe_psql('postgres',
+ "SELECT injection_points_wakeup('transaction-timeout');");
+
+# Check that the timeout was logged.
+$node->wait_for_log('terminating connection due to transaction timeout',
+ $log_offset);
+
+# If we send \q with $psql_session->quit it can get to pump already closed.
+# So \q is in initial script, here we only finish IPC::Run.
+$psql_session->{run}->finish;
+
+
+#
+# 2. Test of the sidle in transaction timeout
+#
+
+$node->safe_psql('postgres',
+ "SELECT injection_points_attach('idle-in-transaction-session-timeout', 'wait');"
+);
+
+# We begin a transaction and the hand on the line
+$psql_session = $node->background_psql('postgres');
+$psql_session->query_until(
+ qr/starting_bg_psql/, q(
+ \echo starting_bg_psql
+ SET idle_in_transaction_session_timeout to '10ms';
+ BEGIN;
+));
+
+# Wait until the backend is in the timeout injection point.
+$node->wait_for_event('client backend',
+ 'idle-in-transaction-session-timeout');
+
+$log_offset = -s $node->logfile;
+
+# Remove the injection point.
+$node->safe_psql('postgres',
+ "SELECT injection_points_wakeup('idle-in-transaction-session-timeout');");
+
+# Check that the timeout was logged.
+$node->wait_for_log(
+ 'terminating connection due to idle-in-transaction timeout', $log_offset);
+
+ok($psql_session->quit);
+
+
+#
+# 3. Test of the idle session timeout
+#
+$node->safe_psql('postgres',
+ "SELECT injection_points_attach('idle-session-timeout', 'wait');");
+
+# We just initialize the GUC and wait. No transaction is required.
+$psql_session = $node->background_psql('postgres');
+$psql_session->query_until(
+ qr/starting_bg_psql/, q(
+ \echo starting_bg_psql
+ SET idle_session_timeout to '10ms';
+));
+
+# Wait until the backend is in the timeout injection point.
+$node->wait_for_event('client backend', 'idle-session-timeout');
+
+$log_offset = -s $node->logfile;
+
+# Remove the injection point.
+$node->safe_psql('postgres',
+ "SELECT injection_points_wakeup('idle-session-timeout');");
+
+# Check that the timeout was logged.
+$node->wait_for_log('terminating connection due to idle-session timeout',
+ $log_offset);
+
+ok($psql_session->quit);
+
+done_testing();