/src/postgres/src/backend/foreign/foreign.c
Line | Count | Source |
1 | | /*------------------------------------------------------------------------- |
2 | | * |
3 | | * foreign.c |
4 | | * support for foreign-data wrappers, servers and user mappings. |
5 | | * |
6 | | * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group |
7 | | * |
8 | | * IDENTIFICATION |
9 | | * src/backend/foreign/foreign.c |
10 | | * |
11 | | *------------------------------------------------------------------------- |
12 | | */ |
13 | | #include "postgres.h" |
14 | | |
15 | | #include "access/htup_details.h" |
16 | | #include "access/reloptions.h" |
17 | | #include "catalog/pg_foreign_data_wrapper.h" |
18 | | #include "catalog/pg_foreign_server.h" |
19 | | #include "catalog/pg_foreign_table.h" |
20 | | #include "catalog/pg_user_mapping.h" |
21 | | #include "foreign/fdwapi.h" |
22 | | #include "foreign/foreign.h" |
23 | | #include "funcapi.h" |
24 | | #include "miscadmin.h" |
25 | | #include "optimizer/paths.h" |
26 | | #include "tcop/tcopprot.h" |
27 | | #include "utils/builtins.h" |
28 | | #include "utils/memutils.h" |
29 | | #include "utils/rel.h" |
30 | | #include "utils/syscache.h" |
31 | | #include "utils/tuplestore.h" |
32 | | #include "utils/varlena.h" |
33 | | |
34 | | |
35 | | /* |
36 | | * GetForeignDataWrapper - look up the foreign-data wrapper by OID. |
37 | | */ |
38 | | ForeignDataWrapper * |
39 | | GetForeignDataWrapper(Oid fdwid) |
40 | 0 | { |
41 | 0 | return GetForeignDataWrapperExtended(fdwid, 0); |
42 | 0 | } |
43 | | |
44 | | |
45 | | /* |
46 | | * GetForeignDataWrapperExtended - look up the foreign-data wrapper |
47 | | * by OID. If flags uses FDW_MISSING_OK, return NULL if the object cannot |
48 | | * be found instead of raising an error. |
49 | | */ |
50 | | ForeignDataWrapper * |
51 | | GetForeignDataWrapperExtended(Oid fdwid, uint16 flags) |
52 | 0 | { |
53 | 0 | Form_pg_foreign_data_wrapper fdwform; |
54 | 0 | ForeignDataWrapper *fdw; |
55 | 0 | Datum datum; |
56 | 0 | HeapTuple tp; |
57 | 0 | bool isnull; |
58 | |
|
59 | 0 | tp = SearchSysCache1(FOREIGNDATAWRAPPEROID, ObjectIdGetDatum(fdwid)); |
60 | |
|
61 | 0 | if (!HeapTupleIsValid(tp)) |
62 | 0 | { |
63 | 0 | if ((flags & FDW_MISSING_OK) == 0) |
64 | 0 | elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid); |
65 | 0 | return NULL; |
66 | 0 | } |
67 | | |
68 | 0 | fdwform = (Form_pg_foreign_data_wrapper) GETSTRUCT(tp); |
69 | |
|
70 | 0 | fdw = palloc_object(ForeignDataWrapper); |
71 | 0 | fdw->fdwid = fdwid; |
72 | 0 | fdw->owner = fdwform->fdwowner; |
73 | 0 | fdw->fdwname = pstrdup(NameStr(fdwform->fdwname)); |
74 | 0 | fdw->fdwhandler = fdwform->fdwhandler; |
75 | 0 | fdw->fdwvalidator = fdwform->fdwvalidator; |
76 | 0 | fdw->fdwconnection = fdwform->fdwconnection; |
77 | | |
78 | | /* Extract the fdwoptions */ |
79 | 0 | datum = SysCacheGetAttr(FOREIGNDATAWRAPPEROID, |
80 | 0 | tp, |
81 | 0 | Anum_pg_foreign_data_wrapper_fdwoptions, |
82 | 0 | &isnull); |
83 | 0 | if (isnull) |
84 | 0 | fdw->options = NIL; |
85 | 0 | else |
86 | 0 | fdw->options = untransformRelOptions(datum); |
87 | |
|
88 | 0 | ReleaseSysCache(tp); |
89 | |
|
90 | 0 | return fdw; |
91 | 0 | } |
92 | | |
93 | | |
94 | | /* |
95 | | * GetForeignDataWrapperByName - look up the foreign-data wrapper |
96 | | * definition by name. |
97 | | */ |
98 | | ForeignDataWrapper * |
99 | | GetForeignDataWrapperByName(const char *fdwname, bool missing_ok) |
100 | 0 | { |
101 | 0 | Oid fdwId = get_foreign_data_wrapper_oid(fdwname, missing_ok); |
102 | |
|
103 | 0 | if (!OidIsValid(fdwId)) |
104 | 0 | return NULL; |
105 | | |
106 | 0 | return GetForeignDataWrapper(fdwId); |
107 | 0 | } |
108 | | |
109 | | |
110 | | /* |
111 | | * GetForeignServer - look up the foreign server definition. |
112 | | */ |
113 | | ForeignServer * |
114 | | GetForeignServer(Oid serverid) |
115 | 0 | { |
116 | 0 | return GetForeignServerExtended(serverid, 0); |
117 | 0 | } |
118 | | |
119 | | |
120 | | /* |
121 | | * GetForeignServerExtended - look up the foreign server definition. If |
122 | | * flags uses FSV_MISSING_OK, return NULL if the object cannot be found |
123 | | * instead of raising an error. |
124 | | */ |
125 | | ForeignServer * |
126 | | GetForeignServerExtended(Oid serverid, uint16 flags) |
127 | 0 | { |
128 | 0 | Form_pg_foreign_server serverform; |
129 | 0 | ForeignServer *server; |
130 | 0 | HeapTuple tp; |
131 | 0 | Datum datum; |
132 | 0 | bool isnull; |
133 | |
|
134 | 0 | tp = SearchSysCache1(FOREIGNSERVEROID, ObjectIdGetDatum(serverid)); |
135 | |
|
136 | 0 | if (!HeapTupleIsValid(tp)) |
137 | 0 | { |
138 | 0 | if ((flags & FSV_MISSING_OK) == 0) |
139 | 0 | elog(ERROR, "cache lookup failed for foreign server %u", serverid); |
140 | 0 | return NULL; |
141 | 0 | } |
142 | | |
143 | 0 | serverform = (Form_pg_foreign_server) GETSTRUCT(tp); |
144 | |
|
145 | 0 | server = palloc_object(ForeignServer); |
146 | 0 | server->serverid = serverid; |
147 | 0 | server->servername = pstrdup(NameStr(serverform->srvname)); |
148 | 0 | server->owner = serverform->srvowner; |
149 | 0 | server->fdwid = serverform->srvfdw; |
150 | | |
151 | | /* Extract server type */ |
152 | 0 | datum = SysCacheGetAttr(FOREIGNSERVEROID, |
153 | 0 | tp, |
154 | 0 | Anum_pg_foreign_server_srvtype, |
155 | 0 | &isnull); |
156 | 0 | server->servertype = isnull ? NULL : TextDatumGetCString(datum); |
157 | | |
158 | | /* Extract server version */ |
159 | 0 | datum = SysCacheGetAttr(FOREIGNSERVEROID, |
160 | 0 | tp, |
161 | 0 | Anum_pg_foreign_server_srvversion, |
162 | 0 | &isnull); |
163 | 0 | server->serverversion = isnull ? NULL : TextDatumGetCString(datum); |
164 | | |
165 | | /* Extract the srvoptions */ |
166 | 0 | datum = SysCacheGetAttr(FOREIGNSERVEROID, |
167 | 0 | tp, |
168 | 0 | Anum_pg_foreign_server_srvoptions, |
169 | 0 | &isnull); |
170 | 0 | if (isnull) |
171 | 0 | server->options = NIL; |
172 | 0 | else |
173 | 0 | server->options = untransformRelOptions(datum); |
174 | |
|
175 | 0 | ReleaseSysCache(tp); |
176 | |
|
177 | 0 | return server; |
178 | 0 | } |
179 | | |
180 | | |
181 | | /* |
182 | | * GetForeignServerByName - look up the foreign server definition by name. |
183 | | */ |
184 | | ForeignServer * |
185 | | GetForeignServerByName(const char *srvname, bool missing_ok) |
186 | 0 | { |
187 | 0 | Oid serverid = get_foreign_server_oid(srvname, missing_ok); |
188 | |
|
189 | 0 | if (!OidIsValid(serverid)) |
190 | 0 | return NULL; |
191 | | |
192 | 0 | return GetForeignServer(serverid); |
193 | 0 | } |
194 | | |
195 | | |
196 | | /* |
197 | | * Retrieve connection string from server's FDW. |
198 | | * |
199 | | * NB: leaks into CurrentMemoryContext. |
200 | | */ |
201 | | char * |
202 | | ForeignServerConnectionString(Oid userid, ForeignServer *server) |
203 | 0 | { |
204 | 0 | ForeignDataWrapper *fdw; |
205 | 0 | Datum connection_datum; |
206 | |
|
207 | 0 | fdw = GetForeignDataWrapper(server->fdwid); |
208 | |
|
209 | 0 | if (!OidIsValid(fdw->fdwconnection)) |
210 | 0 | ereport(ERROR, |
211 | 0 | (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
212 | 0 | errmsg("foreign-data wrapper \"%s\" does not support subscription connections", |
213 | 0 | fdw->fdwname), |
214 | 0 | errdetail("Foreign-data wrapper must be defined with CONNECTION specified."))); |
215 | | |
216 | 0 | connection_datum = OidFunctionCall3(fdw->fdwconnection, |
217 | 0 | ObjectIdGetDatum(userid), |
218 | 0 | ObjectIdGetDatum(server->serverid), |
219 | 0 | PointerGetDatum(NULL)); |
220 | |
|
221 | 0 | return text_to_cstring(DatumGetTextPP(connection_datum)); |
222 | 0 | } |
223 | | |
224 | | |
225 | | /* |
226 | | * GetUserMapping - look up the user mapping. |
227 | | * |
228 | | * If no mapping is found for the supplied user, we also look for |
229 | | * PUBLIC mappings (userid == InvalidOid). |
230 | | */ |
231 | | UserMapping * |
232 | | GetUserMapping(Oid userid, Oid serverid) |
233 | 0 | { |
234 | 0 | return GetUserMappingExtended(userid, serverid, ERROR); |
235 | 0 | } |
236 | | |
237 | | /* |
238 | | * Like GetUserMapping(), but allows caller to specify an elevel. If elevel is |
239 | | * less than ERROR, returns NULL if the user mapping doesn't exist. |
240 | | */ |
241 | | UserMapping * |
242 | | GetUserMappingExtended(Oid userid, Oid serverid, int elevel) |
243 | 0 | { |
244 | 0 | Datum datum; |
245 | 0 | HeapTuple tp; |
246 | 0 | bool isnull; |
247 | 0 | UserMapping *um; |
248 | |
|
249 | 0 | tp = SearchSysCache2(USERMAPPINGUSERSERVER, |
250 | 0 | ObjectIdGetDatum(userid), |
251 | 0 | ObjectIdGetDatum(serverid)); |
252 | |
|
253 | 0 | if (!HeapTupleIsValid(tp)) |
254 | 0 | { |
255 | | /* Not found for the specific user -- try PUBLIC */ |
256 | 0 | tp = SearchSysCache2(USERMAPPINGUSERSERVER, |
257 | 0 | ObjectIdGetDatum(InvalidOid), |
258 | 0 | ObjectIdGetDatum(serverid)); |
259 | 0 | } |
260 | |
|
261 | 0 | if (!HeapTupleIsValid(tp)) |
262 | 0 | { |
263 | 0 | ForeignServer *server = GetForeignServer(serverid); |
264 | |
|
265 | 0 | ereport(elevel, |
266 | 0 | (errcode(ERRCODE_UNDEFINED_OBJECT), |
267 | 0 | errmsg("user mapping not found for user \"%s\", server \"%s\"", |
268 | 0 | MappingUserName(userid), server->servername))); |
269 | | |
270 | 0 | return NULL; |
271 | 0 | } |
272 | | |
273 | 0 | um = palloc_object(UserMapping); |
274 | 0 | um->umid = ((Form_pg_user_mapping) GETSTRUCT(tp))->oid; |
275 | 0 | um->userid = userid; |
276 | 0 | um->serverid = serverid; |
277 | | |
278 | | /* Extract the umoptions */ |
279 | 0 | datum = SysCacheGetAttr(USERMAPPINGUSERSERVER, |
280 | 0 | tp, |
281 | 0 | Anum_pg_user_mapping_umoptions, |
282 | 0 | &isnull); |
283 | 0 | if (isnull) |
284 | 0 | um->options = NIL; |
285 | 0 | else |
286 | 0 | um->options = untransformRelOptions(datum); |
287 | |
|
288 | 0 | ReleaseSysCache(tp); |
289 | |
|
290 | 0 | return um; |
291 | 0 | } |
292 | | |
293 | | |
294 | | /* |
295 | | * GetForeignTable - look up the foreign table definition by relation oid. |
296 | | */ |
297 | | ForeignTable * |
298 | | GetForeignTable(Oid relid) |
299 | 0 | { |
300 | 0 | Form_pg_foreign_table tableform; |
301 | 0 | ForeignTable *ft; |
302 | 0 | HeapTuple tp; |
303 | 0 | Datum datum; |
304 | 0 | bool isnull; |
305 | |
|
306 | 0 | tp = SearchSysCache1(FOREIGNTABLEREL, ObjectIdGetDatum(relid)); |
307 | 0 | if (!HeapTupleIsValid(tp)) |
308 | 0 | elog(ERROR, "cache lookup failed for foreign table %u", relid); |
309 | 0 | tableform = (Form_pg_foreign_table) GETSTRUCT(tp); |
310 | |
|
311 | 0 | ft = palloc_object(ForeignTable); |
312 | 0 | ft->relid = relid; |
313 | 0 | ft->serverid = tableform->ftserver; |
314 | | |
315 | | /* Extract the ftoptions */ |
316 | 0 | datum = SysCacheGetAttr(FOREIGNTABLEREL, |
317 | 0 | tp, |
318 | 0 | Anum_pg_foreign_table_ftoptions, |
319 | 0 | &isnull); |
320 | 0 | if (isnull) |
321 | 0 | ft->options = NIL; |
322 | 0 | else |
323 | 0 | ft->options = untransformRelOptions(datum); |
324 | |
|
325 | 0 | ReleaseSysCache(tp); |
326 | |
|
327 | 0 | return ft; |
328 | 0 | } |
329 | | |
330 | | |
331 | | /* |
332 | | * GetForeignColumnOptions - Get attfdwoptions of given relation/attnum |
333 | | * as list of DefElem. |
334 | | */ |
335 | | List * |
336 | | GetForeignColumnOptions(Oid relid, AttrNumber attnum) |
337 | 0 | { |
338 | 0 | List *options; |
339 | 0 | HeapTuple tp; |
340 | 0 | Datum datum; |
341 | 0 | bool isnull; |
342 | |
|
343 | 0 | tp = SearchSysCache2(ATTNUM, |
344 | 0 | ObjectIdGetDatum(relid), |
345 | 0 | Int16GetDatum(attnum)); |
346 | 0 | if (!HeapTupleIsValid(tp)) |
347 | 0 | elog(ERROR, "cache lookup failed for attribute %d of relation %u", |
348 | 0 | attnum, relid); |
349 | 0 | datum = SysCacheGetAttr(ATTNUM, |
350 | 0 | tp, |
351 | 0 | Anum_pg_attribute_attfdwoptions, |
352 | 0 | &isnull); |
353 | 0 | if (isnull) |
354 | 0 | options = NIL; |
355 | 0 | else |
356 | 0 | options = untransformRelOptions(datum); |
357 | |
|
358 | 0 | ReleaseSysCache(tp); |
359 | |
|
360 | 0 | return options; |
361 | 0 | } |
362 | | |
363 | | |
364 | | /* |
365 | | * GetFdwRoutine - call the specified foreign-data wrapper handler routine |
366 | | * to get its FdwRoutine struct. |
367 | | */ |
368 | | FdwRoutine * |
369 | | GetFdwRoutine(Oid fdwhandler) |
370 | 0 | { |
371 | 0 | Datum datum; |
372 | 0 | FdwRoutine *routine; |
373 | | |
374 | | /* Check if the access to foreign tables is restricted */ |
375 | 0 | if (unlikely((restrict_nonsystem_relation_kind & RESTRICT_RELKIND_FOREIGN_TABLE) != 0)) |
376 | 0 | { |
377 | | /* there must not be built-in FDW handler */ |
378 | 0 | ereport(ERROR, |
379 | 0 | (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
380 | 0 | errmsg("access to non-system foreign table is restricted"))); |
381 | 0 | } |
382 | | |
383 | 0 | datum = OidFunctionCall0(fdwhandler); |
384 | 0 | routine = (FdwRoutine *) DatumGetPointer(datum); |
385 | |
|
386 | 0 | if (routine == NULL || !IsA(routine, FdwRoutine)) |
387 | 0 | elog(ERROR, "foreign-data wrapper handler function %u did not return an FdwRoutine struct", |
388 | 0 | fdwhandler); |
389 | | |
390 | 0 | return routine; |
391 | 0 | } |
392 | | |
393 | | |
394 | | /* |
395 | | * GetForeignServerIdByRelId - look up the foreign server |
396 | | * for the given foreign table, and return its OID. |
397 | | */ |
398 | | Oid |
399 | | GetForeignServerIdByRelId(Oid relid) |
400 | 0 | { |
401 | 0 | HeapTuple tp; |
402 | 0 | Form_pg_foreign_table tableform; |
403 | 0 | Oid serverid; |
404 | |
|
405 | 0 | tp = SearchSysCache1(FOREIGNTABLEREL, ObjectIdGetDatum(relid)); |
406 | 0 | if (!HeapTupleIsValid(tp)) |
407 | 0 | elog(ERROR, "cache lookup failed for foreign table %u", relid); |
408 | 0 | tableform = (Form_pg_foreign_table) GETSTRUCT(tp); |
409 | 0 | serverid = tableform->ftserver; |
410 | 0 | ReleaseSysCache(tp); |
411 | |
|
412 | 0 | return serverid; |
413 | 0 | } |
414 | | |
415 | | |
416 | | /* |
417 | | * GetFdwRoutineByServerId - look up the handler of the foreign-data wrapper |
418 | | * for the given foreign server, and retrieve its FdwRoutine struct. |
419 | | */ |
420 | | FdwRoutine * |
421 | | GetFdwRoutineByServerId(Oid serverid) |
422 | 0 | { |
423 | 0 | HeapTuple tp; |
424 | 0 | Form_pg_foreign_data_wrapper fdwform; |
425 | 0 | Form_pg_foreign_server serverform; |
426 | 0 | Oid fdwid; |
427 | 0 | Oid fdwhandler; |
428 | | |
429 | | /* Get foreign-data wrapper OID for the server. */ |
430 | 0 | tp = SearchSysCache1(FOREIGNSERVEROID, ObjectIdGetDatum(serverid)); |
431 | 0 | if (!HeapTupleIsValid(tp)) |
432 | 0 | elog(ERROR, "cache lookup failed for foreign server %u", serverid); |
433 | 0 | serverform = (Form_pg_foreign_server) GETSTRUCT(tp); |
434 | 0 | fdwid = serverform->srvfdw; |
435 | 0 | ReleaseSysCache(tp); |
436 | | |
437 | | /* Get handler function OID for the FDW. */ |
438 | 0 | tp = SearchSysCache1(FOREIGNDATAWRAPPEROID, ObjectIdGetDatum(fdwid)); |
439 | 0 | if (!HeapTupleIsValid(tp)) |
440 | 0 | elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid); |
441 | 0 | fdwform = (Form_pg_foreign_data_wrapper) GETSTRUCT(tp); |
442 | 0 | fdwhandler = fdwform->fdwhandler; |
443 | | |
444 | | /* Complain if FDW has been set to NO HANDLER. */ |
445 | 0 | if (!OidIsValid(fdwhandler)) |
446 | 0 | ereport(ERROR, |
447 | 0 | (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), |
448 | 0 | errmsg("foreign-data wrapper \"%s\" has no handler", |
449 | 0 | NameStr(fdwform->fdwname)))); |
450 | | |
451 | 0 | ReleaseSysCache(tp); |
452 | | |
453 | | /* And finally, call the handler function. */ |
454 | 0 | return GetFdwRoutine(fdwhandler); |
455 | 0 | } |
456 | | |
457 | | |
458 | | /* |
459 | | * GetFdwRoutineByRelId - look up the handler of the foreign-data wrapper |
460 | | * for the given foreign table, and retrieve its FdwRoutine struct. |
461 | | */ |
462 | | FdwRoutine * |
463 | | GetFdwRoutineByRelId(Oid relid) |
464 | 0 | { |
465 | 0 | Oid serverid; |
466 | | |
467 | | /* Get server OID for the foreign table. */ |
468 | 0 | serverid = GetForeignServerIdByRelId(relid); |
469 | | |
470 | | /* Now retrieve server's FdwRoutine struct. */ |
471 | 0 | return GetFdwRoutineByServerId(serverid); |
472 | 0 | } |
473 | | |
474 | | /* |
475 | | * GetFdwRoutineForRelation - look up the handler of the foreign-data wrapper |
476 | | * for the given foreign table, and retrieve its FdwRoutine struct. |
477 | | * |
478 | | * This function is preferred over GetFdwRoutineByRelId because it caches |
479 | | * the data in the relcache entry, saving a number of catalog lookups. |
480 | | * |
481 | | * If makecopy is true then the returned data is freshly palloc'd in the |
482 | | * caller's memory context. Otherwise, it's a pointer to the relcache data, |
483 | | * which will be lost in any relcache reset --- so don't rely on it long. |
484 | | */ |
485 | | FdwRoutine * |
486 | | GetFdwRoutineForRelation(Relation relation, bool makecopy) |
487 | 0 | { |
488 | 0 | FdwRoutine *fdwroutine; |
489 | 0 | FdwRoutine *cfdwroutine; |
490 | |
|
491 | 0 | if (relation->rd_fdwroutine == NULL) |
492 | 0 | { |
493 | | /* Get the info by consulting the catalogs and the FDW code */ |
494 | 0 | fdwroutine = GetFdwRoutineByRelId(RelationGetRelid(relation)); |
495 | | |
496 | | /* Save the data for later reuse in CacheMemoryContext */ |
497 | 0 | cfdwroutine = (FdwRoutine *) MemoryContextAlloc(CacheMemoryContext, |
498 | 0 | sizeof(FdwRoutine)); |
499 | 0 | memcpy(cfdwroutine, fdwroutine, sizeof(FdwRoutine)); |
500 | 0 | relation->rd_fdwroutine = cfdwroutine; |
501 | | |
502 | | /* Give back the locally palloc'd copy regardless of makecopy */ |
503 | 0 | return fdwroutine; |
504 | 0 | } |
505 | | |
506 | | /* We have valid cached data --- does the caller want a copy? */ |
507 | 0 | if (makecopy) |
508 | 0 | { |
509 | 0 | fdwroutine = palloc_object(FdwRoutine); |
510 | 0 | memcpy(fdwroutine, relation->rd_fdwroutine, sizeof(FdwRoutine)); |
511 | 0 | return fdwroutine; |
512 | 0 | } |
513 | | |
514 | | /* Only a short-lived reference is needed, so just hand back cached copy */ |
515 | 0 | return relation->rd_fdwroutine; |
516 | 0 | } |
517 | | |
518 | | |
519 | | /* |
520 | | * IsImportableForeignTable - filter table names for IMPORT FOREIGN SCHEMA |
521 | | * |
522 | | * Returns true if given table name should be imported according to the |
523 | | * statement's import filter options. |
524 | | */ |
525 | | bool |
526 | | IsImportableForeignTable(const char *tablename, |
527 | | ImportForeignSchemaStmt *stmt) |
528 | 0 | { |
529 | 0 | ListCell *lc; |
530 | |
|
531 | 0 | switch (stmt->list_type) |
532 | 0 | { |
533 | 0 | case FDW_IMPORT_SCHEMA_ALL: |
534 | 0 | return true; |
535 | | |
536 | 0 | case FDW_IMPORT_SCHEMA_LIMIT_TO: |
537 | 0 | foreach(lc, stmt->table_list) |
538 | 0 | { |
539 | 0 | RangeVar *rv = (RangeVar *) lfirst(lc); |
540 | |
|
541 | 0 | if (strcmp(tablename, rv->relname) == 0) |
542 | 0 | return true; |
543 | 0 | } |
544 | 0 | return false; |
545 | | |
546 | 0 | case FDW_IMPORT_SCHEMA_EXCEPT: |
547 | 0 | foreach(lc, stmt->table_list) |
548 | 0 | { |
549 | 0 | RangeVar *rv = (RangeVar *) lfirst(lc); |
550 | |
|
551 | 0 | if (strcmp(tablename, rv->relname) == 0) |
552 | 0 | return false; |
553 | 0 | } |
554 | 0 | return true; |
555 | 0 | } |
556 | 0 | return false; /* shouldn't get here */ |
557 | 0 | } |
558 | | |
559 | | |
560 | | /* |
561 | | * pg_options_to_table - Convert options array to name/value table |
562 | | * |
563 | | * This is useful to provide details for information_schema and pg_dump. |
564 | | */ |
565 | | Datum |
566 | | pg_options_to_table(PG_FUNCTION_ARGS) |
567 | 0 | { |
568 | 0 | Datum array = PG_GETARG_DATUM(0); |
569 | 0 | ListCell *cell; |
570 | 0 | List *options; |
571 | 0 | ReturnSetInfo *rsinfo; |
572 | |
|
573 | 0 | options = untransformRelOptions(array); |
574 | 0 | rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
575 | | |
576 | | /* prepare the result set */ |
577 | 0 | InitMaterializedSRF(fcinfo, MAT_SRF_USE_EXPECTED_DESC); |
578 | |
|
579 | 0 | foreach(cell, options) |
580 | 0 | { |
581 | 0 | DefElem *def = lfirst(cell); |
582 | 0 | Datum values[2]; |
583 | 0 | bool nulls[2]; |
584 | |
|
585 | 0 | values[0] = CStringGetTextDatum(def->defname); |
586 | 0 | nulls[0] = false; |
587 | 0 | if (def->arg) |
588 | 0 | { |
589 | 0 | values[1] = CStringGetTextDatum(strVal(def->arg)); |
590 | 0 | nulls[1] = false; |
591 | 0 | } |
592 | 0 | else |
593 | 0 | { |
594 | 0 | values[1] = (Datum) 0; |
595 | 0 | nulls[1] = true; |
596 | 0 | } |
597 | 0 | tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, |
598 | 0 | values, nulls); |
599 | 0 | } |
600 | |
|
601 | 0 | return (Datum) 0; |
602 | 0 | } |
603 | | |
604 | | |
605 | | /* |
606 | | * Describes the valid options for postgresql FDW, server, and user mapping. |
607 | | */ |
608 | | struct ConnectionOption |
609 | | { |
610 | | const char *optname; |
611 | | Oid optcontext; /* Oid of catalog in which option may appear */ |
612 | | }; |
613 | | |
614 | | /* |
615 | | * Copied from fe-connect.c PQconninfoOptions. |
616 | | * |
617 | | * The list is small - don't bother with bsearch if it stays so. |
618 | | */ |
619 | | static const struct ConnectionOption libpq_conninfo_options[] = { |
620 | | {"authtype", ForeignServerRelationId}, |
621 | | {"service", ForeignServerRelationId}, |
622 | | {"user", UserMappingRelationId}, |
623 | | {"password", UserMappingRelationId}, |
624 | | {"connect_timeout", ForeignServerRelationId}, |
625 | | {"dbname", ForeignServerRelationId}, |
626 | | {"host", ForeignServerRelationId}, |
627 | | {"hostaddr", ForeignServerRelationId}, |
628 | | {"port", ForeignServerRelationId}, |
629 | | {"tty", ForeignServerRelationId}, |
630 | | {"options", ForeignServerRelationId}, |
631 | | {"requiressl", ForeignServerRelationId}, |
632 | | {"sslmode", ForeignServerRelationId}, |
633 | | {"gsslib", ForeignServerRelationId}, |
634 | | {"gssdelegation", ForeignServerRelationId}, |
635 | | {NULL, InvalidOid} |
636 | | }; |
637 | | |
638 | | |
639 | | /* |
640 | | * Check if the provided option is one of libpq conninfo options. |
641 | | * context is the Oid of the catalog the option came from, or 0 if we |
642 | | * don't care. |
643 | | */ |
644 | | static bool |
645 | | is_conninfo_option(const char *option, Oid context) |
646 | 0 | { |
647 | 0 | const struct ConnectionOption *opt; |
648 | |
|
649 | 0 | for (opt = libpq_conninfo_options; opt->optname; opt++) |
650 | 0 | if (context == opt->optcontext && strcmp(opt->optname, option) == 0) |
651 | 0 | return true; |
652 | 0 | return false; |
653 | 0 | } |
654 | | |
655 | | |
656 | | /* |
657 | | * Validate the generic option given to SERVER or USER MAPPING. |
658 | | * Raise an ERROR if the option or its value is considered invalid. |
659 | | * |
660 | | * Valid server options are all libpq conninfo options except |
661 | | * user and password -- these may only appear in USER MAPPING options. |
662 | | * |
663 | | * Caution: this function is deprecated, and is now meant only for testing |
664 | | * purposes, because the list of options it knows about doesn't necessarily |
665 | | * square with those known to whichever libpq instance you might be using. |
666 | | * Inquire of libpq itself, instead. |
667 | | */ |
668 | | Datum |
669 | | postgresql_fdw_validator(PG_FUNCTION_ARGS) |
670 | 0 | { |
671 | 0 | List *options_list = untransformRelOptions(PG_GETARG_DATUM(0)); |
672 | 0 | Oid catalog = PG_GETARG_OID(1); |
673 | |
|
674 | 0 | ListCell *cell; |
675 | |
|
676 | 0 | foreach(cell, options_list) |
677 | 0 | { |
678 | 0 | DefElem *def = lfirst(cell); |
679 | |
|
680 | 0 | if (!is_conninfo_option(def->defname, catalog)) |
681 | 0 | { |
682 | 0 | const struct ConnectionOption *opt; |
683 | 0 | const char *closest_match; |
684 | 0 | ClosestMatchState match_state; |
685 | 0 | bool has_valid_options = false; |
686 | | |
687 | | /* |
688 | | * Unknown option specified, complain about it. Provide a hint |
689 | | * with a valid option that looks similar, if there is one. |
690 | | */ |
691 | 0 | initClosestMatch(&match_state, def->defname, 4); |
692 | 0 | for (opt = libpq_conninfo_options; opt->optname; opt++) |
693 | 0 | { |
694 | 0 | if (catalog == opt->optcontext) |
695 | 0 | { |
696 | 0 | has_valid_options = true; |
697 | 0 | updateClosestMatch(&match_state, opt->optname); |
698 | 0 | } |
699 | 0 | } |
700 | |
|
701 | 0 | closest_match = getClosestMatch(&match_state); |
702 | 0 | ereport(ERROR, |
703 | 0 | (errcode(ERRCODE_SYNTAX_ERROR), |
704 | 0 | errmsg("invalid option \"%s\"", def->defname), |
705 | 0 | has_valid_options ? closest_match ? |
706 | 0 | errhint("Perhaps you meant the option \"%s\".", |
707 | 0 | closest_match) : 0 : |
708 | 0 | errhint("There are no valid options in this context."))); |
709 | | |
710 | 0 | PG_RETURN_BOOL(false); |
711 | 0 | } |
712 | 0 | } |
713 | | |
714 | 0 | PG_RETURN_BOOL(true); |
715 | 0 | } |
716 | | |
717 | | |
718 | | /* |
719 | | * get_foreign_data_wrapper_oid - given a FDW name, look up the OID |
720 | | * |
721 | | * If missing_ok is false, throw an error if name not found. If true, just |
722 | | * return InvalidOid. |
723 | | */ |
724 | | Oid |
725 | | get_foreign_data_wrapper_oid(const char *fdwname, bool missing_ok) |
726 | 0 | { |
727 | 0 | Oid oid; |
728 | |
|
729 | 0 | oid = GetSysCacheOid1(FOREIGNDATAWRAPPERNAME, |
730 | 0 | Anum_pg_foreign_data_wrapper_oid, |
731 | 0 | CStringGetDatum(fdwname)); |
732 | 0 | if (!OidIsValid(oid) && !missing_ok) |
733 | 0 | ereport(ERROR, |
734 | 0 | (errcode(ERRCODE_UNDEFINED_OBJECT), |
735 | 0 | errmsg("foreign-data wrapper \"%s\" does not exist", |
736 | 0 | fdwname))); |
737 | 0 | return oid; |
738 | 0 | } |
739 | | |
740 | | |
741 | | /* |
742 | | * get_foreign_server_oid - given a server name, look up the OID |
743 | | * |
744 | | * If missing_ok is false, throw an error if name not found. If true, just |
745 | | * return InvalidOid. |
746 | | */ |
747 | | Oid |
748 | | get_foreign_server_oid(const char *servername, bool missing_ok) |
749 | 0 | { |
750 | 0 | Oid oid; |
751 | |
|
752 | 0 | oid = GetSysCacheOid1(FOREIGNSERVERNAME, Anum_pg_foreign_server_oid, |
753 | 0 | CStringGetDatum(servername)); |
754 | 0 | if (!OidIsValid(oid) && !missing_ok) |
755 | 0 | ereport(ERROR, |
756 | 0 | (errcode(ERRCODE_UNDEFINED_OBJECT), |
757 | 0 | errmsg("server \"%s\" does not exist", servername))); |
758 | 0 | return oid; |
759 | 0 | } |
760 | | |
761 | | /* |
762 | | * Get a copy of an existing local path for a given join relation. |
763 | | * |
764 | | * This function is usually helpful to obtain an alternate local path for EPQ |
765 | | * checks. |
766 | | * |
767 | | * Right now, this function only supports unparameterized foreign joins, so we |
768 | | * only search for unparameterized path in the given list of paths. Since we |
769 | | * are searching for a path which can be used to construct an alternative local |
770 | | * plan for a foreign join, we look for only MergeJoin, HashJoin or NestLoop |
771 | | * paths. |
772 | | * |
773 | | * If the inner or outer subpath of the chosen path is a ForeignScan, we |
774 | | * replace it with its outer subpath. For this reason, and also because the |
775 | | * planner might free the original path later, the path returned by this |
776 | | * function is a shallow copy of the original. There's no need to copy |
777 | | * the substructure, so we don't. |
778 | | * |
779 | | * Since the plan created using this path will presumably only be used to |
780 | | * execute EPQ checks, efficiency of the path is not a concern. But since the |
781 | | * path list in RelOptInfo is anyway sorted by total cost we are likely to |
782 | | * choose the most efficient path, which is all for the best. |
783 | | */ |
784 | | Path * |
785 | | GetExistingLocalJoinPath(RelOptInfo *joinrel) |
786 | 0 | { |
787 | 0 | ListCell *lc; |
788 | |
|
789 | 0 | Assert(IS_JOIN_REL(joinrel)); |
790 | |
|
791 | 0 | foreach(lc, joinrel->pathlist) |
792 | 0 | { |
793 | 0 | Path *path = (Path *) lfirst(lc); |
794 | 0 | JoinPath *joinpath = NULL; |
795 | | |
796 | | /* Skip parameterized paths. */ |
797 | 0 | if (path->param_info != NULL) |
798 | 0 | continue; |
799 | | |
800 | 0 | switch (path->pathtype) |
801 | 0 | { |
802 | 0 | case T_HashJoin: |
803 | 0 | { |
804 | 0 | HashPath *hash_path = makeNode(HashPath); |
805 | |
|
806 | 0 | memcpy(hash_path, path, sizeof(HashPath)); |
807 | 0 | joinpath = (JoinPath *) hash_path; |
808 | 0 | } |
809 | 0 | break; |
810 | | |
811 | 0 | case T_NestLoop: |
812 | 0 | { |
813 | 0 | NestPath *nest_path = makeNode(NestPath); |
814 | |
|
815 | 0 | memcpy(nest_path, path, sizeof(NestPath)); |
816 | 0 | joinpath = (JoinPath *) nest_path; |
817 | 0 | } |
818 | 0 | break; |
819 | | |
820 | 0 | case T_MergeJoin: |
821 | 0 | { |
822 | 0 | MergePath *merge_path = makeNode(MergePath); |
823 | |
|
824 | 0 | memcpy(merge_path, path, sizeof(MergePath)); |
825 | 0 | joinpath = (JoinPath *) merge_path; |
826 | 0 | } |
827 | 0 | break; |
828 | | |
829 | 0 | default: |
830 | | |
831 | | /* |
832 | | * Just skip anything else. We don't know if corresponding |
833 | | * plan would build the output row from whole-row references |
834 | | * of base relations and execute the EPQ checks. |
835 | | */ |
836 | 0 | break; |
837 | 0 | } |
838 | | |
839 | | /* This path isn't good for us, check next. */ |
840 | 0 | if (!joinpath) |
841 | 0 | continue; |
842 | | |
843 | | /* |
844 | | * If either inner or outer path is a ForeignPath corresponding to a |
845 | | * pushed down join, replace it with the fdw_outerpath, so that we |
846 | | * maintain path for EPQ checks built entirely of local join |
847 | | * strategies. |
848 | | */ |
849 | 0 | if (IsA(joinpath->outerjoinpath, ForeignPath)) |
850 | 0 | { |
851 | 0 | ForeignPath *foreign_path; |
852 | |
|
853 | 0 | foreign_path = (ForeignPath *) joinpath->outerjoinpath; |
854 | 0 | if (IS_JOIN_REL(foreign_path->path.parent)) |
855 | 0 | { |
856 | 0 | joinpath->outerjoinpath = foreign_path->fdw_outerpath; |
857 | |
|
858 | 0 | if (joinpath->path.pathtype == T_MergeJoin) |
859 | 0 | { |
860 | 0 | MergePath *merge_path = (MergePath *) joinpath; |
861 | | |
862 | | /* |
863 | | * If the new outer path is already well enough ordered |
864 | | * for the mergejoin, we can skip doing an explicit sort. |
865 | | */ |
866 | 0 | if (merge_path->outersortkeys && |
867 | 0 | pathkeys_count_contained_in(merge_path->outersortkeys, |
868 | 0 | joinpath->outerjoinpath->pathkeys, |
869 | 0 | &merge_path->outer_presorted_keys)) |
870 | 0 | merge_path->outersortkeys = NIL; |
871 | 0 | } |
872 | 0 | } |
873 | 0 | } |
874 | |
|
875 | 0 | if (IsA(joinpath->innerjoinpath, ForeignPath)) |
876 | 0 | { |
877 | 0 | ForeignPath *foreign_path; |
878 | |
|
879 | 0 | foreign_path = (ForeignPath *) joinpath->innerjoinpath; |
880 | 0 | if (IS_JOIN_REL(foreign_path->path.parent)) |
881 | 0 | { |
882 | 0 | joinpath->innerjoinpath = foreign_path->fdw_outerpath; |
883 | |
|
884 | 0 | if (joinpath->path.pathtype == T_MergeJoin) |
885 | 0 | { |
886 | 0 | MergePath *merge_path = (MergePath *) joinpath; |
887 | | |
888 | | /* |
889 | | * If the new inner path is already well enough ordered |
890 | | * for the mergejoin, we can skip doing an explicit sort. |
891 | | */ |
892 | 0 | if (merge_path->innersortkeys && |
893 | 0 | pathkeys_contained_in(merge_path->innersortkeys, |
894 | 0 | joinpath->innerjoinpath->pathkeys)) |
895 | 0 | merge_path->innersortkeys = NIL; |
896 | 0 | } |
897 | 0 | } |
898 | 0 | } |
899 | |
|
900 | 0 | return (Path *) joinpath; |
901 | 0 | } |
902 | 0 | return NULL; |
903 | 0 | } |