Skip to content

Commit 82d3544

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 3f43dcc commit 82d3544

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
#include "utils/tqual.h"
7071

@@ -1414,6 +1415,8 @@ shdepReassignOwned(List *roleids, Oid newrole)
14141415
while ((tuple = systable_getnext(scan)) != NULL)
14151416
{
14161417
Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1418+
MemoryContext cxt,
1419+
oldcxt;
14171420

14181421
/*
14191422
* We only operate on shared objects and objects in the current
@@ -1431,6 +1434,18 @@ shdepReassignOwned(List *roleids, Oid newrole)
14311434
if (sdepForm->deptype != SHARED_DEPENDENCY_OWNER)
14321435
continue;
14331436

1437+
/*
1438+
* The various ALTER OWNER routines tend to leak memory in
1439+
* CurrentMemoryContext. That's not a problem when they're only
1440+
* called once per command; but in this usage where we might be
1441+
* touching many objects, it can amount to a serious memory leak.
1442+
* Fix that by running each call in a short-lived context.
1443+
*/
1444+
cxt = AllocSetContextCreate(CurrentMemoryContext,
1445+
"shdepReassignOwned",
1446+
ALLOCSET_DEFAULT_SIZES);
1447+
oldcxt = MemoryContextSwitchTo(cxt);
1448+
14341449
/* Issue the appropriate ALTER OWNER call */
14351450
switch (sdepForm->classid)
14361451
{
@@ -1519,6 +1534,11 @@ shdepReassignOwned(List *roleids, Oid newrole)
15191534
elog(ERROR, "unexpected classid %u", sdepForm->classid);
15201535
break;
15211536
}
1537+
1538+
/* Clean up */
1539+
MemoryContextSwitchTo(oldcxt);
1540+
MemoryContextDelete(cxt);
1541+
15221542
/* Make sure the next iteration will see my changes */
15231543
CommandCounterIncrement();
15241544
}

0 commit comments

Comments
 (0)