91 #define CONNECTION_CLEANUP_TIMEOUT 30000
94 #define CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel) \
97 snprintf((sql), sizeof(sql), \
98 "ABORT TRANSACTION"); \
100 snprintf((sql), sizeof(sql), \
101 "ROLLBACK TO SAVEPOINT s%d; RELEASE SAVEPOINT s%d", \
102 (entry)->xact_depth, (entry)->xact_depth); \
142 PGresult **result,
bool *timed_out);
145 List **pending_entries,
146 List **cancel_requested);
151 List *cancel_requested,
231 elog(
DEBUG3,
"closing connection %p for option changes to take effect",
241 if (entry->
conn == NULL)
276 if (errdata->
sqlerrcode != ERRCODE_CONNECTION_FAILURE ||
303 (
errmsg_internal(
"could not start remote transaction on connection %p",
307 elog(
DEBUG3,
"closing connection %p to reestablish a new one",
351 memset(&entry->
state, 0,
sizeof(entry->
state));
375 if (strcmp(def->
defname,
"keep_connections") == 0)
377 else if (strcmp(def->
defname,
"parallel_commit") == 0)
379 else if (strcmp(def->
defname,
"parallel_abort") == 0)
386 elog(
DEBUG3,
"new postgres_fdw connection %p for server \"%s\" (user mapping oid %u, userid %u)",
417 for (
int i = 0; keywords[
i] != NULL;
i++)
419 if (strcmp(keywords[
i],
"password") == 0 &&
values[
i][0] !=
'\0')
425 (
errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
426 errmsg(
"password or GSSAPI delegated credentials required"),
427 errdetail(
"Non-superuser cannot connect if the server does not request a password or use GSSAPI with delegated credentials."),
428 errhint(
"Target server's authentication method must be changed or password_required=false set in the user mapping attributes.")));
444 const char **keywords;
446 char *appname = NULL;
457 keywords = (
const char **)
palloc(n *
sizeof(
char *));
462 keywords + n,
values + n);
464 keywords + n,
values + n);
477 keywords[n] =
"application_name";
488 for (
int i = n - 1;
i >= 0;
i--)
490 if (strcmp(keywords[
i],
"application_name") == 0 &&
498 if (appname[0] !=
'\0')
516 keywords[n] =
"fallback_application_name";
517 values[n] =
"postgres_fdw";
521 keywords[n] =
"client_encoding";
525 keywords[n] =
values[n] = NULL;
537 (
errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
538 errmsg(
"could not connect to server \"%s\"",
569 if (entry->
conn != NULL)
585 foreach(cell,
user->options)
589 if (strcmp(def->
defname,
"password_required") == 0)
620 for (
i = 0; keywords[
i] != NULL;
i++)
622 if (strcmp(keywords[
i],
"password") == 0 &&
values[
i][0] !=
'\0')
631 (
errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
632 errmsg(
"password or GSSAPI delegated credentials required"),
633 errdetail(
"Non-superusers must delegate GSSAPI credentials or provide a password in the user mapping.")));
672 if (remoteversion >= 80400)
674 if (remoteversion >= 90000)
736 elog(
DEBUG3,
"starting remote transaction on connection %p",
740 sql =
"START TRANSACTION ISOLATION LEVEL SERIALIZABLE";
742 sql =
"START TRANSACTION ISOLATION LEVEL REPEATABLE READ";
912 bool clear,
const char *sql)
931 sqlstate = ERRCODE_CONNECTION_FAILURE;
938 if (message_primary == NULL)
943 (message_primary != NULL && message_primary[0] !=
'\0') ?
945 errmsg(
"could not obtain message string for remote error"),
947 message_hint ?
errhint(
"%s", message_hint) : 0,
948 message_context ?
errcontext(
"%s", message_context) : 0,
949 sql ?
errcontext(
"remote SQL command: %s", sql) : 0));
988 if (entry->
conn == NULL)
994 elog(
DEBUG3,
"closing remote transaction on connection %p",
1013 pending_entries =
lappend(pending_entries, entry);
1054 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1055 errmsg(
"cannot PREPARE a transaction that has operated on postgres_fdw foreign tables")));
1061 elog(
ERROR,
"missed cleaning up connection during pre-commit");
1084 if (pending_entries || cancel_requested)
1152 elog(
ERROR,
"missed cleaning up remote subtransaction at level %d",
1164 snprintf(sql,
sizeof(sql),
"RELEASE SAVEPOINT s%d", curlevel);
1169 pending_entries =
lappend(pending_entries, entry);
1194 if (pending_entries || cancel_requested)
1241 if (entry->
conn == NULL)
1245 if (hashvalue == 0 ||
1294 (
errcode(ERRCODE_CONNECTION_EXCEPTION),
1295 errmsg(
"connection to server \"%s\" was lost",
1376 if (!
PQcancel(cancel, errbuf,
sizeof(errbuf)))
1379 (
errcode(ERRCODE_CONNECTION_FAILURE),
1380 errmsg(
"could not send cancel request: %s",
1406 (
errcode(ERRCODE_CONNECTION_FAILURE),
1407 errmsg(
"could not get result of cancel request: %s",
1417 (
errmsg(
"could not get result of cancel request due to timeout")));
1420 (
errcode(ERRCODE_CONNECTION_FAILURE),
1421 errmsg(
"could not get result of cancel request: %s",
1460 false, ignore_errors);
1504 (
errmsg(
"could not get query result due to timeout"),
1505 query ?
errcontext(
"remote SQL command: %s", query) : 0));
1516 return ignore_errors;
1538 volatile bool failed =
false;
1539 PGresult *
volatile last_res = NULL;
1558 if (cur_timeout <= 0)
1677 memset(&entry->
state, 0,
sizeof(entry->
state));
1694 List **pending_entries,
List **cancel_requested)
1727 *cancel_requested =
lappend(*cancel_requested, entry);
1736 *pending_entries =
lappend(*pending_entries, entry);
1758 foreach(lc, pending_entries)
1777 pending_deallocs =
lappend(pending_deallocs, entry);
1788 if (!pending_deallocs)
1795 foreach(lc, pending_deallocs)
1832 snprintf(sql,
sizeof(sql),
"RELEASE SAVEPOINT s%d", curlevel);
1833 foreach(lc, pending_entries)
1865 if (cancel_requested)
1867 foreach(lc, cancel_requested)
1902 pending_entries =
lappend(pending_entries, entry);
1907 if (!pending_entries)
1913 foreach(lc, pending_entries)
1950 pending_deallocs =
lappend(pending_deallocs, entry);
1959 memset(&entry->
state, 0,
sizeof(entry->
state));
1967 if (!pending_deallocs)
1975 foreach(lc, pending_deallocs)
1993 endtime,
true,
true))
2004 memset(&entry->
state, 0,
sizeof(entry->
state));
2028 #define POSTGRES_FDW_GET_CONNECTIONS_COLS 2
2086 Assert(entry->conn && entry->xact_depth > 0 && entry->invalidated);
2169 bool result =
false;
2185 if (all || entry->
serverid == serverid)
2209 (
errmsg(
"cannot close dropped server connection because it is still in use")));
2213 (
errmsg(
"cannot close connection for server \"%s\" because it is still in use",
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
TimestampTz GetCurrentTimestamp(void)
Datum now(PG_FUNCTION_ARGS)
bool be_gssapi_get_delegation(Port *port)
static Datum values[MAXATTR]
#define CStringGetTextDatum(s)
#define OidIsValid(objectId)
static unsigned int prep_stmt_number
unsigned int GetCursorNumber(PGconn *conn)
static bool UserMappingPasswordRequired(UserMapping *user)
Datum postgres_fdw_get_connections(PG_FUNCTION_ARGS)
void do_sql_command(PGconn *conn, const char *sql)
static void pgfdw_finish_pre_subcommit_cleanup(List *pending_entries, int curlevel)
static void disconnect_pg_server(ConnCacheEntry *entry)
void ReleaseConnection(PGconn *conn)
PG_FUNCTION_INFO_V1(postgres_fdw_get_connections)
PGresult * pgfdw_get_result(PGconn *conn, const char *query)
static void pgfdw_finish_abort_cleanup(List *pending_entries, List *cancel_requested, bool toplevel)
static void pgfdw_reset_xact_state(ConnCacheEntry *entry, bool toplevel)
static void configure_remote_session(PGconn *conn)
void pgfdw_report_error(int elevel, PGresult *res, PGconn *conn, bool clear, const char *sql)
PGresult * pgfdw_exec_query(PGconn *conn, const char *query, PgFdwConnState *state)
static bool xact_got_connection
#define POSTGRES_FDW_GET_CONNECTIONS_COLS
struct ConnCacheEntry ConnCacheEntry
Datum postgres_fdw_disconnect_all(PG_FUNCTION_ARGS)
static bool pgfdw_cancel_query_end(PGconn *conn, TimestampTz endtime, bool consume_input)
static void do_sql_command_end(PGconn *conn, const char *sql, bool consume_input)
static bool pgfdw_get_cleanup_result(PGconn *conn, TimestampTz endtime, PGresult **result, bool *timed_out)
#define CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel)
static bool pgfdw_cancel_query_begin(PGconn *conn)
static bool pgfdw_abort_cleanup_begin(ConnCacheEntry *entry, bool toplevel, List **pending_entries, List **cancel_requested)
static HTAB * ConnectionHash
static bool pgfdw_exec_cleanup_query_end(PGconn *conn, const char *query, TimestampTz endtime, bool consume_input, bool ignore_errors)
static unsigned int cursor_number
static void make_new_connection(ConnCacheEntry *entry, UserMapping *user)
static void pgfdw_security_check(const char **keywords, const char **values, UserMapping *user, PGconn *conn)
Datum postgres_fdw_disconnect(PG_FUNCTION_ARGS)
static void pgfdw_subxact_callback(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)
static PGconn * connect_pg_server(ForeignServer *server, UserMapping *user)
static bool pgfdw_exec_cleanup_query(PGconn *conn, const char *query, bool ignore_errors)
PGconn * GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
unsigned int GetPrepStmtNumber(PGconn *conn)
static bool pgfdw_exec_cleanup_query_begin(PGconn *conn, const char *query)
static void pgfdw_reject_incomplete_xact_state_change(ConnCacheEntry *entry)
static void check_conn_params(const char **keywords, const char **values, UserMapping *user)
static void pgfdw_inval_callback(Datum arg, int cacheid, uint32 hashvalue)
static void pgfdw_xact_callback(XactEvent event, void *arg)
#define CONNECTION_CLEANUP_TIMEOUT
static void do_sql_command_begin(PGconn *conn, const char *sql)
static void pgfdw_abort_cleanup(ConnCacheEntry *entry, bool toplevel)
static void begin_remote_xact(ConnCacheEntry *entry)
static bool pgfdw_cancel_query(PGconn *conn)
static void pgfdw_finish_pre_commit_cleanup(List *pending_entries)
static bool disconnect_cached_connections(Oid serverid)
char * process_pgfdw_appname(const char *appname)
int ExtractConnectionOptions(List *defelems, const char **keywords, const char **values)
char * pgfdw_application_name
static void PGresult * res
elog(ERROR, "%s: %s", p2, msg)
bool defGetBoolean(DefElem *def)
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
HTAB * hash_create(const char *tabname, long 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,...)
void FreeErrorData(ErrorData *edata)
int errdetail_internal(const char *fmt,...)
int errdetail(const char *fmt,...)
void FlushErrorState(void)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
bool in_error_recursion_trouble(void)
ErrorData * CopyErrorData(void)
#define MAKE_SQLSTATE(ch1, ch2, ch3, ch4, ch5)
#define ereport(elevel,...)
int PQserverVersion(const PGconn *conn)
PGcancel * PQgetCancel(PGconn *conn)
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
int PQconnectionUsedPassword(const PGconn *conn)
int PQconnectionUsedGSSAPI(const PGconn *conn)
char * PQerrorMessage(const PGconn *conn)
int PQcancel(PGcancel *cancel, char *errbuf, int errbufsize)
ConnStatusType PQstatus(const PGconn *conn)
void PQfreeCancel(PGcancel *cancel)
int PQsocket(const PGconn *conn)
ExecStatusType PQresultStatus(const PGresult *res)
PGresult * PQexec(PGconn *conn, const char *query)
int PQconsumeInput(PGconn *conn)
int PQsendQuery(PGconn *conn, const char *query)
int PQisBusy(PGconn *conn)
char * PQresultErrorField(const PGresult *res, int fieldcode)
PGresult * PQgetResult(PGconn *conn)
#define PG_GETARG_TEXT_PP(n)
#define PG_RETURN_BOOL(x)
ForeignServer * GetForeignServerExtended(Oid serverid, bits16 flags)
ForeignServer * GetForeignServer(Oid serverid)
ForeignServer * GetForeignServerByName(const char *srvname, bool missing_ok)
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
int WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
void ResetLatch(Latch *latch)
#define WL_SOCKET_READABLE
#define WL_EXIT_ON_PM_DEATH
static void libpqsrv_disconnect(PGconn *conn)
static PGconn * libpqsrv_connect_params(const char *const *keywords, const char *const *values, int expand_dbname, uint32 wait_event_info)
Assert(fmt[strlen(fmt) - 1] !='\n')
List * lappend(List *list, void *datum)
const char * GetDatabaseEncodingName(void)
char * pchomp(const char *in)
void pfree(void *pointer)
MemoryContext CurrentMemoryContext
#define CHECK_FOR_INTERRUPTS()
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
static int list_length(const List *l)
static Datum BoolGetDatum(bool X)
static Datum ObjectIdGetDatum(Oid X)
#define PG_DIAG_MESSAGE_HINT
#define PG_DIAG_MESSAGE_PRIMARY
#define PG_DIAG_MESSAGE_DETAIL
void process_pending_request(AsyncRequest *areq)
AsyncRequest * pendingAreq
Tuplestorestate * setResult
bool superuser_arg(Oid roleid)
#define GetSysCacheHashValue1(cacheId, key1)
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
#define TimestampTzPlusMilliseconds(tz, ms)
char * text_to_cstring(const text *t)
int GetCurrentTransactionNestLevel(void)
void RegisterXactCallback(XactCallback callback, void *arg)
void RegisterSubXactCallback(SubXactCallback callback, void *arg)
@ SUBXACT_EVENT_PRE_COMMIT_SUB
@ SUBXACT_EVENT_ABORT_SUB
@ XACT_EVENT_PARALLEL_PRE_COMMIT
@ XACT_EVENT_PARALLEL_COMMIT
@ XACT_EVENT_PARALLEL_ABORT
#define IsolationIsSerializable()