Skip to content

Commit 7be8946

Browse files
committed
Avoid deep recursion when assigning XIDs to multiple levels of subxacts.
Backpatch to 8.0. Andres Freund, with cleanup and adjustment for older branches by me.
1 parent bca03b1 commit 7be8946

File tree

1 file changed

+25
-3
lines changed
  • src/backend/access/transam

1 file changed

+25
-3
lines changed

src/backend/access/transam/xact.c

Lines changed: 25 additions & 3 deletions
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.293 2010/07/06 19:18:55 momjian Exp $
13+
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.294 2010/07/23 00:43:00 rhaas Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -408,10 +408,32 @@ AssignTransactionId(TransactionState s)
408408

409409
/*
410410
* Ensure parent(s) have XIDs, so that a child always has an XID later
411-
* than its parent.
411+
* than its parent. Musn't recurse here, or we might get a stack overflow
412+
* if we're at the bottom of a huge stack of subtransactions none of which
413+
* have XIDs yet.
412414
*/
413415
if (isSubXact && !TransactionIdIsValid(s->parent->transactionId))
414-
AssignTransactionId(s->parent);
416+
{
417+
TransactionState p = s->parent;
418+
TransactionState *parents;
419+
size_t parentOffset = 0;
420+
421+
parents = palloc(sizeof(TransactionState) * s->nestingLevel);
422+
while (p != NULL && !TransactionIdIsValid(p->transactionId))
423+
{
424+
parents[parentOffset++] = p;
425+
p = p->parent;
426+
}
427+
428+
/*
429+
* This is technically a recursive call, but the recursion will
430+
* never be more than one layer deep.
431+
*/
432+
while (parentOffset != 0)
433+
AssignTransactionId(parents[--parentOffset]);
434+
435+
pfree(parents);
436+
}
415437

416438
/*
417439
* Generate a new Xid and record it in PG_PROC and pg_subtrans.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy