Skip to content

Commit 5cf08b4

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 2f908e1 commit 5cf08b4

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 "miscadmin.h"
6666
#include "utils/acl.h"
6767
#include "utils/fmgroids.h"
68+
#include "utils/memutils.h"
6869
#include "utils/syscache.h"
6970

7071

@@ -1507,6 +1508,8 @@ shdepReassignOwned(List *roleids, Oid newrole)
15071508
while ((tuple = systable_getnext(scan)) != NULL)
15081509
{
15091510
Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1511+
MemoryContext cxt,
1512+
oldcxt;
15101513

15111514
/*
15121515
* We only operate on shared objects and objects in the current
@@ -1524,6 +1527,18 @@ shdepReassignOwned(List *roleids, Oid newrole)
15241527
if (sdepForm->deptype != SHARED_DEPENDENCY_OWNER)
15251528
continue;
15261529

1530+
/*
1531+
* The various ALTER OWNER routines tend to leak memory in
1532+
* CurrentMemoryContext. That's not a problem when they're only
1533+
* called once per command; but in this usage where we might be
1534+
* touching many objects, it can amount to a serious memory leak.
1535+
* Fix that by running each call in a short-lived context.
1536+
*/
1537+
cxt = AllocSetContextCreate(CurrentMemoryContext,
1538+
"shdepReassignOwned",
1539+
ALLOCSET_DEFAULT_SIZES);
1540+
oldcxt = MemoryContextSwitchTo(cxt);
1541+
15271542
/* Issue the appropriate ALTER OWNER call */
15281543
switch (sdepForm->classid)
15291544
{
@@ -1612,6 +1627,11 @@ shdepReassignOwned(List *roleids, Oid newrole)
16121627
elog(ERROR, "unexpected classid %u", sdepForm->classid);
16131628
break;
16141629
}
1630+
1631+
/* Clean up */
1632+
MemoryContextSwitchTo(oldcxt);
1633+
MemoryContextDelete(cxt);
1634+
16151635
/* Make sure the next iteration will see my changes */
16161636
CommandCounterIncrement();
16171637
}

0 commit comments

Comments
 (0)