Skip to content

Commit babe545

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 3d858af commit babe545

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
@@ -1497,6 +1498,8 @@ shdepReassignOwned(List *roleids, Oid newrole)
14971498
while ((tuple = systable_getnext(scan)) != NULL)
14981499
{
14991500
Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
1501+
MemoryContext cxt,
1502+
oldcxt;
15001503

15011504
/*
15021505
* We only operate on shared objects and objects in the current
@@ -1510,6 +1513,18 @@ shdepReassignOwned(List *roleids, Oid newrole)
15101513
if (sdepForm->deptype != SHARED_DEPENDENCY_OWNER)
15111514
continue;
15121515

1516+
/*
1517+
* The various ALTER OWNER routines tend to leak memory in
1518+
* CurrentMemoryContext. That's not a problem when they're only
1519+
* called once per command; but in this usage where we might be
1520+
* touching many objects, it can amount to a serious memory leak.
1521+
* Fix that by running each call in a short-lived context.
1522+
*/
1523+
cxt = AllocSetContextCreate(CurrentMemoryContext,
1524+
"shdepReassignOwned",
1525+
ALLOCSET_DEFAULT_SIZES);
1526+
oldcxt = MemoryContextSwitchTo(cxt);
1527+
15131528
/* Issue the appropriate ALTER OWNER call */
15141529
switch (sdepForm->classid)
15151530
{
@@ -1598,6 +1613,11 @@ shdepReassignOwned(List *roleids, Oid newrole)
15981613
elog(ERROR, "unexpected classid %u", sdepForm->classid);
15991614
break;
16001615
}
1616+
1617+
/* Clean up */
1618+
MemoryContextSwitchTo(oldcxt);
1619+
MemoryContextDelete(cxt);
1620+
16011621
/* Make sure the next iteration will see my changes */
16021622
CommandCounterIncrement();
16031623
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy