-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfdw.c
403 lines (335 loc) · 11.6 KB
/
fdw.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
#include <assert.h>
#include <stdio.h>
#include <stdbool.h>
#include <postgres.h>
#include <fmgr.h>
#include <access/htup_details.h>
#include <access/xact.h>
#include <catalog/pg_type.h>
#include <executor/executor.h>
#include <foreign/fdwapi.h>
#include <foreign/foreign.h>
#include <nodes/makefuncs.h>
#include <nodes/pg_list.h>
#include <optimizer/pathnode.h>
#include <optimizer/planmain.h>
#include <optimizer/restrictinfo.h>
#include <utils/builtins.h> // for TextDatumGetCString()
#include <utils/rel.h>
#include <utils/typcache.h>
#include "go_functions.h"
#include "dns.h"
#include "misc.h"
#include "fdw.h"
PG_MODULE_MAGIC;
/*----------------
* for all following functions, refer to
* https://www.postgresql.org/docs/12/fdw-callbacks.html
*----------------
*/
void r53dbGetForeignRelSize(
PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid
) {
// OPTIONAL: set estimates: baserel->rows, ->width, ->tuples
// This needs to exist, but doesn't really need to do anything for now.
elog(DEBUG1, "r53db GetForeignRelSize(): NOP");
}
void r53dbGetForeignPaths(
PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid
) {
elog(DEBUG1, "r53db GetForeignPaths()");
ForeignPath *fp = create_foreignscan_path(
root,
baserel,
NULL, // baserel->reltarget, // target
baserel->rows, // rows
(Cost) 53, // startup_cost,
(Cost) 53, // total_cost,
NULL, // pathkeys
baserel->lateral_relids, // required_outer,
NULL, // fdw_outerpath,
NULL // fdw_private
);
add_path(baserel, (Path *) fp);
}
ForeignScan *r53dbGetForeignPlan(
PlannerInfo *root,
RelOptInfo *baserel,
Oid foreigntableid,
ForeignPath *best_path,
List *tlist,
List *scan_clauses,
Plan *outer_plan
) {
elog(DEBUG1, "r53db GetForeignPlan()");
return make_foreignscan(
tlist, // qptlist
extract_actual_clauses(scan_clauses, false), // qpqual
baserel->relid, // scanrelid
NULL, // fdw_exprs
best_path->fdw_private, // fdw_private
NULL, // fdw_scan_tlist
NULL, // fdw_recheck_quals
outer_plan // outer_plan
);
}
void r53dbBeginForeignScan(ForeignScanState *node, int eflags) {
elog(DEBUG1, "r53db BeginForeignScan()");
r53dbScanState *scanState = (r53dbScanState *) palloc0(sizeof(r53dbScanState));
scanState->column_positions = NIL;
scanState->results = NIL;
node->fdw_state = (void *) scanState;
TupleTableSlot *tts = (TupleTableSlot *) node->ss.ss_ScanTupleSlot;
char *hosted_zone_id = get_relation_hosted_zone_id(node->ss.ss_currentRelation->rd_id);
char *relname = NameStr(node->ss.ss_currentRelation->rd_rel->relname);
elog(DEBUG1, "BeginForeignScan of foreign table %s (hosted_zone_id %s)", relname, hosted_zone_id);
scanState->column_positions = get_column_positions(tts->tts_tupleDescriptor);
r53dbGoBeginScan((void *) scanState, hosted_zone_id);
}
TupleTableSlot *r53dbIterateForeignScan(ForeignScanState *node) {
elog(DEBUG1, "r53db IterateForeignScan()");
TupleTableSlot *tts = node->ss.ss_ScanTupleSlot;
Datum *values = tts->tts_values;
bool *isnull = tts->tts_isnull;
// init all results to NULL
// (precaution only, as every result should be set below)
for (int i = 0; i < tts->tts_tupleDescriptor->natts; i++) {
values[i] = PointerGetDatum(NULL);
isnull[i] = true;
}
r53dbScanState *scanState = (r53dbScanState *) node->fdw_state;
if (scanState->result_index == list_length(scanState->results)) {
// done
return NULL;
}
r53dbDNSRR *rr = (r53dbDNSRR *) list_nth(scanState->results, scanState->result_index);
scanState->result_index++;
elog(
DEBUG2,
"scanState@%p tts_values@%p tts_isnull=%p rname=%s rdata=%s",
scanState, values, isnull,
rr->name, rr->data != NULL ? rr->data : "(NULL)"
);
ExecClearTuple(tts);
ListCell *lc;
foreach(lc, scanState->column_positions) {
r53dbColumnPosition *cp = (r53dbColumnPosition *) lfirst(lc);
switch (cp->column) {
case name:
values[cp->position] = CStringGetTextDatum(rr->name);
isnull[cp->position] = false;
break;
case type:
values[cp->position] = CStringGetTextDatum(rr->type);
isnull[cp->position] = false;
break;
case ttl:
values[cp->position] = Int32GetDatum(rr->ttl);
isnull[cp->position] = (rr->at_dns_name != NULL);
break;
case data:
values[cp->position] = CStringGetTextDatumOrNULL(rr->data);
isnull[cp->position] = (rr->at_dns_name != NULL);
break;
case at_dns_name:
values[cp->position] = CStringGetTextDatumOrNULL(rr->at_dns_name);
isnull[cp->position] = (rr->at_dns_name == NULL);
break;
case at_hosted_zone_id:
values[cp->position] = CStringGetTextDatumOrNULL(rr->at_hosted_zone_id);
isnull[cp->position] = (rr->at_dns_name == NULL);
break;
case at_evaluate_target_health:
values[cp->position] = BoolGetDatum(rr->at_evaluate_target_health);
isnull[cp->position] = (rr->at_dns_name == NULL);
break;
default:
elog(ERROR, "Internal error: Invalid column %d in column list", cp->column);
}
}
ExecStoreVirtualTuple(tts);
return tts;
}
void r53dbReScanForeignScan(ForeignScanState *node) {
elog(ERROR, "r53db: ReScanForeignScan not yet implemented");
}
void r53dbEndForeignScan(ForeignScanState *node) {
elog(DEBUG1, "r53db EndForeignScan()");
r53dbGoEndScan();
}
List *r53dbImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid) {
elog(DEBUG1, "r53db ImportForeignSchema()");
ForeignServer *fs = GetForeignServer(serverOid);
List *zones = (List *) r53dbGoGetZones(NULL);
elog(DEBUG2, "... zoneList@%p", zones);
elog(DEBUG2, "... serverName@%p", fs->servername);
List *statements = NIL;
ListCell *lc;
foreach(lc, zones) {
r53dbZone *zone = (r53dbZone *) lfirst(lc);
elog(DEBUG2, "... zoneName@%p zoneId@%p", zone->name, zone->id);
char *statement = psprintf(
"CREATE FOREIGN TABLE IF NOT EXISTS %s ("
"name text not null, "
"type text not null, "
"ttl int default 300, " // 300s is the default in the AWS Console
"data text, "
"at_dns_name text, "
"at_hosted_zone_id text, "
"at_evaluate_target_health bool"
") "
"SERVER %s "
"OPTIONS (dns_name '%s', hosted_zone_id '%s')",
zone->table_name,
fs->servername,
zone->name,
zone->id
);
statements = lappend(statements, statement);
elog(DEBUG2, "%s", statement);
}
return statements;
}
void r53dbAddForeignUpdateTargets(Query *parsetree, RangeTblEntry *target_rte, Relation target_relation) {
elog(DEBUG1, "r53db AddForeignUpdateTargets()");
Var *var = makeWholeRowVar(target_rte, parsetree->resultRelation, 0, false);
TargetEntry *tle;
tle = makeTargetEntry((Expr *) var, list_length(parsetree->targetList) + 1, "r53wholerow", true);
parsetree->targetList = lappend(parsetree->targetList, tle);
}
void r53dbBeginForeignModify(
ModifyTableState *mtstate,
ResultRelInfo *rinfo,
List *fdw_private,
int subplan_index,
int eflags
) {
if (IsTransactionBlock()) {
char *table = NameStr(rinfo->ri_RelationDesc->rd_rel->relname);
elog(ERROR, "Route53 data cannot be modified in a transaction (foreign table %s)", table);
return;
}
r53dbModifyState *modifyState = palloc0(sizeof(r53dbModifyState));
modifyState->operation = mtstate->operation;
modifyState->hosted_zone_id = get_relation_hosted_zone_id(rinfo->ri_RelationDesc->rd_id);
modifyState->column_positions = get_column_positions(rinfo->ri_RelationDesc->rd_att);
if (mtstate->operation != CMD_INSERT) {
// For UPDATE/DELETE, figure out at which position our junk row is
modifyState->junk_row_resno = InvalidAttrNumber;
ListCell *lc;
foreach(lc, mtstate->mt_plans[subplan_index]->plan->targetlist) {
TargetEntry *tle = lfirst(lc);
if (tle->resjunk) {
elog(DEBUG2, "... junk column %s at resno %d", tle->resname, tle->resno);
modifyState->junk_row_resno = tle->resno;
}
}
if (modifyState->junk_row_resno == InvalidAttrNumber) {
elog(ERROR, "r53dbBeginForeignModify(): Cannot find resjunk target?!");
return;
}
}
// Pg expects a List node as fdw_private, so let's wrap our state struct
// in a single-element List.
rinfo->ri_FdwState = lappend(NIL, modifyState);
}
TupleTableSlot *r53dbExecForeignModify(
EState *estate,
ResultRelInfo *rinfo,
TupleTableSlot *slot,
TupleTableSlot *planSlot
) {
elog(DEBUG1, "r53db ExecForeignModify()");
Datum *values = NULL;
bool *isnulls = NULL;
TupleDesc tupDesc = NULL;
r53dbDNSRR *oldRR = NULL;
r53dbDNSRR *newRR = NULL;
r53dbModifyState *modifyState = (r53dbModifyState *) linitial(rinfo->ri_FdwState);
elog(DEBUG1, "... zone %s", modifyState->hosted_zone_id);
/*
* INSERT and UPDATE will have a filled tuple with the new value ready-to-go in 'slot'.
*
* For UPDATE and DELETE, we need to identify the old row with the junk column added
* in r53dbAddForeignUpdateTargets() (via 'planSlot').
*/
if (modifyState->operation != CMD_DELETE) {
// for INSERT, UPDATE: Get the new RR
values = slot->tts_values;
isnulls = slot->tts_isnull;
tupDesc = slot->tts_tupleDescriptor;
slot_getallattrs(slot);
newRR = get_rr_from_values(values, isnulls, modifyState->column_positions);
}
if (modifyState->operation != CMD_INSERT) {
// for UPDATE, DELETE: Get the old RR
elog(DEBUG2, "r53db ExecForeignModify: Retrieve junk column");
slot_getallattrs(planSlot);
// Now, having our whole-row junk column, build a HeapTuple and point
// values and isnulls there.
// code stolen from execUtils.c, GetAttributeByName()
// https://doxygen.postgresql.org/execUtils_8c.html#ac7de9629c5bfbf1c4734824f03a5e281
HeapTupleHeader ht_header = DatumGetHeapTupleHeader(planSlot->tts_values[modifyState->junk_row_resno - 1]);
Oid tupType = HeapTupleHeaderGetTypeId(ht_header);
int32 tupTypmod = HeapTupleHeaderGetTypMod(ht_header);
tupDesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
HeapTupleData ht_data;
ht_data.t_len = HeapTupleHeaderGetDatumLength(ht_header);
ItemPointerSetInvalid(&ht_data.t_self);
ht_data.t_tableOid = InvalidOid;
ht_data.t_data = ht_header;
values = palloc0(sizeof(Datum) * tupDesc->natts);
isnulls = palloc0(sizeof(bool) * tupDesc->natts);
heap_deform_tuple((HeapTuple) &ht_data, tupDesc, values, isnulls);
oldRR = get_rr_from_values(values, isnulls, modifyState->column_positions);
ReleaseTupleDesc(tupDesc);
}
bool is_success = false;
switch (modifyState->operation) {
case CMD_INSERT:
is_success = r53dbGoModifyDNSRR(modifyState->hosted_zone_id, newRR, oldRR, DML_INSERT);
break;
case CMD_UPDATE:
is_success = r53dbGoModifyDNSRR(modifyState->hosted_zone_id, newRR, oldRR, DML_UPDATE);
break;
case CMD_DELETE:
is_success = r53dbGoModifyDNSRR(modifyState->hosted_zone_id, newRR, oldRR, DML_DELETE);
if (is_success) {
// fill the passed-in 'slot' with the tuple to be returned
ExecClearTuple(slot);
slot->tts_values = values;
slot->tts_isnull = isnulls;
ExecStoreVirtualTuple(slot);
}
break;
default:
elog(ERROR, "unexpected operation commandType %d", modifyState->operation);
return NULL;
}
elog(DEBUG1, "r53db ExecForeignModify() *DONE*");
return is_success ? slot : NULL;
}
PG_FUNCTION_INFO_V1(r53db_fdw_handler);
Datum r53db_fdw_handler(PG_FUNCTION_ARGS) {
FdwRoutine *fdw = makeNode(FdwRoutine);
elog(DEBUG1, "r53db says hi!");
r53dbGoOnLoad();
fdw->GetForeignRelSize = r53dbGetForeignRelSize;
fdw->GetForeignPaths = r53dbGetForeignPaths;
fdw->GetForeignPlan = r53dbGetForeignPlan;
fdw->BeginForeignScan = r53dbBeginForeignScan;
fdw->IterateForeignScan = r53dbIterateForeignScan;
fdw->ReScanForeignScan = r53dbReScanForeignScan;
fdw->EndForeignScan = r53dbEndForeignScan;
fdw->ImportForeignSchema = r53dbImportForeignSchema;
fdw->BeginForeignModify = r53dbBeginForeignModify;
fdw->ExecForeignInsert = r53dbExecForeignModify;
fdw->ExecForeignUpdate = r53dbExecForeignModify;
fdw->ExecForeignDelete = r53dbExecForeignModify;
fdw->AddForeignUpdateTargets = r53dbAddForeignUpdateTargets;
PG_RETURN_POINTER(fdw);
}