From 34f8ee973742d6a72840184c15aff7718022fae7 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Sat, 14 Jan 2006 00:14:12 +0000
Subject: [PATCH] Add selectivity-calculation code for RowCompareExpr nodes. 
 Simplistic, but a lot better than nothing at all ...

---
 src/backend/optimizer/path/clausesel.c | 10 ++++-
 src/backend/utils/adt/selfuncs.c       | 61 +++++++++++++++++++++++++-
 src/include/utils/selfuncs.h           |  5 ++-
 3 files changed, 73 insertions(+), 3 deletions(-)

diff --git a/src/backend/optimizer/path/clausesel.c b/src/backend/optimizer/path/clausesel.c
index eba6bf1d148..d3e28dc9650 100644
--- a/src/backend/optimizer/path/clausesel.c
+++ b/src/backend/optimizer/path/clausesel.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/optimizer/path/clausesel.c,v 1.76 2005/11/25 19:47:49 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/optimizer/path/clausesel.c,v 1.77 2006/01/14 00:14:11 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -663,6 +663,14 @@ clause_selectivity(PlannerInfo *root,
 							varRelid,
 							jointype);
 	}
+	else if (IsA(clause, RowCompareExpr))
+	{
+		/* Use node specific selectivity calculation function */
+		s1 = rowcomparesel(root,
+						   (RowCompareExpr *) clause,
+						   varRelid,
+						   jointype);
+	}
 	else if (IsA(clause, NullTest))
 	{
 		/* Use node specific selectivity calculation function */
diff --git a/src/backend/utils/adt/selfuncs.c b/src/backend/utils/adt/selfuncs.c
index 20ae7ddc1c4..336c1deaeaa 100644
--- a/src/backend/utils/adt/selfuncs.c
+++ b/src/backend/utils/adt/selfuncs.c
@@ -15,7 +15,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.195 2006/01/10 17:35:52 tgl Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/adt/selfuncs.c,v 1.196 2006/01/14 00:14:11 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1566,6 +1566,65 @@ scalararraysel(PlannerInfo *root,
 	return s1;
 }
 
+/*
+ *		rowcomparesel		- Selectivity of RowCompareExpr Node.
+ *
+ * We estimate RowCompare selectivity by considering just the first (high
+ * order) columns, which makes it equivalent to an ordinary OpExpr.  While
+ * this estimate could be refined by considering additional columns, it
+ * seems unlikely that we could do a lot better without multi-column
+ * statistics.
+ */
+Selectivity
+rowcomparesel(PlannerInfo *root,
+			  RowCompareExpr *clause,
+			  int varRelid, JoinType jointype)
+{
+	Selectivity s1;
+	Oid			opno = linitial_oid(clause->opnos);
+	List	   *opargs;
+	bool		is_join_clause;
+
+	/* Build equivalent arg list for single operator */
+	opargs = list_make2(linitial(clause->largs), linitial(clause->rargs));
+
+	/* Decide if it's a join clause, same as for OpExpr */
+	if (varRelid != 0)
+	{
+		/*
+		 * If we are considering a nestloop join then all clauses are
+		 * restriction clauses, since we are only interested in the one
+		 * relation.
+		 */
+		is_join_clause = false;
+	}
+	else
+	{
+		/*
+		 * Otherwise, it's a join if there's more than one relation used.
+		 * Notice we ignore the low-order columns here.
+		 */
+		is_join_clause = (NumRelids((Node *) opargs) > 1);
+	}
+
+	if (is_join_clause)
+	{
+		/* Estimate selectivity for a join clause. */
+		s1 = join_selectivity(root, opno,
+							  opargs,
+							  jointype);
+	}
+	else
+	{
+		/* Estimate selectivity for a restriction clause. */
+		s1 = restriction_selectivity(root, opno,
+									 opargs,
+									 varRelid);
+	}
+
+	return s1;
+}
+
 /*
  *		eqjoinsel		- Join selectivity of "="
  */
diff --git a/src/include/utils/selfuncs.h b/src/include/utils/selfuncs.h
index 2c021ad2502..541b58e5467 100644
--- a/src/include/utils/selfuncs.h
+++ b/src/include/utils/selfuncs.h
@@ -8,7 +8,7 @@
  * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/utils/selfuncs.h,v 1.26 2005/11/25 19:47:50 tgl Exp $
+ * $PostgreSQL: pgsql/src/include/utils/selfuncs.h,v 1.27 2006/01/14 00:14:12 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -116,6 +116,9 @@ extern Selectivity scalararraysel(PlannerInfo *root,
 								  ScalarArrayOpExpr *clause,
 								  bool is_join_clause,
 								  int varRelid, JoinType jointype);
+extern Selectivity rowcomparesel(PlannerInfo *root,
+								 RowCompareExpr *clause,
+								 int varRelid, JoinType jointype);
 
 extern void mergejoinscansel(PlannerInfo *root, Node *clause,
 				 Selectivity *leftscan,
-- 
GitLab