From aaf4b21dffcabd55f7dd5fb9d1f35f13ba434153 Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Thu, 16 Nov 2023 14:28:22 +0900 Subject: [PATCH v6 3/4] Add regression test to show snapbuild consistency Reverting 409f9ca44713 causes the test to fail. The test added here relies on the existing callbacks in test_injection_points. --- src/backend/replication/logical/snapbuild.c | 3 ++ .../modules/test_injection_points/Makefile | 2 + .../modules/test_injection_points/meson.build | 5 ++ .../t/001_snapshot_status.pl | 47 +++++++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 src/test/modules/test_injection_points/t/001_snapshot_status.pl diff --git a/src/backend/replication/logical/snapbuild.c b/src/backend/replication/logical/snapbuild.c index fec190a8b2..3491e5a872 100644 --- a/src/backend/replication/logical/snapbuild.c +++ b/src/backend/replication/logical/snapbuild.c @@ -141,6 +141,7 @@ #include "storage/procarray.h" #include "storage/standby.h" #include "utils/builtins.h" +#include "utils/injection_point.h" #include "utils/memutils.h" #include "utils/snapmgr.h" #include "utils/snapshot.h" @@ -654,6 +655,8 @@ SnapBuildInitialSnapshot(SnapBuild *builder) snap->xcnt = newxcnt; snap->xip = newxip; + INJECTION_POINT("SnapBuildInitialSnapshot"); + return snap; } diff --git a/src/test/modules/test_injection_points/Makefile b/src/test/modules/test_injection_points/Makefile index 65bcdde782..4696c1b013 100644 --- a/src/test/modules/test_injection_points/Makefile +++ b/src/test/modules/test_injection_points/Makefile @@ -10,6 +10,8 @@ EXTENSION = test_injection_points DATA = test_injection_points--1.0.sql REGRESS = test_injection_points +TAP_TESTS = 1 + ifdef USE_PGXS PG_CONFIG = pg_config PGXS := $(shell $(PG_CONFIG) --pgxs) diff --git a/src/test/modules/test_injection_points/meson.build b/src/test/modules/test_injection_points/meson.build index 7509a102ef..6006b38f3d 100644 --- a/src/test/modules/test_injection_points/meson.build +++ b/src/test/modules/test_injection_points/meson.build @@ -34,4 +34,9 @@ tests += { 'test_injection_points', ], }, + 'tap': { + 'tests': [ + 't/001_snapshot_status.pl', + ], + } } diff --git a/src/test/modules/test_injection_points/t/001_snapshot_status.pl b/src/test/modules/test_injection_points/t/001_snapshot_status.pl new file mode 100644 index 0000000000..ca5c6cc7a4 --- /dev/null +++ b/src/test/modules/test_injection_points/t/001_snapshot_status.pl @@ -0,0 +1,47 @@ +# Test consistent of initial snapshot data. + +# This requires a node with wal_level=logical combined with an injection +# point that forces a failure when a snapshot is initially built with a +# logical slot created. +# +# See bug https://postgr.es/m/CAFiTN-s0zA1Kj0ozGHwkYkHwa5U0zUE94RSc_g81WrpcETB5=w@mail.gmail.com. + +use strict; +use warnings; + +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +my $node = PostgreSQL::Test::Cluster->new('node'); +$node->init(allows_streaming => 'logical'); +$node->start; + +$node->safe_psql('postgres', 'CREATE EXTENSION test_injection_points;'); +$node->safe_psql('postgres', + "SELECT test_injection_points_attach('SnapBuildInitialSnapshot', 'error');"); + +my $node_host = $node->host; +my $node_port = $node->port; +my $connstr_common = "host=$node_host port=$node_port"; +my $connstr_db = "$connstr_common replication=database dbname=postgres"; + +# This requires a single session, with two commands. +my $psql_session = + $node->background_psql('postgres', on_error_stop => 0, + extra_params => [ '-d', $connstr_db ]); +my ($output, $ret) = $psql_session->query( + 'CREATE_REPLICATION_SLOT "slot" LOGICAL "pgoutput";'); +ok($ret != 0, "First CREATE_REPLICATION_SLOT fails on injected error"); + +# Now remove the injected error and check that the second command works. +$node->safe_psql('postgres', + "SELECT test_injection_points_detach('SnapBuildInitialSnapshot');"); + +($output, $ret) = $psql_session->query( + 'CREATE_REPLICATION_SLOT "slot" LOGICAL "pgoutput";'); +print "BOO" . substr($output, 0, 4) . "\n"; +ok(substr($output, 0, 4) eq 'slot', + "Second CREATE_REPLICATION_SLOT passes"); + +done_testing(); -- 2.43.0