From 9def031bd2821f35b5f506260d922482648a8bb0 Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Tue, 1 Mar 2016 19:53:18 -0300
Subject: [PATCH] Add more tests for commit_timestamp feature

These tests verify that 1) WAL replay preserves the stored value,
2) a streaming standby server replays the value obtained from the
master, and 3) the behavior is sensible in the face of repeated
configuration changes.

One annoyance is that tmp_check/ subdir from the TAP tests is clobbered
when the pg_regress test runs in the same subdirectory.  This is
bothersome but not too terrible a problem, since the pg_regress test is
not run anyway if the TAP tests fail (unless "make -k" is used).

I had these tests around since commit 69e7235c93e2; add them now that we
have the recovery test framework in place.
---
 src/test/modules/commit_ts/Makefile           |  5 ++
 src/test/modules/commit_ts/t/001_base.pl      | 29 ++++++++++
 src/test/modules/commit_ts/t/002_standby.pl   | 55 ++++++++++++++++++
 src/test/modules/commit_ts/t/003_standby_2.pl | 56 +++++++++++++++++++
 4 files changed, 145 insertions(+)
 create mode 100644 src/test/modules/commit_ts/t/001_base.pl
 create mode 100644 src/test/modules/commit_ts/t/002_standby.pl
 create mode 100644 src/test/modules/commit_ts/t/003_standby_2.pl

diff --git a/src/test/modules/commit_ts/Makefile b/src/test/modules/commit_ts/Makefile
index b3cb3151107..f814f767c44 100644
--- a/src/test/modules/commit_ts/Makefile
+++ b/src/test/modules/commit_ts/Makefile
@@ -3,6 +3,11 @@
 REGRESS = commit_timestamp
 REGRESS_OPTS = --temp-config=$(top_srcdir)/src/test/modules/commit_ts/commit_ts.conf
 
+check: prove-check
+
+prove-check:
+	$(prove_check)
+
 ifdef USE_PGXS
 PG_CONFIG = pg_config
 PGXS := $(shell $(PG_CONFIG) --pgxs)
