From 8e454f1aae54317d1d346012c06179ba62fbef47 Mon Sep 17 00:00:00 2001
From: Tomas Vondra <tomas.vondra@postgresql.org>
Date: Mon, 13 Dec 2021 02:28:27 +0100
Subject: [PATCH 2/5] simplification

---
 src/backend/utils/adt/selfuncs.c | 46 +++++++++++++++++---------------
 1 file changed, 24 insertions(+), 22 deletions(-)

diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index 59038895ec..1031a0fd9e 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -260,7 +260,7 @@ eqsel_internal(PG_FUNCTION_ARGS, bool negate)
 	/*
 	 * It it's (variable = variable) with the same variable on both sides, it's
 	 * a special case and we know it's not expected to filter anything, so we
-	 * estimate the selectivity as 1.0 (or 0.0 if it's negated).
+	 * estimate the selectivity as 1.0 (or 0.0 when it's negated).
 	 */
 	if (matching_restriction_variables(root, args, varRelid))
 		return (negate) ? 0.0 : 1.0;
@@ -4896,11 +4896,11 @@ get_restriction_variable(PlannerInfo *root, List *args, int varRelid,
 	return false;
 }
 
-
 /*
  * matching_restriction_variable
- *		Examine the args of a restriction clause to see if it's of the
- *		form (variable op variable) with the same variable on both sides.
+ *		Check if the two arguments of a restriction clause refer to the same
+ *		variable, i.e. if the condition is of the form (variable op variable).
+ *		We can deduce selectivity for such (in)equality clauses.
  *
  * Inputs:
  *	root: the planner info
@@ -4914,9 +4914,7 @@ matching_restriction_variables(PlannerInfo *root, List *args, int varRelid)
 {
 	Node	   *left,
 			   *right;
-	VariableStatData ldata;
-	VariableStatData rdata;
-	bool		res = false;
+	Var		   *lvar;
 
 	/* Fail if not a binary opclause (probably shouldn't happen) */
 	if (list_length(args) != 2)
@@ -4925,25 +4923,29 @@ matching_restriction_variables(PlannerInfo *root, List *args, int varRelid)
 	left = (Node *) linitial(args);
 	right = (Node *) lsecond(args);
 
-	/*
-	 * Examine both sides.  Note that when varRelid is nonzero, Vars of other
-	 * relations will be treated as pseudoconstants.
-	 */
-	examine_variable(root, left, varRelid, &ldata);
-	examine_variable(root, right, varRelid, &rdata);
+	/* Look inside any binary-compatible relabeling */
+
+	if (IsA(left, RelabelType))
+		left = (Node *) ((RelabelType *) left)->arg;
+
+	if (IsA(right, RelabelType))
+		right = (Node *) ((RelabelType *) right)->arg;
+
+	/* We only care about simple Vars from our relation for now */
+	if (!IsA(left, Var) || !IsA(right, Var))
+		return false;
+
+	lvar = (Var *) left;
 
 	/*
-	 * If both sides are variable, and are equal, we win.
+	 * We only check one variable, because we call equals() later, which checks
+	 * the other variable automatically.
 	 */
-	if ((ldata.rel != NULL && rdata.rel != NULL) &&
-		equal(ldata.var, rdata.var))
-		res = true;
-
-	/* We don't need the stats. */
-	ReleaseVariableStats(ldata);
-	ReleaseVariableStats(rdata);
+	if ((varRelid != 0) && (varRelid != lvar->varno))
+		return false;
 
-	return res;
+	/* The two variables need to match */
+	return equal(left, right);
 }
 
 /*
-- 
2.31.1

