21
21
#include "aqo.h"
22
22
#include "hash.h"
23
23
24
+ #include "postgres_fdw.h"
24
25
25
26
/*
26
27
* Hook on creation of a plan node. We need to store AQO-specific data to
@@ -56,6 +57,31 @@ create_aqo_plan_node()
56
57
return node ;
57
58
}
58
59
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
+
59
85
/*
60
86
* Extract an AQO node from the plan private field.
61
87
* 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)
415
441
return ;
416
442
417
443
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 )));
419
446
420
447
node = get_aqo_plan_node (plan , true);
421
448
@@ -431,8 +458,32 @@ aqo_create_plan_hook(PlannerInfo *root, Path *src, Plan **dest)
431
458
432
459
if (is_join_path )
433
460
{
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
+ }
436
487
}
437
488
else if (IsA (src , AggPath ))
438
489
/* Aggregation node must store grouping clauses. */
0 commit comments