Skip to content

Commit 7413caa

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 64e8456 commit 7413caa

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
@@ -1508,6 +1509,8 @@ shdepReassignOwned(List *roleids, Oid newrole)
15081509
while ((tuple = systable_getnext(scan)) != NULL)
15091510
{
15101511
Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1512+
MemoryContext cxt,
1513+
oldcxt;
15111514

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

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

0 commit comments

Comments
 (0)