Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcc592c4

Browse files
committed
postgres_fdw: More preliminary refactoring for upcoming join pushdown.
The code that generates a complete SQL query for a given foreign relationwas repeated in two places, and they didn't quite agree: the EXPLAIN caseleft out the locking clause. Centralize the code so we get the samebehavior everywhere, and adjust calling conventions and which functionsare static vs. extern accordingly . Centralize the code so we get the samebehavior everywhere, and adjust calling conventions and which functionsare static vs. extern accordingly.Ashutosh Bapat, reviewed and slightly adjusted by me.
1 parent2251179 commitcc592c4

File tree

3 files changed

+93
-90
lines changed

3 files changed

+93
-90
lines changed

‎contrib/postgres_fdw/deparse.c

Lines changed: 76 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ static void printRemoteParam(int paramindex, Oid paramtype, int32 paramtypmod,
141141
deparse_expr_cxt*context);
142142
staticvoidprintRemotePlaceholder(Oidparamtype,int32paramtypmod,
143143
deparse_expr_cxt*context);
144+
staticvoiddeparseSelectSql(Bitmapset*attrs_used,List**retrieved_attrs,
145+
deparse_expr_cxt*context);
146+
staticvoiddeparseLockingClause(deparse_expr_cxt*context);
147+
staticvoidappendWhereClause(List*exprs,deparse_expr_cxt*context);
148+
staticvoidappendOrderByClause(List*pathkeys,deparse_expr_cxt*context);
144149

145150

