rd_partkeycxt-pg10.patch

text/plain

Filename: rd_partkeycxt-pg10.patch
Type: text/plain
Part: 1
Message: Re: hyrax vs. RelationBuildPartitionDesc

Patch

Format: unified
File+
src/backend/catalog/partition.c 6 1
src/backend/utils/cache/relcache.c 9 2
src/include/utils/rel.h 3 0
diff --git a/src/backend/catalog/partition.c b/src/backend/catalog/partition.c
index 8219d05d83..a13e1185b0 100644
--- a/src/backend/catalog/partition.c
+++ b/src/backend/catalog/partition.c
@@ -1883,7 +1883,12 @@ generate_partition_qual(Relation rel)
 		elog(ERROR, "unexpected whole-row reference found in partition key");
 
 	/* Save a copy in the relcache */
-	oldcxt = MemoryContextSwitchTo(CacheMemoryContext);
+	rel->rd_partcheckcxt = AllocSetContextCreate(CacheMemoryContext,
+												 "partition constraint",
+												 ALLOCSET_DEFAULT_MINSIZE,
+												 ALLOCSET_DEFAULT_INITSIZE,
+												 ALLOCSET_DEFAULT_MAXSIZE);
+	oldcxt = MemoryContextSwitchTo(rel->rd_partcheckcxt);
 	rel->rd_partcheck = copyObject(result);
 	MemoryContextSwitchTo(oldcxt);
 
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index e2be8ea28c..f44050b89b 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -1365,6 +1365,12 @@ RelationBuildDesc(Oid targetRelId, bool insertIt)
 	/* It's fully valid */
 	relation->rd_isvalid = true;
 
+	/*
+	 * Initialized by generate_partition_qual() if it's ever called in this
+	 * session.
+	 */
+	relation->rd_partcheckcxt = NULL;
+
 	return relation;
 }
 
@@ -2403,8 +2409,8 @@ RelationDestroyRelation(Relation relation, bool remember_tupdesc)
 		MemoryContextDelete(relation->rd_partkeycxt);
 	if (relation->rd_pdcxt)
 		MemoryContextDelete(relation->rd_pdcxt);
-	if (relation->rd_partcheck)
-		pfree(relation->rd_partcheck);
+	if (relation->rd_partcheckcxt)
+		MemoryContextDelete(relation->rd_partcheckcxt);
 	if (relation->rd_fdwroutine)
 		pfree(relation->rd_fdwroutine);
 	pfree(relation);
@@ -5674,6 +5680,7 @@ load_relcache_init_file(bool shared)
 		rel->rd_partkey = NULL;
 		rel->rd_pdcxt = NULL;
 		rel->rd_partdesc = NULL;
+		rel->rd_partcheckcxt = NULL;
 		rel->rd_partcheck = NIL;
 		rel->rd_indexprs = NIL;
 		rel->rd_indpred = NIL;
diff --git a/src/include/utils/rel.h b/src/include/utils/rel.h
index 4bc61e5380..eea0403ed7 100644
--- a/src/include/utils/rel.h
+++ b/src/include/utils/rel.h
@@ -216,6 +216,9 @@ typedef struct RelationData
 
 	/* use "struct" here to avoid needing to include pgstat.h: */
 	struct PgStat_TableStatus *pgstat_info; /* statistics collection area */
+
+	/* Memory context to hold the content of rd_partcheck. */
+	MemoryContext	rd_partcheckcxt;
 } RelationData;