@@ -141,6 +141,11 @@ static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod,
141141 deparse_expr_cxt * context );
142142static void printRemotePlaceholder (Oid paramtype , int32 paramtypmod ,
143143 deparse_expr_cxt * context );
144+ static void deparseSelectSql (Bitmapset * attrs_used , List * * retrieved_attrs ,
145+ deparse_expr_cxt * context );
146+ static void deparseLockingClause (deparse_expr_cxt * context );
147+ static void appendWhereClause (List * exprs , deparse_expr_cxt * context );
148+ static void appendOrderByClause (List * pathkeys , deparse_expr_cxt * context );
144149
145150
146151/*
@@ -697,6 +702,51 @@ deparse_type_name(Oid type_oid, int32 typemod)
697702 return format_type_with_typemod_qualified (type_oid , typemod );
698703}
699704
705+ /*
706+ * Deparse SELECT statement for given relation into buf.
707+ *
708+ * remote_conds is the list of conditions to be deparsed as WHERE clause.
709+ *
710+ * pathkeys is the list of pathkeys to order the result by.
711+ *
712+ * List of columns selected is returned in retrieved_attrs.
713+ *
714+ * If params_list is not NULL, it receives a list of Params and other-relation
715+ * Vars used in the clauses; these values must be transmitted to the remote
716+ * server as parameter values.
717+ *
718+ * If params_list is NULL, we're generating the query for EXPLAIN purposes,
719+ * so Params and other-relation Vars should be replaced by dummy values.
720+ */
721+ extern void
722+ deparseSelectStmtForRel (StringInfo buf , PlannerInfo * root , RelOptInfo * rel ,
723+ List * remote_conds , List * pathkeys ,
724+ List * * retrieved_attrs , List * * params_list )
725+ {
726+ PgFdwRelationInfo * fpinfo = (PgFdwRelationInfo * ) rel -> fdw_private ;
727+ deparse_expr_cxt context ;
728+
729+ /* Initialize params_list if caller needs one */
730+ if (params_list )
731+ * params_list = NIL ;
732+
733+ context .buf = buf ;
734+ context .root = root ;
735+ context .foreignrel = rel ;
736+ context .params_list = params_list ;
737+
738+ deparseSelectSql (fpinfo -> attrs_used , retrieved_attrs , & context );
739+
740+ if (remote_conds )
741+ appendWhereClause (remote_conds , & context );
742+
743+ /* Add ORDER BY clause if we found any useful pathkeys */
744+ if (pathkeys )
745+ appendOrderByClause (pathkeys , & context );
746+
747+ /* Add any necessary FOR UPDATE/SHARE. */
748+ deparseLockingClause (& context );
749+ }
700750
701751/*
702752 * Construct a simple SELECT statement that retrieves desired columns
@@ -707,13 +757,13 @@ deparse_type_name(Oid type_oid, int32 typemod)
707757 * returned to *retrieved_attrs.
708758 */
709759void
710- deparseSelectSql (StringInfo buf ,
711- PlannerInfo * root ,
712- RelOptInfo * baserel ,
713- Bitmapset * attrs_used ,
714- List * * retrieved_attrs )
760+ deparseSelectSql (Bitmapset * attrs_used , List * * retrieved_attrs ,
761+ deparse_expr_cxt * context )
715762{
716- RangeTblEntry * rte = planner_rt_fetch (baserel -> relid , root );
763+ StringInfo buf = context -> buf ;
764+ RelOptInfo * foreignrel = context -> foreignrel ;
765+ PlannerInfo * root = context -> root ;
766+ RangeTblEntry * rte = planner_rt_fetch (foreignrel -> relid , root );
717767 Relation rel ;
718768
719769 /*
@@ -726,7 +776,7 @@ deparseSelectSql(StringInfo buf,
726776 * Construct SELECT list
727777 */
728778 appendStringInfoString (buf , "SELECT " );
729- deparseTargetList (buf , root , baserel -> relid , rel , attrs_used ,
779+ deparseTargetList (buf , root , foreignrel -> relid , rel , attrs_used ,
730780 retrieved_attrs );
731781
732782 /*
@@ -811,11 +861,15 @@ deparseTargetList(StringInfo buf,
811861
812862/*
813863 * Deparse the appropriate locking clause (FOR SELECT or FOR SHARE) for a
814- * given relation.
864+ * given relation (context->foreignrel) .
815865 */
816- void
817- deparseLockingClause (StringInfo buf , PlannerInfo * root , RelOptInfo * rel )
866+ static void
867+ deparseLockingClause (deparse_expr_cxt * context )
818868{
869+ StringInfo buf = context -> buf ;
870+ PlannerInfo * root = context -> root ;
871+ RelOptInfo * rel = context -> foreignrel ;
872+
819873 /*
820874 * Add FOR UPDATE/SHARE if appropriate. We apply locking during the
821875 * initial row fetch, rather than later on as is done for local tables.
@@ -868,39 +922,16 @@ deparseLockingClause(StringInfo buf, PlannerInfo *root, RelOptInfo *rel)
868922}
869923
870924/*
871- * Deparse WHERE clauses in given list of RestrictInfos and append them to buf.
872- *
873- * baserel is the foreign table we're planning for.
874- *
875- * If no WHERE clause already exists in the buffer, is_first should be true.
876- *
877- * If params is not NULL, it receives a list of Params and other-relation Vars
878- * used in the clauses; these values must be transmitted to the remote server
879- * as parameter values.
880- *
881- * If params is NULL, we're generating the query for EXPLAIN purposes,
882- * so Params and other-relation Vars should be replaced by dummy values.
925+ * Deparse WHERE clauses in given list of RestrictInfos and append them to
926+ * context->buf.
883927 */
884- void
885- appendWhereClause (StringInfo buf ,
886- PlannerInfo * root ,
887- RelOptInfo * baserel ,
888- List * exprs ,
889- bool is_first ,
890- List * * params )
928+ static void
929+ appendWhereClause (List * exprs , deparse_expr_cxt * context )
891930{
892- deparse_expr_cxt context ;
893931 int nestlevel ;
894932 ListCell * lc ;
895-
896- if (params )
897- * params = NIL ; /* initialize result list to empty */
898-
899- /* Set up context struct for recursion */
900- context .root = root ;
901- context .foreignrel = baserel ;
902- context .buf = buf ;
903- context .params_list = params ;
933+ bool is_first = true;
934+ StringInfo buf = context -> buf ;
904935
905936 /* Make sure any constants in the exprs are printed portably */
906937 nestlevel = set_transmission_modes ();
@@ -916,7 +947,7 @@ appendWhereClause(StringInfo buf,
916947 appendStringInfoString (buf , " AND " );
917948
918949 appendStringInfoChar (buf , '(' );
919- deparseExpr (ri -> clause , & context );
950+ deparseExpr (ri -> clause , context );
920951 appendStringInfoChar (buf , ')' );
921952
922953 is_first = false;
@@ -1946,20 +1977,14 @@ printRemotePlaceholder(Oid paramtype, int32 paramtypmod,
19461977 * relation. From given pathkeys expressions belonging entirely to the given
19471978 * base relation are obtained and deparsed.
19481979 */
1949- void
1950- appendOrderByClause (StringInfo buf , PlannerInfo * root , RelOptInfo * baserel ,
1951- List * pathkeys )
1980+ static void
1981+ appendOrderByClause (List * pathkeys , deparse_expr_cxt * context )
19521982{
19531983 ListCell * lcell ;
1954- deparse_expr_cxt context ;
19551984 int nestlevel ;
19561985 char * delim = " " ;
1957-
1958- /* Set up context struct for recursion */
1959- context .root = root ;
1960- context .foreignrel = baserel ;
1961- context .buf = buf ;
1962- context .params_list = NULL ;
1986+ RelOptInfo * baserel = context -> foreignrel ;
1987+ StringInfo buf = context -> buf ;
19631988
19641989 /* Make sure any constants in the exprs are printed portably */
19651990 nestlevel = set_transmission_modes ();
@@ -1974,7 +1999,7 @@ appendOrderByClause(StringInfo buf, PlannerInfo *root, RelOptInfo *baserel,
19741999 Assert (em_expr != NULL );
19752000
19762001 appendStringInfoString (buf , delim );
1977- deparseExpr (em_expr , & context );
2002+ deparseExpr (em_expr , context );
19782003 if (pathkey -> pk_strategy == BTLessStrategyNumber )
19792004 appendStringInfoString (buf , " ASC" );
19802005 else
0 commit comments