mirror of
https://github.com/postgres/postgres.git
synced 2025-11-12 05:01:15 +03:00
Restructure subtransaction handling to reduce resource consumption,
as per recent discussions. Invent SubTransactionIds that are managed like CommandIds (ie, counter is reset at start of each top transaction), and use these instead of TransactionIds to keep track of subtransaction status in those modules that need it. This means that a subtransaction does not need an XID unless it actually inserts/modifies rows in the database. Accordingly, don't assign it an XID nor take a lock on the XID until it tries to do that. This saves a lot of overhead for subtransactions that are only used for error recovery (eg plpgsql exceptions). Also, arrange to release a subtransaction's XID lock as soon as the subtransaction exits, in both the commit and abort cases. This avoids holding many unique locks after a long series of subtransactions. The price is some additional overhead in XactLockTableWait, but that seems acceptable. Finally, restructure the state machine in xact.c to have a more orthogonal set of states for subtransactions.
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/xid.c,v 1.5 2004/08/29 04:12:52 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/adt/xid.c,v 1.6 2004/09/16 16:58:34 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -93,7 +93,7 @@ Datum
|
||||
xid_age(PG_FUNCTION_ARGS)
|
||||
{
|
||||
TransactionId xid = PG_GETARG_TRANSACTIONID(0);
|
||||
TransactionId now = GetCurrentTransactionId();
|
||||
TransactionId now = GetTopTransactionId();
|
||||
|
||||
/* Permanent XIDs are always infinitely old */
|
||||
if (!TransactionIdIsNormal(xid))
|
||||
|
||||
32
src/backend/utils/cache/relcache.c
vendored
32
src/backend/utils/cache/relcache.c
vendored
@@ -8,7 +8,7 @@
|
||||
*
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.210 2004/08/29 05:06:50 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/cache/relcache.c,v 1.211 2004/09/16 16:58:35 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -836,7 +836,7 @@ RelationBuildDesc(RelationBuildDescInfo buildinfo,
|
||||
*/
|
||||
relation->rd_refcnt = 0;
|
||||
relation->rd_isnailed = false;
|
||||
relation->rd_createxact = InvalidTransactionId;
|
||||
relation->rd_createSubid = InvalidSubTransactionId;
|
||||
relation->rd_istemp = isTempNamespace(relation->rd_rel->relnamespace);
|
||||
|
||||
/*
|
||||
@@ -1287,7 +1287,7 @@ formrdesc(const char *relationName,
|
||||
* for new or temp relations.
|
||||
*/
|
||||
relation->rd_isnailed = true;
|
||||
relation->rd_createxact = InvalidTransactionId;
|
||||
relation->rd_createSubid = InvalidSubTransactionId;
|
||||
relation->rd_istemp = false;
|
||||
|
||||
/*
|
||||
@@ -1578,7 +1578,7 @@ RelationClose(Relation relation)
|
||||
|
||||
#ifdef RELCACHE_FORCE_RELEASE
|
||||
if (RelationHasReferenceCountZero(relation) &&
|
||||
!TransactionIdIsValid(relation->rd_createxact))
|
||||
relation->rd_createSubid == InvalidSubTransactionId)
|
||||
RelationClearRelation(relation, false);
|
||||
#endif
|
||||
}
|
||||
@@ -1736,7 +1736,7 @@ RelationClearRelation(Relation relation, bool rebuild)
|
||||
{
|
||||
/*
|
||||
* When rebuilding an open relcache entry, must preserve ref count
|
||||
* and rd_createxact state. Also attempt to preserve the
|
||||
* and rd_createSubid state. Also attempt to preserve the
|
||||
* tupledesc and rewrite-rule substructures in place.
|
||||
*
|
||||
* Note that this process does not touch CurrentResourceOwner; which
|
||||
@@ -1744,7 +1744,7 @@ RelationClearRelation(Relation relation, bool rebuild)
|
||||
* necessarily belong to that resource owner.
|
||||
*/
|
||||
int old_refcnt = relation->rd_refcnt;
|
||||
TransactionId old_createxact = relation->rd_createxact;
|
||||
SubTransactionId old_createSubid = relation->rd_createSubid;
|
||||
TupleDesc old_att = relation->rd_att;
|
||||
RuleLock *old_rules = relation->rd_rules;
|
||||
MemoryContext old_rulescxt = relation->rd_rulescxt;
|
||||
@@ -1765,7 +1765,7 @@ RelationClearRelation(Relation relation, bool rebuild)
|
||||
buildinfo.i.info_id);
|
||||
}
|
||||
relation->rd_refcnt = old_refcnt;
|
||||
relation->rd_createxact = old_createxact;
|
||||
relation->rd_createSubid = old_createSubid;
|
||||
if (equalTupleDescs(old_att, relation->rd_att))
|
||||
{
|
||||
/* needn't flush typcache here */
|
||||
@@ -1802,7 +1802,7 @@ RelationFlushRelation(Relation relation)
|
||||
{
|
||||
bool rebuild;
|
||||
|
||||
if (TransactionIdIsValid(relation->rd_createxact))
|
||||
if (relation->rd_createSubid != InvalidSubTransactionId)
|
||||
{
|
||||
/*
|
||||
* New relcache entries are always rebuilt, not flushed; else we'd
|
||||
@@ -1948,7 +1948,7 @@ RelationCacheInvalidate(void)
|
||||
}
|
||||
|
||||
/* Ignore new relations, since they are never SI targets */
|
||||
if (TransactionIdIsValid(relation->rd_createxact))
|
||||
if (relation->rd_createSubid != InvalidSubTransactionId)
|
||||
continue;
|
||||
|
||||
relcacheInvalsReceived++;
|
||||
@@ -2032,10 +2032,10 @@ AtEOXact_RelationCache(bool isCommit)
|
||||
* flush, the entry will get deleted anyway by shared-cache-inval
|
||||
* processing of the aborted pg_class insertion.)
|
||||
*/
|
||||
if (TransactionIdIsValid(relation->rd_createxact))
|
||||
if (relation->rd_createSubid != InvalidSubTransactionId)
|
||||
{
|
||||
if (isCommit)
|
||||
relation->rd_createxact = InvalidTransactionId;
|
||||
relation->rd_createSubid = InvalidSubTransactionId;
|
||||
else
|
||||
{
|
||||
RelationClearRelation(relation, false);
|
||||
@@ -2097,8 +2097,8 @@ AtEOXact_RelationCache(bool isCommit)
|
||||
* Note: this must be called *before* processing invalidation messages.
|
||||
*/
|
||||
void
|
||||
AtEOSubXact_RelationCache(bool isCommit, TransactionId myXid,
|
||||
TransactionId parentXid)
|
||||
AtEOSubXact_RelationCache(bool isCommit, SubTransactionId mySubid,
|
||||
SubTransactionId parentSubid)
|
||||
{
|
||||
HASH_SEQ_STATUS status;
|
||||
RelIdCacheEnt *idhentry;
|
||||
@@ -2115,10 +2115,10 @@ AtEOSubXact_RelationCache(bool isCommit, TransactionId myXid,
|
||||
* During subcommit, mark it as belonging to the parent, instead.
|
||||
* During subabort, simply delete the relcache entry.
|
||||
*/
|
||||
if (TransactionIdEquals(relation->rd_createxact, myXid))
|
||||
if (relation->rd_createSubid == mySubid)
|
||||
{
|
||||
if (isCommit)
|
||||
relation->rd_createxact = parentXid;
|
||||
relation->rd_createSubid = parentSubid;
|
||||
else
|
||||
{
|
||||
Assert(RelationHasReferenceCountZero(relation));
|
||||
@@ -2182,7 +2182,7 @@ RelationBuildLocalRelation(const char *relname,
|
||||
rel->rd_refcnt = nailit ? 1 : 0;
|
||||
|
||||
/* it's being created in this transaction */
|
||||
rel->rd_createxact = GetCurrentTransactionId();
|
||||
rel->rd_createSubid = GetCurrentSubTransactionId();
|
||||
|
||||
/* is it a temporary relation? */
|
||||
rel->rd_istemp = isTempNamespace(relnamespace);
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
* Portions Copyright (c) 1994, Regents of the University of California
|
||||
*
|
||||
* IDENTIFICATION
|
||||
* $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.71 2004/08/29 05:06:51 momjian Exp $
|
||||
* $PostgreSQL: pgsql/src/backend/utils/mmgr/portalmem.c,v 1.72 2004/09/16 16:58:36 tgl Exp $
|
||||
*
|
||||
*-------------------------------------------------------------------------
|
||||
*/
|
||||
@@ -192,7 +192,7 @@ CreatePortal(const char *name, bool allowDup, bool dupSilent)
|
||||
|
||||
/* initialize portal fields that don't start off zero */
|
||||
portal->cleanup = PortalCleanup;
|
||||
portal->createXact = GetCurrentTransactionId();
|
||||
portal->createSubid = GetCurrentSubTransactionId();
|
||||
portal->strategy = PORTAL_MULTI_QUERY;
|
||||
portal->cursorOptions = CURSOR_OPT_NO_SCROLL;
|
||||
portal->atStart = true;
|
||||
@@ -427,7 +427,6 @@ AtCommit_Portals(void)
|
||||
{
|
||||
HASH_SEQ_STATUS status;
|
||||
PortalHashEnt *hentry;
|
||||
TransactionId xact = GetCurrentTransactionId();
|
||||
|
||||
hash_seq_init(&status, PortalHashTable);
|
||||
|
||||
@@ -450,12 +449,9 @@ AtCommit_Portals(void)
|
||||
|
||||
/*
|
||||
* Do nothing else to cursors held over from a previous
|
||||
* transaction. (This test must include checking CURSOR_OPT_HOLD,
|
||||
* else we will fail to clean up a VACUUM portal if it fails after
|
||||
* its first sub-transaction.)
|
||||
* transaction.
|
||||
*/
|
||||
if (portal->createXact != xact &&
|
||||
(portal->cursorOptions & CURSOR_OPT_HOLD))
|
||||
if (portal->createSubid == InvalidSubTransactionId)
|
||||
continue;
|
||||
|
||||
if ((portal->cursorOptions & CURSOR_OPT_HOLD) &&
|
||||
@@ -479,6 +475,12 @@ AtCommit_Portals(void)
|
||||
* longer have its own resources.
|
||||
*/
|
||||
portal->resowner = NULL;
|
||||
|
||||
/*
|
||||
* Having successfully exported the holdable cursor, mark it
|
||||
* as not belonging to this transaction.
|
||||
*/
|
||||
portal->createSubid = InvalidSubTransactionId;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -502,7 +504,6 @@ AtAbort_Portals(void)
|
||||
{
|
||||
HASH_SEQ_STATUS status;
|
||||
PortalHashEnt *hentry;
|
||||
TransactionId xact = GetCurrentTransactionId();
|
||||
|
||||
hash_seq_init(&status, PortalHashTable);
|
||||
|
||||
@@ -515,12 +516,9 @@ AtAbort_Portals(void)
|
||||
|
||||
/*
|
||||
* Do nothing else to cursors held over from a previous
|
||||
* transaction. (This test must include checking CURSOR_OPT_HOLD,
|
||||
* else we will fail to clean up a VACUUM portal if it fails after
|
||||
* its first sub-transaction.)
|
||||
* transaction.
|
||||
*/
|
||||
if (portal->createXact != xact &&
|
||||
(portal->cursorOptions & CURSOR_OPT_HOLD))
|
||||
if (portal->createSubid == InvalidSubTransactionId)
|
||||
continue;
|
||||
|
||||
/* let portalcmds.c clean up the state it knows about */
|
||||
@@ -548,7 +546,6 @@ AtCleanup_Portals(void)
|
||||
{
|
||||
HASH_SEQ_STATUS status;
|
||||
PortalHashEnt *hentry;
|
||||
TransactionId xact = GetCurrentTransactionId();
|
||||
|
||||
hash_seq_init(&status, PortalHashTable);
|
||||
|
||||
@@ -556,14 +553,8 @@ AtCleanup_Portals(void)
|
||||
{
|
||||
Portal portal = hentry->portal;
|
||||
|
||||
/*
|
||||
* Do nothing else to cursors held over from a previous
|
||||
* transaction. (This test must include checking CURSOR_OPT_HOLD,
|
||||
* else we will fail to clean up a VACUUM portal if it fails after
|
||||
* its first sub-transaction.)
|
||||
*/
|
||||
if (portal->createXact != xact &&
|
||||
(portal->cursorOptions & CURSOR_OPT_HOLD))
|
||||
/* Do nothing to cursors held over from a previous transaction */
|
||||
if (portal->createSubid == InvalidSubTransactionId)
|
||||
{
|
||||
Assert(portal->status != PORTAL_ACTIVE);
|
||||
Assert(portal->resowner == NULL);
|
||||
@@ -579,15 +570,15 @@ AtCleanup_Portals(void)
|
||||
* Pre-subcommit processing for portals.
|
||||
*
|
||||
* Reassign the portals created in the current subtransaction to the parent
|
||||
* transaction.
|
||||
* subtransaction.
|
||||
*/
|
||||
void
|
||||
AtSubCommit_Portals(TransactionId parentXid,
|
||||
AtSubCommit_Portals(SubTransactionId mySubid,
|
||||
SubTransactionId parentSubid,
|
||||
ResourceOwner parentXactOwner)
|
||||
{
|
||||
HASH_SEQ_STATUS status;
|
||||
PortalHashEnt *hentry;
|
||||
TransactionId curXid = GetCurrentTransactionId();
|
||||
|
||||
hash_seq_init(&status, PortalHashTable);
|
||||
|
||||
@@ -595,9 +586,9 @@ AtSubCommit_Portals(TransactionId parentXid,
|
||||
{
|
||||
Portal portal = hentry->portal;
|
||||
|
||||
if (portal->createXact == curXid)
|
||||
if (portal->createSubid == mySubid)
|
||||
{
|
||||
portal->createXact = parentXid;
|
||||
portal->createSubid = parentSubid;
|
||||
if (portal->resowner)
|
||||
ResourceOwnerNewParent(portal->resowner, parentXactOwner);
|
||||
}
|
||||
@@ -612,12 +603,12 @@ AtSubCommit_Portals(TransactionId parentXid,
|
||||
* in descendants of the subtransaction too.
|
||||
*/
|
||||
void
|
||||
AtSubAbort_Portals(TransactionId parentXid,
|
||||
AtSubAbort_Portals(SubTransactionId mySubid,
|
||||
SubTransactionId parentSubid,
|
||||
ResourceOwner parentXactOwner)
|
||||
{
|
||||
HASH_SEQ_STATUS status;
|
||||
PortalHashEnt *hentry;
|
||||
TransactionId curXid = GetCurrentTransactionId();
|
||||
|
||||
hash_seq_init(&status, PortalHashTable);
|
||||
|
||||
@@ -625,7 +616,7 @@ AtSubAbort_Portals(TransactionId parentXid,
|
||||
{
|
||||
Portal portal = hentry->portal;
|
||||
|
||||
if (portal->createXact != curXid)
|
||||
if (portal->createSubid != mySubid)
|
||||
continue;
|
||||
|
||||
/*
|
||||
@@ -644,7 +635,7 @@ AtSubAbort_Portals(TransactionId parentXid,
|
||||
*/
|
||||
if (portal->status == PORTAL_READY)
|
||||
{
|
||||
portal->createXact = parentXid;
|
||||
portal->createSubid = parentSubid;
|
||||
if (portal->resowner)
|
||||
ResourceOwnerNewParent(portal->resowner, parentXactOwner);
|
||||
}
|
||||
@@ -674,11 +665,10 @@ AtSubAbort_Portals(TransactionId parentXid,
|
||||
* we will not drop any that were reassigned to the parent above).
|
||||
*/
|
||||
void
|
||||
AtSubCleanup_Portals(void)
|
||||
AtSubCleanup_Portals(SubTransactionId mySubid)
|
||||
{
|
||||
HASH_SEQ_STATUS status;
|
||||
PortalHashEnt *hentry;
|
||||
TransactionId curXid = GetCurrentTransactionId();
|
||||
|
||||
hash_seq_init(&status, PortalHashTable);
|
||||
|
||||
@@ -686,7 +676,7 @@ AtSubCleanup_Portals(void)
|
||||
{
|
||||
Portal portal = hentry->portal;
|
||||
|
||||
if (portal->createXact != curXid)
|
||||
if (portal->createSubid != mySubid)
|
||||
continue;
|
||||
|
||||
/* AtSubAbort_Portals should have fixed these: */
|
||||
|
||||
Reference in New Issue
Block a user