146151
/*
@@ -697,6 +702,51 @@ deparse_type_name(Oid type_oid, int32 typemod)
697702
returnformat_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+
externvoid
722+
deparseSelectStmtForRel(StringInfobuf,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_cxtcontext;
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
*/
709759
void
710-
deparseSelectSql(StringInfobuf,
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+
StringInfobuf=context->buf;
764+
RelOptInfo*foreignrel=context->foreignrel;
765+
PlannerInfo*root=context->root;
766+
RangeTblEntry*rte=planner_rt_fetch(foreignrel->relid,root);
717767
Relationrel;
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(StringInfobuf,PlannerInfo*root,RelOptInfo*rel)
866+
staticvoid
867+
deparseLockingClause(deparse_expr_cxt*context)
818868
{
869+
StringInfobuf=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(StringInfobuf,
886-
PlannerInfo*root,
887-
RelOptInfo*baserel,
888-
List*exprs,
889-
boolis_first,
890-
List**params)
928+
staticvoid
929+
appendWhereClause(List*exprs,deparse_expr_cxt*context)
891930
{
892-
deparse_expr_cxtcontext;
893931
intnestlevel;
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+
boolis_first= true;
934+
StringInfobuf=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(StringInfobuf,PlannerInfo*root,RelOptInfo*baserel,
1951-
List*pathkeys)
1980+
staticvoid
1981+
appendOrderByClause(List*pathkeys,deparse_expr_cxt*context)
19521982
{
19531983
ListCell*lcell;
1954-
deparse_expr_cxtcontext;
19551984
intnestlevel;
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+
StringInfobuf=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

‎contrib/postgres_fdw/postgres_fdw.c

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,19 +1003,9 @@ postgresGetForeignPlan(PlannerInfo *root,
10031003
* expressions to be sent as parameters.
10041004
*/
10051005
initStringInfo(&sql);
1006-
deparseSelectSql(&sql,root,baserel,fpinfo->attrs_used,
1007-
&retrieved_attrs);
1008-
if (remote_conds)
1009-
appendWhereClause(&sql,root,baserel,remote_conds,
1010-
true,&params_list);
1011-
1012-
/* Add ORDER BY clause if we found any useful pathkeys */
1013-
if (best_path->path.pathkeys)
1014-
appendOrderByClause(&sql,root,baserel,best_path->path.pathkeys);
1015-
1016-
/* Add any necessary FOR UPDATE/SHARE. */
1017-
deparseLockingClause(&sql,root,baserel);
1018-
1006+
deparseSelectStmtForRel(&sql,root,baserel,remote_conds,
1007+
best_path->path.pathkeys,&retrieved_attrs,
1008+
&params_list);
10191009
/*
10201010
* Build the fdw_private list that will be available to the executor.
10211011
* Items in the list must match enum FdwScanPrivateIndex, above.
@@ -1909,6 +1899,7 @@ estimate_path_cost_size(PlannerInfo *root,
19091899
PGconn*conn;
19101900
Selectivitylocal_sel;
19111901
QualCostlocal_cost;
1902+
List*remote_conds;
19121903

19131904
/*
19141905
* join_conds might contain both clauses that are safe to send across,
@@ -1917,24 +1908,23 @@ estimate_path_cost_size(PlannerInfo *root,
19171908
classifyConditions(root,baserel,join_conds,
19181909
&remote_join_conds,&local_join_conds);
19191910

1911+
/*
1912+
* The complete list of remote conditions includes everything from
1913+
* baserestrictinfo plus any extra join_conds relevant to this
1914+
* particular path.
1915+
*/
1916+
remote_conds=list_concat(list_copy(remote_join_conds),
1917+
fpinfo->remote_conds);
1918+
19201919
/*
19211920
* Construct EXPLAIN query including the desired SELECT, FROM, and
19221921
* WHERE clauses. Params and other-relation Vars are replaced by
19231922
* dummy values.
19241923
*/
19251924
initStringInfo(&sql);
19261925
appendStringInfoString(&sql,"EXPLAIN ");
1927-
deparseSelectSql(&sql,root,baserel,fpinfo->attrs_used,
1928-
&retrieved_attrs);
1929-
if (fpinfo->remote_conds)
1930-
appendWhereClause(&sql,root,baserel,fpinfo->remote_conds,
1931-
true,NULL);
1932-
if (remote_join_conds)
1933-
appendWhereClause(&sql,root,baserel,remote_join_conds,
1934-
(fpinfo->remote_conds==NIL),NULL);
1935-
1936-
if (pathkeys)
1937-
appendOrderByClause(&sql,root,baserel,pathkeys);
1926+
deparseSelectStmtForRel(&sql,root,baserel,remote_conds,pathkeys,
1927+
&retrieved_attrs,NULL);
19381928

19391929
/* Get the remote estimate */
19401930
conn=GetConnection(fpinfo->user, false);

‎contrib/postgres_fdw/postgres_fdw.h

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,6 @@ extern void classifyConditions(PlannerInfo *root,
8383
externboolis_foreign_expr(PlannerInfo*root,
8484
RelOptInfo*baserel,
8585
Expr*expr);
86-
externvoiddeparseSelectSql(StringInfobuf,
87-
PlannerInfo*root,
88-
RelOptInfo*baserel,
89-
Bitmapset*attrs_used,
90-
List**retrieved_attrs);
91-
externvoiddeparseLockingClause(StringInfobuf,
92-
PlannerInfo*root,RelOptInfo*rel);
93-
externvoidappendWhereClause(StringInfobuf,
94-
PlannerInfo*root,
95-
RelOptInfo*baserel,
96-
List*exprs,
97-
boolis_first,
98-
List**params);
9986
externvoiddeparseInsertSql(StringInfobuf,PlannerInfo*root,
10087
Indexrtindex,Relationrel,
10188
List*targetAttrs,booldoNothing,List*returningList,
@@ -113,8 +100,9 @@ extern void deparseAnalyzeSql(StringInfo buf, Relation rel,
113100
List**retrieved_attrs);
114101
externvoiddeparseStringLiteral(StringInfobuf,constchar*val);
115102
externExpr*find_em_expr_for_rel(EquivalenceClass*ec,RelOptInfo*rel);
116-
externvoidappendOrderByClause(StringInfobuf,PlannerInfo*root,
117-
RelOptInfo*baserel,List*pathkeys);
103+
externvoiddeparseSelectStmtForRel(StringInfobuf,PlannerInfo*root,
104+
RelOptInfo*baserel,List*remote_conds,List*pathkeys,
105+
List**retrieved_attrs,List**params_list);
118106

119107
/* in shippable.c */
120108
externboolis_builtin(OidobjectId);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp