forked from pgspider/pgspider_ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgspider_ext.c
2468 lines (2128 loc) · 73 KB
/
pgspider_ext.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------------
*
* pgspider_ext.c
* contrib/pgspider_ext/pgspider_ext.c
*
* Portions Copyright (c) 2020 - 2022, TOSHIBA CORPORATION
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "c.h"
#include "fmgr.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
#include <stdio.h>
#include <stddef.h>
#include "catalog/namespace.h"
#include "commands/explain.h"
#include "catalog/partition.h"
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
#include "nodes/nodeFuncs.h"
#include "nodes/pg_list.h"
#include "nodes/makefuncs.h"
#include "optimizer/cost.h"
#include "optimizer/pathnode.h"
#include "optimizer/paths.h"
#include "optimizer/planmain.h"
#include "optimizer/optimizer.h"
#include "optimizer/tlist.h"
#include "parser/parse_clause.h"
#include "parser/parse_oper.h"
#include "utils/builtins.h"
#include "utils/guc.h"
#include "utils/partcache.h"
#include "utils/syscache.h"
#include "storage/lmgr.h"
#include "pgspider_ext.h"
#define PGSPIDER_FDW_NAME "pgspider_fdw"
#define PARQUET_S3_FDW_NAME "parquet_s3_fdw"
#define createChildPathKey(pathkey, attrno_to_child) \
pathKeyDeepCopyWithAttrnoChange(pathkey, attrno_to_child)
#define createParentPathKey(pathkey, attrno_to_parent) \
pathKeyDeepCopyWithAttrnoChange(pathkey, attrno_to_parent)
/*
* This structure stores a child node plan information for child table.
*/
typedef struct ChildPlanInfo
{
Oid server_oid; /* child's server oid */
Oid table_oid; /* child's table oid */
AttrNumber *attrno_to_child; /* attno mapping from parent to child */
AttrNumber *attrno_to_parent; /* attno mapping from child to parent */
FdwRoutine *fdw_routine; /* FDW routine */
RelOptInfo *baserel; /* child's baserel */
Path *path; /* child's path */
Plan *plan; /* child's plan */
PlannerInfo *root; /* child's root */
RelOptInfo *grouped_rel_local; /* child's upper relation */
PlannerInfo *grouped_root_local; /* child's upper root */
} ChildPlanInfo;
/*
* This structure stores a child node state when executing a query.
*/
typedef struct ChildScanInfo
{
Oid server_oid; /* child's server oid */
Oid table_oid; /* child's table oid */
AttrNumber *attrno_to_child; /* attno mapping from parent to child */
AttrNumber *attrno_to_parent; /* attno mapping from child to parent */
FdwRoutine *fdw_routine; /* FDW routine */
Plan *plan; /* child's plan */
Query *parse; /* child's root->parse */
ForeignScanState *fsstate; /* ForeignScan state data */
} ChildScanInfo;
/*
* This structure stores information shared between FDW routines of planning like
* GetForeignRelSize(), GetForeignPaths(), GetForeignUpperPaths() and GetForeignPlan().
*/
typedef struct SpdFdwPlanState
{
Oid table_oid; /* parent's server oid */
bool is_upper; /* true if upper relation */
int partkey_idx; /* location of partition key in table slot. -1
* if not used */
AttrNumber partkey_attno; /* column number of partition key */
List *partkey_conds; /* Expr using partition key in WHERE clause */
Expr *partkey_expr; /* Expr using partition key in target list on
* upper path */
RelOptInfo *outerrel; /* join information */
ChildPlanInfo child_plan_info; /* child information */
HTAB *aggsplit_history; /* store old aggsplit value */
} SpdFdwPlanState;
/*
* This structure stores information shared between FDW routines of execution like
* BeginForeignScan(), IterateForeignScan() and EndForeignScan().
*/
typedef struct SpdFdwScanState
{
Oid table_oid; /* parent's server oid */
int partkey_idx; /* index of partition key in table slot. -1 if
* not used */
AttrNumber partkey_attno; /* column number of partition key */
bool is_upper; /* true if upper relation */
bool is_first; /* true if it is a first time of
* IterationForeignScan */
ChildScanInfo child_scan_info; /* child information */
} SpdFdwScanState;
typedef struct SpdFdwModifyState
{
Oid modify_server_oid;
} SpdFdwModifyState;
/*
* FDW callback routines
*/
static void spdGetForeignRelSize(PlannerInfo *root, RelOptInfo *baserel,
Oid foreigntableid);
static void spdGetForeignPaths(PlannerInfo *root, RelOptInfo *baserel,
Oid foreigntableid);
static ForeignScan *spdGetForeignPlan(PlannerInfo *root, RelOptInfo *baserel,
Oid foreigntableid, ForeignPath *best_path,
List *tlist, List *scan_clauses,
Plan *outer_plan);
static void spdBeginForeignScan(ForeignScanState *node, int eflags);
static TupleTableSlot *spdIterateForeignScan(ForeignScanState *node);
static void spdReScanForeignScan(ForeignScanState *node);
static void spdEndForeignScan(ForeignScanState *node);
static void spdAddForeignUpdateTargets(Query *parsetree,
RangeTblEntry *target_rte,
Relation target_relation);
static List *spdPlanForeignModify(PlannerInfo *root,
ModifyTable *plan,
Index resultRelation,
int subplan_index);
static void spdBeginForeignModify(ModifyTableState *mtstate,
ResultRelInfo *rinfo,
List *fdw_private,
int subplan_index,
int eflags);
static TupleTableSlot *spdExecForeignInsert(EState *estate,
ResultRelInfo *rinfo,
TupleTableSlot *slot,
TupleTableSlot *planSlot);
static TupleTableSlot *spdExecForeignUpdate(EState *estate,
ResultRelInfo *rinfo,
TupleTableSlot *slot,
TupleTableSlot *planSlot);
static TupleTableSlot *spdExecForeignDelete(EState *estate,
ResultRelInfo *rinfo,
TupleTableSlot *slot,
TupleTableSlot *planSlot);
static void spdEndForeignModify(EState *estate,
ResultRelInfo *rinfo);
static void spdExplainForeignScan(ForeignScanState *node, ExplainState *es);
static void spdGetForeignUpperPaths(PlannerInfo *root,
UpperRelationKind stage,
RelOptInfo *input_rel,
RelOptInfo *output_rel, void *extra);
static bool spdIsForeignScanParallelSafe(PlannerInfo *root,
RelOptInfo *rel, RangeTblEntry *rte);
/* Declarations for dynamic loading */
PG_FUNCTION_INFO_V1(pgspider_ext_handler);
/*
* pgspider_ext_handler populates an FdwRoutine with pointers to the functions
* implemented within this file.
*/
Datum
pgspider_ext_handler(PG_FUNCTION_ARGS)
{
FdwRoutine *routine = makeNode(FdwRoutine);
/* Functions for scanning foreign tables */
routine->GetForeignRelSize = spdGetForeignRelSize;
routine->GetForeignPaths = spdGetForeignPaths;
routine->GetForeignPlan = spdGetForeignPlan;
routine->BeginForeignScan = spdBeginForeignScan;
routine->IterateForeignScan = spdIterateForeignScan;
routine->ReScanForeignScan = spdReScanForeignScan;
routine->EndForeignScan = spdEndForeignScan;
/* Functions for updating foreign tables */
routine->AddForeignUpdateTargets = spdAddForeignUpdateTargets;
routine->PlanForeignModify = spdPlanForeignModify;
routine->BeginForeignModify = spdBeginForeignModify;
routine->ExecForeignInsert = spdExecForeignInsert;
routine->ExecForeignUpdate = spdExecForeignUpdate;
routine->ExecForeignDelete = spdExecForeignDelete;
routine->EndForeignModify = spdEndForeignModify;
routine->BeginForeignInsert = NULL;
routine->EndForeignInsert = NULL;
routine->IsForeignRelUpdatable = NULL;
routine->PlanDirectModify = NULL;
routine->BeginDirectModify = NULL;
routine->IterateDirectModify = NULL;
routine->EndDirectModify = NULL;
/* Function for EvalPlanQual rechecks */
routine->RecheckForeignScan = NULL;
/* Support functions for EXPLAIN */
routine->ExplainForeignScan = spdExplainForeignScan;
routine->ExplainForeignModify = NULL;
routine->ExplainDirectModify = NULL;
/* Support functions for ANALYZE */
routine->AnalyzeForeignTable = NULL;
/* Support functions for IMPORT FOREIGN SCHEMA */
routine->ImportForeignSchema = NULL;
/* Support functions for join push-down */
routine->GetForeignJoinPaths = NULL;
/* Support functions for upper relation push-down */
routine->GetForeignUpperPaths = spdGetForeignUpperPaths;
/* Support functions for parallelism under Gather node */
routine->IsForeignScanParallelSafe = spdIsForeignScanParallelSafe;
PG_RETURN_POINTER(routine);
}
/*
* child_tableid_from_parentid
* Get child table oid from parent table oid. If an option of child
* table name is specified, we use it. If it is not specified, we
* use the parent table name + "_child".
*
* @param[in] foreigntableid - parent's table oid
* @return Oid - child's table oid
*/
static Oid
child_tableid_from_parentid(Oid foreigntableid)
{
char *child_name;
SpdPpt *option = spd_get_options(foreigntableid);
Oid child_table_oid;
bool change_search_path = false;
if (option->child_name)
child_name = option->child_name;
else
{
Relation rel = RelationIdGetRelation(foreigntableid);
char *parent_name = RelationGetRelationName(rel);
child_name = psprintf("%s_child", parent_name);
RelationClose(rel);
}
/*
* By default, the search path is "pg_catalog" for remote session.
* Therefore, need to update namespace_search_path to be able to
* search in remote server
*/
if (strcmp(namespace_search_path, "pg_catalog") == 0)
{
change_search_path = true;
namespace_search_path = "\"$user\", public";
assign_search_path("", NULL);
}
child_table_oid = RelnameGetRelid(child_name);
/*
* Change back the search path to avoid affecting original behavior
*/
if (change_search_path)
{
namespace_search_path = "pg_catalog";
assign_search_path("", NULL);
}
if (!OidIsValid(child_table_oid))
elog(ERROR, "Not found child table: %s", child_name);
return child_table_oid;
}
/*
* serverid_of_relation
* Get server oid from table oid.
*
* @param[in] foreigntableid - table oid
* @return Oid - server oid
*/
static Oid
serverid_of_relation(Oid foreigntableid)
{
ForeignTable *ft = GetForeignTable(foreigntableid);
return ft->serverid;
}
/*
* pathKeyDeepCopyWithAttrnoChange
* Create new pathkey by deep-copy manually because copyObject for PathKey
* is sharrow-copy. Only necessary variables is copied. attribute numbers
* are also updated based on attrno_to.
*
* @param[in] src - pathkey as a source
* @param[in] attrno_to_child - information of attrno mapping
* @return PathKey - created pathkey
*/
static PathKey *
pathKeyDeepCopyWithAttrnoChange(PathKey *src, AttrNumber *attrno_to)
{
PathKey *pathkey;
EquivalenceClass *pk_eclass;
ListCell *lc;
List *ec_members = NULL;
/* This is sharrow copy. */
pathkey = (PathKey *) copyObject(src);
/* Copy PathKey->pk_eclass. */
pk_eclass = (EquivalenceClass *) palloc0(sizeof(EquivalenceClass));
memcpy(pk_eclass, src->pk_eclass, sizeof(EquivalenceClass));
pathkey->pk_eclass = pk_eclass;
/* Copy PathKey->pk_eclass->ec_members. */
foreach(lc, pk_eclass->ec_members)
{
Expr *expr;
EquivalenceMember *em = (EquivalenceMember *) palloc0(sizeof(EquivalenceMember));
memcpy(em, (EquivalenceMember *) lfirst(lc), sizeof(EquivalenceMember));
ec_members = lappend(ec_members, em);
/* Copy PathKey->pk_eclass->ec_members->expr. */
expr = (Expr *) copyObject(em->em_expr);
mapVarAttnos((Node *) expr, attrno_to);
em->em_expr = expr;
}
pk_eclass->ec_members = ec_members;
return pathkey;
}
/*
* freePathKey
* Free memory of pathkey allocated by createChildPathKey.
*
* @param[in] pathkey to be freed
*/
static void
freePathKey(PathKey *pathkey)
{
EquivalenceClass *pk_eclass = pathkey->pk_eclass;
ListCell *lc;
foreach(lc, pk_eclass->ec_members)
{
EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
pfree(em);
}
list_free(pk_eclass->ec_members);
pfree(pk_eclass);
}
/*
* freePathKeys
* Free memory allocated by createChildPathKey in the pathkey list.
*
* @param[in] pathkeys pathey's list
*/
static void
freePathKeyList(List *pathkeys)
{
ListCell *lc;
foreach(lc, pathkeys)
{
PathKey *pathkey = (PathKey *) lfirst(lc);
freePathKey(pathkey);
}
list_free(pathkeys);
}
/*
* createChildRoot
* Create a child PlanerInfo.
*
* @param[in] root - parent's planner info
* @param[in] baserel - parent's relation option
* @param[in] tableid - child's table oid
* @param[in] partkey_attno - column number of partition key
* @param[in] attrno_to_child - information of attrno mapping from parent to child
* @return PlannerInfo* - child's planner info
*/
static PlannerInfo *
createChildRoot(PlannerInfo *root, RelOptInfo *baserel, Oid tableid,
AttrNumber partkey_attno, AttrNumber *attrno_to_child)
{
RangeTblEntry *rte;
Query *query;
int k;
PlannerInfo *child_root;
ListCell *lc;
List *query_pathkeys = NIL;
/* Build a minimal RTE for the rel */
rte = makeNode(RangeTblEntry);
rte->rtekind = RTE_RELATION;
rte->relid = tableid;
rte->relkind = RELKIND_RELATION;
rte->eref = makeNode(Alias);
rte->eref->aliasname = pstrdup("");
rte->lateral = false;
rte->inh = false;
rte->inFromCl = true;
rte->eref = makeAlias(pstrdup(""), NIL);
rte->rellockmode = AccessShareLock;
/*
* Because in build_simple_rel() function, it assumes that a relation was
* already locked before open. So, we need to lock relation by id in dummy
* root in advance.
*/
LockRelationOid(rte->relid, rte->rellockmode);
/*
* Set up mostly-dummy planner state PlannerInfo can not deep copy with
* copyObject(). BUt It should create dummy PlannerInfo for each child
* tables. Following code is copy from plan_cluster_use_sort(), it create
* simple PlannerInfo.
*/
query = makeNode(Query);
query->commandType = CMD_SELECT;
/* Create child range table */
query->rtable = list_make1(rte);
for (k = 1; k < baserel->relid; k++)
{
query->rtable = lappend(query->rtable, rte);
}
child_root = makeNode(PlannerInfo);
child_root->parse = query;
child_root->glob = makeNode(PlannerGlobal);
child_root->query_level = 1;
child_root->planner_cxt = CurrentMemoryContext;
child_root->wt_param_id = -1;
child_root->rowMarks = (List *) copyObject(root->rowMarks);
/*
* Check whether ORDER BY clause uses a partition key. If it is used, we
* cannot pushdown. If it is not used, we can pass the information to
* child with updating attrno.
*/
foreach(lc, root->query_pathkeys)
{
PathKey *pathkey = (PathKey *) lfirst(lc);
PathKey *child_pathkey;
Expr *expr;
/* Check if a partition key is used. */
expr = getExprInPathKey(pathkey, baserel);
if (hasPartKeyExpr((Node *) expr, partkey_attno))
{
freePathKeyList(query_pathkeys);
query_pathkeys = NULL;
break;
}
child_pathkey = createChildPathKey(pathkey, attrno_to_child);
query_pathkeys = lappend(query_pathkeys, child_pathkey);
}
child_root->query_pathkeys = query_pathkeys;
/*
* Use placeholder list only for child node's GetForeignRelSize in this
* routine. PlaceHolderVar in relation target list will be checked against
* PlaceHolder List in root planner info.
*/
child_root->placeholder_list = (List *) copyObject(root->placeholder_list);
/* Set up RTE/RelOptInfo arrays */
setup_simple_rel_arrays(child_root);
return child_root;
}
/*
* createChildBaserel
* Create child base relation based on from parent information.
* varattno in expr in relation target are updated.
*
* @param[in] root - parent's planner info
* @param[in] baserel - parent's base relation
* @param[in] child_root - child's planner info
* @param[in] attrno_to_child - information of attrno mapping from parent to child
* @param[in] is_pgspider_fdw - true if it is PGSpider FDW
* @param[in] partkey_attno - column number of partition key
* @param[out] partkey_conds - WHERE consition expressions which contains partition key
* @param[out] partkey_idx - index of partition key in target list
* @return RelOptInfo - created relation info
*/
static RelOptInfo *
createChildBaserel(PlannerInfo *root, RelOptInfo *baserel,
PlannerInfo *child_root, AttrNumber *attrno_to_child,
AttrNumber partkey_attno, List **partkey_conds,
List **partkey_idxes)
{
RelOptInfo *child_baserel;
ListCell *lc;
List *exprs = NIL; /* new exprs after remove a partition key */
List *restrictinfo = NIL; /* new restrictinfo after remove a
* partition key */
List *removed_exprs = NIL; /* Removed exprs */
PartkeyWalkerContext context;
context.partkey_attno = partkey_attno;
context.exprs = NIL;
child_baserel = build_simple_rel(child_root, baserel->relid, (RelOptInfo *) RELOPT_BASEREL);
/*
* Get target lists by removing a partition key from parent's target
* lists.
*/
exprs = copyObject(baserel->reltarget->exprs);
exprs = removePartkeyFromTargets(exprs, partkey_attno, partkey_idxes);
/* Update varattno for mapping from a parent table to a child table. */
exprs = mapVarAttnosInList(exprs, attrno_to_child);
/*
* Create restrictinfo and append exprs of WHERE clauses if a partition
* key is not contained.
*/
foreach(lc, baserel->baserestrictinfo)
{
RestrictInfo *clause = (RestrictInfo *) lfirst(lc);
Expr *expr = (Expr *) clause->clause;
/*
* If the condition uses a partition key, it cannot pushdown to child
* FDW. PGSpider finds variables which are used in the condition in
* order to add them into a target list. PostgreSQL core needs them
* for calculating the clause. If the condition does not use partition
* key, it can pass to child FDW. So we add them into restrictinfo for
* child.
*/
if (hasPartKeyExpr((Node *) expr, partkey_attno))
{
removed_exprs = lappend(removed_exprs, expr);
extract_var_walker((Node *) expr, &context);
}
else
{
RestrictInfo *child_clause = (RestrictInfo *) copyObject(clause);
/*
* Update varattno for mapping from a parent table to a child
* table.
*/
child_clause->clause = (Expr *) mapVarAttnos((Node *) child_clause->clause,
attrno_to_child);
restrictinfo = lappend(restrictinfo, child_clause);
}
}
/* Append exprs used in restrictinfo which cannot be pushed down. */
foreach(lc, context.exprs)
{
Expr *expr = (Expr *) lfirst(lc);
/* Chech if it already exists. */
if (!exprlist_member(expr, exprs))
exprs = lappend(exprs, expr);
}
child_baserel->reltarget->exprs = exprs;
child_baserel->baserestrictinfo = restrictinfo;
*partkey_conds = removed_exprs;
return child_baserel;
}
/*
* getForeignRelSizeChild
* Call GetForeignRelSize for child FDW and set ChildInfo.
*
* @param[in] root - parent's planner info
* @param[in] baserel - parent's relation option
* @param[in] foreigntableid - parent's table oid
* @param[in] partkey_attno - column number of partition key
* @param[out] childplaninfo - child's plan info is set to here
* @param[out] partkey_conds - WHERE consition expressions which contains a partition key
* @param[out] partkey_idx - index of partition key in the target list
*/
static void
getForeignRelSizeChild(PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid,
AttrNumber partkey_attno, ChildPlanInfo * childplaninfo,
List **partkey_conds, List **partkey_idx)
{
Oid child_table_oid;
FdwRoutine *fdw_routine;
Oid child_server_oid;
PlannerInfo *child_root;
RelOptInfo *child_baserel;
AttrNumber *attrno_to_child;
AttrNumber *attrno_to_parent;
/* Find an oid of child table. */
child_table_oid = child_tableid_from_parentid(foreigntableid);
/* Find a child FDW routine. */
child_server_oid = serverid_of_relation(child_table_oid);
fdw_routine = GetFdwRoutineByServerId(child_server_oid);
/*
* Create base plan for each child table with updating varattno in
* relation target. Refer createVarAttrnoMapping()'s comment for the
* reason why varattno are updated.
*/
createVarAttrnoMapping(foreigntableid, child_table_oid, partkey_attno, &attrno_to_child, &attrno_to_parent);
child_root = createChildRoot(root, baserel, child_table_oid, partkey_attno, attrno_to_child);
child_baserel = createChildBaserel(root, baserel, child_root, attrno_to_child,
partkey_attno, partkey_conds, partkey_idx);
fdw_routine->GetForeignRelSize(child_root, child_baserel, child_table_oid);
childplaninfo->table_oid = child_table_oid;
childplaninfo->server_oid = child_server_oid;
childplaninfo->fdw_routine = fdw_routine;
childplaninfo->root = child_root;
childplaninfo->baserel = child_baserel;
childplaninfo->attrno_to_child = attrno_to_child;
childplaninfo->attrno_to_parent = attrno_to_parent;
}
/*
* getPartColumnAttno
* Get a column number of partition key.
*
* @param[in] foreigntableid - parent's foreing table oid
* @return AttrNumber - column number
*/
static AttrNumber
getPartColumnAttno(Oid foreigntableid)
{
Oid parentid;
Relation relation;
PartitionKey partkey;
int i;
int num_dropped = 0;
/* Get a partion parent table. */
parentid = get_partition_parent(foreigntableid);
relation = RelationIdGetRelation(parentid);
/* Get a partion key information. */
partkey = RelationGetPartitionKey(relation);
/* We expects that a partition key is only single column. */
if (partkey->partexprs != NULL || partkey->partnatts != 1)
elog(ERROR, "A partition key must be only single column.");
for (i = 0; i < partkey->partattrs[0]; i++)
{
Form_pg_attribute attr = TupleDescAttr(relation->rd_att, i);
if (attr->attisdropped)
num_dropped++;
}
RelationClose(relation);
return partkey->partattrs[0] - num_dropped;
}
/*
* spdGetForeignRelSize
* Create a base plan for child table and save it into fdw_private.
* Then estimate # of rows and width of the result of the scan based on child relation.
*
* @param[in] root - palrent's planner information
* @param[in] baserel - parent's relation option
* @param[in] foreigntableid - parent's foreing table oid
*/
static void
spdGetForeignRelSize(PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid)
{
SpdFdwPlanState *fdw_private;
List *partkey_idxes = NIL;
AttrNumber partkey_attno;
elog(DEBUG1, "GetForeignRelSize");
fdw_private = (SpdFdwPlanState *) palloc0(sizeof(SpdFdwPlanState));
baserel->fdw_private = (void *) fdw_private;
partkey_attno = getPartColumnAttno(foreigntableid);
getForeignRelSizeChild(root, baserel, foreigntableid, partkey_attno,
&fdw_private->child_plan_info, &fdw_private->partkey_conds,
&partkey_idxes);
/* Use the estimated values of child. */
if (baserel->pages == 0 && baserel->tuples == 0)
{
baserel->rows = fdw_private->child_plan_info.baserel->rows;
baserel->pages = fdw_private->child_plan_info.baserel->pages;
baserel->tuples = fdw_private->child_plan_info.baserel->tuples;
}
set_baserel_size_estimates(root, baserel);
fdw_private->table_oid = foreigntableid;
fdw_private->partkey_attno = partkey_attno;
/* Memorize expr if the target list has a partition key. */
if (list_length(partkey_idxes) > 0)
{
int partkey_idx;
Assert(list_length(partkey_idxes) == 1);
partkey_idx = intVal(list_nth(partkey_idxes, 0));
fdw_private->partkey_expr = (Expr *) list_nth(baserel->reltarget->exprs, partkey_idx);
}
}
/*
* spdGetForeignPaths
* Create foreign paths for child tables.
* Then create parent scan paths based on child paths.
*
* @param[in] root - palrent's planner information
* @param[in] baserel - parent's relation option
* @param[in] foreigntableid - parent's foreing table oid
*/
static void
spdGetForeignPaths(PlannerInfo *root, RelOptInfo *baserel, Oid foreigntableid)
{
SpdFdwPlanState *fdw_private = (SpdFdwPlanState *) baserel->fdw_private;
ChildPlanInfo *child_plan_info;
ListCell *lc;
int path_pos = 0;
Oid oid_server;
ForeignServer *fs;
ForeignDataWrapper *fdw;
elog(DEBUG1, "GetForeignPaths");
if (fdw_private == NULL)
elog(ERROR, "fdw_private is NULL");
child_plan_info = &fdw_private->child_plan_info;
oid_server = serverid_of_relation(child_plan_info->table_oid);
fs = GetForeignServer(oid_server);
fdw = GetForeignDataWrapper(fs->fdwid);
/*
* The ECs need to reached canonical state. Otherwise, pathkeys of
* parquet_s3_fdw could be rendered non-canonical.
*/
if (strcmp(fdw->fdwname, PARQUET_S3_FDW_NAME) == 0)
child_plan_info->root->ec_merging_done = root->ec_merging_done;
/* Create Foreign paths using base_rel_list to each child node. */
child_plan_info->fdw_routine->GetForeignPaths(child_plan_info->root,
child_plan_info->baserel,
child_plan_info->table_oid);
if (child_plan_info->baserel->pathlist == NULL)
return;
/* Add paths based on child paths. */
foreach(lc, child_plan_info->baserel->pathlist)
{
Path *childpath = (Path *) lfirst(lc);
Cost startup_cost;
Cost total_cost;
Cost rows;
PathTarget *target;
List *pathkeys = NIL;
/* Use child node costs */
startup_cost = childpath->startup_cost;
total_cost = childpath->total_cost;
rows = childpath->rows;
/*
* Construct a target list of parent based on child by deep-copy of
* Expr.
*/
target = copy_pathtarget(childpath->pathtarget);
list_free(target->exprs);
target->exprs = copyObject(childpath->pathtarget->exprs);
/* Update varattno for mapping from a child table to a parent table. */
target->exprs = mapVarAttnosInList(target->exprs, child_plan_info->attrno_to_parent);
/* Inserted a partition key. */
if (fdw_private->partkey_expr)
{
/*
* If tuple descriptor of child slot will be created based on
* relation, parent is also created as same way by setting target
* = NULL.
*/
if (childpath->pathtarget == child_plan_info->baserel->reltarget)
target = NULL;
else
{
Expr *expr = (Expr *) copyObject(fdw_private->partkey_expr);
target->exprs = lappend(target->exprs, expr);
}
}
if (childpath->pathkeys)
{
/*
* PathKey's equality is done by comparing pointers in PostgreSQL
* core. So we cannot use copyObject if it needs to keep the
* equality.
*/
if (compare_pathkeys(childpath->pathkeys, child_plan_info->root->query_pathkeys) == PATHKEYS_EQUAL)
pathkeys = root->query_pathkeys;
}
/*
* We specify path_pos as a fdw_private so that spdGetForeignPlan()
* can know which path is selected as a best path.
*/
add_path(baserel, (Path *) create_foreignscan_path(root, baserel,
target,
rows,
startup_cost,
total_cost,
pathkeys,
baserel->lateral_relids,
NULL, /* no outerpath */
list_make1_int(path_pos))); /* fdw_private */
path_pos++;
}
}
/*
* serializeSpdFdwPrivate
* Serialize SpdFdwPlanState as a list in order to share variables to SpdFdwScanState.
* The order of elements in the list must be the same on serializing and deserializing functions.
*
* @param[in] fdw_private - seriarizing data
* @return List* - serialized data
*/
static List *
serializeSpdFdwPrivate(SpdFdwPlanState * fdw_private)
{
List *fdw_private_list = NIL;
ChildPlanInfo *child_plan_info = &fdw_private->child_plan_info;
fdw_private_list = lappend(fdw_private_list, makeInteger(fdw_private->table_oid));
fdw_private_list = lappend(fdw_private_list, makeInteger(fdw_private->partkey_idx));
fdw_private_list = lappend(fdw_private_list, makeInteger(fdw_private->is_upper));
fdw_private_list = lappend(fdw_private_list, makeInteger(fdw_private->partkey_attno));
/* Append ChildInfo */
fdw_private_list = lappend(fdw_private_list, makeInteger(child_plan_info->server_oid));
fdw_private_list = lappend(fdw_private_list, makeInteger(child_plan_info->table_oid));
fdw_private_list = lappend(fdw_private_list, child_plan_info->plan);
fdw_private_list = lappend(fdw_private_list, child_plan_info->root->parse);
return fdw_private_list;
}
/*
* deserializeSpdFdwPrivate
* Deserialize SpdFdwScanState from SpdFdwPlanState.
* The order of elements in the list must be the same on serializing and deserializing functions.
*
* @param[in] fdw_private_list - deseriarizing data
* @return SpdFdwScanState* - deserialized data
*/
static SpdFdwScanState *
deserializeSpdFdwPrivate(List *fdw_private_list)
{
ListCell *lc = list_head(fdw_private_list);
SpdFdwScanState *fdw_state = (SpdFdwScanState *) palloc0(sizeof(SpdFdwScanState));
ChildScanInfo *child_scan_info = &fdw_state->child_scan_info;
AttrNumber *attrno_to_child;
AttrNumber *attrno_to_parent;
fdw_state->table_oid = intVal(lfirst(lc));
lc = lnext(fdw_private_list, lc);
fdw_state->partkey_idx = intVal(lfirst(lc));
lc = lnext(fdw_private_list, lc);
fdw_state->is_upper = intVal(lfirst(lc)) ? true : false;
lc = lnext(fdw_private_list, lc);
fdw_state->partkey_attno = intVal(lfirst(lc));
lc = lnext(fdw_private_list, lc);
child_scan_info->server_oid = intVal(lfirst(lc));
lc = lnext(fdw_private_list, lc);
child_scan_info->table_oid = intVal(lfirst(lc));
lc = lnext(fdw_private_list, lc);
child_scan_info->plan = (Plan *) lfirst(lc);
lc = lnext(fdw_private_list, lc);
child_scan_info->parse = (Query *) lfirst(lc);
lc = lnext(fdw_private_list, lc);
/*
* Following varibales cannot be serialized because copyObject called by
* PostgreSQL core does not support it. So we calculate them again.
*/
createVarAttrnoMapping(fdw_state->table_oid, child_scan_info->table_oid, fdw_state->partkey_attno,
&attrno_to_child, &attrno_to_parent);
child_scan_info->attrno_to_child = attrno_to_child;
child_scan_info->attrno_to_parent = attrno_to_parent;
child_scan_info->fdw_routine = GetFdwRoutineByServerId(child_scan_info->server_oid);
return fdw_state;
}
/*
* createChildPlan
* Create foreign plan for child tables.
*
* @param[in] root - parent's planner info
* @param[in] baserel - parent's base relation
* @param[in] best_path_pos - position of best path in the path list
* @param[in] tlist - parent's target list
* @param[in] outer_plan - parent's outer plan
* @param[in] partkey_attno - column number of partition key
* @param[in,out] child_plan_info - child plan info
* @return ForeignScan - created foreign plan
*/
static ForeignScan *
createChildPlan(PlannerInfo *root, RelOptInfo *baserel, int best_path_pos,
List *tlist, Plan *outer_plan, AttrNumber partkey_attno,
ChildPlanInfo * child_plan_info)
{
ForeignScan *child_plan;
Path *child_path;
List *child_tlist = NIL;
Oid child_table_oid = child_plan_info->table_oid;
PlannerInfo *child_root;
RelOptInfo *child_baserel;
List *restrictinfo;
List *idxes = NIL;
/* Choose the best path in the list. */
child_path = (Path *) list_nth(child_plan_info->baserel->pathlist, best_path_pos);
child_plan_info->path = child_path;
/* Create child tlist based on that of parent by removing a partition key. */
child_tlist = removePartkeyFromTargets(copyObject(tlist), partkey_attno, &idxes);
child_tlist = mapVarAttnosInList(child_tlist, child_plan_info->attrno_to_child);
/* Create plan of child FDW */
if (IS_SIMPLE_REL(baserel))
{
child_root = child_plan_info->root;
child_baserel = child_plan_info->baserel;
restrictinfo = child_plan_info->baserel->baserestrictinfo;
}
else
{
child_root = child_plan_info->grouped_root_local;
child_baserel = child_plan_info->grouped_rel_local;
restrictinfo = NIL;
}
child_plan = child_plan_info->fdw_routine->GetForeignPlan(child_root,
child_baserel,
child_table_oid,
(ForeignPath *) child_path,
child_tlist,