]> granicus.if.org Git - postgresql/commitdiff
Avoid testing tuple visibility without buffer lock in RI_FKey_check().
authorTom Lane <tgl@sss.pgh.pa.us>
Sun, 23 Oct 2016 19:01:24 +0000 (15:01 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Sun, 23 Oct 2016 19:01:24 +0000 (15:01 -0400)
Despite the argumentation I wrote in commit 7a2fe85b0, it's unsafe to do
this, because in corner cases it's possible for HeapTupleSatisfiesSelf
to try to set hint bits on the target tuple; and at least since 8.2 we
have required the buffer content lock to be held while setting hint bits.

The added regression test exercises one such corner case.  Unpatched, it
causes an assertion failure in assert-enabled builds, or otherwise would
cause a hint bit change in a buffer we don't hold lock on, which given
the right race condition could result in checksum failures or other data
consistency problems.  The odds of a problem in the field are probably
pretty small, but nonetheless back-patch to all supported branches.

Report: <19391.1477244876@sss.pgh.pa.us>

src/backend/utils/adt/ri_triggers.c
src/test/regress/expected/foreign_key.out
src/test/regress/sql/foreign_key.sql

index 977349a94fda8eb11010f671f2946d13fa645a36..b4ada9ede2ea241e99c981eca62fb3f2b534bd6f 100644 (file)
@@ -42,6 +42,7 @@
 #include "parser/parse_coerce.h"
 #include "parser/parse_relation.h"
 #include "miscadmin.h"
+#include "storage/bufmgr.h"
 #include "utils/acl.h"
 #include "utils/builtins.h"
 #include "utils/fmgroids.h"
@@ -290,20 +291,17 @@ RI_FKey_check(PG_FUNCTION_ARGS)
         * We should not even consider checking the row if it is no longer valid,
         * since it was either deleted (so the deferred check should be skipped)
         * or updated (in which case only the latest version of the row should be
-        * checked).  Test its liveness according to SnapshotSelf.
-        *
-        * NOTE: The normal coding rule is that one must acquire the buffer
-        * content lock to call HeapTupleSatisfiesVisibility.  We can skip that
-        * here because we know that AfterTriggerExecute just fetched the tuple
-        * successfully, so there cannot be a VACUUM compaction in progress on the
-        * page (either heap_fetch would have waited for the VACUUM, or the
-        * VACUUM's LockBufferForCleanup would be waiting for us to drop pin). And
-        * since this is a row inserted by our open transaction, no one else can
-        * be entitled to change its xmin/xmax.
+        * checked).  Test its liveness according to SnapshotSelf.  We need pin
+        * and lock on the buffer to call HeapTupleSatisfiesVisibility.  Caller
+        * should be holding pin, but not lock.
         */
-       Assert(new_row_buf != InvalidBuffer);
+       LockBuffer(new_row_buf, BUFFER_LOCK_SHARE);
        if (!HeapTupleSatisfiesVisibility(new_row, SnapshotSelf, new_row_buf))
+       {
+               LockBuffer(new_row_buf, BUFFER_LOCK_UNLOCK);
                return PointerGetDatum(NULL);
+       }
+       LockBuffer(new_row_buf, BUFFER_LOCK_UNLOCK);
 
        /*
         * Get the relation descriptors of the FK and PK tables.
index cddcda79b6083137f56dfc4c85c01fd58e94ac37..ac7af78330f2739ddb5857658b14cbf272e94038 100644 (file)
@@ -1319,3 +1319,25 @@ begin;
 (2 rows)
 
 commit;
+--
+-- Test deferred FK check on a tuple deleted by a rolled-back subtransaction
+--
+create table pktable2(f1 int primary key);
+NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "pktable2_pkey" for table "pktable2"
+create table fktable2(f1 int references pktable2 deferrable initially deferred);
+insert into pktable2 values(1);
+begin;
+insert into fktable2 values(1);
+savepoint x;
+delete from fktable2;
+rollback to x;
+commit;
+begin;
+insert into fktable2 values(2);
+savepoint x;
+delete from fktable2;
+rollback to x;
+commit; -- fail
+ERROR:  insert or update on table "fktable2" violates foreign key constraint "fktable2_f1_fkey"
+DETAIL:  Key (f1)=(2) is not present in table "pktable2".
+drop table pktable2, fktable2;
index fe9bfde101664b09e7eb9e5caf37b05c9d7eca94..37b1f2c46ab6b6ce0c5e2e032821b6de331cfe17 100644 (file)
@@ -943,3 +943,26 @@ begin;
     update selfref set a = 456 where a = 123;
     select a, b from selfref;
 commit;
+
+--
+-- Test deferred FK check on a tuple deleted by a rolled-back subtransaction
+--
+create table pktable2(f1 int primary key);
+create table fktable2(f1 int references pktable2 deferrable initially deferred);
+insert into pktable2 values(1);
+
+begin;
+insert into fktable2 values(1);
+savepoint x;
+delete from fktable2;
+rollback to x;
+commit;
+
+begin;
+insert into fktable2 values(2);
+savepoint x;
+delete from fktable2;
+rollback to x;
+commit; -- fail
+
+drop table pktable2, fktable2;