60#include "utils/fmgroids.h"
187 errmsg(
"connection \"%s\" not available", conname)));
191 errmsg(
"connection not available")));
232 errmsg(
"could not establish connection"),
237 "received message via remote connection");
333 errmsg(
"could not establish connection"),
338 "received message via remote connection");
369 char *conname =
NULL;
408 char *conname =
NULL;
451 if (!rconn || !rconn->
conn)
482 "while opening cursor \"%s\"",
curname);
500 char *conname =
NULL;
539 if (!rconn || !rconn->
conn)
551 "while closing cursor \"%s\"",
curname);
585 char *conname =
NULL;
655 "while fetching from cursor \"%s\"",
curname);
696 elog(
ERROR,
"wrong number of arguments");
726 char *conname =
NULL;
763 elog(
ERROR,
"wrong number of arguments");
783 elog(
ERROR,
"wrong number of arguments");
806 "while executing query");
842 errmsg(
"set-valued function called in context that cannot accept a set")));
846 errmsg(
"materialize mode required, but it is not allowed in this context")));
903 errmsg(
"function returning record called in context "
904 "that cannot accept type record")));
908 elog(
ERROR,
"return type must be a row type");
921 if (nfields != tupdesc->
natts)
924 errmsg(
"remote query result rowtype does not match "
925 "the specified FROM clause rowtype")));
944 rsinfo->setResult = tupstore;
945 rsinfo->setDesc = tupdesc;
951 for (row = 0; row < ntuples; row++)
959 for (
i = 0;
i < nfields;
i++)
1013 "dblink temporary context",
1024 "while executing query");
1051 rsinfo->setResult = tupstore;
1052 rsinfo->setDesc = tupdesc;
1106 elog(
ERROR,
"failed to set single-row mode for dblink query");
1196 errmsg(
"function returning record called in context "
1197 "that cannot accept type record")));
1201 elog(
ERROR,
"return type must be a row type");
1209 if (nfields != tupdesc->
natts)
1212 errmsg(
"remote query result rowtype does not match "
1213 "the specified FROM clause rowtype")));
1222 rsinfo->setDesc = tupdesc;
1251 for (
i = 0;
i < nfields;
i++)
1377 if (msg ==
NULL || msg[0] ==
'\0')
1400 char *conname =
NULL;
1435 elog(
ERROR,
"wrong number of arguments");
1446 "while executing command");
1468 errmsg(
"statement returning results not allowed")));
1537 funcctx->attinmeta = attinmeta;
1562 call_cntr =
funcctx->call_cntr;
1563 max_calls =
funcctx->max_calls;
1565 results = (
char **)
funcctx->user_fctx;
1566 attinmeta =
funcctx->attinmeta;
1568 if (call_cntr < max_calls)
1576 values[1] = results[call_cntr];
1654 errmsg(
"source key array length must match number of key attributes")));
1668 errmsg(
"target key array length must match number of key attributes")));
1740 errmsg(
"target key array length must match number of key attributes")));
1819 errmsg(
"source key array length must match number of key attributes")));
1833 errmsg(
"target key array length must match number of key attributes")));
1873#define DBLINK_NOTIFY_COLS 3
1898 memset(nulls, 0,
sizeof(nulls));
1952 errdetail(
"Could not get libpq's default connection options.")));
1987 errhint(
"Perhaps you meant the option \"%s\".",
1989 errhint(
"There are no valid options in this context.")));
2016 char **result =
NULL;
2039 if (
index->indisprimary)
2093 if (bitmap && (*bitmap & bitmask) == 0)
2108 if (bitmask == 0x100)
2138 natts = tupdesc->
natts;
2144 errmsg(
"source row not found")));
2149 for (
i = 0;
i < natts;
i++)
2153 if (
att->attisdropped)
2170 for (
i = 0;
i < natts;
i++)
2255 natts = tupdesc->
natts;
2261 errmsg(
"source row not found")));
2269 for (
i = 0;
i < natts;
i++)
2273 if (attr->attisdropped)
2379 natts = tupdesc->
natts;
2390 for (
i = 0;
i < natts;
i++)
2397 if (attr->attisdropped)
2436 errmsg(
"source criteria matched more than one record")));
2570 errmsg(
"duplicate connection name")));
2596 errmsg(
"undefined connection name")));
2616 bool has_scram_keys =
false;
2704 errmsg(
"password or GSSAPI delegated credentials required"),
2705 errdetail(
"Non-superusers may only connect using credentials they provide, eg: password in connection string or delegated GSSAPI credentials"),
2706 errhint(
"Ensure provided credentials match target server's authentication method.")));
2771 errmsg(
"password or GSSAPI delegated credentials required"),
2772 errdetail(
"Non-superusers must provide a password in the connection string or send delegated GSSAPI credentials.")));
2788 bool fail,
const char *
fmt,...)
2837 errmsg(
"could not obtain message string for remote error"),
2842 (
errcontext(
"%s on dblink connection named \"%s\"",
2844 (
errcontext(
"%s on unnamed dblink connection",
2881 errdetail(
"Could not get libpq's default connection options.")));
2911 foreach(cell,
fdw->options)
2920 foreach(cell, foreign_server->
options)
2960 if (*
cp ==
'\\' || *
cp ==
'\'')
2989 int natts = tupdesc->
natts;
2999 errmsg(
"number of key attributes must be > 0")));
3020 for (
j = 0;
j < natts;
j++)
3031 (*pkattnums)[
i] =
j;
3205 elog(
ERROR,
"could not encode SCRAM client key");
3214 elog(
ERROR,
"could not encode SCRAM server key");
3230 foreach(cell, foreign_server->
options)
3238 foreach(cell,
user->options)
void aclcheck_error(AclResult aclerr, ObjectType objtype, const char *objectname)
AclResult object_aclcheck(Oid classid, Oid objectid, Oid roleid, AclMode mode)
AclResult pg_class_aclcheck(Oid table_oid, Oid roleid, AclMode mode)
#define PG_GETARG_ARRAYTYPE_P(n)
#define ARR_NULLBITMAP(a)
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
int ArrayGetNItems(int ndim, const int *dims)
Datum current_query(PG_FUNCTION_ARGS)
TimestampTz GetCurrentTimestamp(void)
int pg_b64_enc_len(int srclen)
int pg_b64_encode(const uint8 *src, int len, char *dst, int dstlen)
bool be_gssapi_get_delegation(Port *port)
static Datum values[MAXATTR]
#define CStringGetTextDatum(s)
#define TextDatumGetCString(d)
#define Assert(condition)
#define pg_attribute_printf(f, a)
#define OidIsValid(objectId)
static bool UseScramPassthrough(ForeignServer *foreign_server, UserMapping *user)
static bool dblink_connstr_has_pw(const char *connstr)
static uint32 dblink_we_get_result
static pg_noreturn void dblink_res_internalerror(PGconn *conn, PGresult *res, const char *p2)
Datum dblink_get_result(PG_FUNCTION_ARGS)
static int get_attnum_pk_pos(int *pkattnums, int pknumatts, int key)
Datum dblink_build_sql_delete(PG_FUNCTION_ARGS)
static PGresult * storeQueryResult(storeInfo *sinfo, PGconn *conn, const char *sql)
Datum dblink_get_pkey(PG_FUNCTION_ARGS)
static bool is_valid_dblink_fdw_option(const PQconninfoOption *options, const char *option, Oid context)
static void RangeVarCallbackForDblink(const RangeVar *relation, Oid relId, Oid oldRelId, void *arg)
Datum dblink_open(PG_FUNCTION_ARGS)
static remoteConn * pconn
static pg_noreturn void dblink_conn_not_avail(const char *conname)
static void appendSCRAMKeysInfo(StringInfo buf)
static HeapTuple get_tuple_of_interest(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals)
static uint32 dblink_we_connect
Datum dblink_close(PG_FUNCTION_ARGS)
static void restoreLocalGucs(int nestlevel)
static char * escape_param_str(const char *str)
static char * quote_ident_cstr(char *rawstr)
static HTAB * createConnHash(void)
Datum dblink_exec(PG_FUNCTION_ARGS)
static void dblink_connstr_check(const char *connstr)
static char * get_sql_insert(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals, char **tgt_pkattvals)
static void storeRow(storeInfo *sinfo, PGresult *res, bool first)
static char * get_sql_update(Relation rel, int *pkattnums, int pknumatts, char **src_pkattvals, char **tgt_pkattvals)
static bool is_valid_dblink_option(const PQconninfoOption *options, const char *option, Oid context)
Datum dblink_build_sql_insert(PG_FUNCTION_ARGS)
static remoteConn * getConnectionByName(const char *name)
Datum dblink_get_notify(PG_FUNCTION_ARGS)
static HTAB * remoteConnHash
Datum dblink_disconnect(PG_FUNCTION_ARGS)
static PGconn * dblink_get_named_conn(const char *conname)
static void dblink_get_conn(char *conname_or_str, PGconn *volatile *conn_p, char **conname_p, volatile bool *freeconn_p)
static int applyRemoteGucs(PGconn *conn)
Datum dblink_fdw_validator(PG_FUNCTION_ARGS)
static void dblink_init(void)
static remoteConn * createNewConnection(const char *name)
static Datum dblink_record_internal(FunctionCallInfo fcinfo, bool is_async)
Datum dblink_get_connections(PG_FUNCTION_ARGS)
static char ** get_pkey_attnames(Relation rel, int16 *indnkeyatts)
static void prepTuplestoreResult(FunctionCallInfo fcinfo)
static void dblink_res_error(PGconn *conn, const char *conname, PGresult *res, bool fail, const char *fmt,...) pg_attribute_printf(5
static void dblink_security_check(PGconn *conn, const char *connname, const char *connstr)
Datum dblink_record(PG_FUNCTION_ARGS)
static void materializeResult(FunctionCallInfo fcinfo, PGconn *conn, PGresult *res)
Datum dblink_is_busy(PG_FUNCTION_ARGS)
static char ** get_text_array_contents(ArrayType *array, int *numitems)
static bool dblink_connstr_has_required_scram_options(const char *connstr)
static void static char * get_connect_string(const char *servername)
Datum dblink_connect(PG_FUNCTION_ARGS)
Datum dblink_current_query(PG_FUNCTION_ARGS)
Datum dblink_fetch(PG_FUNCTION_ARGS)
Datum dblink_build_sql_update(PG_FUNCTION_ARGS)
static void deleteConnection(const char *name)
static char * generate_relation_name(Relation rel)
#define DBLINK_NOTIFY_COLS
Datum dblink_cancel_query(PG_FUNCTION_ARGS)
Datum dblink_send_query(PG_FUNCTION_ARGS)
static char * get_sql_delete(Relation rel, int *pkattnums, int pknumatts, char **tgt_pkattvals)
Datum dblink_error_message(PG_FUNCTION_ARGS)
static void validate_pkattnums(Relation rel, int2vector *pkattnums_arg, int32 pknumatts_arg, int **pkattnums, int *pknumatts)
static void materializeQueryResult(FunctionCallInfo fcinfo, PGconn *conn, const char *conname, const char *sql, bool fail)
static uint32 dblink_we_get_conn
static Relation get_rel_from_relname(text *relname_text, LOCKMODE lockmode, AclMode aclmode)
bool defGetBoolean(DefElem *def)
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
HTAB * hash_create(const char *tabname, int64 nelem, const HASHCTL *info, int flags)
void * hash_seq_search(HASH_SEQ_STATUS *status)
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
int errmsg_internal(const char *fmt,...)
int errdetail_internal(const char *fmt,...)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define MAKE_SQLSTATE(ch1, ch2, ch3, ch4, ch5)
#define ereport(elevel,...)
HeapTuple BuildTupleFromCStrings(AttInMetadata *attinmeta, char **values)
AttInMetadata * TupleDescGetAttInMetadata(TupleDesc tupdesc)
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
int PQconnectionUsedPassword(const PGconn *conn)
void PQconninfoFree(PQconninfoOption *connOptions)
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
int PQconnectionUsedGSSAPI(const PGconn *conn)
ConnStatusType PQstatus(const PGconn *conn)
int PQclientEncoding(const PGconn *conn)
PQnoticeReceiver PQsetNoticeReceiver(PGconn *conn, PQnoticeReceiver proc, void *arg)
PQconninfoOption * PQconndefaults(void)
char * PQerrorMessage(const PGconn *conn)
int PQsetClientEncoding(PGconn *conn, const char *encoding)
int PQsetSingleRowMode(PGconn *conn)
void PQfreemem(void *ptr)
int PQconsumeInput(PGconn *conn)
int PQsendQuery(PGconn *conn, const char *query)
int PQisBusy(PGconn *conn)
PGnotify * PQnotifies(PGconn *conn)
#define palloc_array(type, count)
Oid get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
#define PG_GETARG_TEXT_PP(n)
#define DatumGetTextPP(X)
#define PG_GETARG_POINTER(n)
#define PG_MODULE_MAGIC_EXT(...)
#define DirectFunctionCall1(func, arg1)
#define PG_GETARG_DATUM(n)
#define PG_FUNCTION_INFO_V1(funcname)
#define PG_RETURN_TEXT_P(x)
#define PG_RETURN_INT32(x)
#define PG_GETARG_INT32(n)
#define PG_GETARG_BOOL(n)
#define PG_RETURN_DATUM(x)
ForeignDataWrapper * GetForeignDataWrapper(Oid fdwid)
ForeignServer * GetForeignServerByName(const char *srvname, bool missing_ok)
UserMapping * GetUserMapping(Oid userid, Oid serverid)
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
#define SRF_IS_FIRSTCALL()
#define SRF_PERCALL_SETUP()
#define SRF_RETURN_NEXT(_funcctx, _result)
#define SRF_FIRSTCALL_INIT()
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
#define SRF_RETURN_DONE(_funcctx)
void systable_endscan(SysScanDesc sysscan)
HeapTuple systable_getnext(SysScanDesc sysscan)
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
int NewGUCNestLevel(void)
const char * GetConfigOption(const char *name, bool missing_ok, bool restrict_privileged)
void AtEOXact_GUC(bool isCommit, int nestLevel)
int set_config_option(const char *name, const char *value, GucContext context, GucSource source, GucAction action, bool changeVal, int elevel, bool is_reload)
#define HeapTupleIsValid(tuple)
static void * GETSTRUCT(const HeapTupleData *tuple)
struct parser_state match_state[5]
static const char * libpqsrv_cancel(PGconn *conn, TimestampTz endtime)
static PGresult * libpqsrv_exec(PGconn *conn, const char *query, uint32 wait_event_info)
static PGconn * libpqsrv_connect(const char *conninfo, uint32 wait_event_info)
static PGresult * libpqsrv_get_result(PGconn *conn, uint32 wait_event_info)
static void libpqsrv_notice_receiver(void *arg, const PGresult *res)
static void libpqsrv_disconnect(PGconn *conn)
#define PQresultErrorField
void get_typlenbyvalalign(Oid typid, int16 *typlen, bool *typbyval, char *typalign)
char get_rel_relkind(Oid relid)
char * get_namespace_name(Oid nspid)
int GetDatabaseEncoding(void)
const char * GetDatabaseEncodingName(void)
void * MemoryContextAlloc(MemoryContext context, Size size)
void MemoryContextReset(MemoryContext context)
char * pstrdup(const char *in)
void pfree(void *pointer)
void * palloc0(Size size)
MemoryContext TopMemoryContext
char * pchomp(const char *in)
MemoryContext CurrentMemoryContext
void MemoryContextDelete(MemoryContext context)
#define AllocSetContextCreate
#define ALLOCSET_DEFAULT_SIZES
#define CHECK_FOR_INTERRUPTS()
bool RelationIsVisible(Oid relid)
RangeVar * makeRangeVarFromNameList(const List *names)
Oid RangeVarGetRelidExtended(const RangeVar *relation, LOCKMODE lockmode, uint32 flags, RangeVarGetRelidCallback callback, void *callback_arg)
#define IsA(nodeptr, _type_)
ObjectType get_relkind_objtype(char relkind)
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
FormData_pg_attribute * Form_pg_attribute
static const char * connstr
FormData_pg_index * Form_pg_index
static char buf[DEFAULT_XLOG_SEG_SIZE]
static Datum PointerGetDatum(const void *X)
static Datum ObjectIdGetDatum(Oid X)
static Datum Int32GetDatum(int32 X)
#define PG_DIAG_MESSAGE_HINT
#define PG_DIAG_MESSAGE_PRIMARY
#define PG_DIAG_MESSAGE_DETAIL
char * psprintf(const char *fmt,...)
char * quote_literal_cstr(const char *rawstr)
Datum quote_ident(PG_FUNCTION_ARGS)
#define RelationGetRelid(relation)
#define RelationGetRelationName(relation)
List * untransformRelOptions(Datum options)
char * quote_qualified_identifier(const char *qualifier, const char *ident)
void ScanKeyInit(ScanKey entry, AttrNumber attributeNumber, StrategyNumber strategy, RegProcedure procedure, Datum argument)
void truncate_identifier(char *ident, int len, bool warn)
SPITupleTable * SPI_tuptable
int SPI_exec(const char *src, long tcount)
char * SPI_getvalue(HeapTuple tuple, TupleDesc tupdesc, int fnumber)
HeapTuple SPI_copytuple(HeapTuple tuple)
char * SPI_fname(TupleDesc tupdesc, int fnumber)
void relation_close(Relation relation, LOCKMODE lockmode)
#define BTEqualStrategyNumber
#define ERRCODE_DUPLICATE_OBJECT
void appendStringInfo(StringInfo str, const char *fmt,...)
void appendStringInfoString(StringInfo str, const char *s)
void appendStringInfoChar(StringInfo str, char ch)
void initStringInfo(StringInfo str)
uint8 scram_ServerKey[SCRAM_MAX_KEY_LEN]
uint8 scram_ClientKey[SCRAM_MAX_KEY_LEN]
Tuplestorestate * tuplestore
AttInMetadata * attinmeta
void table_close(Relation relation, LOCKMODE lockmode)
Relation table_open(Oid relationId, LOCKMODE lockmode)
TupleDesc CreateTemplateTupleDesc(int natts)
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
void tuplestore_end(Tuplestorestate *state)
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
#define att_align_nominal(cur_offset, attalign)
#define att_addlength_pointer(cur_offset, attlen, attptr)
#define TimestampTzPlusMilliseconds(tz, ms)
const char * getClosestMatch(ClosestMatchState *state)
text * cstring_to_text(const char *s)
void initClosestMatch(ClosestMatchState *state, const char *source, int max_d)
void updateClosestMatch(ClosestMatchState *state, const char *candidate)
char * text_to_cstring(const text *t)
List * textToQualifiedNameList(text *textval)
uint32 WaitEventExtensionNew(const char *wait_event_name)