Skip to content

Commit 353a1cc

Browse files
committed
Release any detoasted copies of arrays that are made temporarily in
ri_FetchConstraintInfo, to avoid a query-duration memory leak when that routine is called by RI_FKey_keyequal_upd_fk (which isn't executed in a short-lived context). This problem was latent when the routine was added in February, but it didn't become serious until the varvarlena patch made it quite likely that the fields being examined would be "toasted" (ie, have short headers). Per report from Stephen Denne.
1 parent 79a323a commit 353a1cc

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/backend/utils/adt/ri_triggers.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*
1616
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
1717
*
18-
* $PostgreSQL: pgsql/src/backend/utils/adt/ri_triggers.c,v 1.101 2008/01/03 21:23:15 tgl Exp $
18+
* $PostgreSQL: pgsql/src/backend/utils/adt/ri_triggers.c,v 1.102 2008/01/25 04:46:07 tgl Exp $
1919
*
2020
* ----------
2121
*/
@@ -3099,6 +3099,8 @@ ri_FetchConstraintInfo(RI_ConstraintInfo *riinfo,
30993099
elog(ERROR, "conkey is not a 1-D smallint array");
31003100
riinfo->nkeys = numkeys;
31013101
memcpy(riinfo->fk_attnums, ARR_DATA_PTR(arr), numkeys * sizeof(int16));
3102+
if ((Pointer) arr != DatumGetPointer(adatum))
3103+
pfree(arr); /* free de-toasted copy, if any */
31023104

31033105
adatum = SysCacheGetAttr(CONSTROID, tup,
31043106
Anum_pg_constraint_confkey, &isNull);
@@ -3113,6 +3115,8 @@ ri_FetchConstraintInfo(RI_ConstraintInfo *riinfo,
31133115
ARR_ELEMTYPE(arr) != INT2OID)
31143116
elog(ERROR, "confkey is not a 1-D smallint array");
31153117
memcpy(riinfo->pk_attnums, ARR_DATA_PTR(arr), numkeys * sizeof(int16));
3118+
if ((Pointer) arr != DatumGetPointer(adatum))
3119+
pfree(arr); /* free de-toasted copy, if any */
31163120

31173121
adatum = SysCacheGetAttr(CONSTROID, tup,
31183122
Anum_pg_constraint_conpfeqop, &isNull);
@@ -3127,6 +3131,8 @@ ri_FetchConstraintInfo(RI_ConstraintInfo *riinfo,
31273131
ARR_ELEMTYPE(arr) != OIDOID)
31283132
elog(ERROR, "conpfeqop is not a 1-D Oid array");
31293133
memcpy(riinfo->pf_eq_oprs, ARR_DATA_PTR(arr), numkeys * sizeof(Oid));
3134+
if ((Pointer) arr != DatumGetPointer(adatum))
3135+
pfree(arr); /* free de-toasted copy, if any */
31303136

31313137
adatum = SysCacheGetAttr(CONSTROID, tup,
31323138
Anum_pg_constraint_conppeqop, &isNull);
@@ -3141,6 +3147,8 @@ ri_FetchConstraintInfo(RI_ConstraintInfo *riinfo,
31413147
ARR_ELEMTYPE(arr) != OIDOID)
31423148
elog(ERROR, "conppeqop is not a 1-D Oid array");
31433149
memcpy(riinfo->pp_eq_oprs, ARR_DATA_PTR(arr), numkeys * sizeof(Oid));
3150+
if ((Pointer) arr != DatumGetPointer(adatum))
3151+
pfree(arr); /* free de-toasted copy, if any */
31443152

31453153
adatum = SysCacheGetAttr(CONSTROID, tup,
31463154
Anum_pg_constraint_conffeqop, &isNull);
@@ -3155,6 +3163,8 @@ ri_FetchConstraintInfo(RI_ConstraintInfo *riinfo,
31553163
ARR_ELEMTYPE(arr) != OIDOID)
31563164
elog(ERROR, "conffeqop is not a 1-D Oid array");
31573165
memcpy(riinfo->ff_eq_oprs, ARR_DATA_PTR(arr), numkeys * sizeof(Oid));
3166+
if ((Pointer) arr != DatumGetPointer(adatum))
3167+
pfree(arr); /* free de-toasted copy, if any */
31583168

31593169
ReleaseSysCache(tup);
31603170
}

0 commit comments

Comments
 (0)