diff --git a/src/test/modules/commit_ts/t/001_base.pl b/src/test/modules/commit_ts/t/001_base.pl
new file mode 100644
index 00000000000..122b51557ad
--- /dev/null
+++ b/src/test/modules/commit_ts/t/001_base.pl
@@ -0,0 +1,29 @@
+# Single-node test: value can be set, and is still present after recovery
+
+use strict;
+use warnings;
+
+use TestLib;
+use Test::More tests => 2;
+use PostgresNode;
+
+my $node = get_new_node();
+$node->init;
+$node->append_conf('postgresql.conf', 'track_commit_timestamp = on');
+$node->start;
+
+# Create a table, compare "now()" to the commit TS of its xmin
+$node->psql('postgres', 'create table t as select now from (select now(), pg_sleep(1)) f');
+my $true = $node->psql('postgres',
+	'select t.now - ts.* < \'1s\' from t, pg_class c, pg_xact_commit_timestamp(c.xmin) ts where relname = \'t\'');
+is($true, 't', 'commit TS is set');
+my $ts = $node->psql('postgres',
+	'select ts.* from pg_class, pg_xact_commit_timestamp(xmin) ts where relname = \'t\'');
+
+# Verify that we read the same TS after crash recovery
+$node->stop('immediate');
+$node->start;
+
+my $recovered_ts = $node->psql('postgres',
+	'select ts.* from pg_class, pg_xact_commit_timestamp(xmin) ts where relname = \'t\'');
+is($recovered_ts, $ts, 'commit TS remains after crash recovery');
diff --git a/src/test/modules/commit_ts/t/002_standby.pl b/src/test/modules/commit_ts/t/002_standby.pl
new file mode 100644
index 00000000000..5cc2501c36c
--- /dev/null
+++ b/src/test/modules/commit_ts/t/002_standby.pl
@@ -0,0 +1,55 @@
+# Test simple scenario involving a standby
+
+use strict;
+use warnings;
+
+use TestLib;
+use Test::More tests => 2;
+use PostgresNode;
+
+my $bkplabel = 'backup';
+my $master = get_new_node('master');
+$master->init(allows_streaming => 1);
+
+$master->append_conf('postgresql.conf', qq{
+	track_commit_timestamp = on
+	max_wal_senders = 5
+	wal_level = hot_standby
+	});
+$master->start;
+$master->backup($bkplabel);
+
+my $standby = get_new_node('standby');
+$standby->init_from_backup($master, $bkplabel, has_streaming => 1);
+$standby->start;
+
+for my $i (1 .. 10)
+{
+	$master->psql('postgres', "create table t$i()");
+}
+my $master_ts = $master->psql('postgres',
+	qq{SELECT ts.* FROM pg_class, pg_xact_commit_timestamp(xmin) AS ts WHERE relname = 't10'});
+my $master_lsn = $master->psql('postgres',
+	'select pg_current_xlog_location()');
+$standby->poll_query_until('postgres',
+	qq{SELECT '$master_lsn'::pg_lsn <= pg_last_xlog_replay_location()})
+	or die "slave never caught up";
+
+my $standby_ts = $standby->psql('postgres',
+	qq{select ts.* from pg_class, pg_xact_commit_timestamp(xmin) ts where relname = 't10'});
+is($master_ts, $standby_ts, "standby gives same value as master");
+
+$master->append_conf('postgresql.conf', 'track_commit_timestamp = off');
+$master->restart;
+$master->psql('postgres', 'checkpoint');
+$master_lsn = $master->psql('postgres',
+	'select pg_current_xlog_location()');
+$standby->poll_query_until('postgres',
+	qq{SELECT '$master_lsn'::pg_lsn <= pg_last_xlog_replay_location()})
+	or die "slave never caught up";
+$standby->psql('postgres', 'checkpoint');
+
+# This one should raise an error now
+$standby_ts = $standby->psql('postgres',
+	'select ts.* from pg_class, pg_xact_commit_timestamp(xmin) ts where relname = \'t10\'');
+is($standby_ts, '', "standby gives no value when master turned feature off");
diff --git a/src/test/modules/commit_ts/t/003_standby_2.pl b/src/test/modules/commit_ts/t/003_standby_2.pl
new file mode 100644
index 00000000000..fadb6a237d9
--- /dev/null
+++ b/src/test/modules/commit_ts/t/003_standby_2.pl
@@ -0,0 +1,56 @@
+# Test master/standby scenario where the track_commit_timestamp GUC is
+# repeatedly toggled on and off.
+use strict;
+use warnings;
+
+use TestLib;
+use Test::More tests => 2;
+use PostgresNode;
+
+my $bkplabel = 'backup';
+my $master = get_new_node('master');
+$master->init(allows_streaming => 1);
+$master->append_conf('postgresql.conf', qq{
+	track_commit_timestamp = on
+	max_wal_senders = 5
+	wal_level = hot_standby
+	});
+$master->start;
+$master->backup($bkplabel);
+
+my $standby = get_new_node('standby');
+$standby->init_from_backup($master, $bkplabel, has_streaming => 1);
+$standby->start;
+
+for my $i (1 .. 10)
+{
+	$master->psql('postgres', "create table t$i()");
+}
+$master->append_conf('postgresql.conf', 'track_commit_timestamp = off');
+$master->restart;
+$master->psql('postgres', 'checkpoint');
+my $master_lsn = $master->psql('postgres',
+	'select pg_current_xlog_location()');
+$standby->poll_query_until('postgres',
+	qq{SELECT '$master_lsn'::pg_lsn <= pg_last_xlog_replay_location()})
+	or die "slave never caught up";
+
+$standby->psql('postgres', 'checkpoint');
+$standby->restart;
+
+my $standby_ts = $standby->psql('postgres',
+	qq{SELECT ts.* FROM pg_class, pg_xact_commit_timestamp(xmin) AS ts WHERE relname = 't10'});
+is($standby_ts, '', "standby does not return a value after restart");
+
+$master->append_conf('postgresql.conf', 'track_commit_timestamp = on');
+$master->restart;
+$master->append_conf('postgresql.conf', 'track_commit_timestamp = off');
+$master->restart;
+
+system_or_bail('pg_ctl', '-w', '-D', $standby->data_dir, 'promote');
+$standby->poll_query_until('postgres', "SELECT pg_is_in_recovery() <> true");
+
+$standby->psql('postgres', "create table t11()");
+$standby_ts = $standby->psql('postgres',
+	qq{SELECT ts.* FROM pg_class, pg_xact_commit_timestamp(xmin) AS ts WHERE relname = 't11'});
+isnt($standby_ts, '', "standby gives valid value ($standby_ts) after promotion");
-- 
GitLab