From 24ad5df52200226ceea449388792802397e1b94a Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
Date: Mon, 28 Nov 2022 16:36:33 +0100
Subject: [PATCH v28 2/8] rename GetRelAllUpdatedCols ->
 get_rel_all_updated_cols

---
 contrib/postgres_fdw/postgres_fdw.c  |   2 +-
 portlock/53981.rsv                   |   1 -
 src/backend/optimizer/util/inherit.c | 106 +++++++++++++--------------
 src/include/optimizer/inherit.h      |   3 +-
 4 files changed, 55 insertions(+), 57 deletions(-)
 delete mode 100644 portlock/53981.rsv

diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index ae2c11dc79..c0dc485301 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -1812,7 +1812,7 @@ postgresPlanForeignModify(PlannerInfo *root,
 	{
 		int			col;
 		RelOptInfo *rel = find_base_rel(root, resultRelation);
-		Bitmapset  *allUpdatedCols = GetRelAllUpdatedCols(root, rel);
+		Bitmapset  *allUpdatedCols = get_rel_all_updated_cols(root, rel);
 
 		col = -1;
 		while ((col = bms_next_member(allUpdatedCols, col)) >= 0)
diff --git a/portlock/53981.rsv b/portlock/53981.rsv
deleted file mode 100644
index 8a1c26310e..0000000000
--- a/portlock/53981.rsv
+++ /dev/null
@@ -1 +0,0 @@
-    102212
diff --git a/src/backend/optimizer/util/inherit.c b/src/backend/optimizer/util/inherit.c
index 09e2ffdfbc..d8db04da73 100644
--- a/src/backend/optimizer/util/inherit.c
+++ b/src/backend/optimizer/util/inherit.c
@@ -49,6 +49,10 @@ static void expand_single_inheritance_child(PlannerInfo *root,
 											Index *childRTindex_p);
 static Bitmapset *translate_col_privs(const Bitmapset *parent_privs,
 									  List *translated_vars);
+static Bitmapset *translate_col_privs_multilevel(PlannerInfo *root,
+												 RelOptInfo *rel,
+												 RelOptInfo *top_parent_rel,
+												 Bitmapset *top_parent_cols);
 static void expand_appendrel_subquery(PlannerInfo *root, RelOptInfo *rel,
 									  RangeTblEntry *rte, Index rti);
 
@@ -650,6 +654,54 @@ expand_single_inheritance_child(PlannerInfo *root, RangeTblEntry *parentrte,
 	}
 }
 
+/*
+ * get_rel_all_updated_cols
+ * 		Returns the set of columns of a given "simple" relation that are
+ * 		updated by this query.
+ */
+Bitmapset *
+get_rel_all_updated_cols(PlannerInfo *root, RelOptInfo *rel)
+{
+	Index	relid;
+	RangeTblEntry *rte;
+	RTEPermissionInfo *perminfo;
+	Bitmapset *updatedCols,
+			  *extraUpdatedCols;
+
+	Assert(root->parse->commandType == CMD_UPDATE);
+	Assert(IS_SIMPLE_REL(rel));
+
+	/*
+	 * We obtain updatedCols and extraUpdatedCols for the query's result
+	 * relation.  Then, if necessary, we map it to the column numbers of
+	 * the relation for which they were requested.
+	 */
+	relid = root->parse->resultRelation;
+	rte = planner_rt_fetch(relid, root);
+	perminfo = GetRTEPermissionInfo(root->parse->rtepermlist, rte);
+
+	updatedCols = perminfo->updatedCols;
+	extraUpdatedCols = rte->extraUpdatedCols;
+
+	/*
+	 * For "other" rels, we must look up the root parent relation mentioned in
+	 * the query, and translate the column numbers.
+	 */
+	if (rel->relid != relid)
+	{
+		RelOptInfo *top_parent_rel = find_base_rel(root, relid);
+
+		Assert(IS_OTHER_REL(rel));
+
+		updatedCols = translate_col_privs_multilevel(root, rel, top_parent_rel,
+													 updatedCols);
+		extraUpdatedCols = translate_col_privs_multilevel(root, rel, top_parent_rel,
+														  extraUpdatedCols);
+	}
+
+	return bms_union(updatedCols, extraUpdatedCols);
+}
+
 /*
  * translate_col_privs
  *	  Translate a bitmapset representing per-column privileges from the
@@ -905,57 +957,3 @@ translate_col_privs_multilevel(PlannerInfo *root, RelOptInfo *rel,
 
 	return result;
 }
-
-/*
- * GetRelAllUpdatedCols
- * 		Returns the set of columns of a given "simple" relation that are updated
- * 		by this query
- */
-Bitmapset *
-GetRelAllUpdatedCols(PlannerInfo *root, RelOptInfo *rel)
-{
-	Index	use_relid;
-	RangeTblEntry *rte;
-	RTEPermissionInfo *perminfo;
-	Bitmapset *updatedCols,
-			  *extraUpdatedCols;
-
-	Assert(root->parse->commandType == CMD_UPDATE);
-
-	if (!IS_SIMPLE_REL(rel))
-		return NULL;
-
-	/*
-	 * We need to get the updatedCols bitmapset from the relation's
-	 * RTEPermissionInfo.
-	 *
-	 * If it's a simple "base" rel, can just fetch its RTEPermissionInfo that
-	 * must always be present; this cannot get called on non-RELATION RTEs.
-	 *
-	 * For "other" rels, must look up the root parent relation mentioned in the
-	 * query, because only that one gets assigned a RTEPermissionInfo, and
-	 * translate the columns found therein to match the given relation.
-	 */
-	use_relid = rel->top_parent_relids == NULL ? rel->relid :
-		bms_singleton_member(rel->top_parent_relids);
-	Assert(use_relid == root->parse->resultRelation);
-	rte =  planner_rt_fetch(use_relid, root);
-	perminfo = GetRTEPermissionInfo(root->parse->rtepermlist, rte);
-
-	if (use_relid != rel->relid)
-	{
-		RelOptInfo *top_parent_rel = find_base_rel(root, use_relid);
-
-		updatedCols = translate_col_privs_multilevel(root, rel, top_parent_rel,
-													 perminfo->updatedCols);
-		extraUpdatedCols = translate_col_privs_multilevel(root, rel, top_parent_rel,
-														  rte->extraUpdatedCols);
-	}
-	else
-	{
-		updatedCols = perminfo->updatedCols;
-		extraUpdatedCols = rte->extraUpdatedCols;
-	}
-
-	return bms_union(updatedCols, extraUpdatedCols);
-}
diff --git a/src/include/optimizer/inherit.h b/src/include/optimizer/inherit.h
index 9a4f86920c..8ebd42b757 100644
--- a/src/include/optimizer/inherit.h
+++ b/src/include/optimizer/inherit.h
@@ -20,9 +20,10 @@
 extern void expand_inherited_rtentry(PlannerInfo *root, RelOptInfo *rel,
 									 RangeTblEntry *rte, Index rti);
 
+extern Bitmapset *get_rel_all_updated_cols(PlannerInfo *root, RelOptInfo *rel);
+
 extern bool apply_child_basequals(PlannerInfo *root, RelOptInfo *parentrel,
 								  RelOptInfo *childrel, RangeTblEntry *childRTE,
 								  AppendRelInfo *appinfo);
-extern Bitmapset *GetRelAllUpdatedCols(PlannerInfo *root, RelOptInfo *rel);
 
 #endif							/* INHERIT_H */
-- 
2.30.2

