Skip to content

Commit ffab494

Browse files
author
Etsuro Fujita
committed
postgres_fdw: Perform the (ORDERED, NULL) upperrel operations remotely.
The upper-planner pathification allows FDWs to arrange to push down different types of upper-stage operations to the remote side. This commit teaches postgres_fdw to do it for the (ORDERED, NULL) upperrel, which is responsible for evaluating the query's ORDER BY ordering. Since postgres_fdw is already able to evaluate that ordering remotely for foreign baserels and foreign joinrels (see commit aa09cd2 et al.), this adds support for that for foreign grouping relations. Author: Etsuro Fujita Reviewed-By: Antonin Houska and Jeff Janes Discussion: https://postgr.es/m/87pnz1aby9.fsf@news-spur.riddles.org.uk
1 parent e2d28c0 commit ffab494

File tree

4 files changed

+457
-138
lines changed

4 files changed

+457
-138
lines changed

contrib/postgres_fdw/deparse.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ static void printRemotePlaceholder(Oid paramtype, int32 paramtypmod,
167167
static void deparseSelectSql(List *tlist, bool is_subquery, List **retrieved_attrs,
168168
deparse_expr_cxt *context);
169169
static void deparseLockingClause(deparse_expr_cxt *context);
170-
static void appendOrderByClause(List *pathkeys, deparse_expr_cxt *context);
170+
static void appendOrderByClause(List *pathkeys, bool has_final_sort,
171+
deparse_expr_cxt *context);
171172
static void appendConditions(List *exprs, deparse_expr_cxt *context);
172173
static void deparseFromExprForRel(StringInfo buf, PlannerInfo *root,
173174
RelOptInfo *foreignrel, bool use_alias,
@@ -929,8 +930,8 @@ build_tlist_to_deparse(RelOptInfo *foreignrel)
929930
void
930931
deparseSelectStmtForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *rel,
931932
List *tlist, List *remote_conds, List *pathkeys,
932-
bool is_subquery, List **retrieved_attrs,
933-
List **params_list)
933+
bool has_final_sort, bool is_subquery,
934+
List **retrieved_attrs, List **params_list)
934935
{
935936
deparse_expr_cxt context;
936937
PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) rel->fdw_private;
@@ -985,7 +986,7 @@ deparseSelectStmtForRel(StringInfo buf, PlannerInfo *root, RelOptInfo *rel,
985986

986987
/* Add ORDER BY clause if we found any useful pathkeys */
987988
if (pathkeys)
988-
appendOrderByClause(pathkeys, &context);
989+
appendOrderByClause(pathkeys, has_final_sort, &context);
989990

990991
/* Add any necessary FOR UPDATE/SHARE. */
991992
deparseLockingClause(&context);
@@ -1590,7 +1591,7 @@ deparseRangeTblRef(StringInfo buf, PlannerInfo *root, RelOptInfo *foreignrel,
15901591
/* Deparse the subquery representing the relation. */
15911592
appendStringInfoChar(buf, '(');
15921593
deparseSelectStmtForRel(buf, root, foreignrel, NIL,
1593-
fpinfo->remote_conds, NIL, true,
1594+
fpinfo->remote_conds, NIL, false, true,
15941595
&retrieved_attrs, params_list);
15951596
appendStringInfoChar(buf, ')');
15961597

@@ -3109,7 +3110,8 @@ appendGroupByClause(List *tlist, deparse_expr_cxt *context)
31093110
* base relation are obtained and deparsed.
31103111
*/
31113112
static void
3112-
appendOrderByClause(List *pathkeys, deparse_expr_cxt *context)
3113+
appendOrderByClause(List *pathkeys, bool has_final_sort,
3114+
deparse_expr_cxt *context)
31133115
{
31143116
ListCell *lcell;
31153117
int nestlevel;
@@ -3126,7 +3128,19 @@ appendOrderByClause(List *pathkeys, deparse_expr_cxt *context)
31263128
PathKey *pathkey = lfirst(lcell);
31273129
Expr *em_expr;
31283130

3129-
em_expr = find_em_expr_for_rel(pathkey->pk_eclass, baserel);
3131+
if (has_final_sort)
3132+
{
3133+
/*
3134+
* By construction, context->foreignrel is the input relation to
3135+
* the final sort.
3136+
*/
3137+
em_expr = find_em_expr_for_input_target(context->root,
3138+
pathkey->pk_eclass,
3139+
context->foreignrel->reltarget);
3140+
}
3141+
else
3142+
em_expr = find_em_expr_for_rel(pathkey->pk_eclass, baserel);
3143+
31303144
Assert(em_expr != NULL);
31313145

31323146
appendStringInfoString(buf, delim);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy