Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2c83f43

Browse files
committed
Rework PostgresNode's psql method
This makes the psql() method much more capable: it captures both stdoutand stderr; it now returns the psql exit code rather than stdout; atimeout can now be specified, as can ON_ERROR_STOP behavior; it gained anew "on_error_die" (defaulting to off) parameter to raise an exceptionif there's any problem. Finally, additional parameters to psql can bepassed if there's need for further tweaking.For convenience, a new safe_psql() method retains much of the oldbehavior of psql(), except that it uses on_error_die on, so thatproblems like syntax errors in SQL commands can be detected more easily.Many existing TAP test files now use safe_psql, which is what is reallywanted. A couple of ->psql() calls are now added in the commit_tstests, which verify that the right thing is happening on certain errors.Some ->command_fails() calls in recovery tests that were verifying thatpsql failed also became ->psql() calls now.Author: Craig Ringer. Some tweaks by Álvaro HerreraReviewed-By: Michaël Paquier
1 parent7d9a430 commit2c83f43

16 files changed

+326
-102
lines changed

‎src/bin/pg_basebackup/t/010_pg_basebackup.pl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@
112112
symlink"$tempdir",$shorter_tempdir;
113113

114114
mkdir"$tempdir/tblspc1";
115-
$node->psql('postgres',
115+
$node->safe_psql('postgres',
116116
"CREATE TABLESPACE tblspc1 LOCATION '$shorter_tempdir/tblspc1';");
117-
$node->psql('postgres',"CREATE TABLE test1 (a int) TABLESPACE tblspc1;");
117+
$node->safe_psql('postgres',"CREATE TABLE test1 (a int) TABLESPACE tblspc1;");
118118
$node->command_ok(['pg_basebackup','-D',"$tempdir/tarbackup2",'-Ft' ],
119119
'tar format with tablespaces');
120120
ok(-f"$tempdir/tarbackup2/base.tar",'backup tar was created');
@@ -140,25 +140,25 @@
140140
closedir$dh;
141141

142142
mkdir"$tempdir/tbl=spc2";
143-
$node->psql('postgres',"DROP TABLE test1;");
144-
$node->psql('postgres',"DROP TABLESPACE tblspc1;");
145-
$node->psql('postgres',
143+
$node->safe_psql('postgres',"DROP TABLE test1;");
144+
$node->safe_psql('postgres',"DROP TABLESPACE tblspc1;");
145+
$node->safe_psql('postgres',
146146
"CREATE TABLESPACE tblspc2 LOCATION '$shorter_tempdir/tbl=spc2';");
147147
$node->command_ok(
148148
['pg_basebackup','-D',"$tempdir/backup3",'-Fp',
149149
"-T$shorter_tempdir/tbl\\=spc2=$tempdir/tbackup/tbl\\=spc2" ],
150150
'mapping tablespace with = sign in path');
151151
ok(-d"$tempdir/tbackup/tbl=spc2",
152152
'tablespace with = sign was relocated');
153-
$node->psql('postgres',"DROP TABLESPACE tblspc2;");
153+
$node->safe_psql('postgres',"DROP TABLESPACE tblspc2;");
154154

155155
mkdir"$tempdir/$superlongname";
156-
$node->psql('postgres',
156+
$node->safe_psql('postgres',
157157
"CREATE TABLESPACE tblspc3 LOCATION '$tempdir/$superlongname';");
158158
$node->command_ok(
159159
['pg_basebackup','-D',"$tempdir/tarbackup_l3",'-Ft' ],
160160
'pg_basebackup tar with long symlink target');
161-
$node->psql('postgres',"DROP TABLESPACE tblspc3;");
161+
$node->safe_psql('postgres',"DROP TABLESPACE tblspc3;");
162162
}
163163

164164
$node->command_ok(['pg_basebackup','-D',"$tempdir/backupR",'-R' ],
@@ -199,17 +199,17 @@
199199
'slot1' ],
200200
'pg_basebackup fails with nonexistent replication slot');
201201

202-
$node->psql('postgres',
202+
$node->safe_psql('postgres',
203203
q{SELECT * FROM pg_create_physical_replication_slot('slot1')});
204-
my$lsn =$node->psql('postgres',
204+
my$lsn =$node->safe_psql('postgres',
205205
q{SELECT restart_lsn FROM pg_replication_slots WHERE slot_name = 'slot1'}
206206
);
207207
is($lsn,'','restart LSN of new slot is null');
208208
$node->command_ok(
209209
['pg_basebackup','-D',"$tempdir/backupxs_sl",'-X',
210210
'stream','-S','slot1' ],
211211
'pg_basebackup -X stream with replication slot runs');
212-
$lsn =$node->psql('postgres',
212+
$lsn =$node->safe_psql('postgres',
213213
q{SELECT restart_lsn FROM pg_replication_slots WHERE slot_name = 'slot1'}
214214
);
215215
like($lsn,qr!^0/[0-9A-Z]{7,8}$!,'restart LSN of slot has advanced');

‎src/bin/pgbench/t/001_pgbench.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
my$node = get_new_node('main');
1313
$node->init;
1414
$node->start;
15-
$node->psql('postgres',
15+
$node->safe_psql('postgres',
1616
'CREATE UNLOGGED TABLE oid_tbl () WITH OIDS;'
1717
.'ALTER TABLE oid_tbl ADD UNIQUE (oid);');
1818
my$script =$node->basedir .'/pgbench_script';

‎src/bin/scripts/t/010_clusterdb.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
$node->command_fails(['clusterdb','-t','nonexistent' ],
2222
'fails with nonexistent table');
2323

24-
$node->psql('postgres',
24+
$node->safe_psql('postgres',
2525
'CREATE TABLE test1 (a int); CREATE INDEX test1x ON test1 (a); CLUSTER test1 USING test1x'
2626
);
2727
$node->issues_sql_like(

‎src/bin/scripts/t/030_createlang.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
$node->command_fails(['createlang','plpgsql' ],
1717
'fails if language already exists');
1818

19-
$node->psql('postgres','DROP EXTENSION plpgsql');
19+
$node->safe_psql('postgres','DROP EXTENSION plpgsql');
2020
$node->issues_sql_like(
2121
['createlang','plpgsql' ],
2222
qr/statement: CREATE EXTENSION "plpgsql"/,

‎src/bin/scripts/t/050_dropdb.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
$node->init;
1414
$node->start;
1515

16-
$node->psql('postgres','CREATE DATABASE foobar1');
16+
$node->safe_psql('postgres','CREATE DATABASE foobar1');
1717
$node->issues_sql_like(
1818
['dropdb','foobar1' ],
1919
qr/statement: DROP DATABASE foobar1/,

‎src/bin/scripts/t/070_dropuser.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
$node->init;
1414
$node->start;
1515

16-
$node->psql('postgres','CREATE ROLE foobar1');
16+
$node->safe_psql('postgres','CREATE ROLE foobar1');
1717
$node->issues_sql_like(
1818
['dropuser','foobar1' ],
1919
qr/statement: DROP ROLE foobar1/,

‎src/bin/scripts/t/090_reindexdb.pl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
qr/statement: REINDEX DATABASE postgres;/,
2121
'SQL REINDEX run');
2222

23-
$node->psql('postgres',
23+
$node->safe_psql('postgres',
2424
'CREATE TABLE test1 (a int); CREATE INDEX test1x ON test1 (a);');
2525
$node->issues_sql_like(
2626
['reindexdb','-t','test1','postgres' ],

‎src/test/modules/commit_ts/t/001_base.pl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
$node->start;
1414

1515
# Create a table, compare "now()" to the commit TS of its xmin
16-
$node->psql('postgres','create table t as select now from (select now(), pg_sleep(1)) f');
17-
my$true =$node->psql('postgres',
16+
$node->safe_psql('postgres','create table t as select now from (select now(), pg_sleep(1)) f');
17+
my$true =$node->safe_psql('postgres',
1818
'select t.now - ts.* <\'1s\' from t, pg_class c, pg_xact_commit_timestamp(c.xmin) ts where relname =\'t\'');
1919
is($true,'t','commit TS is set');
20-
my$ts =$node->psql('postgres',
20+
my$ts =$node->safe_psql('postgres',
2121
'select ts.* from pg_class, pg_xact_commit_timestamp(xmin) ts where relname =\'t\'');
2222

2323
# Verify that we read the same TS after crash recovery
2424
$node->stop('immediate');
2525
$node->start;
2626

27-
my$recovered_ts =$node->psql('postgres',
27+
my$recovered_ts =$node->safe_psql('postgres',
2828
'select ts.* from pg_class, pg_xact_commit_timestamp(xmin) ts where relname =\'t\'');
2929
is($recovered_ts,$ts,'commit TS remains after crash recovery');

‎src/test/modules/commit_ts/t/002_standby.pl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use warnings;
55

66
use TestLib;
7-
use Test::Moretests=>2;
7+
use Test::Moretests=>4;
88
use PostgresNode;
99

1010
my$bkplabel ='backup';
@@ -25,31 +25,33 @@
2525

2626
formy$i (1 .. 10)
2727
{
28-
$master->psql('postgres',"create table t$i()");
28+
$master->safe_psql('postgres',"create table t$i()");
2929
}
30-
my$master_ts =$master->psql('postgres',
30+
my$master_ts =$master->safe_psql('postgres',
3131
qq{SELECT ts.* FROM pg_class, pg_xact_commit_timestamp(xmin) AS ts WHERE relname = 't10'});
32-
my$master_lsn =$master->psql('postgres',
32+
my$master_lsn =$master->safe_psql('postgres',
3333
'select pg_current_xlog_location()');
3434
$standby->poll_query_until('postgres',
3535
qq{SELECT '$master_lsn'::pg_lsn <= pg_last_xlog_replay_location()})
3636
ordie"slave never caught up";
3737

38-
my$standby_ts =$standby->psql('postgres',
38+
my$standby_ts =$standby->safe_psql('postgres',
3939
qq{select ts.* from pg_class, pg_xact_commit_timestamp(xmin) ts where relname = 't10'});
4040
is($master_ts,$standby_ts,"standby gives same value as master");
4141

4242
$master->append_conf('postgresql.conf','track_commit_timestamp = off');
4343
$master->restart;
44-
$master->psql('postgres','checkpoint');
45-
$master_lsn =$master->psql('postgres',
44+
$master->safe_psql('postgres','checkpoint');
45+
$master_lsn =$master->safe_psql('postgres',
4646
'select pg_current_xlog_location()');
4747
$standby->poll_query_until('postgres',
4848
qq{SELECT '$master_lsn'::pg_lsn <= pg_last_xlog_replay_location()})
4949
ordie"slave never caught up";
50-
$standby->psql('postgres','checkpoint');
50+
$standby->safe_psql('postgres','checkpoint');
5151

5252
# This one should raise an error now
53-
$standby_ts =$standby->psql('postgres',
53+
my ($ret,$standby_ts_stdout,$standby_ts_stderr) =$standby->psql('postgres',
5454
'select ts.* from pg_class, pg_xact_commit_timestamp(xmin) ts where relname =\'t10\'');
55-
is($standby_ts,'',"standby gives no value when master turned feature off");
55+
is($ret, 3,'standby errors when master turned feature off');
56+
is($standby_ts_stdout,'',"standby gives no value when master turned feature off");
57+
like($standby_ts_stderr,qr/could not get commit timestamp data/,'expected error when master turned feature off');

‎src/test/modules/commit_ts/t/003_standby_2.pl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use warnings;
55

66
use TestLib;
7-
use Test::Moretests=>2;
7+
use Test::Moretests=>4;
88
use PostgresNode;
99

1010
my$bkplabel ='backup';
@@ -24,23 +24,25 @@
2424

2525
formy$i (1 .. 10)
2626
{
27-
$master->psql('postgres',"create table t$i()");
27+
$master->safe_psql('postgres',"create table t$i()");
2828
}
2929
$master->append_conf('postgresql.conf','track_commit_timestamp = off');
3030
$master->restart;
31-
$master->psql('postgres','checkpoint');
32-
my$master_lsn =$master->psql('postgres',
31+
$master->safe_psql('postgres','checkpoint');
32+
my$master_lsn =$master->safe_psql('postgres',
3333
'select pg_current_xlog_location()');
3434
$standby->poll_query_until('postgres',
3535
qq{SELECT '$master_lsn'::pg_lsn <= pg_last_xlog_replay_location()})
3636
ordie"slave never caught up";
3737

38-
$standby->psql('postgres','checkpoint');
38+
$standby->safe_psql('postgres','checkpoint');
3939
$standby->restart;
4040

41-
my$standby_ts =$standby->psql('postgres',
41+
my($psql_ret,$standby_ts_stdout,$standby_ts_stderr) =$standby->psql('postgres',
4242
qq{SELECT ts.* FROM pg_class, pg_xact_commit_timestamp(xmin) AS ts WHERE relname = 't10'});
43-
is($standby_ts,'',"standby does not return a value after restart");
43+
is($psql_ret, 3,'expect error when getting commit timestamp after restart');
44+
is($standby_ts_stdout,'',"standby does not return a value after restart");
45+
like($standby_ts_stderr,qr/could not get commit timestamp data/,'expected err msg after restart');
4446

4547
$master->append_conf('postgresql.conf','track_commit_timestamp = on');
4648
$master->restart;
@@ -50,7 +52,7 @@
5052
system_or_bail('pg_ctl','-w','-D',$standby->data_dir,'promote');
5153
$standby->poll_query_until('postgres',"SELECT pg_is_in_recovery() <> true");
5254

53-
$standby->psql('postgres',"create table t11()");
54-
$standby_ts =$standby->psql('postgres',
55+
$standby->safe_psql('postgres',"create table t11()");
56+
my$standby_ts =$standby->safe_psql('postgres',
5557
qq{SELECT ts.* FROM pg_class, pg_xact_commit_timestamp(xmin) AS ts WHERE relname = 't11'});
5658
isnt($standby_ts,'',"standby gives valid value ($standby_ts) after promotion");

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp