summaryrefslogtreecommitdiff
path: root/src/backend/utils/cache/partcache.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/utils/cache/partcache.c')
-rw-r--r--src/backend/utils/cache/partcache.c32
1 files changed, 26 insertions, 6 deletions
diff --git a/src/backend/utils/cache/partcache.c b/src/backend/utils/cache/partcache.c
index 342ab4dbaa6..e2144c83aba 100644
--- a/src/backend/utils/cache/partcache.c
+++ b/src/backend/utils/cache/partcache.c
@@ -37,9 +37,32 @@
#include "utils/syscache.h"
+static void RelationBuildPartitionKey(Relation relation);
static List *generate_partition_qual(Relation rel);
/*
+ * RelationGetPartitionKey -- get partition key, if relation is partitioned
+ *
+ * Note: partition keys are not allowed to change after the partitioned rel
+ * is created. RelationClearRelation knows this and preserves rd_partkey
+ * across relcache rebuilds, as long as the relation is open. Therefore,
+ * even though we hand back a direct pointer into the relcache entry, it's
+ * safe for callers to continue to use that pointer as long as they hold
+ * the relation open.
+ */
+PartitionKey
+RelationGetPartitionKey(Relation rel)
+{
+ if (rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
+ return NULL;
+
+ if (unlikely(rel->rd_partkey == NULL))
+ RelationBuildPartitionKey(rel);
+
+ return rel->rd_partkey;
+}
+
+/*
* RelationBuildPartitionKey
* Build partition key data of relation, and attach to relcache
*
@@ -54,7 +77,7 @@ static List *generate_partition_qual(Relation rel);
* that some of our callees allocate memory on their own which would be leaked
* permanently.
*/
-void
+static void
RelationBuildPartitionKey(Relation relation)
{
Form_pg_partitioned_table form;
@@ -74,12 +97,9 @@ RelationBuildPartitionKey(Relation relation)
tuple = SearchSysCache1(PARTRELID,
ObjectIdGetDatum(RelationGetRelid(relation)));
- /*
- * The following happens when we have created our pg_class entry but not
- * the pg_partitioned_table entry yet.
- */
if (!HeapTupleIsValid(tuple))
- return;
+ elog(ERROR, "cache lookup failed for partition key of relation %u",
+ RelationGetRelid(relation));
partkeycxt = AllocSetContextCreate(CurTransactionContext,
"partition key",