Commit 5fa6b0d1 authored by Tom Lane's avatar Tom Lane

Remove unnecessary PG_TRY overhead for CurrentResourceOwner changes.

resowner/README contained advice to use a PG_TRY block to restore the
old CurrentResourceOwner value anywhere that that variable is transiently
changed.  That advice was only inconsistently followed, however, and
on reflection it seems like unnecessary overhead.  We don't bother
with such a convention for transient CurrentMemoryContext changes,
on the grounds that any (sub)transaction abort will start out by
resetting CurrentMemoryContext to what it wants.  But the same is
true of CurrentResourceOwner, so there seems no need to treat it
differently.

Hence, remove PG_TRY blocks that exist only to restore CurrentResourceOwner
before re-throwing the error.  There are a couple of places that restore
it along with some other actions, and I left those alone; the restore is
probably unnecessary but no noticeable gain will result from removing it.

Discussion: https://postgr.es/m/5236.1507583529@sss.pgh.pa.us
parent f6766166
...@@ -575,18 +575,10 @@ AssignTransactionId(TransactionState s) ...@@ -575,18 +575,10 @@ AssignTransactionId(TransactionState s)
* ResourceOwner. * ResourceOwner.
*/ */
currentOwner = CurrentResourceOwner; currentOwner = CurrentResourceOwner;
PG_TRY();
{
CurrentResourceOwner = s->curTransactionOwner; CurrentResourceOwner = s->curTransactionOwner;
XactLockTableInsert(s->transactionId); XactLockTableInsert(s->transactionId);
}
PG_CATCH();
{
/* Ensure CurrentResourceOwner is restored on error */
CurrentResourceOwner = currentOwner;
PG_RE_THROW();
}
PG_END_TRY();
CurrentResourceOwner = currentOwner; CurrentResourceOwner = currentOwner;
/* /*
......
...@@ -294,21 +294,13 @@ PortalCleanup(Portal portal) ...@@ -294,21 +294,13 @@ PortalCleanup(Portal portal)
/* We must make the portal's resource owner current */ /* We must make the portal's resource owner current */
saveResourceOwner = CurrentResourceOwner; saveResourceOwner = CurrentResourceOwner;
PG_TRY();
{
if (portal->resowner) if (portal->resowner)
CurrentResourceOwner = portal->resowner; CurrentResourceOwner = portal->resowner;
ExecutorFinish(queryDesc); ExecutorFinish(queryDesc);
ExecutorEnd(queryDesc); ExecutorEnd(queryDesc);
FreeQueryDesc(queryDesc); FreeQueryDesc(queryDesc);
}
PG_CATCH();
{
/* Ensure CurrentResourceOwner is restored on error */
CurrentResourceOwner = saveResourceOwner;
PG_RE_THROW();
}
PG_END_TRY();
CurrentResourceOwner = saveResourceOwner; CurrentResourceOwner = saveResourceOwner;
} }
} }
......
...@@ -1055,18 +1055,10 @@ lock_and_open_sequence(SeqTable seq) ...@@ -1055,18 +1055,10 @@ lock_and_open_sequence(SeqTable seq)
ResourceOwner currentOwner; ResourceOwner currentOwner;
currentOwner = CurrentResourceOwner; currentOwner = CurrentResourceOwner;
PG_TRY();
{
CurrentResourceOwner = TopTransactionResourceOwner; CurrentResourceOwner = TopTransactionResourceOwner;
LockRelationOid(seq->relid, RowExclusiveLock); LockRelationOid(seq->relid, RowExclusiveLock);
}
PG_CATCH();
{
/* Ensure CurrentResourceOwner is restored on error */
CurrentResourceOwner = currentOwner;
PG_RE_THROW();
}
PG_END_TRY();
CurrentResourceOwner = currentOwner; CurrentResourceOwner = currentOwner;
/* Flag that we have a lock in the current xact */ /* Flag that we have a lock in the current xact */
......
...@@ -3639,17 +3639,10 @@ GetCurrentFDWTuplestore(void) ...@@ -3639,17 +3639,10 @@ GetCurrentFDWTuplestore(void)
*/ */
oldcxt = MemoryContextSwitchTo(CurTransactionContext); oldcxt = MemoryContextSwitchTo(CurTransactionContext);
saveResourceOwner = CurrentResourceOwner; saveResourceOwner = CurrentResourceOwner;
PG_TRY();
{
CurrentResourceOwner = CurTransactionResourceOwner; CurrentResourceOwner = CurTransactionResourceOwner;
ret = tuplestore_begin_heap(false, false, work_mem); ret = tuplestore_begin_heap(false, false, work_mem);
}
PG_CATCH();
{
CurrentResourceOwner = saveResourceOwner;
PG_RE_THROW();
}
PG_END_TRY();
CurrentResourceOwner = saveResourceOwner; CurrentResourceOwner = saveResourceOwner;
MemoryContextSwitchTo(oldcxt); MemoryContextSwitchTo(oldcxt);
...@@ -4468,20 +4461,13 @@ MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType) ...@@ -4468,20 +4461,13 @@ MakeTransitionCaptureState(TriggerDesc *trigdesc, Oid relid, CmdType cmdType)
/* Now create required tuplestore(s), if we don't have them already. */ /* Now create required tuplestore(s), if we don't have them already. */
oldcxt = MemoryContextSwitchTo(CurTransactionContext); oldcxt = MemoryContextSwitchTo(CurTransactionContext);
saveResourceOwner = CurrentResourceOwner; saveResourceOwner = CurrentResourceOwner;
PG_TRY();
{
CurrentResourceOwner = CurTransactionResourceOwner; CurrentResourceOwner = CurTransactionResourceOwner;
if (need_old && table->old_tuplestore == NULL) if (need_old && table->old_tuplestore == NULL)
table->old_tuplestore = tuplestore_begin_heap(false, false, work_mem); table->old_tuplestore = tuplestore_begin_heap(false, false, work_mem);
if (need_new && table->new_tuplestore == NULL) if (need_new && table->new_tuplestore == NULL)
table->new_tuplestore = tuplestore_begin_heap(false, false, work_mem); table->new_tuplestore = tuplestore_begin_heap(false, false, work_mem);
}
PG_CATCH();
{
CurrentResourceOwner = saveResourceOwner;
PG_RE_THROW();
}
PG_END_TRY();
CurrentResourceOwner = saveResourceOwner; CurrentResourceOwner = saveResourceOwner;
MemoryContextSwitchTo(oldcxt); MemoryContextSwitchTo(oldcxt);
......
...@@ -75,8 +75,6 @@ open_lo_relation(void) ...@@ -75,8 +75,6 @@ open_lo_relation(void)
/* Arrange for the top xact to own these relation references */ /* Arrange for the top xact to own these relation references */
currentOwner = CurrentResourceOwner; currentOwner = CurrentResourceOwner;
PG_TRY();
{
CurrentResourceOwner = TopTransactionResourceOwner; CurrentResourceOwner = TopTransactionResourceOwner;
/* Use RowExclusiveLock since we might either read or write */ /* Use RowExclusiveLock since we might either read or write */
...@@ -84,14 +82,7 @@ open_lo_relation(void) ...@@ -84,14 +82,7 @@ open_lo_relation(void)
lo_heap_r = heap_open(LargeObjectRelationId, RowExclusiveLock); lo_heap_r = heap_open(LargeObjectRelationId, RowExclusiveLock);
if (lo_index_r == NULL) if (lo_index_r == NULL)
lo_index_r = index_open(LargeObjectLOidPNIndexId, RowExclusiveLock); lo_index_r = index_open(LargeObjectLOidPNIndexId, RowExclusiveLock);
}
PG_CATCH();
{
/* Ensure CurrentResourceOwner is restored on error */
CurrentResourceOwner = currentOwner;
PG_RE_THROW();
}
PG_END_TRY();
CurrentResourceOwner = currentOwner; CurrentResourceOwner = currentOwner;
} }
...@@ -112,22 +103,13 @@ close_lo_relation(bool isCommit) ...@@ -112,22 +103,13 @@ close_lo_relation(bool isCommit)
ResourceOwner currentOwner; ResourceOwner currentOwner;
currentOwner = CurrentResourceOwner; currentOwner = CurrentResourceOwner;
PG_TRY();
{
CurrentResourceOwner = TopTransactionResourceOwner; CurrentResourceOwner = TopTransactionResourceOwner;
if (lo_index_r) if (lo_index_r)
index_close(lo_index_r, NoLock); index_close(lo_index_r, NoLock);
if (lo_heap_r) if (lo_heap_r)
heap_close(lo_heap_r, NoLock); heap_close(lo_heap_r, NoLock);
}
PG_CATCH();
{
/* Ensure CurrentResourceOwner is restored on error */
CurrentResourceOwner = currentOwner;
PG_RE_THROW();
}
PG_END_TRY();
CurrentResourceOwner = currentOwner; CurrentResourceOwner = currentOwner;
} }
lo_heap_r = NULL; lo_heap_r = NULL;
......
...@@ -80,7 +80,3 @@ CurrentResourceOwner must point to the same resource owner that was current ...@@ -80,7 +80,3 @@ CurrentResourceOwner must point to the same resource owner that was current
when the buffer, lock, or cache reference was acquired. It would be possible when the buffer, lock, or cache reference was acquired. It would be possible
to relax this restriction given additional bookkeeping effort, but at present to relax this restriction given additional bookkeeping effort, but at present
there seems no need. there seems no need.
Code that transiently changes CurrentResourceOwner should generally use a
PG_TRY construct to ensure that the previous value of CurrentResourceOwner
is restored if control is lost during an error exit.
...@@ -473,21 +473,8 @@ ResourceOwnerRelease(ResourceOwner owner, ...@@ -473,21 +473,8 @@ ResourceOwnerRelease(ResourceOwner owner,
bool isCommit, bool isCommit,
bool isTopLevel) bool isTopLevel)
{ {
/* Rather than PG_TRY at every level of recursion, set it up once */ /* There's not currently any setup needed before recursing */
ResourceOwner save;
save = CurrentResourceOwner;
PG_TRY();
{
ResourceOwnerReleaseInternal(owner, phase, isCommit, isTopLevel); ResourceOwnerReleaseInternal(owner, phase, isCommit, isTopLevel);
}
PG_CATCH();
{
CurrentResourceOwner = save;
PG_RE_THROW();
}
PG_END_TRY();
CurrentResourceOwner = save;
} }
static void static void
...@@ -507,8 +494,7 @@ ResourceOwnerReleaseInternal(ResourceOwner owner, ...@@ -507,8 +494,7 @@ ResourceOwnerReleaseInternal(ResourceOwner owner,
/* /*
* Make CurrentResourceOwner point to me, so that ReleaseBuffer etc don't * Make CurrentResourceOwner point to me, so that ReleaseBuffer etc don't
* get confused. We needn't PG_TRY here because the outermost level will * get confused.
* fix it on error abort.
*/ */
save = CurrentResourceOwner; save = CurrentResourceOwner;
CurrentResourceOwner = owner; CurrentResourceOwner = owner;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment