Skip to content

Commit 8f4b020

Browse files
committed
Avoid leaking memory during large-scale REASSIGN OWNED BY operations.
The various ALTER OWNER routines tend to leak memory in CurrentMemoryContext. That's not a problem when they're only called once per command; but in this usage where we might be touching many objects, it can amount to a serious memory leak. Fix that by running each call in a short-lived context. (DROP OWNED BY likely has a similar issue, except that you'll probably run out of lock table space before noticing. REASSIGN is worth fixing since for most non-table object types, it won't take any lock.) Back-patch to all supported branches. Unfortunately, in the back branches this helps to only a limited extent, since the sinval message queue bloats quite a lot in this usage before commit 3aafc03, consuming memory more or less comparable to what's actually leaked. Still, it's clearly a leak with a simple fix, so we might as well fix it. Justin Pryzby, per report from Guillaume Lelarge Discussion: https://postgr.es/m/CAECtzeW2DAoioEGBRjR=CzHP6TdL=yosGku8qZxfX9hhtrBB0Q@mail.gmail.com
1 parent 4b8eec7 commit 8f4b020

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/backend/catalog/pg_shdepend.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
#include "storage/lmgr.h"
6666
#include "utils/acl.h"
6767
#include "utils/fmgroids.h"
68+
#include "utils/memutils.h"
6869
#include "utils/syscache.h"
6970

7071
typedef enum
@@ -1549,6 +1550,8 @@ shdepReassignOwned(List *roleids, Oid newrole)
15491550
while ((tuple = systable_getnext(scan)) != NULL)
15501551
{
15511552
Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1553+
MemoryContext cxt,
1554+
oldcxt;
15521555

15531556
/*
15541557
* We only operate on shared objects and objects in the current
@@ -1566,6 +1569,18 @@ shdepReassignOwned(List *roleids, Oid newrole)
15661569
if (sdepForm->deptype != SHARED_DEPENDENCY_OWNER)
15671570
continue;
15681571

1572+
/*
1573+
* The various ALTER OWNER routines tend to leak memory in
1574+
* CurrentMemoryContext. That's not a problem when they're only
1575+
* called once per command; but in this usage where we might be
1576+
* touching many objects, it can amount to a serious memory leak.
1577+
* Fix that by running each call in a short-lived context.
1578+
*/
1579+
cxt = AllocSetContextCreate(CurrentMemoryContext,
1580+
"shdepReassignOwned",
1581+
ALLOCSET_DEFAULT_SIZES);
1582+
oldcxt = MemoryContextSwitchTo(cxt);
1583+
15691584
/* Issue the appropriate ALTER OWNER call */
15701585
switch (sdepForm->classid)
15711586
{
@@ -1654,6 +1669,11 @@ shdepReassignOwned(List *roleids, Oid newrole)
16541669
elog(ERROR, "unexpected classid %u", sdepForm->classid);
16551670
break;
16561671
}
1672+
1673+
/* Clean up */
1674+
MemoryContextSwitchTo(oldcxt);
1675+
MemoryContextDelete(cxt);
1676+
16571677
/* Make sure the next iteration will see my changes */
16581678
CommandCounterIncrement();
16591679
}

0 commit comments

Comments
 (0)