Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 23645f0

Browse files
committed
Fix incorrect ordering of smgr cleanup relative to buffer pin cleanup
during transaction abort. Add a regression test case to catch related mistakes in future. Alvaro Herrera and Tom Lane.
1 parent eb917c1 commit 23645f0

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

src/backend/access/transam/xact.c

+14-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.185 2004/08/30 19:00:03 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.186 2004/09/06 17:56:04 tgl Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -1333,9 +1333,6 @@ CommitTransaction(void)
13331333
* backend-wide state.
13341334
*/
13351335

1336-
smgrDoPendingDeletes(true);
1337-
/* smgrcommit already done */
1338-
13391336
CallXactCallbacks(XACT_EVENT_COMMIT, InvalidTransactionId);
13401337

13411338
ResourceOwnerRelease(TopTransactionResourceOwner,
@@ -1352,6 +1349,14 @@ CommitTransaction(void)
13521349
*/
13531350
AtEOXact_Inval(true);
13541351

1352+
/*
1353+
* Likewise, dropping of files deleted during the transaction is best done
1354+
* after releasing relcache and buffer pins. (This is not strictly
1355+
* necessary during commit, since such pins should have been released
1356+
* already, but this ordering is definitely critical during abort.)
1357+
*/
1358+
smgrDoPendingDeletes(true);
1359+
13551360
ResourceOwnerRelease(TopTransactionResourceOwner,
13561361
RESOURCE_RELEASE_LOCKS,
13571362
true, true);
@@ -1363,6 +1368,7 @@ CommitTransaction(void)
13631368
AtEOXact_SPI(true);
13641369
AtEOXact_on_commit_actions(true, s->transactionIdData);
13651370
AtEOXact_Namespace(true);
1371+
/* smgrcommit already done */
13661372
AtEOXact_Files();
13671373
pgstat_count_xact_commit();
13681374

@@ -1481,15 +1487,13 @@ AbortTransaction(void)
14811487
* ordering.
14821488
*/
14831489

1484-
smgrDoPendingDeletes(false);
1485-
smgrabort();
1486-
14871490
CallXactCallbacks(XACT_EVENT_ABORT, InvalidTransactionId);
14881491

14891492
ResourceOwnerRelease(TopTransactionResourceOwner,
14901493
RESOURCE_RELEASE_BEFORE_LOCKS,
14911494
false, true);
14921495
AtEOXact_Inval(false);
1496+
smgrDoPendingDeletes(false);
14931497
ResourceOwnerRelease(TopTransactionResourceOwner,
14941498
RESOURCE_RELEASE_LOCKS,
14951499
false, true);
@@ -1501,6 +1505,7 @@ AbortTransaction(void)
15011505
AtEOXact_SPI(false);
15021506
AtEOXact_on_commit_actions(false, s->transactionIdData);
15031507
AtEOXact_Namespace(false);
1508+
smgrabort();
15041509
AtEOXact_Files();
15051510
pgstat_count_xact_rollback();
15061511

@@ -3014,7 +3019,6 @@ CommitSubTransaction(void)
30143019
AtSubCommit_Notify();
30153020
AtEOSubXact_UpdatePasswordFile(true, s->transactionIdData,
30163021
s->parent->transactionIdData);
3017-
AtSubCommit_smgr();
30183022

30193023
CallXactCallbacks(XACT_EVENT_COMMIT_SUB, s->parent->transactionIdData);
30203024

@@ -3024,6 +3028,7 @@ CommitSubTransaction(void)
30243028
AtEOSubXact_RelationCache(true, s->transactionIdData,
30253029
s->parent->transactionIdData);
30263030
AtEOSubXact_Inval(true);
3031+
AtSubCommit_smgr();
30273032
ResourceOwnerRelease(s->curTransactionOwner,
30283033
RESOURCE_RELEASE_LOCKS,
30293034
true, false);
@@ -3109,8 +3114,6 @@ AbortSubTransaction(void)
31093114
RecordSubTransactionAbort();
31103115

31113116
/* Post-abort cleanup */
3112-
AtSubAbort_smgr();
3113-
31143117
CallXactCallbacks(XACT_EVENT_ABORT_SUB, s->parent->transactionIdData);
31153118

31163119
ResourceOwnerRelease(s->curTransactionOwner,
@@ -3119,6 +3122,7 @@ AbortSubTransaction(void)
31193122
AtEOSubXact_RelationCache(false, s->transactionIdData,
31203123
s->parent->transactionIdData);
31213124
AtEOSubXact_Inval(false);
3125+
AtSubAbort_smgr();
31223126
ResourceOwnerRelease(s->curTransactionOwner,
31233127
RESOURCE_RELEASE_LOCKS,
31243128
false, false);

src/backend/storage/smgr/smgr.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/storage/smgr/smgr.c,v 1.81 2004/08/30 02:54:39 momjian Exp $
14+
* $PostgreSQL: pgsql/src/backend/storage/smgr/smgr.c,v 1.82 2004/09/06 17:56:16 tgl Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -784,7 +784,7 @@ smgrcommit(void)
784784
}
785785

786786
/*
787-
* smgrabort() -- Abort changes made during the current transaction.
787+
* smgrabort() -- Clean up after transaction abort.
788788
*/
789789
void
790790
smgrabort(void)

src/test/regress/expected/transactions.out

+15
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,21 @@ ERROR: portal "c" cannot be run
374374
FETCH 10 FROM c;
375375
ERROR: portal "c" cannot be run
376376
COMMIT;
377+
-- test case for problems with dropping an open relation during abort
378+
BEGIN;
379+
savepoint x;
380+
CREATE TABLE koju (a INT UNIQUE);
381+
NOTICE: CREATE TABLE / UNIQUE will create implicit index "koju_a_key" for table "koju"
382+
INSERT INTO koju VALUES (1);
383+
INSERT INTO koju VALUES (1);
384+
ERROR: duplicate key violates unique constraint "koju_a_key"
385+
rollback to x;
386+
CREATE TABLE koju (a INT UNIQUE);
387+
NOTICE: CREATE TABLE / UNIQUE will create implicit index "koju_a_key" for table "koju"
388+
INSERT INTO koju VALUES (1);
389+
INSERT INTO koju VALUES (1);
390+
ERROR: duplicate key violates unique constraint "koju_a_key"
391+
ROLLBACK;
377392
DROP TABLE foo;
378393
DROP TABLE baz;
379394
DROP TABLE barbaz;

src/test/regress/sql/transactions.sql

+13
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ BEGIN;
231231
FETCH 10 FROM c;
232232
COMMIT;
233233

234+
-- test case for problems with dropping an open relation during abort
235+
BEGIN;
236+
savepoint x;
237+
CREATE TABLE koju (a INT UNIQUE);
238+
INSERT INTO koju VALUES (1);
239+
INSERT INTO koju VALUES (1);
240+
rollback to x;
241+
242+
CREATE TABLE koju (a INT UNIQUE);
243+
INSERT INTO koju VALUES (1);
244+
INSERT INTO koju VALUES (1);
245+
ROLLBACK;
246+
234247
DROP TABLE foo;
235248
DROP TABLE baz;
236249
DROP TABLE barbaz;

0 commit comments

Comments
 (0)