Skip to content

Commit 8a0596c

Browse files
committed
Get rid of copy_partition_key
That function currently exists to avoid leaking memory in CacheMemoryContext in case of trouble while the partition key is being built, but there's a better way: allocate everything in a memcxt that goes away if the current (sub)transaction fails, and once the partition key is built and no further errors can occur, make the memcxt permanent by making it a child of CacheMemoryContext. Reviewed-by: Tom Lane Discussion: https://postgr.es/m/20171027172730.eh2domlkpn4ja62m@alvherre.pgsql
1 parent 9ef6aba commit 8a0596c

File tree

1 file changed

+9
-62
lines changed

1 file changed

+9
-62
lines changed

src/backend/utils/cache/relcache.c

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,6 @@ static Relation AllocateRelationDesc(Form_pg_class relp);
262262
static void RelationParseRelOptions(Relation relation, HeapTuple tuple);
263263
static void RelationBuildTupleDesc(Relation relation);
264264
static void RelationBuildPartitionKey(Relation relation);
265-
static PartitionKey copy_partition_key(PartitionKey fromkey);
266265
static Relation RelationBuildDesc(Oid targetRelId, bool insertIt);
267266
static void RelationInitPhysicalAddr(Relation relation);
268267
static void load_critical_index(Oid indexoid, Oid heapoid);
@@ -847,6 +846,12 @@ RelationBuildPartitionKey(Relation relation)
847846
if (!HeapTupleIsValid(tuple))
848847
return;
849848

849+
partkeycxt = AllocSetContextCreateExtended(CurTransactionContext,
850+
RelationGetRelationName(relation),
851+
MEMCONTEXT_COPY_NAME,
852+
ALLOCSET_SMALL_SIZES);
853+
oldcxt = MemoryContextSwitchTo(partkeycxt);
854+
850855
key = (PartitionKey) palloc0(sizeof(PartitionKeyData));
851856

852857
/* Fixed-length attributes */
@@ -984,71 +989,13 @@ RelationBuildPartitionKey(Relation relation)
984989

985990
ReleaseSysCache(tuple);
986991

987-
/* Success --- now copy to the cache memory */
988-
partkeycxt = AllocSetContextCreateExtended(CacheMemoryContext,
989-
RelationGetRelationName(relation),
990-
MEMCONTEXT_COPY_NAME,
991-
ALLOCSET_SMALL_SIZES);
992+
/* Success --- make the relcache point to the newly constructed key */
993+
MemoryContextSetParent(partkeycxt, CacheMemoryContext);
992994
relation->rd_partkeycxt = partkeycxt;
993-
oldcxt = MemoryContextSwitchTo(relation->rd_partkeycxt);
994-
relation->rd_partkey = copy_partition_key(key);
995+
relation->rd_partkey = key;
995996
MemoryContextSwitchTo(oldcxt);
996997
}
997998

998-
/*
999-
* copy_partition_key
1000-
*
1001-
* The copy is allocated in the current memory context.
1002-
*/
1003-
static PartitionKey
1004-
copy_partition_key(PartitionKey fromkey)
1005-
{
1006-
PartitionKey newkey;
1007-
int n;
1008-
1009-
newkey = (PartitionKey) palloc(sizeof(PartitionKeyData));
1010-
1011-
newkey->strategy = fromkey->strategy;
1012-
newkey->partnatts = n = fromkey->partnatts;
1013-
1014-
newkey->partattrs = (AttrNumber *) palloc(n * sizeof(AttrNumber));
1015-
memcpy(newkey->partattrs, fromkey->partattrs, n * sizeof(AttrNumber));
1016-
1017-
newkey->partexprs = copyObject(fromkey->partexprs);
1018-
1019-
newkey->partopfamily = (Oid *) palloc(n * sizeof(Oid));
1020-
memcpy(newkey->partopfamily, fromkey->partopfamily, n * sizeof(Oid));
1021-
1022-
newkey->partopcintype = (Oid *) palloc(n * sizeof(Oid));
1023-
memcpy(newkey->partopcintype, fromkey->partopcintype, n * sizeof(Oid));
1024-
1025-
newkey->partsupfunc = (FmgrInfo *) palloc(n * sizeof(FmgrInfo));
1026-
memcpy(newkey->partsupfunc, fromkey->partsupfunc, n * sizeof(FmgrInfo));
1027-
1028-
newkey->partcollation = (Oid *) palloc(n * sizeof(Oid));
1029-
memcpy(newkey->partcollation, fromkey->partcollation, n * sizeof(Oid));
1030-
1031-
newkey->parttypid = (Oid *) palloc(n * sizeof(Oid));
1032-
memcpy(newkey->parttypid, fromkey->parttypid, n * sizeof(Oid));
1033-
1034-
newkey->parttypmod = (int32 *) palloc(n * sizeof(int32));
1035-
memcpy(newkey->parttypmod, fromkey->parttypmod, n * sizeof(int32));
1036-
1037-
newkey->parttyplen = (int16 *) palloc(n * sizeof(int16));
1038-
memcpy(newkey->parttyplen, fromkey->parttyplen, n * sizeof(int16));
1039-
1040-
newkey->parttypbyval = (bool *) palloc(n * sizeof(bool));
1041-
memcpy(newkey->parttypbyval, fromkey->parttypbyval, n * sizeof(bool));
1042-
1043-
newkey->parttypalign = (char *) palloc(n * sizeof(bool));
1044-
memcpy(newkey->parttypalign, fromkey->parttypalign, n * sizeof(char));
1045-
1046-
newkey->parttypcoll = (Oid *) palloc(n * sizeof(Oid));
1047-
memcpy(newkey->parttypcoll, fromkey->parttypcoll, n * sizeof(Oid));
1048-
1049-
return newkey;
1050-
}
1051-
1052999
/*
10531000
* equalRuleLocks
10541001
*

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