2008-12-19 17:25:19 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* foreign.c
|
2009-06-11 16:49:15 +02:00
|
|
|
* support for foreign-data wrappers, servers and user mappings.
|
2008-12-19 17:25:19 +01:00
|
|
|
*
|
2016-01-02 19:33:40 +01:00
|
|
|
* Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
|
2008-12-19 17:25:19 +01:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/foreign/foreign.c
|
2008-12-19 17:25:19 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2012-08-30 22:15:44 +02:00
|
|
|
#include "access/htup_details.h"
|
2008-12-19 17:25:19 +01:00
|
|
|
#include "access/reloptions.h"
|
|
|
|
#include "catalog/pg_foreign_data_wrapper.h"
|
|
|
|
#include "catalog/pg_foreign_server.h"
|
2011-02-20 06:17:18 +01:00
|
|
|
#include "catalog/pg_foreign_table.h"
|
2008-12-19 17:25:19 +01:00
|
|
|
#include "catalog/pg_user_mapping.h"
|
2011-02-20 06:17:18 +01:00
|
|
|
#include "foreign/fdwapi.h"
|
2008-12-19 17:25:19 +01:00
|
|
|
#include "foreign/foreign.h"
|
2012-08-29 01:02:00 +02:00
|
|
|
#include "lib/stringinfo.h"
|
2008-12-19 17:25:19 +01:00
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "utils/builtins.h"
|
2013-03-07 05:47:38 +01:00
|
|
|
#include "utils/memutils.h"
|
|
|
|
#include "utils/rel.h"
|
2008-12-19 17:25:19 +01:00
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
|
|
|
|
|
|
|
extern Datum pg_options_to_table(PG_FUNCTION_ARGS);
|
2009-02-24 11:06:36 +01:00
|
|
|
extern Datum postgresql_fdw_validator(PG_FUNCTION_ARGS);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
2016-01-28 20:05:36 +01:00
|
|
|
static HeapTuple find_user_mapping(Oid userid, Oid serverid);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* GetForeignDataWrapper - look up the foreign-data wrapper by OID.
|
2008-12-19 17:25:19 +01:00
|
|
|
*/
|
|
|
|
ForeignDataWrapper *
|
|
|
|
GetForeignDataWrapper(Oid fdwid)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
Form_pg_foreign_data_wrapper fdwform;
|
|
|
|
ForeignDataWrapper *fdw;
|
|
|
|
Datum datum;
|
|
|
|
HeapTuple tp;
|
|
|
|
bool isnull;
|
2008-12-19 17:25:19 +01:00
|
|
|
|
2010-02-14 19:42:19 +01:00
|
|
|
tp = SearchSysCache1(FOREIGNDATAWRAPPEROID, ObjectIdGetDatum(fdwid));
|
2008-12-19 17:25:19 +01:00
|
|
|
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
|
|
|
|
|
|
|
|
fdwform = (Form_pg_foreign_data_wrapper) GETSTRUCT(tp);
|
|
|
|
|
2011-02-20 06:17:18 +01:00
|
|
|
fdw = (ForeignDataWrapper *) palloc(sizeof(ForeignDataWrapper));
|
2008-12-19 17:25:19 +01:00
|
|
|
fdw->fdwid = fdwid;
|
|
|
|
fdw->owner = fdwform->fdwowner;
|
|
|
|
fdw->fdwname = pstrdup(NameStr(fdwform->fdwname));
|
2011-02-19 06:06:18 +01:00
|
|
|
fdw->fdwhandler = fdwform->fdwhandler;
|
2009-02-24 11:06:36 +01:00
|
|
|
fdw->fdwvalidator = fdwform->fdwvalidator;
|
2008-12-19 17:25:19 +01:00
|
|
|
|
2011-02-20 06:17:18 +01:00
|
|
|
/* Extract the fdwoptions */
|
2008-12-19 17:25:19 +01:00
|
|
|
datum = SysCacheGetAttr(FOREIGNDATAWRAPPEROID,
|
|
|
|
tp,
|
|
|
|
Anum_pg_foreign_data_wrapper_fdwoptions,
|
|
|
|
&isnull);
|
2011-02-20 06:17:18 +01:00
|
|
|
if (isnull)
|
|
|
|
fdw->options = NIL;
|
|
|
|
else
|
|
|
|
fdw->options = untransformRelOptions(datum);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
|
|
|
return fdw;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GetForeignDataWrapperByName - look up the foreign-data wrapper
|
|
|
|
* definition by name.
|
|
|
|
*/
|
|
|
|
ForeignDataWrapper *
|
|
|
|
GetForeignDataWrapperByName(const char *fdwname, bool missing_ok)
|
|
|
|
{
|
2011-04-01 17:28:28 +02:00
|
|
|
Oid fdwId = get_foreign_data_wrapper_oid(fdwname, missing_ok);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
2011-02-20 06:17:18 +01:00
|
|
|
if (!OidIsValid(fdwId))
|
2008-12-19 17:25:19 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return GetForeignDataWrapper(fdwId);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GetForeignServer - look up the foreign server definition.
|
|
|
|
*/
|
|
|
|
ForeignServer *
|
|
|
|
GetForeignServer(Oid serverid)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
Form_pg_foreign_server serverform;
|
|
|
|
ForeignServer *server;
|
|
|
|
HeapTuple tp;
|
|
|
|
Datum datum;
|
|
|
|
bool isnull;
|
2008-12-19 17:25:19 +01:00
|
|
|
|
2010-02-14 19:42:19 +01:00
|
|
|
tp = SearchSysCache1(FOREIGNSERVEROID, ObjectIdGetDatum(serverid));
|
2008-12-19 17:25:19 +01:00
|
|
|
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
elog(ERROR, "cache lookup failed for foreign server %u", serverid);
|
|
|
|
|
|
|
|
serverform = (Form_pg_foreign_server) GETSTRUCT(tp);
|
|
|
|
|
2011-02-20 06:17:18 +01:00
|
|
|
server = (ForeignServer *) palloc(sizeof(ForeignServer));
|
2008-12-19 17:25:19 +01:00
|
|
|
server->serverid = serverid;
|
|
|
|
server->servername = pstrdup(NameStr(serverform->srvname));
|
|
|
|
server->owner = serverform->srvowner;
|
|
|
|
server->fdwid = serverform->srvfdw;
|
|
|
|
|
|
|
|
/* Extract server type */
|
|
|
|
datum = SysCacheGetAttr(FOREIGNSERVEROID,
|
|
|
|
tp,
|
|
|
|
Anum_pg_foreign_server_srvtype,
|
|
|
|
&isnull);
|
2016-03-10 05:29:05 +01:00
|
|
|
server->servertype = isnull ? NULL : TextDatumGetCString(datum);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
|
|
|
/* Extract server version */
|
|
|
|
datum = SysCacheGetAttr(FOREIGNSERVEROID,
|
|
|
|
tp,
|
|
|
|
Anum_pg_foreign_server_srvversion,
|
|
|
|
&isnull);
|
2016-03-10 05:29:05 +01:00
|
|
|
server->serverversion = isnull ? NULL : TextDatumGetCString(datum);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
|
|
|
/* Extract the srvoptions */
|
|
|
|
datum = SysCacheGetAttr(FOREIGNSERVEROID,
|
|
|
|
tp,
|
|
|
|
Anum_pg_foreign_server_srvoptions,
|
|
|
|
&isnull);
|
2011-02-20 06:17:18 +01:00
|
|
|
if (isnull)
|
|
|
|
server->options = NIL;
|
|
|
|
else
|
|
|
|
server->options = untransformRelOptions(datum);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
|
|
|
return server;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GetForeignServerByName - look up the foreign server definition by name.
|
|
|
|
*/
|
|
|
|
ForeignServer *
|
|
|
|
GetForeignServerByName(const char *srvname, bool missing_ok)
|
|
|
|
{
|
2011-04-01 17:28:28 +02:00
|
|
|
Oid serverid = get_foreign_server_oid(srvname, missing_ok);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
2011-02-20 06:17:18 +01:00
|
|
|
if (!OidIsValid(serverid))
|
2008-12-19 17:25:19 +01:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return GetForeignServer(serverid);
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:05:09 +01:00
|
|
|
/*
|
|
|
|
* GetUserMappingById - look up the user mapping by its OID.
|
|
|
|
*/
|
|
|
|
UserMapping *
|
|
|
|
GetUserMappingById(Oid umid)
|
|
|
|
{
|
|
|
|
Datum datum;
|
|
|
|
HeapTuple tp;
|
|
|
|
bool isnull;
|
|
|
|
UserMapping *um;
|
|
|
|
|
|
|
|
tp = SearchSysCache1(USERMAPPINGOID, ObjectIdGetDatum(umid));
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
elog(ERROR, "cache lookup failed for user mapping %u", umid);
|
|
|
|
|
|
|
|
um = (UserMapping *) palloc(sizeof(UserMapping));
|
|
|
|
um->umid = umid;
|
|
|
|
|
|
|
|
/* Extract the umuser */
|
|
|
|
datum = SysCacheGetAttr(USERMAPPINGOID,
|
|
|
|
tp,
|
|
|
|
Anum_pg_user_mapping_umuser,
|
|
|
|
&isnull);
|
|
|
|
Assert(!isnull);
|
|
|
|
um->userid = DatumGetObjectId(datum);
|
|
|
|
|
|
|
|
/* Extract the umserver */
|
|
|
|
datum = SysCacheGetAttr(USERMAPPINGOID,
|
|
|
|
tp,
|
|
|
|
Anum_pg_user_mapping_umserver,
|
|
|
|
&isnull);
|
|
|
|
Assert(!isnull);
|
|
|
|
um->serverid = DatumGetObjectId(datum);
|
|
|
|
|
|
|
|
/* Extract the umoptions */
|
|
|
|
datum = SysCacheGetAttr(USERMAPPINGOID,
|
|
|
|
tp,
|
|
|
|
Anum_pg_user_mapping_umoptions,
|
|
|
|
&isnull);
|
|
|
|
if (isnull)
|
|
|
|
um->options = NIL;
|
|
|
|
else
|
|
|
|
um->options = untransformRelOptions(datum);
|
|
|
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
|
|
|
return um;
|
|
|
|
}
|
2008-12-19 17:25:19 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* GetUserMapping - look up the user mapping.
|
|
|
|
*
|
|
|
|
* If no mapping is found for the supplied user, we also look for
|
|
|
|
* PUBLIC mappings (userid == InvalidOid).
|
|
|
|
*/
|
|
|
|
UserMapping *
|
|
|
|
GetUserMapping(Oid userid, Oid serverid)
|
|
|
|
{
|
|
|
|
Datum datum;
|
2009-06-11 16:49:15 +02:00
|
|
|
HeapTuple tp;
|
2008-12-19 17:25:19 +01:00
|
|
|
bool isnull;
|
2009-06-11 16:49:15 +02:00
|
|
|
UserMapping *um;
|
2008-12-19 17:25:19 +01:00
|
|
|
|
2016-01-28 20:05:36 +01:00
|
|
|
tp = find_user_mapping(userid, serverid);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
2011-02-20 06:17:18 +01:00
|
|
|
um = (UserMapping *) palloc(sizeof(UserMapping));
|
Avoid multiple foreign server connections when all use same user mapping.
Previously, postgres_fdw's connection cache was keyed by user OID and
server OID, but this can lead to multiple connections when it's not
really necessary. In particular, if all relevant users are mapped to
the public user mapping, then their connection options are certainly
the same, so one connection can be used for all of them.
While we're cleaning things up here, drop the "server" argument to
GetConnection(), which isn't really needed. This saves a few cycles
because callers no longer have to look this up; the function itself
does, but only when establishing a new connection, not when reusing
an existing one.
Ashutosh Bapat, with a few small changes by me.
2016-01-28 18:05:19 +01:00
|
|
|
um->umid = HeapTupleGetOid(tp);
|
2011-02-20 06:17:18 +01:00
|
|
|
um->userid = userid;
|
|
|
|
um->serverid = serverid;
|
|
|
|
|
2008-12-19 17:25:19 +01:00
|
|
|
/* Extract the umoptions */
|
|
|
|
datum = SysCacheGetAttr(USERMAPPINGUSERSERVER,
|
|
|
|
tp,
|
|
|
|
Anum_pg_user_mapping_umoptions,
|
|
|
|
&isnull);
|
2011-02-20 06:17:18 +01:00
|
|
|
if (isnull)
|
|
|
|
um->options = NIL;
|
|
|
|
else
|
|
|
|
um->options = untransformRelOptions(datum);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
|
|
|
return um;
|
|
|
|
}
|
|
|
|
|
2016-01-28 20:05:36 +01:00
|
|
|
/*
|
|
|
|
* GetUserMappingId - look up the user mapping, and return its OID
|
|
|
|
*
|
|
|
|
* If no mapping is found for the supplied user, we also look for
|
|
|
|
* PUBLIC mappings (userid == InvalidOid).
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
GetUserMappingId(Oid userid, Oid serverid)
|
|
|
|
{
|
|
|
|
HeapTuple tp;
|
|
|
|
Oid umid;
|
|
|
|
|
|
|
|
tp = find_user_mapping(userid, serverid);
|
|
|
|
|
|
|
|
/* Extract the Oid */
|
|
|
|
umid = HeapTupleGetOid(tp);
|
|
|
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
|
|
|
return umid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find_user_mapping - Guts of GetUserMapping family.
|
|
|
|
*
|
|
|
|
* If no mapping is found for the supplied user, we also look for
|
|
|
|
* PUBLIC mappings (userid == InvalidOid).
|
|
|
|
*/
|
|
|
|
static HeapTuple
|
|
|
|
find_user_mapping(Oid userid, Oid serverid)
|
|
|
|
{
|
|
|
|
HeapTuple tp;
|
|
|
|
|
|
|
|
tp = SearchSysCache2(USERMAPPINGUSERSERVER,
|
|
|
|
ObjectIdGetDatum(userid),
|
|
|
|
ObjectIdGetDatum(serverid));
|
|
|
|
|
|
|
|
if (HeapTupleIsValid(tp))
|
|
|
|
return tp;
|
|
|
|
|
|
|
|
/* Not found for the specific user -- try PUBLIC */
|
|
|
|
tp = SearchSysCache2(USERMAPPINGUSERSERVER,
|
2016-02-04 23:05:09 +01:00
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
ObjectIdGetDatum(serverid));
|
2016-01-28 20:05:36 +01:00
|
|
|
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("user mapping not found for \"%s\"",
|
|
|
|
MappingUserName(userid))));
|
|
|
|
|
|
|
|
return tp;
|
|
|
|
}
|
|
|
|
|
2008-12-19 17:25:19 +01:00
|
|
|
|
2011-02-20 06:17:18 +01:00
|
|
|
/*
|
|
|
|
* GetForeignTable - look up the foreign table definition by relation oid.
|
|
|
|
*/
|
|
|
|
ForeignTable *
|
|
|
|
GetForeignTable(Oid relid)
|
|
|
|
{
|
|
|
|
Form_pg_foreign_table tableform;
|
|
|
|
ForeignTable *ft;
|
|
|
|
HeapTuple tp;
|
|
|
|
Datum datum;
|
|
|
|
bool isnull;
|
|
|
|
|
|
|
|
tp = SearchSysCache1(FOREIGNTABLEREL, ObjectIdGetDatum(relid));
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
elog(ERROR, "cache lookup failed for foreign table %u", relid);
|
|
|
|
tableform = (Form_pg_foreign_table) GETSTRUCT(tp);
|
|
|
|
|
|
|
|
ft = (ForeignTable *) palloc(sizeof(ForeignTable));
|
|
|
|
ft->relid = relid;
|
|
|
|
ft->serverid = tableform->ftserver;
|
|
|
|
|
|
|
|
/* Extract the ftoptions */
|
|
|
|
datum = SysCacheGetAttr(FOREIGNTABLEREL,
|
|
|
|
tp,
|
|
|
|
Anum_pg_foreign_table_ftoptions,
|
|
|
|
&isnull);
|
|
|
|
if (isnull)
|
|
|
|
ft->options = NIL;
|
|
|
|
else
|
|
|
|
ft->options = untransformRelOptions(datum);
|
|
|
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
|
|
|
return ft;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-08 00:20:58 +01:00
|
|
|
/*
|
|
|
|
* GetForeignColumnOptions - Get attfdwoptions of given relation/attnum
|
|
|
|
* as list of DefElem.
|
|
|
|
*/
|
|
|
|
List *
|
|
|
|
GetForeignColumnOptions(Oid relid, AttrNumber attnum)
|
|
|
|
{
|
|
|
|
List *options;
|
|
|
|
HeapTuple tp;
|
|
|
|
Datum datum;
|
|
|
|
bool isnull;
|
|
|
|
|
|
|
|
tp = SearchSysCache2(ATTNUM,
|
|
|
|
ObjectIdGetDatum(relid),
|
|
|
|
Int16GetDatum(attnum));
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
elog(ERROR, "cache lookup failed for attribute %d of relation %u",
|
|
|
|
attnum, relid);
|
|
|
|
datum = SysCacheGetAttr(ATTNUM,
|
|
|
|
tp,
|
|
|
|
Anum_pg_attribute_attfdwoptions,
|
|
|
|
&isnull);
|
|
|
|
if (isnull)
|
|
|
|
options = NIL;
|
|
|
|
else
|
|
|
|
options = untransformRelOptions(datum);
|
|
|
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-20 06:17:18 +01:00
|
|
|
/*
|
|
|
|
* GetFdwRoutine - call the specified foreign-data wrapper handler routine
|
|
|
|
* to get its FdwRoutine struct.
|
|
|
|
*/
|
|
|
|
FdwRoutine *
|
|
|
|
GetFdwRoutine(Oid fdwhandler)
|
|
|
|
{
|
|
|
|
Datum datum;
|
|
|
|
FdwRoutine *routine;
|
|
|
|
|
|
|
|
datum = OidFunctionCall0(fdwhandler);
|
|
|
|
routine = (FdwRoutine *) DatumGetPointer(datum);
|
|
|
|
|
|
|
|
if (routine == NULL || !IsA(routine, FdwRoutine))
|
|
|
|
elog(ERROR, "foreign-data wrapper handler function %u did not return an FdwRoutine struct",
|
|
|
|
fdwhandler);
|
|
|
|
|
|
|
|
return routine;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
Code review for foreign/custom join pushdown patch.
Commit e7cb7ee14555cc9c5773e2c102efd6371f6f2005 included some design
decisions that seem pretty questionable to me, and there was quite a lot
of stuff not to like about the documentation and comments. Clean up
as follows:
* Consider foreign joins only between foreign tables on the same server,
rather than between any two foreign tables with the same underlying FDW
handler function. In most if not all cases, the FDW would simply have had
to apply the same-server restriction itself (far more expensively, both for
lack of caching and because it would be repeated for each combination of
input sub-joins), or else risk nasty bugs. Anyone who's really intent on
doing something outside this restriction can always use the
set_join_pathlist_hook.
* Rename fdw_ps_tlist/custom_ps_tlist to fdw_scan_tlist/custom_scan_tlist
to better reflect what they're for, and allow these custom scan tlists
to be used even for base relations.
* Change make_foreignscan() API to include passing the fdw_scan_tlist
value, since the FDW is required to set that. Backwards compatibility
doesn't seem like an adequate reason to expect FDWs to set it in some
ad-hoc extra step, and anyway existing FDWs can just pass NIL.
* Change the API of path-generating subroutines of add_paths_to_joinrel,
and in particular that of GetForeignJoinPaths and set_join_pathlist_hook,
so that various less-used parameters are passed in a struct rather than
as separate parameter-list entries. The objective here is to reduce the
probability that future additions to those parameter lists will result in
source-level API breaks for users of these hooks. It's possible that this
is even a small win for the core code, since most CPU architectures can't
pass more than half a dozen parameters efficiently anyway. I kept root,
joinrel, outerrel, innerrel, and jointype as separate parameters to reduce
code churn in joinpath.c --- in particular, putting jointype into the
struct would have been problematic because of the subroutines' habit of
changing their local copies of that variable.
* Avoid ad-hocery in ExecAssignScanProjectionInfo. It was probably all
right for it to know about IndexOnlyScan, but if the list is to grow
we should refactor the knowledge out to the callers.
* Restore nodeForeignscan.c's previous use of the relcache to avoid
extra GetFdwRoutine lookups for base-relation scans.
* Lots of cleanup of documentation and missed comments. Re-order some
code additions into more logical places.
2015-05-10 20:36:30 +02:00
|
|
|
* GetForeignServerIdByRelId - look up the foreign server
|
|
|
|
* for the given foreign table, and return its OID.
|
2011-02-20 06:17:18 +01:00
|
|
|
*/
|
2015-05-01 14:50:35 +02:00
|
|
|
Oid
|
Code review for foreign/custom join pushdown patch.
Commit e7cb7ee14555cc9c5773e2c102efd6371f6f2005 included some design
decisions that seem pretty questionable to me, and there was quite a lot
of stuff not to like about the documentation and comments. Clean up
as follows:
* Consider foreign joins only between foreign tables on the same server,
rather than between any two foreign tables with the same underlying FDW
handler function. In most if not all cases, the FDW would simply have had
to apply the same-server restriction itself (far more expensively, both for
lack of caching and because it would be repeated for each combination of
input sub-joins), or else risk nasty bugs. Anyone who's really intent on
doing something outside this restriction can always use the
set_join_pathlist_hook.
* Rename fdw_ps_tlist/custom_ps_tlist to fdw_scan_tlist/custom_scan_tlist
to better reflect what they're for, and allow these custom scan tlists
to be used even for base relations.
* Change make_foreignscan() API to include passing the fdw_scan_tlist
value, since the FDW is required to set that. Backwards compatibility
doesn't seem like an adequate reason to expect FDWs to set it in some
ad-hoc extra step, and anyway existing FDWs can just pass NIL.
* Change the API of path-generating subroutines of add_paths_to_joinrel,
and in particular that of GetForeignJoinPaths and set_join_pathlist_hook,
so that various less-used parameters are passed in a struct rather than
as separate parameter-list entries. The objective here is to reduce the
probability that future additions to those parameter lists will result in
source-level API breaks for users of these hooks. It's possible that this
is even a small win for the core code, since most CPU architectures can't
pass more than half a dozen parameters efficiently anyway. I kept root,
joinrel, outerrel, innerrel, and jointype as separate parameters to reduce
code churn in joinpath.c --- in particular, putting jointype into the
struct would have been problematic because of the subroutines' habit of
changing their local copies of that variable.
* Avoid ad-hocery in ExecAssignScanProjectionInfo. It was probably all
right for it to know about IndexOnlyScan, but if the list is to grow
we should refactor the knowledge out to the callers.
* Restore nodeForeignscan.c's previous use of the relcache to avoid
extra GetFdwRoutine lookups for base-relation scans.
* Lots of cleanup of documentation and missed comments. Re-order some
code additions into more logical places.
2015-05-10 20:36:30 +02:00
|
|
|
GetForeignServerIdByRelId(Oid relid)
|
2011-02-20 06:17:18 +01:00
|
|
|
{
|
|
|
|
HeapTuple tp;
|
|
|
|
Form_pg_foreign_table tableform;
|
|
|
|
Oid serverid;
|
|
|
|
|
|
|
|
tp = SearchSysCache1(FOREIGNTABLEREL, ObjectIdGetDatum(relid));
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
elog(ERROR, "cache lookup failed for foreign table %u", relid);
|
|
|
|
tableform = (Form_pg_foreign_table) GETSTRUCT(tp);
|
|
|
|
serverid = tableform->ftserver;
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
Code review for foreign/custom join pushdown patch.
Commit e7cb7ee14555cc9c5773e2c102efd6371f6f2005 included some design
decisions that seem pretty questionable to me, and there was quite a lot
of stuff not to like about the documentation and comments. Clean up
as follows:
* Consider foreign joins only between foreign tables on the same server,
rather than between any two foreign tables with the same underlying FDW
handler function. In most if not all cases, the FDW would simply have had
to apply the same-server restriction itself (far more expensively, both for
lack of caching and because it would be repeated for each combination of
input sub-joins), or else risk nasty bugs. Anyone who's really intent on
doing something outside this restriction can always use the
set_join_pathlist_hook.
* Rename fdw_ps_tlist/custom_ps_tlist to fdw_scan_tlist/custom_scan_tlist
to better reflect what they're for, and allow these custom scan tlists
to be used even for base relations.
* Change make_foreignscan() API to include passing the fdw_scan_tlist
value, since the FDW is required to set that. Backwards compatibility
doesn't seem like an adequate reason to expect FDWs to set it in some
ad-hoc extra step, and anyway existing FDWs can just pass NIL.
* Change the API of path-generating subroutines of add_paths_to_joinrel,
and in particular that of GetForeignJoinPaths and set_join_pathlist_hook,
so that various less-used parameters are passed in a struct rather than
as separate parameter-list entries. The objective here is to reduce the
probability that future additions to those parameter lists will result in
source-level API breaks for users of these hooks. It's possible that this
is even a small win for the core code, since most CPU architectures can't
pass more than half a dozen parameters efficiently anyway. I kept root,
joinrel, outerrel, innerrel, and jointype as separate parameters to reduce
code churn in joinpath.c --- in particular, putting jointype into the
struct would have been problematic because of the subroutines' habit of
changing their local copies of that variable.
* Avoid ad-hocery in ExecAssignScanProjectionInfo. It was probably all
right for it to know about IndexOnlyScan, but if the list is to grow
we should refactor the knowledge out to the callers.
* Restore nodeForeignscan.c's previous use of the relcache to avoid
extra GetFdwRoutine lookups for base-relation scans.
* Lots of cleanup of documentation and missed comments. Re-order some
code additions into more logical places.
2015-05-10 20:36:30 +02:00
|
|
|
return serverid;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GetFdwRoutineByServerId - look up the handler of the foreign-data wrapper
|
|
|
|
* for the given foreign server, and retrieve its FdwRoutine struct.
|
|
|
|
*/
|
|
|
|
FdwRoutine *
|
|
|
|
GetFdwRoutineByServerId(Oid serverid)
|
|
|
|
{
|
|
|
|
HeapTuple tp;
|
|
|
|
Form_pg_foreign_data_wrapper fdwform;
|
|
|
|
Form_pg_foreign_server serverform;
|
|
|
|
Oid fdwid;
|
|
|
|
Oid fdwhandler;
|
|
|
|
|
2011-02-20 06:17:18 +01:00
|
|
|
/* Get foreign-data wrapper OID for the server. */
|
|
|
|
tp = SearchSysCache1(FOREIGNSERVEROID, ObjectIdGetDatum(serverid));
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
elog(ERROR, "cache lookup failed for foreign server %u", serverid);
|
|
|
|
serverform = (Form_pg_foreign_server) GETSTRUCT(tp);
|
|
|
|
fdwid = serverform->srvfdw;
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
|
|
|
/* Get handler function OID for the FDW. */
|
|
|
|
tp = SearchSysCache1(FOREIGNDATAWRAPPEROID, ObjectIdGetDatum(fdwid));
|
|
|
|
if (!HeapTupleIsValid(tp))
|
|
|
|
elog(ERROR, "cache lookup failed for foreign-data wrapper %u", fdwid);
|
|
|
|
fdwform = (Form_pg_foreign_data_wrapper) GETSTRUCT(tp);
|
|
|
|
fdwhandler = fdwform->fdwhandler;
|
|
|
|
|
|
|
|
/* Complain if FDW has been set to NO HANDLER. */
|
|
|
|
if (!OidIsValid(fdwhandler))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("foreign-data wrapper \"%s\" has no handler",
|
|
|
|
NameStr(fdwform->fdwname))));
|
|
|
|
|
|
|
|
ReleaseSysCache(tp);
|
|
|
|
|
Code review for foreign/custom join pushdown patch.
Commit e7cb7ee14555cc9c5773e2c102efd6371f6f2005 included some design
decisions that seem pretty questionable to me, and there was quite a lot
of stuff not to like about the documentation and comments. Clean up
as follows:
* Consider foreign joins only between foreign tables on the same server,
rather than between any two foreign tables with the same underlying FDW
handler function. In most if not all cases, the FDW would simply have had
to apply the same-server restriction itself (far more expensively, both for
lack of caching and because it would be repeated for each combination of
input sub-joins), or else risk nasty bugs. Anyone who's really intent on
doing something outside this restriction can always use the
set_join_pathlist_hook.
* Rename fdw_ps_tlist/custom_ps_tlist to fdw_scan_tlist/custom_scan_tlist
to better reflect what they're for, and allow these custom scan tlists
to be used even for base relations.
* Change make_foreignscan() API to include passing the fdw_scan_tlist
value, since the FDW is required to set that. Backwards compatibility
doesn't seem like an adequate reason to expect FDWs to set it in some
ad-hoc extra step, and anyway existing FDWs can just pass NIL.
* Change the API of path-generating subroutines of add_paths_to_joinrel,
and in particular that of GetForeignJoinPaths and set_join_pathlist_hook,
so that various less-used parameters are passed in a struct rather than
as separate parameter-list entries. The objective here is to reduce the
probability that future additions to those parameter lists will result in
source-level API breaks for users of these hooks. It's possible that this
is even a small win for the core code, since most CPU architectures can't
pass more than half a dozen parameters efficiently anyway. I kept root,
joinrel, outerrel, innerrel, and jointype as separate parameters to reduce
code churn in joinpath.c --- in particular, putting jointype into the
struct would have been problematic because of the subroutines' habit of
changing their local copies of that variable.
* Avoid ad-hocery in ExecAssignScanProjectionInfo. It was probably all
right for it to know about IndexOnlyScan, but if the list is to grow
we should refactor the knowledge out to the callers.
* Restore nodeForeignscan.c's previous use of the relcache to avoid
extra GetFdwRoutine lookups for base-relation scans.
* Lots of cleanup of documentation and missed comments. Re-order some
code additions into more logical places.
2015-05-10 20:36:30 +02:00
|
|
|
/* And finally, call the handler function. */
|
|
|
|
return GetFdwRoutine(fdwhandler);
|
2015-05-01 14:50:35 +02:00
|
|
|
}
|
|
|
|
|
Code review for foreign/custom join pushdown patch.
Commit e7cb7ee14555cc9c5773e2c102efd6371f6f2005 included some design
decisions that seem pretty questionable to me, and there was quite a lot
of stuff not to like about the documentation and comments. Clean up
as follows:
* Consider foreign joins only between foreign tables on the same server,
rather than between any two foreign tables with the same underlying FDW
handler function. In most if not all cases, the FDW would simply have had
to apply the same-server restriction itself (far more expensively, both for
lack of caching and because it would be repeated for each combination of
input sub-joins), or else risk nasty bugs. Anyone who's really intent on
doing something outside this restriction can always use the
set_join_pathlist_hook.
* Rename fdw_ps_tlist/custom_ps_tlist to fdw_scan_tlist/custom_scan_tlist
to better reflect what they're for, and allow these custom scan tlists
to be used even for base relations.
* Change make_foreignscan() API to include passing the fdw_scan_tlist
value, since the FDW is required to set that. Backwards compatibility
doesn't seem like an adequate reason to expect FDWs to set it in some
ad-hoc extra step, and anyway existing FDWs can just pass NIL.
* Change the API of path-generating subroutines of add_paths_to_joinrel,
and in particular that of GetForeignJoinPaths and set_join_pathlist_hook,
so that various less-used parameters are passed in a struct rather than
as separate parameter-list entries. The objective here is to reduce the
probability that future additions to those parameter lists will result in
source-level API breaks for users of these hooks. It's possible that this
is even a small win for the core code, since most CPU architectures can't
pass more than half a dozen parameters efficiently anyway. I kept root,
joinrel, outerrel, innerrel, and jointype as separate parameters to reduce
code churn in joinpath.c --- in particular, putting jointype into the
struct would have been problematic because of the subroutines' habit of
changing their local copies of that variable.
* Avoid ad-hocery in ExecAssignScanProjectionInfo. It was probably all
right for it to know about IndexOnlyScan, but if the list is to grow
we should refactor the knowledge out to the callers.
* Restore nodeForeignscan.c's previous use of the relcache to avoid
extra GetFdwRoutine lookups for base-relation scans.
* Lots of cleanup of documentation and missed comments. Re-order some
code additions into more logical places.
2015-05-10 20:36:30 +02:00
|
|
|
|
2015-05-01 14:50:35 +02:00
|
|
|
/*
|
|
|
|
* GetFdwRoutineByRelId - look up the handler of the foreign-data wrapper
|
|
|
|
* for the given foreign table, and retrieve its FdwRoutine struct.
|
|
|
|
*/
|
|
|
|
FdwRoutine *
|
|
|
|
GetFdwRoutineByRelId(Oid relid)
|
|
|
|
{
|
Code review for foreign/custom join pushdown patch.
Commit e7cb7ee14555cc9c5773e2c102efd6371f6f2005 included some design
decisions that seem pretty questionable to me, and there was quite a lot
of stuff not to like about the documentation and comments. Clean up
as follows:
* Consider foreign joins only between foreign tables on the same server,
rather than between any two foreign tables with the same underlying FDW
handler function. In most if not all cases, the FDW would simply have had
to apply the same-server restriction itself (far more expensively, both for
lack of caching and because it would be repeated for each combination of
input sub-joins), or else risk nasty bugs. Anyone who's really intent on
doing something outside this restriction can always use the
set_join_pathlist_hook.
* Rename fdw_ps_tlist/custom_ps_tlist to fdw_scan_tlist/custom_scan_tlist
to better reflect what they're for, and allow these custom scan tlists
to be used even for base relations.
* Change make_foreignscan() API to include passing the fdw_scan_tlist
value, since the FDW is required to set that. Backwards compatibility
doesn't seem like an adequate reason to expect FDWs to set it in some
ad-hoc extra step, and anyway existing FDWs can just pass NIL.
* Change the API of path-generating subroutines of add_paths_to_joinrel,
and in particular that of GetForeignJoinPaths and set_join_pathlist_hook,
so that various less-used parameters are passed in a struct rather than
as separate parameter-list entries. The objective here is to reduce the
probability that future additions to those parameter lists will result in
source-level API breaks for users of these hooks. It's possible that this
is even a small win for the core code, since most CPU architectures can't
pass more than half a dozen parameters efficiently anyway. I kept root,
joinrel, outerrel, innerrel, and jointype as separate parameters to reduce
code churn in joinpath.c --- in particular, putting jointype into the
struct would have been problematic because of the subroutines' habit of
changing their local copies of that variable.
* Avoid ad-hocery in ExecAssignScanProjectionInfo. It was probably all
right for it to know about IndexOnlyScan, but if the list is to grow
we should refactor the knowledge out to the callers.
* Restore nodeForeignscan.c's previous use of the relcache to avoid
extra GetFdwRoutine lookups for base-relation scans.
* Lots of cleanup of documentation and missed comments. Re-order some
code additions into more logical places.
2015-05-10 20:36:30 +02:00
|
|
|
Oid serverid;
|
2015-05-01 14:50:35 +02:00
|
|
|
|
Code review for foreign/custom join pushdown patch.
Commit e7cb7ee14555cc9c5773e2c102efd6371f6f2005 included some design
decisions that seem pretty questionable to me, and there was quite a lot
of stuff not to like about the documentation and comments. Clean up
as follows:
* Consider foreign joins only between foreign tables on the same server,
rather than between any two foreign tables with the same underlying FDW
handler function. In most if not all cases, the FDW would simply have had
to apply the same-server restriction itself (far more expensively, both for
lack of caching and because it would be repeated for each combination of
input sub-joins), or else risk nasty bugs. Anyone who's really intent on
doing something outside this restriction can always use the
set_join_pathlist_hook.
* Rename fdw_ps_tlist/custom_ps_tlist to fdw_scan_tlist/custom_scan_tlist
to better reflect what they're for, and allow these custom scan tlists
to be used even for base relations.
* Change make_foreignscan() API to include passing the fdw_scan_tlist
value, since the FDW is required to set that. Backwards compatibility
doesn't seem like an adequate reason to expect FDWs to set it in some
ad-hoc extra step, and anyway existing FDWs can just pass NIL.
* Change the API of path-generating subroutines of add_paths_to_joinrel,
and in particular that of GetForeignJoinPaths and set_join_pathlist_hook,
so that various less-used parameters are passed in a struct rather than
as separate parameter-list entries. The objective here is to reduce the
probability that future additions to those parameter lists will result in
source-level API breaks for users of these hooks. It's possible that this
is even a small win for the core code, since most CPU architectures can't
pass more than half a dozen parameters efficiently anyway. I kept root,
joinrel, outerrel, innerrel, and jointype as separate parameters to reduce
code churn in joinpath.c --- in particular, putting jointype into the
struct would have been problematic because of the subroutines' habit of
changing their local copies of that variable.
* Avoid ad-hocery in ExecAssignScanProjectionInfo. It was probably all
right for it to know about IndexOnlyScan, but if the list is to grow
we should refactor the knowledge out to the callers.
* Restore nodeForeignscan.c's previous use of the relcache to avoid
extra GetFdwRoutine lookups for base-relation scans.
* Lots of cleanup of documentation and missed comments. Re-order some
code additions into more logical places.
2015-05-10 20:36:30 +02:00
|
|
|
/* Get server OID for the foreign table. */
|
|
|
|
serverid = GetForeignServerIdByRelId(relid);
|
|
|
|
|
|
|
|
/* Now retrieve server's FdwRoutine struct. */
|
|
|
|
return GetFdwRoutineByServerId(serverid);
|
2011-02-20 06:17:18 +01:00
|
|
|
}
|
|
|
|
|
2013-03-07 05:47:38 +01:00
|
|
|
/*
|
|
|
|
* GetFdwRoutineForRelation - look up the handler of the foreign-data wrapper
|
|
|
|
* for the given foreign table, and retrieve its FdwRoutine struct.
|
|
|
|
*
|
|
|
|
* This function is preferred over GetFdwRoutineByRelId because it caches
|
|
|
|
* the data in the relcache entry, saving a number of catalog lookups.
|
|
|
|
*
|
|
|
|
* If makecopy is true then the returned data is freshly palloc'd in the
|
|
|
|
* caller's memory context. Otherwise, it's a pointer to the relcache data,
|
|
|
|
* which will be lost in any relcache reset --- so don't rely on it long.
|
|
|
|
*/
|
|
|
|
FdwRoutine *
|
|
|
|
GetFdwRoutineForRelation(Relation relation, bool makecopy)
|
|
|
|
{
|
|
|
|
FdwRoutine *fdwroutine;
|
|
|
|
FdwRoutine *cfdwroutine;
|
|
|
|
|
|
|
|
if (relation->rd_fdwroutine == NULL)
|
|
|
|
{
|
|
|
|
/* Get the info by consulting the catalogs and the FDW code */
|
|
|
|
fdwroutine = GetFdwRoutineByRelId(RelationGetRelid(relation));
|
|
|
|
|
|
|
|
/* Save the data for later reuse in CacheMemoryContext */
|
|
|
|
cfdwroutine = (FdwRoutine *) MemoryContextAlloc(CacheMemoryContext,
|
|
|
|
sizeof(FdwRoutine));
|
|
|
|
memcpy(cfdwroutine, fdwroutine, sizeof(FdwRoutine));
|
|
|
|
relation->rd_fdwroutine = cfdwroutine;
|
|
|
|
|
|
|
|
/* Give back the locally palloc'd copy regardless of makecopy */
|
|
|
|
return fdwroutine;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have valid cached data --- does the caller want a copy? */
|
|
|
|
if (makecopy)
|
|
|
|
{
|
|
|
|
fdwroutine = (FdwRoutine *) palloc(sizeof(FdwRoutine));
|
|
|
|
memcpy(fdwroutine, relation->rd_fdwroutine, sizeof(FdwRoutine));
|
|
|
|
return fdwroutine;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only a short-lived reference is needed, so just hand back cached copy */
|
|
|
|
return relation->rd_fdwroutine;
|
|
|
|
}
|
|
|
|
|
2011-02-20 06:17:18 +01:00
|
|
|
|
2014-07-10 21:01:31 +02:00
|
|
|
/*
|
|
|
|
* IsImportableForeignTable - filter table names for IMPORT FOREIGN SCHEMA
|
|
|
|
*
|
|
|
|
* Returns TRUE if given table name should be imported according to the
|
|
|
|
* statement's import filter options.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
IsImportableForeignTable(const char *tablename,
|
|
|
|
ImportForeignSchemaStmt *stmt)
|
|
|
|
{
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
switch (stmt->list_type)
|
|
|
|
{
|
|
|
|
case FDW_IMPORT_SCHEMA_ALL:
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case FDW_IMPORT_SCHEMA_LIMIT_TO:
|
|
|
|
foreach(lc, stmt->table_list)
|
|
|
|
{
|
|
|
|
RangeVar *rv = (RangeVar *) lfirst(lc);
|
|
|
|
|
|
|
|
if (strcmp(tablename, rv->relname) == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case FDW_IMPORT_SCHEMA_EXCEPT:
|
|
|
|
foreach(lc, stmt->table_list)
|
|
|
|
{
|
|
|
|
RangeVar *rv = (RangeVar *) lfirst(lc);
|
|
|
|
|
|
|
|
if (strcmp(tablename, rv->relname) == 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false; /* shouldn't get here */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-19 17:25:19 +01:00
|
|
|
/*
|
|
|
|
* deflist_to_tuplestore - Helper function to convert DefElem list to
|
|
|
|
* tuplestore usable in SRF.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
deflist_to_tuplestore(ReturnSetInfo *rsinfo, List *options)
|
|
|
|
{
|
|
|
|
ListCell *cell;
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
Tuplestorestate *tupstore;
|
|
|
|
Datum values[2];
|
2011-02-20 06:17:18 +01:00
|
|
|
bool nulls[2];
|
2008-12-19 17:25:19 +01:00
|
|
|
MemoryContext per_query_ctx;
|
|
|
|
MemoryContext oldcontext;
|
|
|
|
|
|
|
|
/* check to see if caller supports us returning a tuplestore */
|
|
|
|
if (rsinfo == NULL || !IsA(rsinfo, ReturnSetInfo))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("set-valued function called in context that cannot accept a set")));
|
2009-06-11 18:14:18 +02:00
|
|
|
if (!(rsinfo->allowedModes & SFRM_Materialize) ||
|
|
|
|
rsinfo->expectedDesc == NULL)
|
2008-12-19 17:25:19 +01:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("materialize mode required, but it is not allowed in this context")));
|
|
|
|
|
|
|
|
per_query_ctx = rsinfo->econtext->ecxt_per_query_memory;
|
|
|
|
oldcontext = MemoryContextSwitchTo(per_query_ctx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now prepare the result set.
|
|
|
|
*/
|
|
|
|
tupdesc = CreateTupleDescCopy(rsinfo->expectedDesc);
|
|
|
|
tupstore = tuplestore_begin_heap(true, false, work_mem);
|
|
|
|
rsinfo->returnMode = SFRM_Materialize;
|
|
|
|
rsinfo->setResult = tupstore;
|
|
|
|
rsinfo->setDesc = tupdesc;
|
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
foreach(cell, options)
|
2008-12-19 17:25:19 +01:00
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
DefElem *def = lfirst(cell);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
|
|
|
values[0] = CStringGetTextDatum(def->defname);
|
2011-09-13 17:36:49 +02:00
|
|
|
nulls[0] = false;
|
|
|
|
if (def->arg)
|
|
|
|
{
|
|
|
|
values[1] = CStringGetTextDatum(((Value *) (def->arg))->val.str);
|
|
|
|
nulls[1] = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
values[1] = (Datum) 0;
|
|
|
|
nulls[1] = true;
|
|
|
|
}
|
2008-12-19 17:25:19 +01:00
|
|
|
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* clean up and return the tuplestore */
|
|
|
|
tuplestore_donestoring(tupstore);
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(oldcontext);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert options array to name/value table. Useful for information
|
|
|
|
* schema and pg_dump.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_options_to_table(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
Datum array = PG_GETARG_DATUM(0);
|
2008-12-19 17:25:19 +01:00
|
|
|
|
2011-02-20 06:17:18 +01:00
|
|
|
deflist_to_tuplestore((ReturnSetInfo *) fcinfo->resultinfo,
|
|
|
|
untransformRelOptions(array));
|
2008-12-19 17:25:19 +01:00
|
|
|
|
|
|
|
return (Datum) 0;
|
|
|
|
}
|
2009-02-24 11:06:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Describes the valid options for postgresql FDW, server, and user mapping.
|
|
|
|
*/
|
2009-06-11 16:49:15 +02:00
|
|
|
struct ConnectionOption
|
|
|
|
{
|
|
|
|
const char *optname;
|
|
|
|
Oid optcontext; /* Oid of catalog in which option may appear */
|
2009-02-24 11:06:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copied from fe-connect.c PQconninfoOptions.
|
|
|
|
*
|
|
|
|
* The list is small - don't bother with bsearch if it stays so.
|
|
|
|
*/
|
|
|
|
static struct ConnectionOption libpq_conninfo_options[] = {
|
2009-06-11 16:49:15 +02:00
|
|
|
{"authtype", ForeignServerRelationId},
|
|
|
|
{"service", ForeignServerRelationId},
|
|
|
|
{"user", UserMappingRelationId},
|
|
|
|
{"password", UserMappingRelationId},
|
|
|
|
{"connect_timeout", ForeignServerRelationId},
|
|
|
|
{"dbname", ForeignServerRelationId},
|
|
|
|
{"host", ForeignServerRelationId},
|
|
|
|
{"hostaddr", ForeignServerRelationId},
|
|
|
|
{"port", ForeignServerRelationId},
|
|
|
|
{"tty", ForeignServerRelationId},
|
|
|
|
{"options", ForeignServerRelationId},
|
|
|
|
{"requiressl", ForeignServerRelationId},
|
|
|
|
{"sslmode", ForeignServerRelationId},
|
|
|
|
{"gsslib", ForeignServerRelationId},
|
|
|
|
{NULL, InvalidOid}
|
2009-02-24 11:06:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if the provided option is one of libpq conninfo options.
|
|
|
|
* context is the Oid of the catalog the option came from, or 0 if we
|
|
|
|
* don't care.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
is_conninfo_option(const char *option, Oid context)
|
|
|
|
{
|
|
|
|
struct ConnectionOption *opt;
|
|
|
|
|
|
|
|
for (opt = libpq_conninfo_options; opt->optname; opt++)
|
2009-12-23 13:23:59 +01:00
|
|
|
if (context == opt->optcontext && strcmp(opt->optname, option) == 0)
|
2009-02-24 11:06:36 +01:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Validate the generic option given to SERVER or USER MAPPING.
|
2013-02-21 11:26:23 +01:00
|
|
|
* Raise an ERROR if the option or its value is considered invalid.
|
2009-02-24 11:06:36 +01:00
|
|
|
*
|
|
|
|
* Valid server options are all libpq conninfo options except
|
|
|
|
* user and password -- these may only appear in USER MAPPING options.
|
2013-02-21 11:26:23 +01:00
|
|
|
*
|
|
|
|
* Caution: this function is deprecated, and is now meant only for testing
|
|
|
|
* purposes, because the list of options it knows about doesn't necessarily
|
|
|
|
* square with those known to whichever libpq instance you might be using.
|
|
|
|
* Inquire of libpq itself, instead.
|
2009-02-24 11:06:36 +01:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
postgresql_fdw_validator(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
List *options_list = untransformRelOptions(PG_GETARG_DATUM(0));
|
|
|
|
Oid catalog = PG_GETARG_OID(1);
|
2009-02-24 11:06:36 +01:00
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
ListCell *cell;
|
2009-02-24 11:06:36 +01:00
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
foreach(cell, options_list)
|
2009-02-24 11:06:36 +01:00
|
|
|
{
|
|
|
|
DefElem *def = lfirst(cell);
|
|
|
|
|
|
|
|
if (!is_conninfo_option(def->defname, catalog))
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
struct ConnectionOption *opt;
|
|
|
|
StringInfoData buf;
|
2009-02-24 11:06:36 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Unknown option specified, complain about it. Provide a hint
|
|
|
|
* with list of valid options for the object.
|
|
|
|
*/
|
|
|
|
initStringInfo(&buf);
|
|
|
|
for (opt = libpq_conninfo_options; opt->optname; opt++)
|
2009-12-23 13:23:59 +01:00
|
|
|
if (catalog == opt->optcontext)
|
2009-02-24 11:06:36 +01:00
|
|
|
appendStringInfo(&buf, "%s%s", (buf.len > 0) ? ", " : "",
|
|
|
|
opt->optname);
|
|
|
|
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_SYNTAX_ERROR),
|
|
|
|
errmsg("invalid option \"%s\"", def->defname),
|
2011-02-20 06:17:18 +01:00
|
|
|
errhint("Valid options in this context are: %s",
|
|
|
|
buf.data)));
|
2009-02-24 11:06:36 +01:00
|
|
|
|
|
|
|
PG_RETURN_BOOL(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PG_RETURN_BOOL(true);
|
|
|
|
}
|
2011-04-01 17:28:28 +02:00
|
|
|
|
2012-03-08 00:20:58 +01:00
|
|
|
|
2011-04-01 17:28:28 +02:00
|
|
|
/*
|
|
|
|
* get_foreign_data_wrapper_oid - given a FDW name, look up the OID
|
|
|
|
*
|
|
|
|
* If missing_ok is false, throw an error if name not found. If true, just
|
|
|
|
* return InvalidOid.
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
get_foreign_data_wrapper_oid(const char *fdwname, bool missing_ok)
|
|
|
|
{
|
|
|
|
Oid oid;
|
|
|
|
|
|
|
|
oid = GetSysCacheOid1(FOREIGNDATAWRAPPERNAME, CStringGetDatum(fdwname));
|
|
|
|
if (!OidIsValid(oid) && !missing_ok)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("foreign-data wrapper \"%s\" does not exist",
|
|
|
|
fdwname)));
|
|
|
|
return oid;
|
|
|
|
}
|
|
|
|
|
2012-03-08 00:20:58 +01:00
|
|
|
|
2011-04-01 17:28:28 +02:00
|
|
|
/*
|
Code review for foreign/custom join pushdown patch.
Commit e7cb7ee14555cc9c5773e2c102efd6371f6f2005 included some design
decisions that seem pretty questionable to me, and there was quite a lot
of stuff not to like about the documentation and comments. Clean up
as follows:
* Consider foreign joins only between foreign tables on the same server,
rather than between any two foreign tables with the same underlying FDW
handler function. In most if not all cases, the FDW would simply have had
to apply the same-server restriction itself (far more expensively, both for
lack of caching and because it would be repeated for each combination of
input sub-joins), or else risk nasty bugs. Anyone who's really intent on
doing something outside this restriction can always use the
set_join_pathlist_hook.
* Rename fdw_ps_tlist/custom_ps_tlist to fdw_scan_tlist/custom_scan_tlist
to better reflect what they're for, and allow these custom scan tlists
to be used even for base relations.
* Change make_foreignscan() API to include passing the fdw_scan_tlist
value, since the FDW is required to set that. Backwards compatibility
doesn't seem like an adequate reason to expect FDWs to set it in some
ad-hoc extra step, and anyway existing FDWs can just pass NIL.
* Change the API of path-generating subroutines of add_paths_to_joinrel,
and in particular that of GetForeignJoinPaths and set_join_pathlist_hook,
so that various less-used parameters are passed in a struct rather than
as separate parameter-list entries. The objective here is to reduce the
probability that future additions to those parameter lists will result in
source-level API breaks for users of these hooks. It's possible that this
is even a small win for the core code, since most CPU architectures can't
pass more than half a dozen parameters efficiently anyway. I kept root,
joinrel, outerrel, innerrel, and jointype as separate parameters to reduce
code churn in joinpath.c --- in particular, putting jointype into the
struct would have been problematic because of the subroutines' habit of
changing their local copies of that variable.
* Avoid ad-hocery in ExecAssignScanProjectionInfo. It was probably all
right for it to know about IndexOnlyScan, but if the list is to grow
we should refactor the knowledge out to the callers.
* Restore nodeForeignscan.c's previous use of the relcache to avoid
extra GetFdwRoutine lookups for base-relation scans.
* Lots of cleanup of documentation and missed comments. Re-order some
code additions into more logical places.
2015-05-10 20:36:30 +02:00
|
|
|
* get_foreign_server_oid - given a server name, look up the OID
|
2011-04-01 17:28:28 +02:00
|
|
|
*
|
|
|
|
* If missing_ok is false, throw an error if name not found. If true, just
|
|
|
|
* return InvalidOid.
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
get_foreign_server_oid(const char *servername, bool missing_ok)
|
|
|
|
{
|
|
|
|
Oid oid;
|
|
|
|
|
|
|
|
oid = GetSysCacheOid1(FOREIGNSERVERNAME, CStringGetDatum(servername));
|
|
|
|
if (!OidIsValid(oid) && !missing_ok)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("server \"%s\" does not exist", servername)));
|
|
|
|
return oid;
|
|
|
|
}
|
2016-02-04 23:05:09 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a copy of an existing local path for a given join relation.
|
|
|
|
*
|
|
|
|
* This function is usually helpful to obtain an alternate local path for EPQ
|
|
|
|
* checks.
|
|
|
|
*
|
|
|
|
* Right now, this function only supports unparameterized foreign joins, so we
|
|
|
|
* only search for unparameterized path in the given list of paths. Since we
|
|
|
|
* are searching for a path which can be used to construct an alternative local
|
|
|
|
* plan for a foreign join, we look for only MergeJoin, HashJoin or NestLoop
|
|
|
|
* paths.
|
|
|
|
*
|
|
|
|
* If the inner or outer subpath of the chosen path is a ForeignScan, we
|
|
|
|
* replace it with its outer subpath. For this reason, and also because the
|
|
|
|
* planner might free the original path later, the path returned by this
|
|
|
|
* function is a shallow copy of the original. There's no need to copy
|
|
|
|
* the substructure, so we don't.
|
|
|
|
*
|
|
|
|
* Since the plan created using this path will presumably only be used to
|
|
|
|
* execute EPQ checks, efficiency of the path is not a concern. But since the
|
2016-02-05 14:07:38 +01:00
|
|
|
* path list in RelOptInfo is anyway sorted by total cost we are likely to
|
|
|
|
* choose the most efficient path, which is all for the best.
|
2016-02-04 23:05:09 +01:00
|
|
|
*/
|
|
|
|
extern Path *
|
|
|
|
GetExistingLocalJoinPath(RelOptInfo *joinrel)
|
|
|
|
{
|
|
|
|
ListCell *lc;
|
|
|
|
|
|
|
|
Assert(joinrel->reloptkind == RELOPT_JOINREL);
|
|
|
|
|
|
|
|
foreach(lc, joinrel->pathlist)
|
|
|
|
{
|
|
|
|
Path *path = (Path *) lfirst(lc);
|
|
|
|
JoinPath *joinpath = NULL;
|
|
|
|
|
2016-02-05 14:07:38 +01:00
|
|
|
/* Skip parameterised paths. */
|
|
|
|
if (path->param_info != NULL)
|
2016-02-04 23:05:09 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (path->pathtype)
|
|
|
|
{
|
|
|
|
case T_HashJoin:
|
|
|
|
{
|
|
|
|
HashPath *hash_path = makeNode(HashPath);
|
|
|
|
|
|
|
|
memcpy(hash_path, path, sizeof(HashPath));
|
|
|
|
joinpath = (JoinPath *) hash_path;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_NestLoop:
|
|
|
|
{
|
|
|
|
NestPath *nest_path = makeNode(NestPath);
|
|
|
|
|
|
|
|
memcpy(nest_path, path, sizeof(NestPath));
|
|
|
|
joinpath = (JoinPath *) nest_path;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_MergeJoin:
|
|
|
|
{
|
|
|
|
MergePath *merge_path = makeNode(MergePath);
|
|
|
|
|
|
|
|
memcpy(merge_path, path, sizeof(MergePath));
|
|
|
|
joinpath = (JoinPath *) merge_path;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Just skip anything else. We don't know if corresponding
|
|
|
|
* plan would build the output row from whole-row references
|
|
|
|
* of base relations and execute the EPQ checks.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This path isn't good for us, check next. */
|
|
|
|
if (!joinpath)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If either inner or outer path is a ForeignPath corresponding to a
|
|
|
|
* pushed down join, replace it with the fdw_outerpath, so that we
|
|
|
|
* maintain path for EPQ checks built entirely of local join
|
|
|
|
* strategies.
|
|
|
|
*/
|
|
|
|
if (IsA(joinpath->outerjoinpath, ForeignPath))
|
|
|
|
{
|
|
|
|
ForeignPath *foreign_path;
|
|
|
|
|
|
|
|
foreign_path = (ForeignPath *) joinpath->outerjoinpath;
|
|
|
|
if (foreign_path->path.parent->reloptkind == RELOPT_JOINREL)
|
|
|
|
joinpath->outerjoinpath = foreign_path->fdw_outerpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsA(joinpath->innerjoinpath, ForeignPath))
|
|
|
|
{
|
|
|
|
ForeignPath *foreign_path;
|
|
|
|
|
|
|
|
foreign_path = (ForeignPath *) joinpath->innerjoinpath;
|
|
|
|
if (foreign_path->path.parent->reloptkind == RELOPT_JOINREL)
|
|
|
|
joinpath->innerjoinpath = foreign_path->fdw_outerpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Path *) joinpath;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|