Skip to content

Commit 9f19a09

Browse files
committed
[SHRDM-762] extract info from foreign join
1 parent 53b3910 commit 9f19a09

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

expected/aqo_fdw.out

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ SELECT x FROM frgn;
5757
(5 rows)
5858

5959
-- Push down base filters. Use verbose mode to see filters.
60-
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE))
61-
SELECT x FROM frgn WHERE x < 10;
62-
ERROR: syntax error at or near ")"
63-
LINE 1: ...LAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE))
64-
^
6560
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
6661
SELECT x FROM frgn WHERE x < 10;
6762
QUERY PLAN
@@ -75,6 +70,19 @@ SELECT x FROM frgn WHERE x < 10;
7570
JOINS: 0
7671
(7 rows)
7772

73+
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
74+
SELECT x FROM frgn WHERE x < 10;
75+
QUERY PLAN
76+
-----------------------------------------------------------
77+
Foreign Scan on public.frgn (actual rows=1 loops=1)
78+
AQO: rows=1, error=0%
79+
Output: x
80+
Remote SQL: SELECT x FROM public.local WHERE ((x < 10))
81+
Using aqo: true
82+
AQO mode: LEARN
83+
JOINS: 0
84+
(7 rows)
85+
7886
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
7987
SELECT x FROM frgn WHERE x < -10; -- AQO ignores constants
8088
QUERY PLAN
@@ -143,7 +151,7 @@ SELECT * FROM frgn AS a, frgn AS b WHERE a.x<b.x;
143151
QUERY PLAN
144152
--------------------------------------------------------------------------------------------------------
145153
Foreign Scan (actual rows=0 loops=1)
146-
AQO not used
154+
AQO: rows=1, error=100%
147155
Output: a.x, b.x
148156
Relations: (public.frgn a) INNER JOIN (public.frgn b)
149157
Remote SQL: SELECT r1.x, r2.x FROM (public.local r1 INNER JOIN public.local r2 ON (((r1.x < r2.x))))

path_utils.c

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "aqo.h"
2222
#include "hash.h"
2323

24+
#include "postgres_fdw.h"
2425

2526
/*
2627
* Hook on creation of a plan node. We need to store AQO-specific data to
@@ -56,6 +57,31 @@ create_aqo_plan_node()
5657
return node;
5758
}
5859

60+
61+
/* Ensure that it's postgres_fdw's foreign server oid */
62+
static bool
63+
is_postgres_fdw_server(Oid serverid)
64+
{
65+
ForeignServer *server;
66+
ForeignDataWrapper *fdw;
67+
68+
if (!OidIsValid(serverid))
69+
return false;
70+
71+
server = GetForeignServerExtended(serverid, FSV_MISSING_OK);
72+
if (!server)
73+
return false;
74+
75+
fdw = GetForeignDataWrapperExtended(server->fdwid, FDW_MISSING_OK);
76+
if (!fdw || !fdw->fdwname)
77+
return false;
78+
79+
if (strcmp(fdw->fdwname, "postgres_fdw") != 0)
80+
return false;
81+
82+
return true;
83+
}
84+
5985
/*
6086
* Extract an AQO node from the plan private field.
6187
* If no one node was found, return pointer to the default value or allocate new
@@ -415,7 +441,8 @@ aqo_create_plan_hook(PlannerInfo *root, Path *src, Plan **dest)
415441
return;
416442

417443
is_join_path = (src->type == T_NestPath || src->type == T_MergePath ||
418-
src->type == T_HashPath);
444+
src->type == T_HashPath ||
445+
(src->type == T_ForeignPath && IS_JOIN_REL(src->parent)));
419446

420447
node = get_aqo_plan_node(plan, true);
421448

@@ -431,8 +458,32 @@ aqo_create_plan_hook(PlannerInfo *root, Path *src, Plan **dest)
431458

432459
if (is_join_path)
433460
{
434-
node->clauses = aqo_get_clauses(root, ((JoinPath *) src)->joinrestrictinfo);
435-
node->jointype = ((JoinPath *) src)->jointype;
461+
if (IsA(src, ForeignPath))
462+
{
463+
PgFdwRelationInfo *fpinfo = (PgFdwRelationInfo *) src->parent->fdw_private;
464+
List *restrictclauses = NIL;
465+
466+
if (!fpinfo)
467+
return;
468+
469+
/* We have to ensure that this is postgres_fdw ForeignPath */
470+
if (!is_postgres_fdw_server(src->parent->serverid))
471+
return;
472+
473+
restrictclauses = list_concat(restrictclauses, fpinfo->joinclauses);
474+
restrictclauses = list_concat(restrictclauses, fpinfo->remote_conds);
475+
restrictclauses = list_concat(restrictclauses, fpinfo->local_conds);
476+
477+
node->clauses = aqo_get_clauses(root, restrictclauses);
478+
node->jointype = fpinfo->jointype;
479+
480+
list_free(restrictclauses);
481+
}
482+
else
483+
{
484+
node->clauses = aqo_get_clauses(root, ((JoinPath *) src)->joinrestrictinfo);
485+
node->jointype = ((JoinPath *) src)->jointype;
486+
}
436487
}
437488
else if (IsA(src, AggPath))
438489
/* Aggregation node must store grouping clauses. */

sql/aqo_fdw.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF)
4747
SELECT x FROM frgn;
4848

4949
-- Push down base filters. Use verbose mode to see filters.
50-
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE))
50+
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
5151
SELECT x FROM frgn WHERE x < 10;
5252
EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, VERBOSE)
5353
SELECT x FROM frgn WHERE x < 10;

0 commit comments

Comments
 (0)