diff options
| author | Tom Lane | 2009-04-23 00:23:46 +0000 |
|---|---|---|
| committer | Tom Lane | 2009-04-23 00:23:46 +0000 |
| commit | 8d4f2ecd41312e57422901952cbad234d293060b (patch) | |
| tree | f3345b5dcf1c8980bfe59da77186b332500ff064 /src/test | |
| parent | bae8102f52863bdb29b5c5e1ee7e27a8b0f5b9c7 (diff) | |
Change the default value of max_prepared_transactions to zero, and add
documentation warnings against setting it nonzero unless active use of
prepared transactions is intended and a suitable transaction manager has been
installed. This should help to prevent the type of scenario we've seen
several times now where a prepared transaction is forgotten and eventually
causes severe maintenance problems (or even anti-wraparound shutdown).
The only real reason we had the default be nonzero in the first place was to
support regression testing of the feature. To still be able to do that,
tweak pg_regress to force a nonzero value during "make check". Since we
cannot force a nonzero value in "make installcheck", add a variant regression
test "expected" file that shows the results that will be obtained when
max_prepared_transactions is zero.
Also, extend the HINT messages for transaction wraparound warnings to mention
the possibility that old prepared transactions are causing the problem.
All per today's discussion.
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/regress/expected/prepared_xacts.out | 2 | ||||
| -rw-r--r-- | src/test/regress/expected/prepared_xacts_1.out | 223 | ||||
| -rw-r--r-- | src/test/regress/pg_regress.c | 34 | ||||
| -rw-r--r-- | src/test/regress/sql/prepared_xacts.sql | 1 |
4 files changed, 249 insertions, 11 deletions
diff --git a/src/test/regress/expected/prepared_xacts.out b/src/test/regress/expected/prepared_xacts.out index 535dbe4cbf5..292962ab7b3 100644 --- a/src/test/regress/expected/prepared_xacts.out +++ b/src/test/regress/expected/prepared_xacts.out @@ -214,4 +214,6 @@ SELECT gid FROM pg_prepared_xacts; -- Clean up DROP TABLE pxtest2; +DROP TABLE pxtest3; -- will still be there if prepared xacts are disabled +ERROR: table "pxtest3" does not exist DROP TABLE pxtest4; diff --git a/src/test/regress/expected/prepared_xacts_1.out b/src/test/regress/expected/prepared_xacts_1.out new file mode 100644 index 00000000000..991a11bdfa9 --- /dev/null +++ b/src/test/regress/expected/prepared_xacts_1.out @@ -0,0 +1,223 @@ +-- +-- PREPARED TRANSACTIONS (two-phase commit) +-- +-- We can't readily test persistence of prepared xacts within the +-- regression script framework, unfortunately. Note that a crash +-- isn't really needed ... stopping and starting the postmaster would +-- be enough, but we can't even do that here. +-- create a simple table that we'll use in the tests +CREATE TABLE pxtest1 (foobar VARCHAR(10)); +INSERT INTO pxtest1 VALUES ('aaa'); +-- Test PREPARE TRANSACTION +BEGIN; +UPDATE pxtest1 SET foobar = 'bbb' WHERE foobar = 'aaa'; +SELECT * FROM pxtest1; + foobar +-------- + bbb +(1 row) + +PREPARE TRANSACTION 'foo1'; +ERROR: prepared transactions are disabled +HINT: Set max_prepared_transactions to a nonzero value. +SELECT * FROM pxtest1; + foobar +-------- + aaa +(1 row) + +-- Test pg_prepared_xacts system view +SELECT gid FROM pg_prepared_xacts; + gid +----- +(0 rows) + +-- Test ROLLBACK PREPARED +ROLLBACK PREPARED 'foo1'; +ERROR: prepared transaction with identifier "foo1" does not exist +SELECT * FROM pxtest1; + foobar +-------- + aaa +(1 row) + +SELECT gid FROM pg_prepared_xacts; + gid +----- +(0 rows) + +-- Test COMMIT PREPARED +BEGIN; +INSERT INTO pxtest1 VALUES ('ddd'); +SELECT * FROM pxtest1; + foobar +-------- + aaa + ddd +(2 rows) + +PREPARE TRANSACTION 'foo2'; +ERROR: prepared transactions are disabled +HINT: Set max_prepared_transactions to a nonzero value. +SELECT * FROM pxtest1; + foobar +-------- + aaa +(1 row) + +COMMIT PREPARED 'foo2'; +ERROR: prepared transaction with identifier "foo2" does not exist +SELECT * FROM pxtest1; + foobar +-------- + aaa +(1 row) + +-- Test duplicate gids +BEGIN; +UPDATE pxtest1 SET foobar = 'eee' WHERE foobar = 'ddd'; +SELECT * FROM pxtest1; + foobar +-------- + aaa +(1 row) + +PREPARE TRANSACTION 'foo3'; +ERROR: prepared transactions are disabled +HINT: Set max_prepared_transactions to a nonzero value. +SELECT gid FROM pg_prepared_xacts; + gid +----- +(0 rows) + +BEGIN; +INSERT INTO pxtest1 VALUES ('fff'); +SELECT * FROM pxtest1; + foobar +-------- + aaa + fff +(2 rows) + +-- This should fail, because the gid foo3 is already in use +PREPARE TRANSACTION 'foo3'; +ERROR: prepared transactions are disabled +HINT: Set max_prepared_transactions to a nonzero value. +SELECT * FROM pxtest1; + foobar +-------- + aaa +(1 row) + +ROLLBACK PREPARED 'foo3'; +ERROR: prepared transaction with identifier "foo3" does not exist +SELECT * FROM pxtest1; + foobar +-------- + aaa +(1 row) + +-- Clean up +DROP TABLE pxtest1; +-- Test subtransactions +BEGIN; + CREATE TABLE pxtest2 (a int); + INSERT INTO pxtest2 VALUES (1); + SAVEPOINT a; + INSERT INTO pxtest2 VALUES (2); + ROLLBACK TO a; + SAVEPOINT b; + INSERT INTO pxtest2 VALUES (3); +PREPARE TRANSACTION 'regress-one'; +ERROR: prepared transactions are disabled +HINT: Set max_prepared_transactions to a nonzero value. +CREATE TABLE pxtest3(fff int); +-- Test shared invalidation +BEGIN; + DROP TABLE pxtest3; + CREATE TABLE pxtest4 (a int); + INSERT INTO pxtest4 VALUES (1); + INSERT INTO pxtest4 VALUES (2); + DECLARE foo CURSOR FOR SELECT * FROM pxtest4; + -- Fetch 1 tuple, keeping the cursor open + FETCH 1 FROM foo; + a +--- + 1 +(1 row) + +PREPARE TRANSACTION 'regress-two'; +ERROR: prepared transactions are disabled +HINT: Set max_prepared_transactions to a nonzero value. +-- No such cursor +FETCH 1 FROM foo; +ERROR: cursor "foo" does not exist +-- Table doesn't exist, the creation hasn't been committed yet +SELECT * FROM pxtest2; +ERROR: relation "pxtest2" does not exist +LINE 1: SELECT * FROM pxtest2; + ^ +-- There should be two prepared transactions +SELECT gid FROM pg_prepared_xacts; + gid +----- +(0 rows) + +-- pxtest3 should be locked because of the pending DROP +set statement_timeout to 2000; +SELECT * FROM pxtest3; + fff +----- +(0 rows) + +reset statement_timeout; +-- Disconnect, we will continue testing in a different backend +\c - +-- There should still be two prepared transactions +SELECT gid FROM pg_prepared_xacts; + gid +----- +(0 rows) + +-- pxtest3 should still be locked because of the pending DROP +set statement_timeout to 2000; +SELECT * FROM pxtest3; + fff +----- +(0 rows) + +reset statement_timeout; +-- Commit table creation +COMMIT PREPARED 'regress-one'; +ERROR: prepared transaction with identifier "regress-one" does not exist +\d pxtest2 +SELECT * FROM pxtest2; +ERROR: relation "pxtest2" does not exist +LINE 1: SELECT * FROM pxtest2; + ^ +-- There should be one prepared transaction +SELECT gid FROM pg_prepared_xacts; + gid +----- +(0 rows) + +-- Commit table drop +COMMIT PREPARED 'regress-two'; +ERROR: prepared transaction with identifier "regress-two" does not exist +SELECT * FROM pxtest3; + fff +----- +(0 rows) + +-- There should be no prepared transactions +SELECT gid FROM pg_prepared_xacts; + gid +----- +(0 rows) + +-- Clean up +DROP TABLE pxtest2; +ERROR: table "pxtest2" does not exist +DROP TABLE pxtest3; -- will still be there if prepared xacts are disabled +DROP TABLE pxtest4; +ERROR: table "pxtest4" does not exist diff --git a/src/test/regress/pg_regress.c b/src/test/regress/pg_regress.c index 7bd297f20c1..b51d6ec2b5b 100644 --- a/src/test/regress/pg_regress.c +++ b/src/test/regress/pg_regress.c @@ -11,7 +11,7 @@ * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * - * $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.61 2009/02/12 13:26:03 petere Exp $ + * $PostgreSQL: pgsql/src/test/regress/pg_regress.c,v 1.62 2009/04/23 00:23:46 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -2037,6 +2037,8 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc if (temp_install) { + FILE *pg_conf; + /* * Prepare the temp installation */ @@ -2092,20 +2094,29 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc exit_nicely(2); } - /* add any extra config specified to the postgresql.conf */ + /* + * Adjust the default postgresql.conf as needed for regression testing. + * The user can specify a file to be appended; in any case we set + * max_prepared_transactions to enable testing of prepared xacts. + * (Note: to reduce the probability of unexpected shmmax failures, + * don't set max_prepared_transactions any higher than actually + * needed by the prepared_xacts regression test.) + */ + snprintf(buf, sizeof(buf), "%s/data/postgresql.conf", temp_install); + pg_conf = fopen(buf, "a"); + if (pg_conf == NULL) + { + fprintf(stderr, _("\n%s: could not open \"%s\" for adding extra config: %s\n"), progname, buf, strerror(errno)); + exit_nicely(2); + } + fputs("\n# Configuration added by pg_regress\n\n", pg_conf); + fputs("max_prepared_transactions = 2\n", pg_conf); + if (temp_config != NULL) { FILE *extra_conf; - FILE *pg_conf; char line_buf[1024]; - snprintf(buf, sizeof(buf), "%s/data/postgresql.conf", temp_install); - pg_conf = fopen(buf, "a"); - if (pg_conf == NULL) - { - fprintf(stderr, _("\n%s: could not open \"%s\" for adding extra config: %s\n"), progname, buf, strerror(errno)); - exit_nicely(2); - } extra_conf = fopen(temp_config, "r"); if (extra_conf == NULL) { @@ -2115,9 +2126,10 @@ regression_main(int argc, char *argv[], init_function ifunc, test_function tfunc while (fgets(line_buf, sizeof(line_buf), extra_conf) != NULL) fputs(line_buf, pg_conf); fclose(extra_conf); - fclose(pg_conf); } + fclose(pg_conf); + /* * Check if there is a postmaster running already. */ diff --git a/src/test/regress/sql/prepared_xacts.sql b/src/test/regress/sql/prepared_xacts.sql index 337567271de..39d323a15b6 100644 --- a/src/test/regress/sql/prepared_xacts.sql +++ b/src/test/regress/sql/prepared_xacts.sql @@ -134,4 +134,5 @@ SELECT gid FROM pg_prepared_xacts; -- Clean up DROP TABLE pxtest2; +DROP TABLE pxtest3; -- will still be there if prepared xacts are disabled DROP TABLE pxtest4; |
