From 2cdb2bda31bca10c8f643e36cc6d76f3dd64163e Mon Sep 17 00:00:00 2001
From: Andrey Borodin <amborodin@acm.org>
Date: Thu, 25 Jun 2020 14:31:22 +0500
Subject: [PATCH 1/2] Avoid segfaults on 1.5-upgraded installations before
 10e6c71

---
 src/include/pathman.h    |  1 +
 src/init.c               |  9 ++++++---
 src/partition_creation.c | 12 ++++++------
 src/pl_funcs.c           | 12 ++++++++++--
 src/pl_range_funcs.c     |  4 ++--
 src/relation_info.c      |  4 ++--
 6 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/src/include/pathman.h b/src/include/pathman.h
index b9acfe59..dd6a789b 100644
--- a/src/include/pathman.h
+++ b/src/include/pathman.h
@@ -46,6 +46,7 @@
  */
 #define PATHMAN_CONFIG						"pathman_config"
 #define Natts_pathman_config				4
+#define Natts_pathman_config_historic		5
 #define Anum_pathman_config_partrel			1	/* partitioned relation (regclass) */
 #define Anum_pathman_config_expr			2	/* partition expression (original) */
 #define Anum_pathman_config_parttype		3	/* partitioning type (1|2) */
diff --git a/src/init.c b/src/init.c
index bd85c593..2df08474 100644
--- a/src/init.c
+++ b/src/init.c
@@ -648,6 +648,7 @@ pathman_config_contains_relation(Oid relid, Datum *values, bool *isnull,
 	Snapshot		snapshot;
 	HeapTuple		htup;
 	bool			contains_rel = false;
+	TupleDesc		tupleDescr;
 
 	ScanKeyInit(&key[0],
 				Anum_pathman_config_partrel,
@@ -656,13 +657,15 @@ pathman_config_contains_relation(Oid relid, Datum *values, bool *isnull,
 
 	/* Open PATHMAN_CONFIG with latest snapshot available */
 	rel = heap_open(get_pathman_config_relid(false), AccessShareLock);
+	tupleDescr = RelationGetDescr(rel);
 
 	/* Check that 'partrel' column is of regclass type */
-	Assert(TupleDescAttr(RelationGetDescr(rel),
+	Assert(TupleDescAttr(tupleDescr,
 				Anum_pathman_config_partrel - 1)->atttypid == REGCLASSOID);
 
 	/* Check that number of columns == Natts_pathman_config */
-	Assert(RelationGetDescr(rel)->natts == Natts_pathman_config);
+	Assert(tupleDescr->natts == Natts_pathman_config
+			|| tupleDescr->natts == Natts_pathman_config_historic);
 
 	snapshot = RegisterSnapshot(GetLatestSnapshot());
 #if PG_VERSION_NUM >= 120000
@@ -679,7 +682,7 @@ pathman_config_contains_relation(Oid relid, Datum *values, bool *isnull,
 		if (values && isnull)
 		{
 			htup = heap_copytuple(htup);
-			heap_deform_tuple(htup, RelationGetDescr(rel), values, isnull);
+			heap_deform_tuple(htup, tupleDescr, values, isnull);
 
 			/* Perform checks for non-NULL columns */
 			Assert(!isnull[Anum_pathman_config_partrel - 1]);
diff --git a/src/partition_creation.c b/src/partition_creation.c
index 3e578e70..1dddbd49 100644
--- a/src/partition_creation.c
+++ b/src/partition_creation.c
@@ -116,8 +116,8 @@ create_single_range_partition_internal(Oid parent_relid,
 	init_callback_params	callback_params;
 	List				   *trigger_columns = NIL;
 	Node				   *expr;
-	Datum		values[Natts_pathman_config];
-	bool		isnull[Natts_pathman_config];
+	Datum		values[Natts_pathman_config_historic];
+	bool		isnull[Natts_pathman_config_historic];
 
 
 	/*
@@ -353,8 +353,8 @@ Oid
 create_partitions_for_value_internal(Oid relid, Datum value, Oid value_type)
 {
 	Oid				partid = InvalidOid; /* last created partition (or InvalidOid) */
-	Datum				values[Natts_pathman_config];
-	bool				isnull[Natts_pathman_config];
+	Datum				values[Natts_pathman_config_historic];
+	bool				isnull[Natts_pathman_config_historic];
 
 	/* Get both PartRelationInfo & PATHMAN_CONFIG contents for this relation */
 	if (pathman_config_contains_relation(relid, values, isnull, NULL, NULL))
@@ -1914,8 +1914,8 @@ build_partitioning_expression(Oid parent_relid,
 							  List **columns)		/* ret val #2 */
 {
 	/* Values extracted from PATHMAN_CONFIG */
-	Datum		values[Natts_pathman_config];
-	bool		isnull[Natts_pathman_config];
+	Datum		values[Natts_pathman_config_historic];
+	bool		isnull[Natts_pathman_config_historic];
 	char	   *expr_cstr;
 	Node	   *expr;
 
diff --git a/src/pl_funcs.c b/src/pl_funcs.c
index ebf80861..068a4ac6 100644
--- a/src/pl_funcs.c
+++ b/src/pl_funcs.c
@@ -751,8 +751,8 @@ add_to_pathman_config(PG_FUNCTION_ARGS)
 	uint32				children_count;
 
 	Relation			pathman_config;
-	Datum				values[Natts_pathman_config];
-	bool				isnull[Natts_pathman_config];
+	Datum				values[Natts_pathman_config_historic];
+	bool				isnull[Natts_pathman_config_historic];
 	HeapTuple			htup;
 
 	Oid					expr_type;
@@ -851,6 +851,14 @@ add_to_pathman_config(PG_FUNCTION_ARGS)
 	values[Anum_pathman_config_expr - 1]		= CStringGetTextDatum(expression);
 	isnull[Anum_pathman_config_expr - 1]		= false;
 
+	/*
+	 * In case of 1.5 update before 10e6c71 there is acutlly 5 attributes in
+	 * pathman_config description (inclusing cooked expression). To avoid
+	 * potential problems we allocate 5th attribute and initialize it with null.
+	 */
+	values[Natts_pathman_config_historic - 1]	= (Datum) 0;
+	isnull[Natts_pathman_config_historic - 1]	= true;
+
 	/* Insert new row into PATHMAN_CONFIG */
 	pathman_config = heap_open(get_pathman_config_relid(false), RowExclusiveLock);
 
diff --git a/src/pl_range_funcs.c b/src/pl_range_funcs.c
index 27361dd3..3c6e65a7 100644
--- a/src/pl_range_funcs.c
+++ b/src/pl_range_funcs.c
@@ -110,8 +110,8 @@ create_single_range_partition_pl(PG_FUNCTION_ARGS)
 	RangeVar   *partition_name_rv;
 	char	   *tablespace;
 
-	Datum		values[Natts_pathman_config];
-	bool		isnull[Natts_pathman_config];
+	Datum		values[Natts_pathman_config_historic];
+	bool		isnull[Natts_pathman_config_historic];
 
 
 	/* Handle 'parent_relid' */
diff --git a/src/relation_info.c b/src/relation_info.c
index 0c79b504..d630563f 100644
--- a/src/relation_info.c
+++ b/src/relation_info.c
@@ -350,8 +350,8 @@ get_pathman_relation_info(Oid relid)
 	{
 		PartRelationInfo   *prel = NULL;
 		ItemPointerData		iptr;
-		Datum				values[Natts_pathman_config];
-		bool				isnull[Natts_pathman_config];
+		Datum				values[Natts_pathman_config_historic];
+		bool				isnull[Natts_pathman_config_historic];
 		bool				found;
 
 		/*

From 0660836e1a112e3aaef003d9f834ca7cb5b3e61f Mon Sep 17 00:00:00 2001
From: Ekaterina Sokolova <e.sokolova@postgrespro.ru>
Date: Mon, 1 Jul 2024 16:34:04 +0300
Subject: [PATCH 2/2] Correct typos in comments.

---
 src/pl_funcs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/pl_funcs.c b/src/pl_funcs.c
index 1db57ae9..029c4f75 100644
--- a/src/pl_funcs.c
+++ b/src/pl_funcs.c
@@ -896,8 +896,8 @@ add_to_pathman_config(PG_FUNCTION_ARGS)
 	isnull[Anum_pathman_config_expr - 1]		= false;
 
 	/*
-	 * In case of 1.5 update before 10e6c71 there is acutlly 5 attributes in
-	 * pathman_config description (inclusing cooked expression). To avoid
+	 * In case of 1.5 update before 10e6c71 there are actually 5 attributes in
+	 * pathman_config description (including cooked expression). To avoid
 	 * potential problems we allocate 5th attribute and initialize it with null.
 	 */
 	values[Natts_pathman_config_historic - 1]	= (Datum) 0;