LCOV - differential code coverage report
Current view: top level - src/backend/foreign - foreign.c (source / functions) Coverage Total Hit LBC UIC UBC GBC GIC GNC CBC EUB ECB DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 91.3 % 241 220 11 9 1 8 151 5 56 12 150 3
Current Date: 2023-04-08 15:15:32 Functions: 100.0 % 21 21 21 21
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

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

Generated by: LCOV version v1.16-55-g56c0a2a