From 04cfdd4d7b442256b31387a7536cb9d18d064fe8 Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <daniel@yesql.se>
Date: Thu, 14 Jun 2018 21:17:33 +0200
Subject: [PATCH 1/2] Use optimized BMS function for testing membership

When all we need to know is if the Bitmapset has zero, one or many
members bms_membership() is faster than obtaining an exact count of
the members with bms_num_members().
---
 contrib/postgres_fdw/deparse.c        | 6 +++---
 src/backend/statistics/dependencies.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/contrib/postgres_fdw/deparse.c b/contrib/postgres_fdw/deparse.c
index d272719ff4..8068e28184 100644
--- a/contrib/postgres_fdw/deparse.c
+++ b/contrib/postgres_fdw/deparse.c
@@ -1076,7 +1076,7 @@ deparseFromExpr(List *quals, deparse_expr_cxt *context)
 	/* Construct FROM clause */
 	appendStringInfoString(buf, " FROM ");
 	deparseFromExprForRel(buf, context->root, scanrel,
-						  (bms_num_members(scanrel->relids) > 1),
+						  (bms_membership(scanrel->relids) == BMS_MULTIPLE),
 						  (Index) 0, NULL, context->params_list);
 
 	/* Construct WHERE clause */
@@ -1262,7 +1262,7 @@ deparseLockingClause(deparse_expr_cxt *context)
 				}
 
 				/* Add the relation alias if we are here for a join relation */
-				if (bms_num_members(rel->relids) > 1 &&
+				if (bms_membership(rel->relids) == BMS_MULTIPLE &&
 					rc->strength != LCS_NONE)
 					appendStringInfo(buf, " OF %s%d", REL_ALIAS_PREFIX, relid);
 			}
@@ -2328,7 +2328,7 @@ deparseVar(Var *node, deparse_expr_cxt *context)
 	int			colno;
 
 	/* Qualify columns when multiple relations are involved. */
-	bool		qualify_col = (bms_num_members(relids) > 1);
+	bool		qualify_col = (bms_membership(relids) == BMS_MULTIPLE);
 
 	/*
 	 * If the Var belongs to the foreign relation that is deparsed as a
diff --git a/src/backend/statistics/dependencies.c b/src/backend/statistics/dependencies.c
index 3b4a09b1a7..1cc537b5b0 100644
--- a/src/backend/statistics/dependencies.c
+++ b/src/backend/statistics/dependencies.c
@@ -992,7 +992,7 @@ dependencies_clauselist_selectivity(PlannerInfo *root,
 	 * of clauses. We must return 1.0 so the calling function's selectivity is
 	 * unaffected.
 	 */
-	if (bms_num_members(clauses_attnums) < 2)
+	if (bms_membership(clauses_attnums) != BMS_MULTIPLE)
 	{
 		pfree(list_attnums);
 		return 1.0;
-- 
2.14.1.145.gb3622a4ee

