*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.189 2004/09/16 16:58:26 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.190 2004/09/16 20:17:16 tgl Exp $
*
*-------------------------------------------------------------------------
*/
/*
* AtSubCommit_Memory
- *
- * We do not throw away the child's CurTransactionContext, since the data
- * it contains will be needed at upper commit.
*/
static void
AtSubCommit_Memory(void)
/* Return to parent transaction level's memory context. */
CurTransactionContext = s->parent->curTransactionContext;
MemoryContextSwitchTo(CurTransactionContext);
+
+ /*
+ * Ordinarily we cannot throw away the child's CurTransactionContext,
+ * since the data it contains will be needed at upper commit. However,
+ * if there isn't actually anything in it, we can throw it away. This
+ * avoids a small memory leak in the common case of "trivial" subxacts.
+ */
+ if (MemoryContextIsEmpty(s->curTransactionContext))
+ {
+ MemoryContextDelete(s->curTransactionContext);
+ s->curTransactionContext = NULL;
+ }
}
/*
Assert(s->parent != NULL);
- old_cxt = MemoryContextSwitchTo(s->parent->curTransactionContext);
+ /*
+ * We keep the child-XID lists in TopTransactionContext; this avoids
+ * setting up child-transaction contexts for what might be just a few
+ * bytes of grandchild XIDs.
+ */
+ old_cxt = MemoryContextSwitchTo(TopTransactionContext);
s->parent->childXids = lappend_xid(s->parent->childXids,
s->transactionId);
- s->parent->childXids = list_concat(s->parent->childXids, s->childXids);
- s->childXids = NIL; /* ensure list not doubly referenced */
+ if (s->childXids != NIL)
+ {
+ s->parent->childXids = list_concat(s->parent->childXids,
+ s->childXids);
+ /*
+ * list_concat doesn't free the list header for the second list;
+ * do so here to avoid memory leakage (kluge)
+ */
+ pfree(s->childXids);
+ s->childXids = NIL;
+ }
MemoryContextSwitchTo(old_cxt);
}
MemoryContextSwitchTo(TopTransactionContext);
}
+/*
+ * AtSubAbort_childXids
+ */
+static void
+AtSubAbort_childXids(void)
+{
+ TransactionState s = CurrentTransactionState;
+
+ /*
+ * We keep the child-XID lists in TopTransactionContext (see
+ * AtSubCommit_childXids). This means we'd better free the list
+ * explicitly at abort to avoid leakage.
+ */
+ list_free(s->childXids);
+ s->childXids = NIL;
+}
+
/*
* RecordSubTransactionAbort
*/
/* Advertise the fact that we aborted in pg_clog. */
if (TransactionIdIsValid(s->transactionId))
+ {
RecordSubTransactionAbort();
+ AtSubAbort_childXids();
+ }
/* Post-abort cleanup */
CallSubXactCallbacks(SUBXACT_EVENT_ABORT_SUB, s->subTransactionId,
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.128 2004/09/16 16:58:29 tgl Exp $
+ * $PostgreSQL: pgsql/src/backend/executor/spi.c,v 1.129 2004/09/16 20:17:20 tgl Exp $
*
*-------------------------------------------------------------------------
*/
_SPI_current = &(_SPI_stack[_SPI_connected]);
_SPI_current->processed = 0;
_SPI_current->tuptable = NULL;
+ _SPI_current->procCxt = NULL; /* in case we fail to create 'em */
+ _SPI_current->execCxt = NULL;
_SPI_current->connectSubid = GetCurrentSubTransactionId();
/*
/* Release memory used in procedure call */
MemoryContextDelete(_SPI_current->execCxt);
+ _SPI_current->execCxt = NULL;
MemoryContextDelete(_SPI_current->procCxt);
+ _SPI_current->procCxt = NULL;
/*
* Reset result variables, especially SPI_tuptable which is probably
found = true;
+ /*
+ * Release procedure memory explicitly (see note in SPI_connect)
+ */
+ if (connection->execCxt)
+ {
+ MemoryContextDelete(connection->execCxt);
+ connection->execCxt = NULL;
+ }
+ if (connection->procCxt)
+ {
+ MemoryContextDelete(connection->procCxt);
+ connection->procCxt = NULL;
+ }
+
/*
* Pop the stack entry and reset global variables. Unlike
* SPI_finish(), we don't risk switching to memory contexts that
- * might be already gone, or deleting memory contexts that have
- * been or will be thrown away anyway.
+ * might be already gone.
*/
_SPI_connected--;
_SPI_curid = _SPI_connected;
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/mmgr/aset.c,v 1.57 2004/08/29 05:06:51 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/mmgr/aset.c,v 1.58 2004/09/16 20:17:33 tgl Exp $
*
* NOTE:
* This is a new (Feb. 05, 1999) implementation of the allocation set
static void AllocSetReset(MemoryContext context);
static void AllocSetDelete(MemoryContext context);
static Size AllocSetGetChunkSpace(MemoryContext context, void *pointer);
+static bool AllocSetIsEmpty(MemoryContext context);
static void AllocSetStats(MemoryContext context);
#ifdef MEMORY_CONTEXT_CHECKING
AllocSetReset,
AllocSetDelete,
AllocSetGetChunkSpace,
+ AllocSetIsEmpty,
AllocSetStats
#ifdef MEMORY_CONTEXT_CHECKING
,AllocSetCheck
return chunk->size + ALLOC_CHUNKHDRSZ;
}
+/*
+ * AllocSetIsEmpty
+ * Is an allocset empty of any allocated space?
+ */
+static bool
+AllocSetIsEmpty(MemoryContext context)
+{
+ AllocSet set = (AllocSet) context;
+
+ /*
+ * For now, we say "empty" only if the context never contained any
+ * space at all. We could examine the freelists to determine if all
+ * space has been freed, but it's not really worth the trouble for
+ * present uses of this functionality.
+ */
+ if (set->blocks == NULL)
+ return true;
+ return false;
+}
+
/*
* AllocSetStats
* Displays stats about memory consumption of an allocset.
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/utils/mmgr/mcxt.c,v 1.50 2004/08/29 05:06:51 momjian Exp $
+ * $PostgreSQL: pgsql/src/backend/utils/mmgr/mcxt.c,v 1.51 2004/09/16 20:17:33 tgl Exp $
*
*-------------------------------------------------------------------------
*/
return header->context;
}
+/*
+ * MemoryContextIsEmpty
+ * Is a memory context empty of any allocated space?
+ */
+bool
+MemoryContextIsEmpty(MemoryContext context)
+{
+ AssertArg(MemoryContextIsValid(context));
+
+ /*
+ * For now, we consider a memory context nonempty if it has any children;
+ * perhaps this should be changed later.
+ */
+ if (context->firstchild != NULL)
+ return false;
+ /* Otherwise use the type-specific inquiry */
+ return (*context->methods->is_empty) (context);
+}
+
/*
* MemoryContextStats
* Print statistics about the named context and all its descendants.
pgport_pfree(void *pointer)
{
pfree(pointer);
- return;
}
-#endif
+#endif /* WIN32 */
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/nodes/memnodes.h,v 1.28 2004/08/29 04:13:07 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/nodes/memnodes.h,v 1.29 2004/09/16 20:17:42 tgl Exp $
*
*-------------------------------------------------------------------------
*/
void (*reset) (MemoryContext context);
void (*delete) (MemoryContext context);
Size (*get_chunk_space) (MemoryContext context, void *pointer);
+ bool (*is_empty) (MemoryContext context);
void (*stats) (MemoryContext context);
#ifdef MEMORY_CONTEXT_CHECKING
void (*check) (MemoryContext context);
* Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $PostgreSQL: pgsql/src/include/utils/memutils.h,v 1.57 2004/08/29 04:13:11 momjian Exp $
+ * $PostgreSQL: pgsql/src/include/utils/memutils.h,v 1.58 2004/09/16 20:17:49 tgl Exp $
*
*-------------------------------------------------------------------------
*/
extern void MemoryContextResetAndDeleteChildren(MemoryContext context);
extern Size GetMemoryChunkSpace(void *pointer);
extern MemoryContext GetMemoryChunkContext(void *pointer);
+extern bool MemoryContextIsEmpty(MemoryContext context);
extern void MemoryContextStats(MemoryContext context);
#ifdef MEMORY_CONTEXT_CHECKING