mirror of
https://github.com/postgres/postgres.git
synced 2025-06-23 14:01:44 +03:00
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 3aafc030a
,
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
This commit is contained in:
@ -65,6 +65,7 @@
|
|||||||
#include "miscadmin.h"
|
#include "miscadmin.h"
|
||||||
#include "utils/acl.h"
|
#include "utils/acl.h"
|
||||||
#include "utils/fmgroids.h"
|
#include "utils/fmgroids.h"
|
||||||
|
#include "utils/memutils.h"
|
||||||
#include "utils/syscache.h"
|
#include "utils/syscache.h"
|
||||||
#include "utils/tqual.h"
|
#include "utils/tqual.h"
|
||||||
|
|
||||||
@ -1414,6 +1415,8 @@ shdepReassignOwned(List *roleids, Oid newrole)
|
|||||||
while ((tuple = systable_getnext(scan)) != NULL)
|
while ((tuple = systable_getnext(scan)) != NULL)
|
||||||
{
|
{
|
||||||
Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
|
Form_pg_shdepend sdepForm = (Form_pg_shdepend) GETSTRUCT(tuple);
|
||||||
|
MemoryContext cxt,
|
||||||
|
oldcxt;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* We only operate on shared objects and objects in the current
|
* We only operate on shared objects and objects in the current
|
||||||
@ -1431,6 +1434,18 @@ shdepReassignOwned(List *roleids, Oid newrole)
|
|||||||
if (sdepForm->deptype != SHARED_DEPENDENCY_OWNER)
|
if (sdepForm->deptype != SHARED_DEPENDENCY_OWNER)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
cxt = AllocSetContextCreate(CurrentMemoryContext,
|
||||||
|
"shdepReassignOwned",
|
||||||
|
ALLOCSET_DEFAULT_SIZES);
|
||||||
|
oldcxt = MemoryContextSwitchTo(cxt);
|
||||||
|
|
||||||
/* Issue the appropriate ALTER OWNER call */
|
/* Issue the appropriate ALTER OWNER call */
|
||||||
switch (sdepForm->classid)
|
switch (sdepForm->classid)
|
||||||
{
|
{
|
||||||
@ -1519,6 +1534,11 @@ shdepReassignOwned(List *roleids, Oid newrole)
|
|||||||
elog(ERROR, "unexpected classid %u", sdepForm->classid);
|
elog(ERROR, "unexpected classid %u", sdepForm->classid);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Clean up */
|
||||||
|
MemoryContextSwitchTo(oldcxt);
|
||||||
|
MemoryContextDelete(cxt);
|
||||||
|
|
||||||
/* Make sure the next iteration will see my changes */
|
/* Make sure the next iteration will see my changes */
|
||||||
CommandCounterIncrement();
|
CommandCounterIncrement();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user