Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id B467FB5DBC4 for ; Mon, 26 Sep 2011 20:59:58 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 54352-03 for ; Mon, 26 Sep 2011 23:59:51 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 0E1FCB5DBC0 for ; Mon, 26 Sep 2011 20:59:50 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p8QNxjYP006383; Mon, 26 Sep 2011 19:59:45 -0400 (EDT) To: Alvaro Herrera cc: yamt , pgsql-hackers@postgreSQL.org Subject: Re: [BUGS] BUG #6218: TRAP: FailedAssertion( "!(owner->nsnapshots == 0)", File: "resowner.c", Line: 365) In-reply-to: <2126.1317073572@sss.pgh.pa.us> References: <5291.1316980809@sss.pgh.pa.us> <20110926100653.4D3BC14A143@mail.netbsd.org> <8244.1317054397@sss.pgh.pa.us> <1317070986-sup-4844@alvh.no-ip.org> <2126.1317073572@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Mon, 26 Sep 2011 17:46:12 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <6348.1317081458.0@sss.pgh.pa.us> Date: Mon, 26 Sep 2011 19:59:45 -0400 Message-ID: <6382.1317081585@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201109/1429 X-Sequence-Number: 195618 ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <6348.1317081458.1@sss.pgh.pa.us> I wrote: > Alvaro Herrera writes: >> Shall I work on a fix? I expect you are plenty busy with commitfest >> stuff, but please let me know otherwise. > I have what-I-think-is-the-fix pretty clear in my own mind, so let me > give it a try. If it doesn't work I'll bounce it back to you. Well, I soon ran into the issue that delaying the snapshot release makes TopTransactionResourceOwner spit up. After some reflection I decided that the real problem is a circular dependency: snapshot management must be considered lower-level than ResourceOwners because ResourceOwners tell snapshot management what to do, but here we have GetTransactionSnapshot trying to use TopTransactionResourceOwner to manage its internal reference to the transaction snapshot. Accordingly, the attached proposed patch gets rid of the circularity by removing snapmgr.c's dependency on TopTransactionResourceOwner, in favor of having it track the refcount "manually". This was messier than I'd hoped because the bogus design had propagated into the SSI manager meanwhile, but removing the TopTransactionResourceOwner dependency from that too seems like a good idea. This passes the regular and isolation regression tests, and it's also okay with Yamamoto-san's prepared-ROLLBACK test case even without the band-aid fix in plancache.c. I can't immediately think of any other test cases to throw at it. Comments? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-patch; charset="us-ascii" Content-ID: <6348.1317081458.2@sss.pgh.pa.us> Content-Description: snapmgr-circularity-fix.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transa= m/xact.c index de3f965b37a4be93913907b1683c07ea24b4e633..3dab45c2da60a1010d566fccf6= 58a4a55ee3ece3 100644 *** a/src/backend/access/transam/xact.c --- b/src/backend/access/transam/xact.c *************** CommitTransaction(void) *** 1906,1914 **** /* Clean up the relation cache */ AtEOXact_RelationCache(true); = - /* Clean up the snapshot manager */ - AtEarlyCommit_Snapshot(); - = /* * Make catalog changes visible to all backends. This has to happen af= ter * relcache references are dropped (see comments for --- 1906,1911 ---- *************** PrepareTransaction(void) *** 2158,2166 **** /* Clean up the relation cache */ AtEOXact_RelationCache(true); = - /* Clean up the snapshot manager */ - AtEarlyCommit_Snapshot(); - = /* notify doesn't need a postprepare call */ = PostPrepare_PgStat(); --- 2155,2160 ---- *************** AbortTransaction(void) *** 2339,2345 **** AtEOXact_ComboCid(); AtEOXact_HashTables(false); AtEOXact_PgStat(false); - AtEOXact_Snapshot(false); pgstat_report_xact_timestamp(0); } = --- 2333,2338 ---- *************** CleanupTransaction(void) *** 2368,2373 **** --- 2361,2367 ---- * do abort cleanup processing */ AtCleanup_Portals(); /* now safe to release portal memory */ + AtEOXact_Snapshot(false); /* and release the transaction's snapshots */ = CurrentResourceOwner =3D NULL; /* and resource owner */ if (TopTransactionResourceOwner) diff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lm= gr/predicate.c index 1754180453daaa54e732bcd82cae5f3b70631524..d39f8975f8816d9bba02c0c398= 323ca470f1340b 100644 *** a/src/backend/storage/lmgr/predicate.c --- b/src/backend/storage/lmgr/predicate.c *************** *** 146,152 **** * PageIsPredicateLocked(Relation relation, BlockNumber blkno) * * predicate lock maintenance ! * RegisterSerializableTransaction(Snapshot snapshot) * RegisterPredicateLockingXid(void) * PredicateLockRelation(Relation relation, Snapshot snapshot) * PredicateLockPage(Relation relation, BlockNumber blkno, --- 146,152 ---- * PageIsPredicateLocked(Relation relation, BlockNumber blkno) * * predicate lock maintenance ! * GetSerializableTransactionSnapshot(Snapshot snapshot) * RegisterPredicateLockingXid(void) * PredicateLockRelation(Relation relation, Snapshot snapshot) * PredicateLockPage(Relation relation, BlockNumber blkno, *************** static void OldSerXidSetActiveSerXmin(Tr *** 417,423 **** static uint32 predicatelock_hash(const void *key, Size keysize); static void SummarizeOldestCommittedSxact(void); static Snapshot GetSafeSnapshot(Snapshot snapshot); ! static Snapshot RegisterSerializableTransactionInt(Snapshot snapshot); static bool PredicateLockExists(const PREDICATELOCKTARGETTAG *targettag)= ; static bool GetParentPredicateLockTag(const PREDICATELOCKTARGETTAG *tag, PREDICATELOCKTARGETTAG *parent); --- 417,423 ---- static uint32 predicatelock_hash(const void *key, Size keysize); static void SummarizeOldestCommittedSxact(void); static Snapshot GetSafeSnapshot(Snapshot snapshot); ! static Snapshot GetSerializableTransactionSnapshotInt(Snapshot snapshot)= ; static bool PredicateLockExists(const PREDICATELOCKTARGETTAG *targettag)= ; static bool GetParentPredicateLockTag(const PREDICATELOCKTARGETTAG *tag, PREDICATELOCKTARGETTAG *parent); *************** SummarizeOldestCommittedSxact(void) *** 1485,1490 **** --- 1485,1494 ---- * without further checks. This requires waiting for concurrent * transactions to complete, and retrying with a new snapshot if * one of them could possibly create a conflict. + * + * As with GetSerializableTransactionSnapshot (which this is a subrouti= ne + * for), the passed-in Snapshot pointer should reference a static data + * area that can safely be passed to GetSnapshotData. */ static Snapshot GetSafeSnapshot(Snapshot origSnapshot) *************** GetSafeSnapshot(Snapshot origSnapshot) *** 1496,1507 **** while (true) { /* ! * RegisterSerializableTransactionInt is going to call ! * GetSnapshotData, so we need to provide it the static snapshot our ! * caller passed to us. It returns a copy of that snapshot and ! * registers it on TopTransactionResourceOwner. */ ! snapshot =3D RegisterSerializableTransactionInt(origSnapshot); = if (MySerializableXact =3D=3D InvalidSerializableXact) return snapshot; /* no concurrent r/w xacts; it's safe */ --- 1500,1511 ---- while (true) { /* ! * GetSerializableTransactionSnapshotInt is going to call ! * GetSnapshotData, so we need to provide it the static snapshot area ! * our caller passed to us. The pointer returned is actually the same ! * one passed to it, but we avoid assuming that here. */ ! snapshot =3D GetSerializableTransactionSnapshotInt(origSnapshot); = if (MySerializableXact =3D=3D InvalidSerializableXact) return snapshot; /* no concurrent r/w xacts; it's safe */ *************** GetSafeSnapshot(Snapshot origSnapshot) *** 1535,1542 **** (errcode(ERRCODE_T_R_SERIALIZATION_FAILURE), errmsg("deferrable snapshot was unsafe; trying a new one"))); ReleasePredicateLocks(false); - UnregisterSnapshotFromOwner(snapshot, - TopTransactionResourceOwner); } = /* --- 1539,1544 ---- *************** GetSafeSnapshot(Snapshot origSnapshot) *** 1549,1576 **** } = /* ! * Acquire and register a snapshot which can be used for this transactio= n.. * Make sure we have a SERIALIZABLEXACT reference in MySerializableXact. * It should be current for this process and be contained in PredXact. */ Snapshot ! RegisterSerializableTransaction(Snapshot snapshot) { Assert(IsolationIsSerializable()); = /* * A special optimization is available for SERIALIZABLE READ ONLY * DEFERRABLE transactions -- we can wait for a suitable snapshot and ! * thereby avoid all SSI overhead once it's running.. */ if (XactReadOnly && XactDeferrable) return GetSafeSnapshot(snapshot); = ! return RegisterSerializableTransactionInt(snapshot); } = static Snapshot ! RegisterSerializableTransactionInt(Snapshot snapshot) { PGPROC *proc; VirtualTransactionId vxid; --- 1551,1584 ---- } = /* ! * Acquire a snapshot that can be used for the current transaction. ! * * Make sure we have a SERIALIZABLEXACT reference in MySerializableXact. * It should be current for this process and be contained in PredXact. + * + * The passed-in Snapshot pointer should reference a static data area th= at + * can safely be passed to GetSnapshotData. The return value is actuall= y + * always this same pointer; no new snapshot data structure is allocated + * within this function. */ Snapshot ! GetSerializableTransactionSnapshot(Snapshot snapshot) { Assert(IsolationIsSerializable()); = /* * A special optimization is available for SERIALIZABLE READ ONLY * DEFERRABLE transactions -- we can wait for a suitable snapshot and ! * thereby avoid all SSI overhead once it's running. */ if (XactReadOnly && XactDeferrable) return GetSafeSnapshot(snapshot); = ! return GetSerializableTransactionSnapshotInt(snapshot); } = static Snapshot ! GetSerializableTransactionSnapshotInt(Snapshot snapshot) { PGPROC *proc; VirtualTransactionId vxid; *************** RegisterSerializableTransactionInt(Snaps *** 1607,1615 **** } } while (!sxact); = ! /* Get and register a snapshot */ snapshot =3D GetSnapshotData(snapshot); - snapshot =3D RegisterSnapshotOnOwner(snapshot, TopTransactionResourceOw= ner); = /* * If there are no serializable transactions which are not read-only, w= e --- 1615,1622 ---- } } while (!sxact); = ! /* Get the snapshot */ snapshot =3D GetSnapshotData(snapshot); = /* * If there are no serializable transactions which are not read-only, w= e diff --git a/src/backend/utils/time/snapmgr.c b/src/backend/utils/time/sna= pmgr.c index bb25ac6ab2c464480258335297ff4500c049bda6..91229cd21bbc43fc27670c65ca= 7c8246668b6434 100644 *** a/src/backend/utils/time/snapmgr.c --- b/src/backend/utils/time/snapmgr.c *************** *** 7,12 **** --- 7,20 ---- * persistent memory. When a snapshot is no longer in any of these list= s * (tracked by separate refcounts on each snapshot), its memory can be f= reed. * + * The FirstXactSnapshot, if any, is treated a bit specially: we increme= nt its + * regd_count and count it in RegisteredSnapshots, but this reference is= not + * tracked by a resource owner. We used to use the TopTransactionResour= ceOwner + * to track this snapshot reference, but that introduces logical circula= rity + * and thus makes it impossible to clean up in a sane fashion. It's bet= ter to + * handle this reference as an internally-tracked registration, so that = this + * module is entirely lower-level than ResourceOwners. + * * These arrangements let us reset MyProc->xmin when there are no snapsh= ots * referenced by this transaction. (One possible improvement would be to= be * able to advance Xmin when the snapshot with the earliest Xmin is no l= onger *************** static int RegisteredSnapshots =3D 0; *** 97,107 **** bool FirstSnapshotSet =3D false; = /* ! * Remembers whether this transaction registered a transaction snapshot = at ! * start. We cannot trust FirstSnapshotSet in combination with ! * IsolationUsesXactSnapshot(), because GUC may be reset before us. */ ! static bool registered_xact_snapshot =3D false; = = static Snapshot CopySnapshot(Snapshot snapshot); --- 105,115 ---- bool FirstSnapshotSet =3D false; = /* ! * Remember the serializable transaction snapshot, if any. We cannot tr= ust ! * FirstSnapshotSet in combination with IsolationUsesXactSnapshot(), bec= ause ! * GUC may be reset before us, changing the value of IsolationUsesXactSn= apshot. */ ! static Snapshot FirstXactSnapshot =3D NULL; = = static Snapshot CopySnapshot(Snapshot snapshot); *************** GetTransactionSnapshot(void) *** 125,147 **** if (!FirstSnapshotSet) { Assert(RegisteredSnapshots =3D=3D 0); = /* * In transaction-snapshot mode, the first snapshot must live until * end of xact regardless of what the caller does with it, so we must ! * register it internally here and unregister it at end of xact. */ if (IsolationUsesXactSnapshot()) { if (IsolationIsSerializable()) ! CurrentSnapshot =3D RegisterSerializableTransaction(&CurrentSnapshot= Data); else - { CurrentSnapshot =3D GetSnapshotData(&CurrentSnapshotData); ! CurrentSnapshot =3D RegisterSnapshotOnOwner(CurrentSnapshot, ! TopTransactionResourceOwner); ! } ! registered_xact_snapshot =3D true; } else CurrentSnapshot =3D GetSnapshotData(&CurrentSnapshotData); --- 133,159 ---- if (!FirstSnapshotSet) { Assert(RegisteredSnapshots =3D=3D 0); + Assert(FirstXactSnapshot =3D=3D NULL); = /* * In transaction-snapshot mode, the first snapshot must live until * end of xact regardless of what the caller does with it, so we must ! * make a copy of it rather than returning CurrentSnapshotData ! * directly. */ if (IsolationUsesXactSnapshot()) { + /* First, create the snapshot in CurrentSnapshotData */ if (IsolationIsSerializable()) ! CurrentSnapshot =3D GetSerializableTransactionSnapshot(&CurrentSnaps= hotData); else CurrentSnapshot =3D GetSnapshotData(&CurrentSnapshotData); ! /* Make a saved copy */ ! CurrentSnapshot =3D CopySnapshot(CurrentSnapshot); ! FirstXactSnapshot =3D CurrentSnapshot; ! /* Mark it as "registered" in FirstXactSnapshot */ ! FirstXactSnapshot->regd_count++; ! RegisteredSnapshots++; } else CurrentSnapshot =3D GetSnapshotData(&CurrentSnapshotData); *************** AtSubAbort_Snapshot(int level) *** 523,554 **** } = /* - * AtEarlyCommit_Snapshot - * - * Snapshot manager's cleanup function, to be called on commit, before - * doing resowner.c resource release. - */ - void - AtEarlyCommit_Snapshot(void) - { - /* - * In transaction-snapshot mode we must unregister our private refcount= to - * the transaction-snapshot. - */ - if (registered_xact_snapshot) - UnregisterSnapshotFromOwner(CurrentSnapshot, - TopTransactionResourceOwner); - registered_xact_snapshot =3D false; - = - } - = - /* * AtEOXact_Snapshot * Snapshot manager's cleanup function for end of transaction */ void AtEOXact_Snapshot(bool isCommit) { /* On commit, complain about leftover snapshots */ if (isCommit) { --- 535,563 ---- } = /* * AtEOXact_Snapshot * Snapshot manager's cleanup function for end of transaction */ void AtEOXact_Snapshot(bool isCommit) { + /* + * In transaction-snapshot mode we must release our privately-managed + * reference to the transaction snapshot. We must decrement + * RegisteredSnapshots to keep the check below happy. But we don't bot= her + * to do FreeSnapshot, for two reasons: the memory will go away with + * TopTransactionContext anyway, and if someone has left the snapshot + * stacked as active, we don't want the code below to be chasing throug= h + * a dangling pointer. + */ + if (FirstXactSnapshot !=3D NULL) + { + Assert(FirstXactSnapshot->regd_count > 0); + Assert(RegisteredSnapshots > 0); + RegisteredSnapshots--; + } + FirstXactSnapshot =3D NULL; + = /* On commit, complain about leftover snapshots */ if (isCommit) { *************** AtEOXact_Snapshot(bool isCommit) *** 574,578 **** SecondarySnapshot =3D NULL; = FirstSnapshotSet =3D false; ! registered_xact_snapshot =3D false; } --- 583,588 ---- SecondarySnapshot =3D NULL; = FirstSnapshotSet =3D false; ! = ! SnapshotResetXmin(); } diff --git a/src/include/storage/predicate.h b/src/include/storage/predica= te.h index 5ddbc1d3f413506fcd4dff851a5960c112f227ea..9603b10ad4052e55109b976d52= 1d6054b656dc8a 100644 *** a/src/include/storage/predicate.h --- b/src/include/storage/predicate.h *************** extern void CheckPointPredicate(void); *** 42,48 **** extern bool PageIsPredicateLocked(Relation relation, BlockNumber blkno); = /* predicate lock maintenance */ ! extern Snapshot RegisterSerializableTransaction(Snapshot snapshot); extern void RegisterPredicateLockingXid(TransactionId xid); extern void PredicateLockRelation(Relation relation, Snapshot snapshot); extern void PredicateLockPage(Relation relation, BlockNumber blkno, Snap= shot snapshot); --- 42,48 ---- extern bool PageIsPredicateLocked(Relation relation, BlockNumber blkno); = /* predicate lock maintenance */ ! extern Snapshot GetSerializableTransactionSnapshot(Snapshot snapshot); extern void RegisterPredicateLockingXid(TransactionId xid); extern void PredicateLockRelation(Relation relation, Snapshot snapshot); extern void PredicateLockPage(Relation relation, BlockNumber blkno, Snap= shot snapshot); diff --git a/src/include/utils/snapmgr.h b/src/include/utils/snapmgr.h index c969a37796173bdab05925d3a30364b3fe1bde7e..e665a28aff8571e50ce318d8a0= ca4710ec503b3c 100644 *** a/src/include/utils/snapmgr.h --- b/src/include/utils/snapmgr.h *************** extern void UnregisterSnapshotFromOwner( *** 40,46 **** = extern void AtSubCommit_Snapshot(int level); extern void AtSubAbort_Snapshot(int level); - extern void AtEarlyCommit_Snapshot(void); extern void AtEOXact_Snapshot(bool isCommit); = #endif /* SNAPMGR_H */ --- 40,45 ---- ------- =_aaaaaaaaaa0--