From cc592c48c58d9c1920f8e2063756dcbcce79e4dd Mon Sep 17 00:00:00 2001 From: Robert Haas Date: Sat, 30 Jan 2016 10:32:38 -0500 Subject: postgres_fdw: More preliminary refactoring for upcoming join pushdown. The code that generates a complete SQL query for a given foreign relation was repeated in two places, and they didn't quite agree: the EXPLAIN case left out the locking clause. Centralize the code so we get the same behavior everywhere, and adjust calling conventions and which functions are static vs. extern accordingly . Centralize the code so we get the same behavior everywhere, and adjust calling conventions and which functions are static vs. extern accordingly. Ashutosh Bapat, reviewed and slightly adjusted by me. --- contrib/postgres_fdw/postgres_fdw.c | 38 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 24 deletions(-) (limited to 'contrib/postgres_fdw/postgres_fdw.c') diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c index 0aa7fbeac04..2ab85f68a75 100644 --- a/contrib/postgres_fdw/postgres_fdw.c +++ b/contrib/postgres_fdw/postgres_fdw.c @@ -1003,19 +1003,9 @@ postgresGetForeignPlan(PlannerInfo *root, * expressions to be sent as parameters. */ initStringInfo(&sql); - deparseSelectSql(&sql, root, baserel, fpinfo->attrs_used, - &retrieved_attrs); - if (remote_conds) - appendWhereClause(&sql, root, baserel, remote_conds, - true, ¶ms_list); - - /* Add ORDER BY clause if we found any useful pathkeys */ - if (best_path->path.pathkeys) - appendOrderByClause(&sql, root, baserel, best_path->path.pathkeys); - - /* Add any necessary FOR UPDATE/SHARE. */ - deparseLockingClause(&sql, root, baserel); - + deparseSelectStmtForRel(&sql, root, baserel, remote_conds, + best_path->path.pathkeys, &retrieved_attrs, + ¶ms_list); /* * Build the fdw_private list that will be available to the executor. * Items in the list must match enum FdwScanPrivateIndex, above. @@ -1909,6 +1899,7 @@ estimate_path_cost_size(PlannerInfo *root, PGconn *conn; Selectivity local_sel; QualCost local_cost; + List *remote_conds; /* * join_conds might contain both clauses that are safe to send across, @@ -1917,6 +1908,14 @@ estimate_path_cost_size(PlannerInfo *root, classifyConditions(root, baserel, join_conds, &remote_join_conds, &local_join_conds); + /* + * The complete list of remote conditions includes everything from + * baserestrictinfo plus any extra join_conds relevant to this + * particular path. + */ + remote_conds = list_concat(list_copy(remote_join_conds), + fpinfo->remote_conds); + /* * Construct EXPLAIN query including the desired SELECT, FROM, and * WHERE clauses. Params and other-relation Vars are replaced by @@ -1924,17 +1923,8 @@ estimate_path_cost_size(PlannerInfo *root, */ initStringInfo(&sql); appendStringInfoString(&sql, "EXPLAIN "); - deparseSelectSql(&sql, root, baserel, fpinfo->attrs_used, - &retrieved_attrs); - if (fpinfo->remote_conds) - appendWhereClause(&sql, root, baserel, fpinfo->remote_conds, - true, NULL); - if (remote_join_conds) - appendWhereClause(&sql, root, baserel, remote_join_conds, - (fpinfo->remote_conds == NIL), NULL); - - if (pathkeys) - appendOrderByClause(&sql, root, baserel, pathkeys); + deparseSelectStmtForRel(&sql, root, baserel, remote_conds, pathkeys, + &retrieved_attrs, NULL); /* Get the remote estimate */ conn = GetConnection(fpinfo->user, false); -- cgit v1.2.3