PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
connection.c File Reference
#include "postgres.h"
#include <poll.h>
#include "access/xact.h"
#include "catalog/pg_user_mapping.h"
#include "commands/defrem.h"
#include "common/base64.h"
#include "funcapi.h"
#include "libpq/libpq-be.h"
#include "libpq/libpq-be-fe-helpers.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "pgstat.h"
#include "postgres_fdw.h"
#include "storage/latch.h"
#include "utils/builtins.h"
#include "utils/hsearch.h"
#include "utils/inval.h"
#include "utils/syscache.h"
Include dependency graph for connection.c:

Go to the source code of this file.

Data Structures

struct  ConnCacheEntry
 

Macros

#define CONNECTION_CLEANUP_TIMEOUT   30000
 
#define RETRY_CANCEL_TIMEOUT   1000
 
#define CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel)
 
#define POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_1   2
 
#define POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_2   6
 
#define POSTGRES_FDW_GET_CONNECTIONS_COLS   6 /* maximum of above */
 

Typedefs

typedef Oid ConnCacheKey
 
typedef struct ConnCacheEntry ConnCacheEntry
 

Enumerations

enum  pgfdwVersion { PGFDW_V1_1 = 0 , PGFDW_V1_2 }
 

Functions

 PG_FUNCTION_INFO_V1 (postgres_fdw_get_connections)
 
 PG_FUNCTION_INFO_V1 (postgres_fdw_get_connections_1_2)
 
 PG_FUNCTION_INFO_V1 (postgres_fdw_disconnect)
 
 PG_FUNCTION_INFO_V1 (postgres_fdw_disconnect_all)
 
static void make_new_connection (ConnCacheEntry *entry, UserMapping *user)
 
static PGconnconnect_pg_server (ForeignServer *server, UserMapping *user)
 
static void disconnect_pg_server (ConnCacheEntry *entry)
 
static void check_conn_params (const char **keywords, const char **values, UserMapping *user)
 
static void configure_remote_session (PGconn *conn)
 
static void do_sql_command_begin (PGconn *conn, const char *sql)
 
static void do_sql_command_end (PGconn *conn, const char *sql, bool consume_input)
 
static void begin_remote_xact (ConnCacheEntry *entry)
 
static void pgfdw_xact_callback (XactEvent event, void *arg)
 
static void pgfdw_subxact_callback (SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)
 
static void pgfdw_inval_callback (Datum arg, int cacheid, uint32 hashvalue)
 
static void pgfdw_reject_incomplete_xact_state_change (ConnCacheEntry *entry)
 
static void pgfdw_reset_xact_state (ConnCacheEntry *entry, bool toplevel)
 
static bool pgfdw_cancel_query (PGconn *conn)
 
static bool pgfdw_cancel_query_begin (PGconn *conn, TimestampTz endtime)
 
static bool pgfdw_cancel_query_end (PGconn *conn, TimestampTz endtime, TimestampTz retrycanceltime, bool consume_input)
 
static bool pgfdw_exec_cleanup_query (PGconn *conn, const char *query, bool ignore_errors)
 
static bool pgfdw_exec_cleanup_query_begin (PGconn *conn, const char *query)
 
static bool pgfdw_exec_cleanup_query_end (PGconn *conn, const char *query, TimestampTz endtime, bool consume_input, bool ignore_errors)
 
static bool pgfdw_get_cleanup_result (PGconn *conn, TimestampTz endtime, TimestampTz retrycanceltime, PGresult **result, bool *timed_out)
 
static void pgfdw_abort_cleanup (ConnCacheEntry *entry, bool toplevel)
 
static bool pgfdw_abort_cleanup_begin (ConnCacheEntry *entry, bool toplevel, List **pending_entries, List **cancel_requested)
 
static void pgfdw_finish_pre_commit_cleanup (List *pending_entries)
 
static void pgfdw_finish_pre_subcommit_cleanup (List *pending_entries, int curlevel)
 
static void pgfdw_finish_abort_cleanup (List *pending_entries, List *cancel_requested, bool toplevel)
 
static void pgfdw_security_check (const char **keywords, const char **values, UserMapping *user, PGconn *conn)
 
static bool UserMappingPasswordRequired (UserMapping *user)
 
static bool UseScramPassthrough (ForeignServer *server, UserMapping *user)
 
static bool disconnect_cached_connections (Oid serverid)
 
static void postgres_fdw_get_connections_internal (FunctionCallInfo fcinfo, enum pgfdwVersion api_version)
 
static int pgfdw_conn_check (PGconn *conn)
 
static bool pgfdw_conn_checkable (void)
 
static bool pgfdw_has_required_scram_options (const char **keywords, const char **values)
 
PGconnGetConnection (UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
 
void do_sql_command (PGconn *conn, const char *sql)
 
void ReleaseConnection (PGconn *conn)
 
unsigned int GetCursorNumber (PGconn *conn)
 
unsigned int GetPrepStmtNumber (PGconn *conn)
 
PGresultpgfdw_exec_query (PGconn *conn, const char *query, PgFdwConnState *state)
 
PGresultpgfdw_get_result (PGconn *conn)
 
void pgfdw_report_error (int elevel, PGresult *res, PGconn *conn, bool clear, const char *sql)
 
Datum postgres_fdw_get_connections_1_2 (PG_FUNCTION_ARGS)
 
Datum postgres_fdw_get_connections (PG_FUNCTION_ARGS)
 
Datum postgres_fdw_disconnect (PG_FUNCTION_ARGS)
 
Datum postgres_fdw_disconnect_all (PG_FUNCTION_ARGS)
 

Variables

static HTABConnectionHash = NULL
 
static unsigned int cursor_number = 0
 
static unsigned int prep_stmt_number = 0
 
static bool xact_got_connection = false
 
static uint32 pgfdw_we_cleanup_result = 0
 
static uint32 pgfdw_we_connect = 0
 
static uint32 pgfdw_we_get_result = 0
 

Macro Definition Documentation

◆ CONNECTION_CLEANUP_TIMEOUT

#define CONNECTION_CLEANUP_TIMEOUT   30000

Definition at line 97 of file connection.c.

◆ CONSTRUCT_ABORT_COMMAND

#define CONSTRUCT_ABORT_COMMAND (   sql,
  entry,
  toplevel 
)
Value:
do { \
if (toplevel) \
snprintf((sql), sizeof(sql), \
"ABORT TRANSACTION"); \
snprintf((sql), sizeof(sql), \
"ROLLBACK TO SAVEPOINT s%d; RELEASE SAVEPOINT s%d", \
(entry)->xact_depth, (entry)->xact_depth); \
} while(0)
#define snprintf
Definition: port.h:239

Definition at line 107 of file connection.c.

◆ POSTGRES_FDW_GET_CONNECTIONS_COLS

#define POSTGRES_FDW_GET_CONNECTIONS_COLS   6 /* maximum of above */

Definition at line 2136 of file connection.c.

◆ POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_1

#define POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_1   2

Definition at line 2134 of file connection.c.

◆ POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_2

#define POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_2   6

Definition at line 2135 of file connection.c.

◆ RETRY_CANCEL_TIMEOUT

#define RETRY_CANCEL_TIMEOUT   1000

Definition at line 104 of file connection.c.

Typedef Documentation

◆ ConnCacheEntry

◆ ConnCacheKey

typedef Oid ConnCacheKey

Definition at line 52 of file connection.c.

Enumeration Type Documentation

◆ pgfdwVersion

Enumerator
PGFDW_V1_1 
PGFDW_V1_2 

Definition at line 121 of file connection.c.

122{
123 PGFDW_V1_1 = 0,
125};
@ PGFDW_V1_1
Definition: connection.c:123
@ PGFDW_V1_2
Definition: connection.c:124

Function Documentation

◆ begin_remote_xact()

static void begin_remote_xact ( ConnCacheEntry entry)
static

Definition at line 848 of file connection.c.

849{
850 int curlevel = GetCurrentTransactionNestLevel();
851
852 /* Start main transaction if we haven't yet */
853 if (entry->xact_depth <= 0)
854 {
855 const char *sql;
856
857 elog(DEBUG3, "starting remote transaction on connection %p",
858 entry->conn);
859
861 sql = "START TRANSACTION ISOLATION LEVEL SERIALIZABLE";
862 else
863 sql = "START TRANSACTION ISOLATION LEVEL REPEATABLE READ";
864 entry->changing_xact_state = true;
865 do_sql_command(entry->conn, sql);
866 entry->xact_depth = 1;
867 entry->changing_xact_state = false;
868 }
869
870 /*
871 * If we're in a subtransaction, stack up savepoints to match our level.
872 * This ensures we can rollback just the desired effects when a
873 * subtransaction aborts.
874 */
875 while (entry->xact_depth < curlevel)
876 {
877 char sql[64];
878
879 snprintf(sql, sizeof(sql), "SAVEPOINT s%d", entry->xact_depth + 1);
880 entry->changing_xact_state = true;
881 do_sql_command(entry->conn, sql);
882 entry->xact_depth++;
883 entry->changing_xact_state = false;
884 }
885}
void do_sql_command(PGconn *conn, const char *sql)
Definition: connection.c:805
#define DEBUG3
Definition: elog.h:28
#define elog(elevel,...)
Definition: elog.h:225
PGconn * conn
Definition: connection.c:57
bool changing_xact_state
Definition: connection.c:63
int GetCurrentTransactionNestLevel(void)
Definition: xact.c:929
#define IsolationIsSerializable()
Definition: xact.h:52

References ConnCacheEntry::changing_xact_state, ConnCacheEntry::conn, DEBUG3, do_sql_command(), elog, GetCurrentTransactionNestLevel(), IsolationIsSerializable, snprintf, and ConnCacheEntry::xact_depth.

Referenced by GetConnection().

◆ check_conn_params()

static void check_conn_params ( const char **  keywords,
const char **  values,
UserMapping user 
)
static

Definition at line 715 of file connection.c.

716{
717 int i;
718
719 /* no check required if superuser */
720 if (superuser_arg(user->userid))
721 return;
722
723#ifdef ENABLE_GSS
724 /* ok if the user provided their own delegated credentials */
726 return;
727#endif
728
729 /* ok if params contain a non-empty password */
730 for (i = 0; keywords[i] != NULL; i++)
731 {
732 if (strcmp(keywords[i], "password") == 0 && values[i][0] != '\0')
733 return;
734 }
735
736 /* ok if the superuser explicitly said so at user mapping creation time */
738 return;
739
740 /*
741 * Ok if SCRAM pass-through is being used and all required scram options
742 * are set correctly. If pgfdw_has_required_scram_options returns true we
743 * assume that UseScramPassthrough is also true since SCRAM options are
744 * only set when UseScramPassthrough is enabled.
745 */
747 return;
748
750 (errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
751 errmsg("password or GSSAPI delegated credentials required"),
752 errdetail("Non-superusers must delegate GSSAPI credentials, provide a password, or enable SCRAM pass-through in user mapping.")));
753}
bool be_gssapi_get_delegation(Port *port)
static Datum values[MAXATTR]
Definition: bootstrap.c:151
static bool UserMappingPasswordRequired(UserMapping *user)
Definition: connection.c:667
static bool pgfdw_has_required_scram_options(const char **keywords, const char **values)
Definition: connection.c:2521
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
struct Port * MyProcPort
Definition: globals.c:52
int i
Definition: isn.c:77
static const JsonPathKeyword keywords[]
static char * user
Definition: pg_regress.c:119
bool has_scram_keys
Definition: libpq-be.h:188
bool superuser_arg(Oid roleid)
Definition: superuser.c:56

References be_gssapi_get_delegation(), ereport, errcode(), errdetail(), errmsg(), ERROR, Port::has_scram_keys, i, keywords, MyProcPort, pgfdw_has_required_scram_options(), superuser_arg(), user, UserMappingPasswordRequired(), and values.

Referenced by connect_pg_server().

◆ configure_remote_session()

static void configure_remote_session ( PGconn conn)
static

Definition at line 767 of file connection.c.

768{
769 int remoteversion = PQserverVersion(conn);
770
771 /* Force the search path to contain only pg_catalog (see deparse.c) */
772 do_sql_command(conn, "SET search_path = pg_catalog");
773
774 /*
775 * Set remote timezone; this is basically just cosmetic, since all
776 * transmitted and returned timestamptzs should specify a zone explicitly
777 * anyway. However it makes the regression test outputs more predictable.
778 *
779 * We don't risk setting remote zone equal to ours, since the remote
780 * server might use a different timezone database. Instead, use GMT
781 * (quoted, because very old servers are picky about case). That's
782 * guaranteed to work regardless of the remote's timezone database,
783 * because pg_tzset() hard-wires it (at least in PG 9.2 and later).
784 */
785 do_sql_command(conn, "SET timezone = 'GMT'");
786
787 /*
788 * Set values needed to ensure unambiguous data output from remote. (This
789 * logic should match what pg_dump does. See also set_transmission_modes
790 * in postgres_fdw.c.)
791 */
792 do_sql_command(conn, "SET datestyle = ISO");
793 if (remoteversion >= 80400)
794 do_sql_command(conn, "SET intervalstyle = postgres");
795 if (remoteversion >= 90000)
796 do_sql_command(conn, "SET extra_float_digits = 3");
797 else
798 do_sql_command(conn, "SET extra_float_digits = 2");
799}
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7609
PGconn * conn
Definition: streamutil.c:52

References conn, do_sql_command(), and PQserverVersion().

Referenced by connect_pg_server().

◆ connect_pg_server()

static PGconn * connect_pg_server ( ForeignServer server,
UserMapping user 
)
static

Definition at line 479 of file connection.c.

480{
481 PGconn *volatile conn = NULL;
482
483 /*
484 * Use PG_TRY block to ensure closing connection on error.
485 */
486 PG_TRY();
487 {
488 const char **keywords;
489 const char **values;
490 char *appname = NULL;
491 int n;
492
493 /*
494 * Construct connection params from generic options of ForeignServer
495 * and UserMapping. (Some of them might not be libpq options, in
496 * which case we'll just waste a few array slots.) Add 4 extra slots
497 * for application_name, fallback_application_name, client_encoding,
498 * end marker, and 3 extra slots for scram keys and required scram
499 * pass-through options.
500 */
501 n = list_length(server->options) + list_length(user->options) + 4 + 3;
502 keywords = (const char **) palloc(n * sizeof(char *));
503 values = (const char **) palloc(n * sizeof(char *));
504
505 n = 0;
507 keywords + n, values + n);
508 n += ExtractConnectionOptions(user->options,
509 keywords + n, values + n);
510
511 /*
512 * Use pgfdw_application_name as application_name if set.
513 *
514 * PQconnectdbParams() processes the parameter arrays from start to
515 * end. If any key word is repeated, the last value is used. Therefore
516 * note that pgfdw_application_name must be added to the arrays after
517 * options of ForeignServer are, so that it can override
518 * application_name set in ForeignServer.
519 */
521 {
522 keywords[n] = "application_name";
524 n++;
525 }
526
527 /*
528 * Search the parameter arrays to find application_name setting, and
529 * replace escape sequences in it with status information if found.
530 * The arrays are searched backwards because the last value is used if
531 * application_name is repeatedly set.
532 */
533 for (int i = n - 1; i >= 0; i--)
534 {
535 if (strcmp(keywords[i], "application_name") == 0 &&
536 *(values[i]) != '\0')
537 {
538 /*
539 * Use this application_name setting if it's not empty string
540 * even after any escape sequences in it are replaced.
541 */
542 appname = process_pgfdw_appname(values[i]);
543 if (appname[0] != '\0')
544 {
545 values[i] = appname;
546 break;
547 }
548
549 /*
550 * This empty application_name is not used, so we set
551 * values[i] to NULL and keep searching the array to find the
552 * next one.
553 */
554 values[i] = NULL;
555 pfree(appname);
556 appname = NULL;
557 }
558 }
559
560 /* Use "postgres_fdw" as fallback_application_name */
561 keywords[n] = "fallback_application_name";
562 values[n] = "postgres_fdw";
563 n++;
564
565 /* Set client_encoding so that libpq can convert encoding properly. */
566 keywords[n] = "client_encoding";
568 n++;
569
570 /* Add required SCRAM pass-through connection options if it's enabled. */
572 {
573 int len;
574 int encoded_len;
575
576 keywords[n] = "scram_client_key";
578 /* don't forget the zero-terminator */
579 values[n] = palloc0(len + 1);
580 encoded_len = pg_b64_encode((const char *) MyProcPort->scram_ClientKey,
582 (char *) values[n], len);
583 if (encoded_len < 0)
584 elog(ERROR, "could not encode SCRAM client key");
585 n++;
586
587 keywords[n] = "scram_server_key";
589 /* don't forget the zero-terminator */
590 values[n] = palloc0(len + 1);
591 encoded_len = pg_b64_encode((const char *) MyProcPort->scram_ServerKey,
593 (char *) values[n], len);
594 if (encoded_len < 0)
595 elog(ERROR, "could not encode SCRAM server key");
596 n++;
597
598 /*
599 * Require scram-sha-256 to ensure that no other auth method is
600 * used when connecting with foreign server.
601 */
602 keywords[n] = "require_auth";
603 values[n] = "scram-sha-256";
604 n++;
605 }
606
607 keywords[n] = values[n] = NULL;
608
609 /* Verify the set of connection parameters. */
611
612 /* first time, allocate or get the custom wait event */
613 if (pgfdw_we_connect == 0)
614 pgfdw_we_connect = WaitEventExtensionNew("PostgresFdwConnect");
615
616 /* OK to make connection */
618 false, /* expand_dbname */
620
621 if (!conn || PQstatus(conn) != CONNECTION_OK)
623 (errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
624 errmsg("could not connect to server \"%s\"",
625 server->servername),
627
628 /* Perform post-connection security checks. */
630
631 /* Prepare new session for use */
633
634 if (appname != NULL)
635 pfree(appname);
637 pfree(values);
638 }
639 PG_CATCH();
640 {
642 PG_RE_THROW();
643 }
644 PG_END_TRY();
645
646 return conn;
647}
int pg_b64_enc_len(int srclen)
Definition: base64.c:224
int pg_b64_encode(const char *src, int len, char *dst, int dstlen)
Definition: base64.c:49
static bool UseScramPassthrough(ForeignServer *server, UserMapping *user)
Definition: connection.c:683
static void configure_remote_session(PGconn *conn)
Definition: connection.c:767
static void pgfdw_security_check(const char **keywords, const char **values, UserMapping *user, PGconn *conn)
Definition: connection.c:432
static void check_conn_params(const char **keywords, const char **values, UserMapping *user)
Definition: connection.c:715
static uint32 pgfdw_we_connect
Definition: connection.c:89
int ExtractConnectionOptions(List *defelems, const char **keywords, const char **values)
Definition: option.c:416
char * pgfdw_application_name
Definition: option.c:51
char * process_pgfdw_appname(const char *appname)
Definition: option.c:493
int errdetail_internal(const char *fmt,...)
Definition: elog.c:1231
#define PG_RE_THROW()
Definition: elog.h:404
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define PG_CATCH(...)
Definition: elog.h:381
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7556
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7619
static PGconn * libpqsrv_connect_params(const char *const *keywords, const char *const *values, int expand_dbname, uint32 wait_event_info)
static void libpqsrv_disconnect(PGconn *conn)
@ CONNECTION_OK
Definition: libpq-fe.h:84
const char * GetDatabaseEncodingName(void)
Definition: mbutils.c:1267
void pfree(void *pointer)
Definition: mcxt.c:2150
void * palloc0(Size size)
Definition: mcxt.c:1973
char * pchomp(const char *in)
Definition: mcxt.c:2353
void * palloc(Size size)
Definition: mcxt.c:1943
const void size_t len
static int list_length(const List *l)
Definition: pg_list.h:152
List * options
Definition: foreign.h:42
char * servername
Definition: foreign.h:39
uint8 scram_ServerKey[SCRAM_MAX_KEY_LEN]
Definition: libpq-be.h:187
uint8 scram_ClientKey[SCRAM_MAX_KEY_LEN]
Definition: libpq-be.h:186
uint32 WaitEventExtensionNew(const char *wait_event_name)
Definition: wait_event.c:163

References check_conn_params(), configure_remote_session(), conn, CONNECTION_OK, elog, ereport, errcode(), errdetail_internal(), errmsg(), ERROR, ExtractConnectionOptions(), GetDatabaseEncodingName(), Port::has_scram_keys, i, keywords, len, libpqsrv_connect_params(), libpqsrv_disconnect(), list_length(), MyProcPort, ForeignServer::options, palloc(), palloc0(), pchomp(), pfree(), pg_b64_enc_len(), pg_b64_encode(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pgfdw_application_name, pgfdw_security_check(), pgfdw_we_connect, PQerrorMessage(), PQstatus(), process_pgfdw_appname(), Port::scram_ClientKey, Port::scram_ServerKey, ForeignServer::servername, user, UseScramPassthrough(), values, and WaitEventExtensionNew().

Referenced by make_new_connection().

◆ disconnect_cached_connections()

static bool disconnect_cached_connections ( Oid  serverid)
static

Definition at line 2394 of file connection.c.

2395{
2396 HASH_SEQ_STATUS scan;
2397 ConnCacheEntry *entry;
2398 bool all = !OidIsValid(serverid);
2399 bool result = false;
2400
2401 /*
2402 * Connection cache hashtable has not been initialized yet in this
2403 * session, so return false.
2404 */
2405 if (!ConnectionHash)
2406 return false;
2407
2409 while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
2410 {
2411 /* Ignore cache entry if no open connection right now. */
2412 if (!entry->conn)
2413 continue;
2414
2415 if (all || entry->serverid == serverid)
2416 {
2417 /*
2418 * Emit a warning because the connection to close is used in the
2419 * current transaction and cannot be disconnected right now.
2420 */
2421 if (entry->xact_depth > 0)
2422 {
2423 ForeignServer *server;
2424
2425 server = GetForeignServerExtended(entry->serverid,
2427
2428 if (!server)
2429 {
2430 /*
2431 * If the foreign server was dropped while its connection
2432 * was used in the current transaction, the connection
2433 * must have been marked as invalid by
2434 * pgfdw_inval_callback at the end of DROP SERVER command.
2435 */
2436 Assert(entry->invalidated);
2437
2439 (errmsg("cannot close dropped server connection because it is still in use")));
2440 }
2441 else
2443 (errmsg("cannot close connection for server \"%s\" because it is still in use",
2444 server->servername)));
2445 }
2446 else
2447 {
2448 elog(DEBUG3, "discarding connection %p", entry->conn);
2449 disconnect_pg_server(entry);
2450 result = true;
2451 }
2452 }
2453 }
2454
2455 return result;
2456}
#define OidIsValid(objectId)
Definition: c.h:746
static void disconnect_pg_server(ConnCacheEntry *entry)
Definition: connection.c:653
static HTAB * ConnectionHash
Definition: connection.c:78
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1421
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1386
#define WARNING
Definition: elog.h:36
ForeignServer * GetForeignServerExtended(Oid serverid, bits16 flags)
Definition: foreign.c:123
#define FSV_MISSING_OK
Definition: foreign.h:61
Assert(PointerIsAligned(start, uint64))
bool invalidated
Definition: connection.c:66

References Assert(), ConnCacheEntry::conn, ConnectionHash, DEBUG3, disconnect_pg_server(), elog, ereport, errmsg(), FSV_MISSING_OK, GetForeignServerExtended(), hash_seq_init(), hash_seq_search(), ConnCacheEntry::invalidated, OidIsValid, ConnCacheEntry::serverid, ForeignServer::servername, WARNING, and ConnCacheEntry::xact_depth.

Referenced by postgres_fdw_disconnect(), and postgres_fdw_disconnect_all().

◆ disconnect_pg_server()

static void disconnect_pg_server ( ConnCacheEntry entry)
static

Definition at line 653 of file connection.c.

654{
655 if (entry->conn != NULL)
656 {
658 entry->conn = NULL;
659 }
660}

References ConnCacheEntry::conn, and libpqsrv_disconnect().

Referenced by disconnect_cached_connections(), GetConnection(), pgfdw_inval_callback(), pgfdw_reject_incomplete_xact_state_change(), and pgfdw_reset_xact_state().

◆ do_sql_command()

void do_sql_command ( PGconn conn,
const char *  sql 
)

Definition at line 805 of file connection.c.

806{
808 do_sql_command_end(conn, sql, false);
809}
static void do_sql_command_end(PGconn *conn, const char *sql, bool consume_input)
Definition: connection.c:819
static void do_sql_command_begin(PGconn *conn, const char *sql)
Definition: connection.c:812

References conn, do_sql_command_begin(), and do_sql_command_end().

Referenced by begin_remote_xact(), configure_remote_session(), pgfdw_subxact_callback(), pgfdw_xact_callback(), and postgresExecForeignTruncate().

◆ do_sql_command_begin()

static void do_sql_command_begin ( PGconn conn,
const char *  sql 
)
static

Definition at line 812 of file connection.c.

813{
814 if (!PQsendQuery(conn, sql))
815 pgfdw_report_error(ERROR, NULL, conn, false, sql);
816}
void pgfdw_report_error(int elevel, PGresult *res, PGconn *conn, bool clear, const char *sql)
Definition: connection.c:977
int PQsendQuery(PGconn *conn, const char *query)
Definition: fe-exec.c:1416

References conn, ERROR, pgfdw_report_error(), and PQsendQuery().

Referenced by do_sql_command(), pgfdw_subxact_callback(), and pgfdw_xact_callback().

◆ do_sql_command_end()

static void do_sql_command_end ( PGconn conn,
const char *  sql,
bool  consume_input 
)
static

Definition at line 819 of file connection.c.

820{
821 PGresult *res;
822
823 /*
824 * If requested, consume whatever data is available from the socket. (Note
825 * that if all data is available, this allows pgfdw_get_result to call
826 * PQgetResult without forcing the overhead of WaitLatchOrSocket, which
827 * would be large compared to the overhead of PQconsumeInput.)
828 */
829 if (consume_input && !PQconsumeInput(conn))
830 pgfdw_report_error(ERROR, NULL, conn, false, sql);
831 res = pgfdw_get_result(conn);
833 pgfdw_report_error(ERROR, res, conn, true, sql);
834 PQclear(res);
835}
PGresult * pgfdw_get_result(PGconn *conn)
Definition: connection.c:958
ExecStatusType PQresultStatus(const PGresult *res)
Definition: fe-exec.c:3411
void PQclear(PGresult *res)
Definition: fe-exec.c:721
int PQconsumeInput(PGconn *conn)
Definition: fe-exec.c:1984
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:125

References conn, ERROR, pgfdw_get_result(), pgfdw_report_error(), PGRES_COMMAND_OK, PQclear(), PQconsumeInput(), and PQresultStatus().

Referenced by do_sql_command(), pgfdw_finish_pre_commit_cleanup(), and pgfdw_finish_pre_subcommit_cleanup().

◆ GetConnection()

PGconn * GetConnection ( UserMapping user,
bool  will_prep_stmt,
PgFdwConnState **  state 
)

Definition at line 203 of file connection.c.

204{
205 bool found;
206 bool retry = false;
207 ConnCacheEntry *entry;
210
211 /* First time through, initialize connection cache hashtable */
212 if (ConnectionHash == NULL)
213 {
214 HASHCTL ctl;
215
216 if (pgfdw_we_get_result == 0)
218 WaitEventExtensionNew("PostgresFdwGetResult");
219
220 ctl.keysize = sizeof(ConnCacheKey);
221 ctl.entrysize = sizeof(ConnCacheEntry);
222 ConnectionHash = hash_create("postgres_fdw connections", 8,
223 &ctl,
225
226 /*
227 * Register some callback functions that manage connection cleanup.
228 * This should be done just once in each backend.
229 */
232 CacheRegisterSyscacheCallback(FOREIGNSERVEROID,
234 CacheRegisterSyscacheCallback(USERMAPPINGOID,
236 }
237
238 /* Set flag that we did GetConnection during the current transaction */
239 xact_got_connection = true;
240
241 /* Create hash key for the entry. Assume no pad bytes in key struct */
242 key = user->umid;
243
244 /*
245 * Find or create cached entry for requested connection.
246 */
247 entry = hash_search(ConnectionHash, &key, HASH_ENTER, &found);
248 if (!found)
249 {
250 /*
251 * We need only clear "conn" here; remaining fields will be filled
252 * later when "conn" is set.
253 */
254 entry->conn = NULL;
255 }
256
257 /* Reject further use of connections which failed abort cleanup. */
259
260 /*
261 * If the connection needs to be remade due to invalidation, disconnect as
262 * soon as we're out of all transactions.
263 */
264 if (entry->conn != NULL && entry->invalidated && entry->xact_depth == 0)
265 {
266 elog(DEBUG3, "closing connection %p for option changes to take effect",
267 entry->conn);
269 }
270
271 /*
272 * If cache entry doesn't have a connection, we have to establish a new
273 * connection. (If connect_pg_server throws an error, the cache entry
274 * will remain in a valid empty state, ie conn == NULL.)
275 */
276 if (entry->conn == NULL)
278
279 /*
280 * We check the health of the cached connection here when using it. In
281 * cases where we're out of all transactions, if a broken connection is
282 * detected, we try to reestablish a new connection later.
283 */
284 PG_TRY();
285 {
286 /* Process a pending asynchronous request if any. */
287 if (entry->state.pendingAreq)
289 /* Start a new transaction or subtransaction if needed. */
290 begin_remote_xact(entry);
291 }
292 PG_CATCH();
293 {
295 ErrorData *errdata = CopyErrorData();
296
297 /*
298 * Determine whether to try to reestablish the connection.
299 *
300 * After a broken connection is detected in libpq, any error other
301 * than connection failure (e.g., out-of-memory) can be thrown
302 * somewhere between return from libpq and the expected ereport() call
303 * in pgfdw_report_error(). In this case, since PQstatus() indicates
304 * CONNECTION_BAD, checking only PQstatus() causes the false detection
305 * of connection failure. To avoid this, we also verify that the
306 * error's sqlstate is ERRCODE_CONNECTION_FAILURE. Note that also
307 * checking only the sqlstate can cause another false detection
308 * because pgfdw_report_error() may report ERRCODE_CONNECTION_FAILURE
309 * for any libpq-originated error condition.
310 */
311 if (errdata->sqlerrcode != ERRCODE_CONNECTION_FAILURE ||
312 PQstatus(entry->conn) != CONNECTION_BAD ||
313 entry->xact_depth > 0)
314 {
316 PG_RE_THROW();
317 }
318
319 /* Clean up the error state */
321 FreeErrorData(errdata);
322 errdata = NULL;
323
324 retry = true;
325 }
326 PG_END_TRY();
327
328 /*
329 * If a broken connection is detected, disconnect it, reestablish a new
330 * connection and retry a new remote transaction. If connection failure is
331 * reported again, we give up getting a connection.
332 */
333 if (retry)
334 {
335 Assert(entry->xact_depth == 0);
336
338 (errmsg_internal("could not start remote transaction on connection %p",
339 entry->conn)),
341
342 elog(DEBUG3, "closing connection %p to reestablish a new one",
343 entry->conn);
345
347
348 begin_remote_xact(entry);
349 }
350
351 /* Remember if caller will prepare statements */
352 entry->have_prep_stmt |= will_prep_stmt;
353
354 /* If caller needs access to the per-connection state, return it. */
355 if (state)
356 *state = &entry->state;
357
358 return entry->conn;
359}
Oid ConnCacheKey
Definition: connection.c:52
static uint32 pgfdw_we_get_result
Definition: connection.c:90
static bool xact_got_connection
Definition: connection.c:85
struct ConnCacheEntry ConnCacheEntry
static void make_new_connection(ConnCacheEntry *entry, UserMapping *user)
Definition: connection.c:366
static void pgfdw_subxact_callback(SubXactEvent event, SubTransactionId mySubid, SubTransactionId parentSubid, void *arg)
Definition: connection.c:1183
static void pgfdw_reject_incomplete_xact_state_change(ConnCacheEntry *entry)
Definition: connection.c:1346
static void pgfdw_inval_callback(Datum arg, int cacheid, uint32 hashvalue)
Definition: connection.c:1296
static void pgfdw_xact_callback(XactEvent event, void *arg)
Definition: connection.c:1033
static void begin_remote_xact(ConnCacheEntry *entry)
Definition: connection.c:848
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:956
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1158
void FreeErrorData(ErrorData *edata)
Definition: elog.c:1823
ErrorData * CopyErrorData(void)
Definition: elog.c:1751
void FlushErrorState(void)
Definition: elog.c:1872
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
void CacheRegisterSyscacheCallback(int cacheid, SyscacheCallbackFunction func, Datum arg)
Definition: inval.c:1812
@ CONNECTION_BAD
Definition: libpq-fe.h:85
MemoryContext CurrentMemoryContext
Definition: mcxt.c:159
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
uintptr_t Datum
Definition: postgres.h:69
void process_pending_request(AsyncRequest *areq)
tree ctl
Definition: radixtree.h:1838
bool have_prep_stmt
Definition: connection.c:61
PgFdwConnState state
Definition: connection.c:72
int sqlerrcode
Definition: elog.h:430
AsyncRequest * pendingAreq
Definition: postgres_fdw.h:139
Definition: regguts.h:323
void RegisterXactCallback(XactCallback callback, void *arg)
Definition: xact.c:3804
void RegisterSubXactCallback(SubXactCallback callback, void *arg)
Definition: xact.c:3864

References Assert(), begin_remote_xact(), CacheRegisterSyscacheCallback(), ConnCacheEntry::conn, CONNECTION_BAD, ConnectionHash, CopyErrorData(), ctl, CurrentMemoryContext, DEBUG3, disconnect_pg_server(), elog, ereport, errdetail_internal(), errmsg_internal(), FlushErrorState(), FreeErrorData(), HASH_BLOBS, hash_create(), HASH_ELEM, HASH_ENTER, hash_search(), ConnCacheEntry::have_prep_stmt, ConnCacheEntry::invalidated, sort-test::key, make_new_connection(), MemoryContextSwitchTo(), pchomp(), PgFdwConnState::pendingAreq, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pgfdw_inval_callback(), pgfdw_reject_incomplete_xact_state_change(), pgfdw_subxact_callback(), pgfdw_we_get_result, pgfdw_xact_callback(), PQerrorMessage(), PQstatus(), process_pending_request(), RegisterSubXactCallback(), RegisterXactCallback(), ErrorData::sqlerrcode, ConnCacheEntry::state, user, WaitEventExtensionNew(), ConnCacheEntry::xact_depth, and xact_got_connection.

Referenced by create_foreign_modify(), dumpDatabase(), dumpDatabaseConfig(), dumpLOs(), dumpTableData_copy(), estimate_path_cost_size(), expand_extension_name_patterns(), expand_foreign_server_name_patterns(), expand_schema_name_patterns(), expand_table_name_patterns(), getTables(), main(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresBeginDirectModify(), postgresBeginForeignScan(), postgresExecForeignTruncate(), postgresGetAnalyzeInfoForForeignTable(), postgresImportForeignSchema(), setup_connection(), StartLogStreamer(), StreamLog(), and StreamLogicalLog().

◆ GetCursorNumber()

unsigned int GetCursorNumber ( PGconn conn)

Definition at line 912 of file connection.c.

913{
914 return ++cursor_number;
915}
static unsigned int cursor_number
Definition: connection.c:81

References cursor_number.

Referenced by postgresAcquireSampleRowsFunc(), and postgresBeginForeignScan().

◆ GetPrepStmtNumber()

unsigned int GetPrepStmtNumber ( PGconn conn)

Definition at line 926 of file connection.c.

927{
928 return ++prep_stmt_number;
929}
static unsigned int prep_stmt_number
Definition: connection.c:82

References prep_stmt_number.

Referenced by prepare_foreign_modify().

◆ make_new_connection()

static void make_new_connection ( ConnCacheEntry entry,
UserMapping user 
)
static

Definition at line 366 of file connection.c.

367{
368 ForeignServer *server = GetForeignServer(user->serverid);
369 ListCell *lc;
370
371 Assert(entry->conn == NULL);
372
373 /* Reset all transient state fields, to be sure all are clean */
374 entry->xact_depth = 0;
375 entry->have_prep_stmt = false;
376 entry->have_error = false;
377 entry->changing_xact_state = false;
378 entry->invalidated = false;
379 entry->serverid = server->serverid;
380 entry->server_hashvalue =
381 GetSysCacheHashValue1(FOREIGNSERVEROID,
382 ObjectIdGetDatum(server->serverid));
383 entry->mapping_hashvalue =
384 GetSysCacheHashValue1(USERMAPPINGOID,
385 ObjectIdGetDatum(user->umid));
386 memset(&entry->state, 0, sizeof(entry->state));
387
388 /*
389 * Determine whether to keep the connection that we're about to make here
390 * open even after the transaction using it ends, so that the subsequent
391 * transactions can re-use it.
392 *
393 * By default, all the connections to any foreign servers are kept open.
394 *
395 * Also determine whether to commit/abort (sub)transactions opened on the
396 * remote server in parallel at (sub)transaction end, which is disabled by
397 * default.
398 *
399 * Note: it's enough to determine these only when making a new connection
400 * because if these settings for it are changed, it will be closed and
401 * re-made later.
402 */
403 entry->keep_connections = true;
404 entry->parallel_commit = false;
405 entry->parallel_abort = false;
406 foreach(lc, server->options)
407 {
408 DefElem *def = (DefElem *) lfirst(lc);
409
410 if (strcmp(def->defname, "keep_connections") == 0)
411 entry->keep_connections = defGetBoolean(def);
412 else if (strcmp(def->defname, "parallel_commit") == 0)
413 entry->parallel_commit = defGetBoolean(def);
414 else if (strcmp(def->defname, "parallel_abort") == 0)
415 entry->parallel_abort = defGetBoolean(def);
416 }
417
418 /* Now try to make the connection */
419 entry->conn = connect_pg_server(server, user);
420
421 elog(DEBUG3, "new postgres_fdw connection %p for server \"%s\" (user mapping oid %u, userid %u)",
422 entry->conn, server->servername, user->umid, user->userid);
423}
static PGconn * connect_pg_server(ForeignServer *server, UserMapping *user)
Definition: connection.c:479
bool defGetBoolean(DefElem *def)
Definition: define.c:94
ForeignServer * GetForeignServer(Oid serverid)
Definition: foreign.c:111
#define lfirst(lc)
Definition: pg_list.h:172
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:257
bool parallel_commit
Definition: connection.c:64
uint32 server_hashvalue
Definition: connection.c:70
uint32 mapping_hashvalue
Definition: connection.c:71
bool keep_connections
Definition: connection.c:67
bool parallel_abort
Definition: connection.c:65
char * defname
Definition: parsenodes.h:826
Oid serverid
Definition: foreign.h:36
#define GetSysCacheHashValue1(cacheId, key1)
Definition: syscache.h:118

References Assert(), ConnCacheEntry::changing_xact_state, ConnCacheEntry::conn, connect_pg_server(), DEBUG3, defGetBoolean(), DefElem::defname, elog, GetForeignServer(), GetSysCacheHashValue1, ConnCacheEntry::have_error, ConnCacheEntry::have_prep_stmt, ConnCacheEntry::invalidated, ConnCacheEntry::keep_connections, lfirst, ConnCacheEntry::mapping_hashvalue, ObjectIdGetDatum(), ForeignServer::options, ConnCacheEntry::parallel_abort, ConnCacheEntry::parallel_commit, ConnCacheEntry::server_hashvalue, ConnCacheEntry::serverid, ForeignServer::serverid, ForeignServer::servername, ConnCacheEntry::state, user, and ConnCacheEntry::xact_depth.

Referenced by GetConnection().

◆ PG_FUNCTION_INFO_V1() [1/4]

PG_FUNCTION_INFO_V1 ( postgres_fdw_disconnect  )

◆ PG_FUNCTION_INFO_V1() [2/4]

PG_FUNCTION_INFO_V1 ( postgres_fdw_disconnect_all  )

◆ PG_FUNCTION_INFO_V1() [3/4]

PG_FUNCTION_INFO_V1 ( postgres_fdw_get_connections  )

◆ PG_FUNCTION_INFO_V1() [4/4]

PG_FUNCTION_INFO_V1 ( postgres_fdw_get_connections_1_2  )

◆ pgfdw_abort_cleanup()

static void pgfdw_abort_cleanup ( ConnCacheEntry entry,
bool  toplevel 
)
static

Definition at line 1731 of file connection.c.

1732{
1733 char sql[100];
1734
1735 /*
1736 * Don't try to clean up the connection if we're already in error
1737 * recursion trouble.
1738 */
1740 entry->changing_xact_state = true;
1741
1742 /*
1743 * If connection is already unsalvageable, don't touch it further.
1744 */
1745 if (entry->changing_xact_state)
1746 return;
1747
1748 /*
1749 * Mark this connection as in the process of changing transaction state.
1750 */
1751 entry->changing_xact_state = true;
1752
1753 /* Assume we might have lost track of prepared statements */
1754 entry->have_error = true;
1755
1756 /*
1757 * If a command has been submitted to the remote server by using an
1758 * asynchronous execution function, the command might not have yet
1759 * completed. Check to see if a command is still being processed by the
1760 * remote server, and if so, request cancellation of the command.
1761 */
1762 if (PQtransactionStatus(entry->conn) == PQTRANS_ACTIVE &&
1763 !pgfdw_cancel_query(entry->conn))
1764 return; /* Unable to cancel running query */
1765
1766 CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel);
1767 if (!pgfdw_exec_cleanup_query(entry->conn, sql, false))
1768 return; /* Unable to abort remote (sub)transaction */
1769
1770 if (toplevel)
1771 {
1772 if (entry->have_prep_stmt && entry->have_error &&
1774 "DEALLOCATE ALL",
1775 true))
1776 return; /* Trouble clearing prepared statements */
1777
1778 entry->have_prep_stmt = false;
1779 entry->have_error = false;
1780 }
1781
1782 /*
1783 * If pendingAreq of the per-connection state is not NULL, it means that
1784 * an asynchronous fetch begun by fetch_more_data_begin() was not done
1785 * successfully and thus the per-connection state was not reset in
1786 * fetch_more_data(); in that case reset the per-connection state here.
1787 */
1788 if (entry->state.pendingAreq)
1789 memset(&entry->state, 0, sizeof(entry->state));
1790
1791 /* Disarm changing_xact_state if it all worked */
1792 entry->changing_xact_state = false;
1793}
#define CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel)
Definition: connection.c:107
static bool pgfdw_exec_cleanup_query(PGconn *conn, const char *query, bool ignore_errors)
Definition: connection.c:1515
static bool pgfdw_cancel_query(PGconn *conn)
Definition: connection.c:1415
bool in_error_recursion_trouble(void)
Definition: elog.c:294
PGTransactionStatusType PQtransactionStatus(const PGconn *conn)
Definition: fe-connect.c:7564
@ PQTRANS_ACTIVE
Definition: libpq-fe.h:148

References ConnCacheEntry::changing_xact_state, ConnCacheEntry::conn, CONSTRUCT_ABORT_COMMAND, ConnCacheEntry::have_error, ConnCacheEntry::have_prep_stmt, in_error_recursion_trouble(), PgFdwConnState::pendingAreq, pgfdw_cancel_query(), pgfdw_exec_cleanup_query(), PQTRANS_ACTIVE, PQtransactionStatus(), and ConnCacheEntry::state.

Referenced by pgfdw_subxact_callback(), and pgfdw_xact_callback().

◆ pgfdw_abort_cleanup_begin()

static bool pgfdw_abort_cleanup_begin ( ConnCacheEntry entry,
bool  toplevel,
List **  pending_entries,
List **  cancel_requested 
)
static

Definition at line 1805 of file connection.c.

1807{
1808 /*
1809 * Don't try to clean up the connection if we're already in error
1810 * recursion trouble.
1811 */
1813 entry->changing_xact_state = true;
1814
1815 /*
1816 * If connection is already unsalvageable, don't touch it further.
1817 */
1818 if (entry->changing_xact_state)
1819 return false;
1820
1821 /*
1822 * Mark this connection as in the process of changing transaction state.
1823 */
1824 entry->changing_xact_state = true;
1825
1826 /* Assume we might have lost track of prepared statements */
1827 entry->have_error = true;
1828
1829 /*
1830 * If a command has been submitted to the remote server by using an
1831 * asynchronous execution function, the command might not have yet
1832 * completed. Check to see if a command is still being processed by the
1833 * remote server, and if so, request cancellation of the command.
1834 */
1836 {
1837 TimestampTz endtime;
1838
1841 if (!pgfdw_cancel_query_begin(entry->conn, endtime))
1842 return false; /* Unable to cancel running query */
1843 *cancel_requested = lappend(*cancel_requested, entry);
1844 }
1845 else
1846 {
1847 char sql[100];
1848
1849 CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel);
1850 if (!pgfdw_exec_cleanup_query_begin(entry->conn, sql))
1851 return false; /* Unable to abort remote transaction */
1852 *pending_entries = lappend(*pending_entries, entry);
1853 }
1854
1855 return true;
1856}
TimestampTz GetCurrentTimestamp(void)
Definition: timestamp.c:1645
static bool pgfdw_cancel_query_begin(PGconn *conn, TimestampTz endtime)
Definition: connection.c:1448
static bool pgfdw_exec_cleanup_query_begin(PGconn *conn, const char *query)
Definition: connection.c:1535
#define CONNECTION_CLEANUP_TIMEOUT
Definition: connection.c:97
int64 TimestampTz
Definition: timestamp.h:39
List * lappend(List *list, void *datum)
Definition: list.c:339
#define TimestampTzPlusMilliseconds(tz, ms)
Definition: timestamp.h:85

References ConnCacheEntry::changing_xact_state, ConnCacheEntry::conn, CONNECTION_CLEANUP_TIMEOUT, CONSTRUCT_ABORT_COMMAND, GetCurrentTimestamp(), ConnCacheEntry::have_error, in_error_recursion_trouble(), lappend(), pgfdw_cancel_query_begin(), pgfdw_exec_cleanup_query_begin(), PQTRANS_ACTIVE, PQtransactionStatus(), and TimestampTzPlusMilliseconds.

Referenced by pgfdw_subxact_callback(), and pgfdw_xact_callback().

◆ pgfdw_cancel_query()

static bool pgfdw_cancel_query ( PGconn conn)
static

Definition at line 1415 of file connection.c.

1416{
1418 TimestampTz endtime;
1419 TimestampTz retrycanceltime;
1420
1421 /*
1422 * If it takes too long to cancel the query and discard the result, assume
1423 * the connection is dead.
1424 */
1426
1427 /*
1428 * Also, lose patience and re-issue the cancel request after a little bit.
1429 * (This serves to close some race conditions.)
1430 */
1432
1433 if (!pgfdw_cancel_query_begin(conn, endtime))
1434 return false;
1435 return pgfdw_cancel_query_end(conn, endtime, retrycanceltime, false);
1436}
Datum now(PG_FUNCTION_ARGS)
Definition: timestamp.c:1609
#define RETRY_CANCEL_TIMEOUT
Definition: connection.c:104
static bool pgfdw_cancel_query_end(PGconn *conn, TimestampTz endtime, TimestampTz retrycanceltime, bool consume_input)
Definition: connection.c:1461

References conn, CONNECTION_CLEANUP_TIMEOUT, GetCurrentTimestamp(), now(), pgfdw_cancel_query_begin(), pgfdw_cancel_query_end(), RETRY_CANCEL_TIMEOUT, and TimestampTzPlusMilliseconds.

Referenced by pgfdw_abort_cleanup().

◆ pgfdw_cancel_query_begin()

static bool pgfdw_cancel_query_begin ( PGconn conn,
TimestampTz  endtime 
)
static

Definition at line 1448 of file connection.c.

1449{
1450 const char *errormsg = libpqsrv_cancel(conn, endtime);
1451
1452 if (errormsg != NULL)
1454 errcode(ERRCODE_CONNECTION_FAILURE),
1455 errmsg("could not send cancel request: %s", errormsg));
1456
1457 return errormsg == NULL;
1458}
static const char * libpqsrv_cancel(PGconn *conn, TimestampTz endtime)

References conn, ereport, errcode(), errmsg(), libpqsrv_cancel(), and WARNING.

Referenced by pgfdw_abort_cleanup_begin(), and pgfdw_cancel_query().

◆ pgfdw_cancel_query_end()

static bool pgfdw_cancel_query_end ( PGconn conn,
TimestampTz  endtime,
TimestampTz  retrycanceltime,
bool  consume_input 
)
static

Definition at line 1461 of file connection.c.

1463{
1464 PGresult *result;
1465 bool timed_out;
1466
1467 /*
1468 * If requested, consume whatever data is available from the socket. (Note
1469 * that if all data is available, this allows pgfdw_get_cleanup_result to
1470 * call PQgetResult without forcing the overhead of WaitLatchOrSocket,
1471 * which would be large compared to the overhead of PQconsumeInput.)
1472 */
1473 if (consume_input && !PQconsumeInput(conn))
1474 {
1476 (errcode(ERRCODE_CONNECTION_FAILURE),
1477 errmsg("could not get result of cancel request: %s",
1479 return false;
1480 }
1481
1482 /* Get and discard the result of the query. */
1483 if (pgfdw_get_cleanup_result(conn, endtime, retrycanceltime,
1484 &result, &timed_out))
1485 {
1486 if (timed_out)
1488 (errmsg("could not get result of cancel request due to timeout")));
1489 else
1491 (errcode(ERRCODE_CONNECTION_FAILURE),
1492 errmsg("could not get result of cancel request: %s",
1494
1495 return false;
1496 }
1497 PQclear(result);
1498
1499 return true;
1500}
static bool pgfdw_get_cleanup_result(PGconn *conn, TimestampTz endtime, TimestampTz retrycanceltime, PGresult **result, bool *timed_out)
Definition: connection.c:1613

References conn, ereport, errcode(), errmsg(), pchomp(), pgfdw_get_cleanup_result(), PQclear(), PQconsumeInput(), PQerrorMessage(), and WARNING.

Referenced by pgfdw_cancel_query(), and pgfdw_finish_abort_cleanup().

◆ pgfdw_conn_check()

static int pgfdw_conn_check ( PGconn conn)
static

Definition at line 2466 of file connection.c.

2467{
2468 int sock = PQsocket(conn);
2469
2470 if (PQstatus(conn) != CONNECTION_OK || sock == -1)
2471 return -1;
2472
2473#if (defined(HAVE_POLL) && defined(POLLRDHUP))
2474 {
2475 struct pollfd input_fd;
2476 int result;
2477
2478 input_fd.fd = sock;
2479 input_fd.events = POLLRDHUP;
2480 input_fd.revents = 0;
2481
2482 do
2483 result = poll(&input_fd, 1, 0);
2484 while (result < 0 && errno == EINTR);
2485
2486 if (result < 0)
2487 return -1;
2488
2489 return (input_fd.revents &
2490 (POLLRDHUP | POLLHUP | POLLERR | POLLNVAL)) ? 1 : 0;
2491 }
2492#else
2493 return 0;
2494#endif
2495}
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:7645
#define EINTR
Definition: win32_port.h:364

References conn, CONNECTION_OK, EINTR, PQsocket(), and PQstatus().

Referenced by postgres_fdw_get_connections_internal().

◆ pgfdw_conn_checkable()

static bool pgfdw_conn_checkable ( void  )
static

Definition at line 2503 of file connection.c.

2504{
2505#if (defined(HAVE_POLL) && defined(POLLRDHUP))
2506 return true;
2507#else
2508 return false;
2509#endif
2510}

Referenced by postgres_fdw_get_connections_internal().

◆ pgfdw_exec_cleanup_query()

static bool pgfdw_exec_cleanup_query ( PGconn conn,
const char *  query,
bool  ignore_errors 
)
static

Definition at line 1515 of file connection.c.

1516{
1517 TimestampTz endtime;
1518
1519 /*
1520 * If it takes too long to execute a cleanup query, assume the connection
1521 * is dead. It's fairly likely that this is why we aborted in the first
1522 * place (e.g. statement timeout, user cancel), so the timeout shouldn't
1523 * be too long.
1524 */
1527
1529 return false;
1530 return pgfdw_exec_cleanup_query_end(conn, query, endtime,
1531 false, ignore_errors);
1532}
static bool pgfdw_exec_cleanup_query_end(PGconn *conn, const char *query, TimestampTz endtime, bool consume_input, bool ignore_errors)
Definition: connection.c:1553

References conn, CONNECTION_CLEANUP_TIMEOUT, GetCurrentTimestamp(), pgfdw_exec_cleanup_query_begin(), pgfdw_exec_cleanup_query_end(), and TimestampTzPlusMilliseconds.

Referenced by pgfdw_abort_cleanup().

◆ pgfdw_exec_cleanup_query_begin()

static bool pgfdw_exec_cleanup_query_begin ( PGconn conn,
const char *  query 
)
static

Definition at line 1535 of file connection.c.

1536{
1537 Assert(query != NULL);
1538
1539 /*
1540 * Submit a query. Since we don't use non-blocking mode, this also can
1541 * block. But its risk is relatively small, so we ignore that for now.
1542 */
1543 if (!PQsendQuery(conn, query))
1544 {
1545 pgfdw_report_error(WARNING, NULL, conn, false, query);
1546 return false;
1547 }
1548
1549 return true;
1550}

References Assert(), conn, pgfdw_report_error(), PQsendQuery(), and WARNING.

Referenced by pgfdw_abort_cleanup_begin(), pgfdw_exec_cleanup_query(), and pgfdw_finish_abort_cleanup().

◆ pgfdw_exec_cleanup_query_end()

static bool pgfdw_exec_cleanup_query_end ( PGconn conn,
const char *  query,
TimestampTz  endtime,
bool  consume_input,
bool  ignore_errors 
)
static

Definition at line 1553 of file connection.c.

1556{
1557 PGresult *result;
1558 bool timed_out;
1559
1560 Assert(query != NULL);
1561
1562 /*
1563 * If requested, consume whatever data is available from the socket. (Note
1564 * that if all data is available, this allows pgfdw_get_cleanup_result to
1565 * call PQgetResult without forcing the overhead of WaitLatchOrSocket,
1566 * which would be large compared to the overhead of PQconsumeInput.)
1567 */
1568 if (consume_input && !PQconsumeInput(conn))
1569 {
1570 pgfdw_report_error(WARNING, NULL, conn, false, query);
1571 return false;
1572 }
1573
1574 /* Get the result of the query. */
1575 if (pgfdw_get_cleanup_result(conn, endtime, endtime, &result, &timed_out))
1576 {
1577 if (timed_out)
1579 (errmsg("could not get query result due to timeout"),
1580 errcontext("remote SQL command: %s", query)));
1581 else
1582 pgfdw_report_error(WARNING, NULL, conn, false, query);
1583
1584 return false;
1585 }
1586
1587 /* Issue a warning if not successful. */
1588 if (PQresultStatus(result) != PGRES_COMMAND_OK)
1589 {
1590 pgfdw_report_error(WARNING, result, conn, true, query);
1591 return ignore_errors;
1592 }
1593 PQclear(result);
1594
1595 return true;
1596}
#define errcontext
Definition: elog.h:197

References Assert(), conn, ereport, errcontext, errmsg(), pgfdw_get_cleanup_result(), pgfdw_report_error(), PGRES_COMMAND_OK, PQclear(), PQconsumeInput(), PQresultStatus(), and WARNING.

Referenced by pgfdw_exec_cleanup_query(), and pgfdw_finish_abort_cleanup().

◆ pgfdw_exec_query()

PGresult * pgfdw_exec_query ( PGconn conn,
const char *  query,
PgFdwConnState state 
)

Definition at line 941 of file connection.c.

942{
943 /* First, process a pending asynchronous request, if any. */
944 if (state && state->pendingAreq)
945 process_pending_request(state->pendingAreq);
946
947 if (!PQsendQuery(conn, query))
948 return NULL;
949 return pgfdw_get_result(conn);
950}

References conn, pgfdw_get_result(), PQsendQuery(), and process_pending_request().

Referenced by close_cursor(), deallocate_query(), fetch_more_data(), get_remote_estimate(), pgfdw_xact_callback(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresGetAnalyzeInfoForForeignTable(), postgresImportForeignSchema(), and postgresReScanForeignScan().

◆ pgfdw_finish_abort_cleanup()

static void pgfdw_finish_abort_cleanup ( List pending_entries,
List cancel_requested,
bool  toplevel 
)
static

Definition at line 1971 of file connection.c.

1973{
1974 List *pending_deallocs = NIL;
1975 ListCell *lc;
1976
1977 /*
1978 * For each of the pending cancel requests (if any), get and discard the
1979 * result of the query, and submit an abort command to the remote server.
1980 */
1981 if (cancel_requested)
1982 {
1983 foreach(lc, cancel_requested)
1984 {
1985 ConnCacheEntry *entry = (ConnCacheEntry *) lfirst(lc);
1987 TimestampTz endtime;
1988 TimestampTz retrycanceltime;
1989 char sql[100];
1990
1992
1993 /*
1994 * Set end time. You might think we should do this before issuing
1995 * cancel request like in normal mode, but that is problematic,
1996 * because if, for example, it took longer than 30 seconds to
1997 * process the first few entries in the cancel_requested list, it
1998 * would cause a timeout error when processing each of the
1999 * remaining entries in the list, leading to slamming that entry's
2000 * connection shut.
2001 */
2004 retrycanceltime = TimestampTzPlusMilliseconds(now,
2006
2007 if (!pgfdw_cancel_query_end(entry->conn, endtime,
2008 retrycanceltime, true))
2009 {
2010 /* Unable to cancel running query */
2011 pgfdw_reset_xact_state(entry, toplevel);
2012 continue;
2013 }
2014
2015 /* Send an abort command in parallel if needed */
2016 CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel);
2017 if (!pgfdw_exec_cleanup_query_begin(entry->conn, sql))
2018 {
2019 /* Unable to abort remote (sub)transaction */
2020 pgfdw_reset_xact_state(entry, toplevel);
2021 }
2022 else
2023 pending_entries = lappend(pending_entries, entry);
2024 }
2025 }
2026
2027 /* No further work if no pending entries */
2028 if (!pending_entries)
2029 return;
2030
2031 /*
2032 * Get the result of the abort command for each of the pending entries
2033 */
2034 foreach(lc, pending_entries)
2035 {
2036 ConnCacheEntry *entry = (ConnCacheEntry *) lfirst(lc);
2037 TimestampTz endtime;
2038 char sql[100];
2039
2041
2042 /*
2043 * Set end time. We do this now, not before issuing the command like
2044 * in normal mode, for the same reason as for the cancel_requested
2045 * entries.
2046 */
2049
2050 CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel);
2051 if (!pgfdw_exec_cleanup_query_end(entry->conn, sql, endtime,
2052 true, false))
2053 {
2054 /* Unable to abort remote (sub)transaction */
2055 pgfdw_reset_xact_state(entry, toplevel);
2056 continue;
2057 }
2058
2059 if (toplevel)
2060 {
2061 /* Do a DEALLOCATE ALL in parallel if needed */
2062 if (entry->have_prep_stmt && entry->have_error)
2063 {
2065 "DEALLOCATE ALL"))
2066 {
2067 /* Trouble clearing prepared statements */
2068 pgfdw_reset_xact_state(entry, toplevel);
2069 }
2070 else
2071 pending_deallocs = lappend(pending_deallocs, entry);
2072 continue;
2073 }
2074 entry->have_prep_stmt = false;
2075 entry->have_error = false;
2076 }
2077
2078 /* Reset the per-connection state if needed */
2079 if (entry->state.pendingAreq)
2080 memset(&entry->state, 0, sizeof(entry->state));
2081
2082 /* We're done with this entry; unset the changing_xact_state flag */
2083 entry->changing_xact_state = false;
2084 pgfdw_reset_xact_state(entry, toplevel);
2085 }
2086
2087 /* No further work if no pending entries */
2088 if (!pending_deallocs)
2089 return;
2090 Assert(toplevel);
2091
2092 /*
2093 * Get the result of the DEALLOCATE command for each of the pending
2094 * entries
2095 */
2096 foreach(lc, pending_deallocs)
2097 {
2098 ConnCacheEntry *entry = (ConnCacheEntry *) lfirst(lc);
2099 TimestampTz endtime;
2100
2102 Assert(entry->have_prep_stmt);
2103 Assert(entry->have_error);
2104
2105 /*
2106 * Set end time. We do this now, not before issuing the command like
2107 * in normal mode, for the same reason as for the cancel_requested
2108 * entries.
2109 */
2112
2113 if (!pgfdw_exec_cleanup_query_end(entry->conn, "DEALLOCATE ALL",
2114 endtime, true, true))
2115 {
2116 /* Trouble clearing prepared statements */
2117 pgfdw_reset_xact_state(entry, toplevel);
2118 continue;
2119 }
2120 entry->have_prep_stmt = false;
2121 entry->have_error = false;
2122
2123 /* Reset the per-connection state if needed */
2124 if (entry->state.pendingAreq)
2125 memset(&entry->state, 0, sizeof(entry->state));
2126
2127 /* We're done with this entry; unset the changing_xact_state flag */
2128 entry->changing_xact_state = false;
2129 pgfdw_reset_xact_state(entry, toplevel);
2130 }
2131}
static void pgfdw_reset_xact_state(ConnCacheEntry *entry, bool toplevel)
Definition: connection.c:1370
#define NIL
Definition: pg_list.h:68
Definition: pg_list.h:54

References Assert(), ConnCacheEntry::changing_xact_state, ConnCacheEntry::conn, CONNECTION_CLEANUP_TIMEOUT, CONSTRUCT_ABORT_COMMAND, GetCurrentTimestamp(), ConnCacheEntry::have_error, ConnCacheEntry::have_prep_stmt, lappend(), lfirst, NIL, now(), PgFdwConnState::pendingAreq, pgfdw_cancel_query_end(), pgfdw_exec_cleanup_query_begin(), pgfdw_exec_cleanup_query_end(), pgfdw_reset_xact_state(), RETRY_CANCEL_TIMEOUT, ConnCacheEntry::state, and TimestampTzPlusMilliseconds.

Referenced by pgfdw_subxact_callback(), and pgfdw_xact_callback().

◆ pgfdw_finish_pre_commit_cleanup()

static void pgfdw_finish_pre_commit_cleanup ( List pending_entries)
static

Definition at line 1863 of file connection.c.

1864{
1865 ConnCacheEntry *entry;
1866 List *pending_deallocs = NIL;
1867 ListCell *lc;
1868
1869 Assert(pending_entries);
1870
1871 /*
1872 * Get the result of the COMMIT command for each of the pending entries
1873 */
1874 foreach(lc, pending_entries)
1875 {
1876 entry = (ConnCacheEntry *) lfirst(lc);
1877
1879
1880 /*
1881 * We might already have received the result on the socket, so pass
1882 * consume_input=true to try to consume it first
1883 */
1884 do_sql_command_end(entry->conn, "COMMIT TRANSACTION", true);
1885 entry->changing_xact_state = false;
1886
1887 /* Do a DEALLOCATE ALL in parallel if needed */
1888 if (entry->have_prep_stmt && entry->have_error)
1889 {
1890 /* Ignore errors (see notes in pgfdw_xact_callback) */
1891 if (PQsendQuery(entry->conn, "DEALLOCATE ALL"))
1892 {
1893 pending_deallocs = lappend(pending_deallocs, entry);
1894 continue;
1895 }
1896 }
1897 entry->have_prep_stmt = false;
1898 entry->have_error = false;
1899
1900 pgfdw_reset_xact_state(entry, true);
1901 }
1902
1903 /* No further work if no pending entries */
1904 if (!pending_deallocs)
1905 return;
1906
1907 /*
1908 * Get the result of the DEALLOCATE command for each of the pending
1909 * entries
1910 */
1911 foreach(lc, pending_deallocs)
1912 {
1913 PGresult *res;
1914
1915 entry = (ConnCacheEntry *) lfirst(lc);
1916
1917 /* Ignore errors (see notes in pgfdw_xact_callback) */
1918 while ((res = PQgetResult(entry->conn)) != NULL)
1919 {
1920 PQclear(res);
1921 /* Stop if the connection is lost (else we'll loop infinitely) */
1922 if (PQstatus(entry->conn) == CONNECTION_BAD)
1923 break;
1924 }
1925 entry->have_prep_stmt = false;
1926 entry->have_error = false;
1927
1928 pgfdw_reset_xact_state(entry, true);
1929 }
1930}
PGresult * PQgetResult(PGconn *conn)
Definition: fe-exec.c:2062

References Assert(), ConnCacheEntry::changing_xact_state, ConnCacheEntry::conn, CONNECTION_BAD, do_sql_command_end(), ConnCacheEntry::have_error, ConnCacheEntry::have_prep_stmt, lappend(), lfirst, NIL, pgfdw_reset_xact_state(), PQclear(), PQgetResult(), PQsendQuery(), and PQstatus().

Referenced by pgfdw_xact_callback().

◆ pgfdw_finish_pre_subcommit_cleanup()

static void pgfdw_finish_pre_subcommit_cleanup ( List pending_entries,
int  curlevel 
)
static

Definition at line 1937 of file connection.c.

1938{
1939 ConnCacheEntry *entry;
1940 char sql[100];
1941 ListCell *lc;
1942
1943 Assert(pending_entries);
1944
1945 /*
1946 * Get the result of the RELEASE command for each of the pending entries
1947 */
1948 snprintf(sql, sizeof(sql), "RELEASE SAVEPOINT s%d", curlevel);
1949 foreach(lc, pending_entries)
1950 {
1951 entry = (ConnCacheEntry *) lfirst(lc);
1952
1954
1955 /*
1956 * We might already have received the result on the socket, so pass
1957 * consume_input=true to try to consume it first
1958 */
1959 do_sql_command_end(entry->conn, sql, true);
1960 entry->changing_xact_state = false;
1961
1962 pgfdw_reset_xact_state(entry, false);
1963 }
1964}

References Assert(), ConnCacheEntry::changing_xact_state, ConnCacheEntry::conn, do_sql_command_end(), lfirst, pgfdw_reset_xact_state(), and snprintf.

Referenced by pgfdw_subxact_callback().

◆ pgfdw_get_cleanup_result()

static bool pgfdw_get_cleanup_result ( PGconn conn,
TimestampTz  endtime,
TimestampTz  retrycanceltime,
PGresult **  result,
bool *  timed_out 
)
static

Definition at line 1613 of file connection.c.

1617{
1618 volatile bool failed = false;
1619 PGresult *volatile last_res = NULL;
1620
1621 *result = NULL;
1622 *timed_out = false;
1623
1624 /* In what follows, do not leak any PGresults on an error. */
1625 PG_TRY();
1626 {
1627 int canceldelta = RETRY_CANCEL_TIMEOUT * 2;
1628
1629 for (;;)
1630 {
1631 PGresult *res;
1632
1633 while (PQisBusy(conn))
1634 {
1635 int wc;
1637 long cur_timeout;
1638
1639 /* If timeout has expired, give up. */
1640 if (now >= endtime)
1641 {
1642 *timed_out = true;
1643 failed = true;
1644 goto exit;
1645 }
1646
1647 /* If we need to re-issue the cancel request, do that. */
1648 if (now >= retrycanceltime)
1649 {
1650 /* We ignore failure to issue the repeated request. */
1651 (void) libpqsrv_cancel(conn, endtime);
1652
1653 /* Recompute "now" in case that took measurable time. */
1655
1656 /* Adjust re-cancel timeout in increasing steps. */
1657 retrycanceltime = TimestampTzPlusMilliseconds(now,
1658 canceldelta);
1659 canceldelta += canceldelta;
1660 }
1661
1662 /* If timeout has expired, give up, else get sleep time. */
1664 Min(endtime,
1665 retrycanceltime));
1666 if (cur_timeout <= 0)
1667 {
1668 *timed_out = true;
1669 failed = true;
1670 goto exit;
1671 }
1672
1673 /* first time, allocate or get the custom wait event */
1674 if (pgfdw_we_cleanup_result == 0)
1675 pgfdw_we_cleanup_result = WaitEventExtensionNew("PostgresFdwCleanupResult");
1676
1677 /* Sleep until there's something to do */
1681 PQsocket(conn),
1682 cur_timeout, pgfdw_we_cleanup_result);
1684
1686
1687 /* Data available in socket? */
1688 if (wc & WL_SOCKET_READABLE)
1689 {
1690 if (!PQconsumeInput(conn))
1691 {
1692 /* connection trouble */
1693 failed = true;
1694 goto exit;
1695 }
1696 }
1697 }
1698
1699 res = PQgetResult(conn);
1700 if (res == NULL)
1701 break; /* query is complete */
1702
1703 PQclear(last_res);
1704 last_res = res;
1705 }
1706exit: ;
1707 }
1708 PG_CATCH();
1709 {
1710 PQclear(last_res);
1711 PG_RE_THROW();
1712 }
1713 PG_END_TRY();
1714
1715 if (failed)
1716 PQclear(last_res);
1717 else
1718 *result = last_res;
1719 return failed;
1720}
long TimestampDifferenceMilliseconds(TimestampTz start_time, TimestampTz stop_time)
Definition: timestamp.c:1757
#define Min(x, y)
Definition: c.h:975
static uint32 pgfdw_we_cleanup_result
Definition: connection.c:88
int PQisBusy(PGconn *conn)
Definition: fe-exec.c:2031
struct Latch * MyLatch
Definition: globals.c:64
int WaitLatchOrSocket(Latch *latch, int wakeEvents, pgsocket sock, long timeout, uint32 wait_event_info)
Definition: latch.c:221
void ResetLatch(Latch *latch)
Definition: latch.c:372
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:123
#define WL_SOCKET_READABLE
Definition: waiteventset.h:35
#define WL_TIMEOUT
Definition: waiteventset.h:37
#define WL_EXIT_ON_PM_DEATH
Definition: waiteventset.h:39
#define WL_LATCH_SET
Definition: waiteventset.h:34

References CHECK_FOR_INTERRUPTS, conn, GetCurrentTimestamp(), libpqsrv_cancel(), Min, MyLatch, now(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, pgfdw_we_cleanup_result, PQclear(), PQconsumeInput(), PQgetResult(), PQisBusy(), PQsocket(), ResetLatch(), RETRY_CANCEL_TIMEOUT, TimestampDifferenceMilliseconds(), TimestampTzPlusMilliseconds, WaitEventExtensionNew(), WaitLatchOrSocket(), WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, WL_SOCKET_READABLE, and WL_TIMEOUT.

Referenced by pgfdw_cancel_query_end(), and pgfdw_exec_cleanup_query_end().

◆ pgfdw_get_result()

PGresult * pgfdw_get_result ( PGconn conn)

Definition at line 958 of file connection.c.

959{
961}
static PGresult * libpqsrv_get_result_last(PGconn *conn, uint32 wait_event_info)

References conn, libpqsrv_get_result_last(), and pgfdw_we_get_result.

Referenced by create_cursor(), do_sql_command_end(), execute_dml_stmt(), execute_foreign_modify(), fetch_more_data(), pgfdw_exec_query(), and prepare_foreign_modify().

◆ pgfdw_has_required_scram_options()

static bool pgfdw_has_required_scram_options ( const char **  keywords,
const char **  values 
)
static

Definition at line 2521 of file connection.c.

2522{
2523 bool has_scram_server_key = false;
2524 bool has_scram_client_key = false;
2525 bool has_require_auth = false;
2526 bool has_scram_keys = false;
2527
2528 /*
2529 * Continue iterating even if we found the keys that we need to validate
2530 * to make sure that there is no other declaration of these keys that can
2531 * overwrite the first.
2532 */
2533 for (int i = 0; keywords[i] != NULL; i++)
2534 {
2535 if (strcmp(keywords[i], "scram_client_key") == 0)
2536 {
2537 if (values[i] != NULL && values[i][0] != '\0')
2538 has_scram_client_key = true;
2539 else
2540 has_scram_client_key = false;
2541 }
2542
2543 if (strcmp(keywords[i], "scram_server_key") == 0)
2544 {
2545 if (values[i] != NULL && values[i][0] != '\0')
2546 has_scram_server_key = true;
2547 else
2548 has_scram_server_key = false;
2549 }
2550
2551 if (strcmp(keywords[i], "require_auth") == 0)
2552 {
2553 if (values[i] != NULL && strcmp(values[i], "scram-sha-256") == 0)
2554 has_require_auth = true;
2555 else
2556 has_require_auth = false;
2557 }
2558 }
2559
2560 has_scram_keys = has_scram_client_key && has_scram_server_key && MyProcPort->has_scram_keys;
2561
2562 return (has_scram_keys && has_require_auth);
2563}

References Port::has_scram_keys, i, keywords, MyProcPort, and values.

Referenced by check_conn_params(), and pgfdw_security_check().

◆ pgfdw_inval_callback()

static void pgfdw_inval_callback ( Datum  arg,
int  cacheid,
uint32  hashvalue 
)
static

Definition at line 1296 of file connection.c.

1297{
1298 HASH_SEQ_STATUS scan;
1299 ConnCacheEntry *entry;
1300
1301 Assert(cacheid == FOREIGNSERVEROID || cacheid == USERMAPPINGOID);
1302
1303 /* ConnectionHash must exist already, if we're registered */
1305 while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
1306 {
1307 /* Ignore invalid entries */
1308 if (entry->conn == NULL)
1309 continue;
1310
1311 /* hashvalue == 0 means a cache reset, must clear all state */
1312 if (hashvalue == 0 ||
1313 (cacheid == FOREIGNSERVEROID &&
1314 entry->server_hashvalue == hashvalue) ||
1315 (cacheid == USERMAPPINGOID &&
1316 entry->mapping_hashvalue == hashvalue))
1317 {
1318 /*
1319 * Close the connection immediately if it's not used yet in this
1320 * transaction. Otherwise mark it as invalid so that
1321 * pgfdw_xact_callback() can close it at the end of this
1322 * transaction.
1323 */
1324 if (entry->xact_depth == 0)
1325 {
1326 elog(DEBUG3, "discarding connection %p", entry->conn);
1327 disconnect_pg_server(entry);
1328 }
1329 else
1330 entry->invalidated = true;
1331 }
1332 }
1333}

References Assert(), ConnCacheEntry::conn, ConnectionHash, DEBUG3, disconnect_pg_server(), elog, hash_seq_init(), hash_seq_search(), ConnCacheEntry::invalidated, ConnCacheEntry::mapping_hashvalue, ConnCacheEntry::server_hashvalue, and ConnCacheEntry::xact_depth.

Referenced by GetConnection().

◆ pgfdw_reject_incomplete_xact_state_change()

static void pgfdw_reject_incomplete_xact_state_change ( ConnCacheEntry entry)
static

Definition at line 1346 of file connection.c.

1347{
1348 ForeignServer *server;
1349
1350 /* nothing to do for inactive entries and entries of sane state */
1351 if (entry->conn == NULL || !entry->changing_xact_state)
1352 return;
1353
1354 /* make sure this entry is inactive */
1355 disconnect_pg_server(entry);
1356
1357 /* find server name to be shown in the message below */
1358 server = GetForeignServer(entry->serverid);
1359
1360 ereport(ERROR,
1361 (errcode(ERRCODE_CONNECTION_EXCEPTION),
1362 errmsg("connection to server \"%s\" was lost",
1363 server->servername)));
1364}

References ConnCacheEntry::changing_xact_state, ConnCacheEntry::conn, disconnect_pg_server(), ereport, errcode(), errmsg(), ERROR, GetForeignServer(), ConnCacheEntry::serverid, and ForeignServer::servername.

Referenced by GetConnection(), pgfdw_subxact_callback(), and pgfdw_xact_callback().

◆ pgfdw_report_error()

void pgfdw_report_error ( int  elevel,
PGresult res,
PGconn conn,
bool  clear,
const char *  sql 
)

Definition at line 977 of file connection.c.

979{
980 /* If requested, PGresult must be released before leaving this function. */
981 PG_TRY();
982 {
983 char *diag_sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
984 char *message_primary = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
985 char *message_detail = PQresultErrorField(res, PG_DIAG_MESSAGE_DETAIL);
986 char *message_hint = PQresultErrorField(res, PG_DIAG_MESSAGE_HINT);
987 char *message_context = PQresultErrorField(res, PG_DIAG_CONTEXT);
988 int sqlstate;
989
990 if (diag_sqlstate)
991 sqlstate = MAKE_SQLSTATE(diag_sqlstate[0],
992 diag_sqlstate[1],
993 diag_sqlstate[2],
994 diag_sqlstate[3],
995 diag_sqlstate[4]);
996 else
997 sqlstate = ERRCODE_CONNECTION_FAILURE;
998
999 /*
1000 * If we don't get a message from the PGresult, try the PGconn. This
1001 * is needed because for connection-level failures, PQgetResult may
1002 * just return NULL, not a PGresult at all.
1003 */
1004 if (message_primary == NULL)
1005 message_primary = pchomp(PQerrorMessage(conn));
1006
1007 ereport(elevel,
1008 (errcode(sqlstate),
1009 (message_primary != NULL && message_primary[0] != '\0') ?
1010 errmsg_internal("%s", message_primary) :
1011 errmsg("could not obtain message string for remote error"),
1012 message_detail ? errdetail_internal("%s", message_detail) : 0,
1013 message_hint ? errhint("%s", message_hint) : 0,
1014 message_context ? errcontext("%s", message_context) : 0,
1015 sql ? errcontext("remote SQL command: %s", sql) : 0));
1016 }
1017 PG_FINALLY();
1018 {
1019 if (clear)
1020 PQclear(res);
1021 }
1022 PG_END_TRY();
1023}
int errhint(const char *fmt,...)
Definition: elog.c:1318
#define MAKE_SQLSTATE(ch1, ch2, ch3, ch4, ch5)
Definition: elog.h:56
#define PG_FINALLY(...)
Definition: elog.h:388
char * PQresultErrorField(const PGresult *res, int fieldcode)
Definition: fe-exec.c:3466
#define PG_DIAG_MESSAGE_HINT
Definition: postgres_ext.h:55
#define PG_DIAG_SQLSTATE
Definition: postgres_ext.h:52
#define PG_DIAG_MESSAGE_PRIMARY
Definition: postgres_ext.h:53
#define PG_DIAG_MESSAGE_DETAIL
Definition: postgres_ext.h:54
#define PG_DIAG_CONTEXT
Definition: postgres_ext.h:59

References conn, ereport, errcode(), errcontext, errdetail_internal(), errhint(), errmsg(), errmsg_internal(), MAKE_SQLSTATE, pchomp(), PG_DIAG_CONTEXT, PG_DIAG_MESSAGE_DETAIL, PG_DIAG_MESSAGE_HINT, PG_DIAG_MESSAGE_PRIMARY, PG_DIAG_SQLSTATE, PG_END_TRY, PG_FINALLY, PG_TRY, PQclear(), PQerrorMessage(), and PQresultErrorField().

Referenced by close_cursor(), create_cursor(), deallocate_query(), do_sql_command_begin(), do_sql_command_end(), execute_dml_stmt(), execute_foreign_modify(), fetch_more_data(), fetch_more_data_begin(), get_remote_estimate(), pgfdw_exec_cleanup_query_begin(), pgfdw_exec_cleanup_query_end(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresForeignAsyncNotify(), postgresGetAnalyzeInfoForForeignTable(), postgresImportForeignSchema(), postgresReScanForeignScan(), and prepare_foreign_modify().

◆ pgfdw_reset_xact_state()

static void pgfdw_reset_xact_state ( ConnCacheEntry entry,
bool  toplevel 
)
static

Definition at line 1370 of file connection.c.

1371{
1372 if (toplevel)
1373 {
1374 /* Reset state to show we're out of a transaction */
1375 entry->xact_depth = 0;
1376
1377 /*
1378 * If the connection isn't in a good idle state, it is marked as
1379 * invalid or keep_connections option of its server is disabled, then
1380 * discard it to recover. Next GetConnection will open a new
1381 * connection.
1382 */
1383 if (PQstatus(entry->conn) != CONNECTION_OK ||
1385 entry->changing_xact_state ||
1386 entry->invalidated ||
1387 !entry->keep_connections)
1388 {
1389 elog(DEBUG3, "discarding connection %p", entry->conn);
1390 disconnect_pg_server(entry);
1391 }
1392 }
1393 else
1394 {
1395 /* Reset state to show we're out of a subtransaction */
1396 entry->xact_depth--;
1397 }
1398}
@ PQTRANS_IDLE
Definition: libpq-fe.h:147

References ConnCacheEntry::changing_xact_state, ConnCacheEntry::conn, CONNECTION_OK, DEBUG3, disconnect_pg_server(), elog, ConnCacheEntry::invalidated, ConnCacheEntry::keep_connections, PQstatus(), PQTRANS_IDLE, PQtransactionStatus(), and ConnCacheEntry::xact_depth.

Referenced by pgfdw_finish_abort_cleanup(), pgfdw_finish_pre_commit_cleanup(), pgfdw_finish_pre_subcommit_cleanup(), pgfdw_subxact_callback(), and pgfdw_xact_callback().

◆ pgfdw_security_check()

static void pgfdw_security_check ( const char **  keywords,
const char **  values,
UserMapping user,
PGconn conn 
)
static

Definition at line 432 of file connection.c.

433{
434 /* Superusers bypass the check */
435 if (superuser_arg(user->userid))
436 return;
437
438#ifdef ENABLE_GSS
439 /* Connected via GSSAPI with delegated credentials- all good. */
441 return;
442#endif
443
444 /* Ok if superuser set PW required false. */
446 return;
447
448 /* Connected via PW, with PW required true, and provided non-empty PW. */
450 {
451 /* ok if params contain a non-empty password */
452 for (int i = 0; keywords[i] != NULL; i++)
453 {
454 if (strcmp(keywords[i], "password") == 0 && values[i][0] != '\0')
455 return;
456 }
457 }
458
459 /*
460 * Ok if SCRAM pass-through is being used and all required SCRAM options
461 * are set correctly. If pgfdw_has_required_scram_options returns true we
462 * assume that UseScramPassthrough is also true since SCRAM options are
463 * only set when UseScramPassthrough is enabled.
464 */
466 return;
467
469 (errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
470 errmsg("password or GSSAPI delegated credentials required"),
471 errdetail("Non-superuser cannot connect if the server does not request a password or use GSSAPI with delegated credentials."),
472 errhint("Target server's authentication method must be changed or password_required=false set in the user mapping attributes.")));
473}
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:7687
int PQconnectionUsedGSSAPI(const PGconn *conn)
Definition: fe-connect.c:7698

References be_gssapi_get_delegation(), conn, ereport, errcode(), errdetail(), errhint(), errmsg(), ERROR, Port::has_scram_keys, i, keywords, MyProcPort, pgfdw_has_required_scram_options(), PQconnectionUsedGSSAPI(), PQconnectionUsedPassword(), superuser_arg(), user, UserMappingPasswordRequired(), and values.

Referenced by connect_pg_server().

◆ pgfdw_subxact_callback()

static void pgfdw_subxact_callback ( SubXactEvent  event,
SubTransactionId  mySubid,
SubTransactionId  parentSubid,
void *  arg 
)
static

Definition at line 1183 of file connection.c.

1185{
1186 HASH_SEQ_STATUS scan;
1187 ConnCacheEntry *entry;
1188 int curlevel;
1189 List *pending_entries = NIL;
1190 List *cancel_requested = NIL;
1191
1192 /* Nothing to do at subxact start, nor after commit. */
1193 if (!(event == SUBXACT_EVENT_PRE_COMMIT_SUB ||
1194 event == SUBXACT_EVENT_ABORT_SUB))
1195 return;
1196
1197 /* Quick exit if no connections were touched in this transaction. */
1199 return;
1200
1201 /*
1202 * Scan all connection cache entries to find open remote subtransactions
1203 * of the current level, and close them.
1204 */
1205 curlevel = GetCurrentTransactionNestLevel();
1207 while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
1208 {
1209 char sql[100];
1210
1211 /*
1212 * We only care about connections with open remote subtransactions of
1213 * the current level.
1214 */
1215 if (entry->conn == NULL || entry->xact_depth < curlevel)
1216 continue;
1217
1218 if (entry->xact_depth > curlevel)
1219 elog(ERROR, "missed cleaning up remote subtransaction at level %d",
1220 entry->xact_depth);
1221
1222 if (event == SUBXACT_EVENT_PRE_COMMIT_SUB)
1223 {
1224 /*
1225 * If abort cleanup previously failed for this connection, we
1226 * can't issue any more commands against it.
1227 */
1229
1230 /* Commit all remote subtransactions during pre-commit */
1231 snprintf(sql, sizeof(sql), "RELEASE SAVEPOINT s%d", curlevel);
1232 entry->changing_xact_state = true;
1233 if (entry->parallel_commit)
1234 {
1235 do_sql_command_begin(entry->conn, sql);
1236 pending_entries = lappend(pending_entries, entry);
1237 continue;
1238 }
1239 do_sql_command(entry->conn, sql);
1240 entry->changing_xact_state = false;
1241 }
1242 else
1243 {
1244 /* Rollback all remote subtransactions during abort */
1245 if (entry->parallel_abort)
1246 {
1247 if (pgfdw_abort_cleanup_begin(entry, false,
1248 &pending_entries,
1249 &cancel_requested))
1250 continue;
1251 }
1252 else
1253 pgfdw_abort_cleanup(entry, false);
1254 }
1255
1256 /* OK, we're outta that level of subtransaction */
1257 pgfdw_reset_xact_state(entry, false);
1258 }
1259
1260 /* If there are any pending connections, finish cleaning them up */
1261 if (pending_entries || cancel_requested)
1262 {
1263 if (event == SUBXACT_EVENT_PRE_COMMIT_SUB)
1264 {
1265 Assert(cancel_requested == NIL);
1266 pgfdw_finish_pre_subcommit_cleanup(pending_entries, curlevel);
1267 }
1268 else
1269 {
1271 pgfdw_finish_abort_cleanup(pending_entries, cancel_requested,
1272 false);
1273 }
1274 }
1275}
static void pgfdw_finish_pre_subcommit_cleanup(List *pending_entries, int curlevel)
Definition: connection.c:1937
static void pgfdw_finish_abort_cleanup(List *pending_entries, List *cancel_requested, bool toplevel)
Definition: connection.c:1971
static bool pgfdw_abort_cleanup_begin(ConnCacheEntry *entry, bool toplevel, List **pending_entries, List **cancel_requested)
Definition: connection.c:1805
static void pgfdw_abort_cleanup(ConnCacheEntry *entry, bool toplevel)
Definition: connection.c:1731
@ SUBXACT_EVENT_PRE_COMMIT_SUB
Definition: xact.h:145
@ SUBXACT_EVENT_ABORT_SUB
Definition: xact.h:144

References Assert(), ConnCacheEntry::changing_xact_state, ConnCacheEntry::conn, ConnectionHash, do_sql_command(), do_sql_command_begin(), elog, ERROR, GetCurrentTransactionNestLevel(), hash_seq_init(), hash_seq_search(), lappend(), NIL, ConnCacheEntry::parallel_abort, ConnCacheEntry::parallel_commit, pgfdw_abort_cleanup(), pgfdw_abort_cleanup_begin(), pgfdw_finish_abort_cleanup(), pgfdw_finish_pre_subcommit_cleanup(), pgfdw_reject_incomplete_xact_state_change(), pgfdw_reset_xact_state(), snprintf, SUBXACT_EVENT_ABORT_SUB, SUBXACT_EVENT_PRE_COMMIT_SUB, ConnCacheEntry::xact_depth, and xact_got_connection.

Referenced by GetConnection().

◆ pgfdw_xact_callback()

static void pgfdw_xact_callback ( XactEvent  event,
void *  arg 
)
static

Definition at line 1033 of file connection.c.

1034{
1035 HASH_SEQ_STATUS scan;
1036 ConnCacheEntry *entry;
1037 List *pending_entries = NIL;
1038 List *cancel_requested = NIL;
1039
1040 /* Quick exit if no connections were touched in this transaction. */
1042 return;
1043
1044 /*
1045 * Scan all connection cache entries to find open remote transactions, and
1046 * close them.
1047 */
1049 while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
1050 {
1051 PGresult *res;
1052
1053 /* Ignore cache entry if no open connection right now */
1054 if (entry->conn == NULL)
1055 continue;
1056
1057 /* If it has an open remote transaction, try to close it */
1058 if (entry->xact_depth > 0)
1059 {
1060 elog(DEBUG3, "closing remote transaction on connection %p",
1061 entry->conn);
1062
1063 switch (event)
1064 {
1067
1068 /*
1069 * If abort cleanup previously failed for this connection,
1070 * we can't issue any more commands against it.
1071 */
1073
1074 /* Commit all remote transactions during pre-commit */
1075 entry->changing_xact_state = true;
1076 if (entry->parallel_commit)
1077 {
1078 do_sql_command_begin(entry->conn, "COMMIT TRANSACTION");
1079 pending_entries = lappend(pending_entries, entry);
1080 continue;
1081 }
1082 do_sql_command(entry->conn, "COMMIT TRANSACTION");
1083 entry->changing_xact_state = false;
1084
1085 /*
1086 * If there were any errors in subtransactions, and we
1087 * made prepared statements, do a DEALLOCATE ALL to make
1088 * sure we get rid of all prepared statements. This is
1089 * annoying and not terribly bulletproof, but it's
1090 * probably not worth trying harder.
1091 *
1092 * DEALLOCATE ALL only exists in 8.3 and later, so this
1093 * constrains how old a server postgres_fdw can
1094 * communicate with. We intentionally ignore errors in
1095 * the DEALLOCATE, so that we can hobble along to some
1096 * extent with older servers (leaking prepared statements
1097 * as we go; but we don't really support update operations
1098 * pre-8.3 anyway).
1099 */
1100 if (entry->have_prep_stmt && entry->have_error)
1101 {
1102 res = pgfdw_exec_query(entry->conn, "DEALLOCATE ALL",
1103 NULL);
1104 PQclear(res);
1105 }
1106 entry->have_prep_stmt = false;
1107 entry->have_error = false;
1108 break;
1110
1111 /*
1112 * We disallow any remote transactions, since it's not
1113 * very reasonable to hold them open until the prepared
1114 * transaction is committed. For the moment, throw error
1115 * unconditionally; later we might allow read-only cases.
1116 * Note that the error will cause us to come right back
1117 * here with event == XACT_EVENT_ABORT, so we'll clean up
1118 * the connection state at that point.
1119 */
1120 ereport(ERROR,
1121 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1122 errmsg("cannot PREPARE a transaction that has operated on postgres_fdw foreign tables")));
1123 break;
1125 case XACT_EVENT_COMMIT:
1126 case XACT_EVENT_PREPARE:
1127 /* Pre-commit should have closed the open transaction */
1128 elog(ERROR, "missed cleaning up connection during pre-commit");
1129 break;
1131 case XACT_EVENT_ABORT:
1132 /* Rollback all remote transactions during abort */
1133 if (entry->parallel_abort)
1134 {
1135 if (pgfdw_abort_cleanup_begin(entry, true,
1136 &pending_entries,
1137 &cancel_requested))
1138 continue;
1139 }
1140 else
1141 pgfdw_abort_cleanup(entry, true);
1142 break;
1143 }
1144 }
1145
1146 /* Reset state to show we're out of a transaction */
1147 pgfdw_reset_xact_state(entry, true);
1148 }
1149
1150 /* If there are any pending connections, finish cleaning them up */
1151 if (pending_entries || cancel_requested)
1152 {
1153 if (event == XACT_EVENT_PARALLEL_PRE_COMMIT ||
1154 event == XACT_EVENT_PRE_COMMIT)
1155 {
1156 Assert(cancel_requested == NIL);
1157 pgfdw_finish_pre_commit_cleanup(pending_entries);
1158 }
1159 else
1160 {
1162 event == XACT_EVENT_ABORT);
1163 pgfdw_finish_abort_cleanup(pending_entries, cancel_requested,
1164 true);
1165 }
1166 }
1167
1168 /*
1169 * Regardless of the event type, we can now mark ourselves as out of the
1170 * transaction. (Note: if we are here during PRE_COMMIT or PRE_PREPARE,
1171 * this saves a useless scan of the hashtable during COMMIT or PREPARE.)
1172 */
1173 xact_got_connection = false;
1174
1175 /* Also reset cursor numbering for next transaction */
1176 cursor_number = 0;
1177}
PGresult * pgfdw_exec_query(PGconn *conn, const char *query, PgFdwConnState *state)
Definition: connection.c:941
static void pgfdw_finish_pre_commit_cleanup(List *pending_entries)
Definition: connection.c:1863
@ XACT_EVENT_PRE_PREPARE
Definition: xact.h:135
@ XACT_EVENT_COMMIT
Definition: xact.h:128
@ XACT_EVENT_PARALLEL_PRE_COMMIT
Definition: xact.h:134
@ XACT_EVENT_PARALLEL_COMMIT
Definition: xact.h:129
@ XACT_EVENT_ABORT
Definition: xact.h:130
@ XACT_EVENT_PRE_COMMIT
Definition: xact.h:133
@ XACT_EVENT_PARALLEL_ABORT
Definition: xact.h:131
@ XACT_EVENT_PREPARE
Definition: xact.h:132

References Assert(), ConnCacheEntry::changing_xact_state, ConnCacheEntry::conn, ConnectionHash, cursor_number, DEBUG3, do_sql_command(), do_sql_command_begin(), elog, ereport, errcode(), errmsg(), ERROR, hash_seq_init(), hash_seq_search(), ConnCacheEntry::have_error, ConnCacheEntry::have_prep_stmt, lappend(), NIL, ConnCacheEntry::parallel_abort, ConnCacheEntry::parallel_commit, pgfdw_abort_cleanup(), pgfdw_abort_cleanup_begin(), pgfdw_exec_query(), pgfdw_finish_abort_cleanup(), pgfdw_finish_pre_commit_cleanup(), pgfdw_reject_incomplete_xact_state_change(), pgfdw_reset_xact_state(), PQclear(), ConnCacheEntry::xact_depth, XACT_EVENT_ABORT, XACT_EVENT_COMMIT, XACT_EVENT_PARALLEL_ABORT, XACT_EVENT_PARALLEL_COMMIT, XACT_EVENT_PARALLEL_PRE_COMMIT, XACT_EVENT_PRE_COMMIT, XACT_EVENT_PRE_PREPARE, XACT_EVENT_PREPARE, and xact_got_connection.

Referenced by GetConnection().

◆ postgres_fdw_disconnect()

Datum postgres_fdw_disconnect ( PG_FUNCTION_ARGS  )

Definition at line 2345 of file connection.c.

2346{
2347 ForeignServer *server;
2348 char *servername;
2349
2350 servername = text_to_cstring(PG_GETARG_TEXT_PP(0));
2351 server = GetForeignServerByName(servername, false);
2352
2354}
static bool disconnect_cached_connections(Oid serverid)
Definition: connection.c:2394
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_BOOL(x)
Definition: fmgr.h:359
ForeignServer * GetForeignServerByName(const char *srvname, bool missing_ok)
Definition: foreign.c:182
char * text_to_cstring(const text *t)
Definition: varlena.c:225

References disconnect_cached_connections(), GetForeignServerByName(), PG_GETARG_TEXT_PP, PG_RETURN_BOOL, ForeignServer::serverid, and text_to_cstring().

◆ postgres_fdw_disconnect_all()

Datum postgres_fdw_disconnect_all ( PG_FUNCTION_ARGS  )

Definition at line 2366 of file connection.c.

2367{
2369}
#define InvalidOid
Definition: postgres_ext.h:35

References disconnect_cached_connections(), InvalidOid, and PG_RETURN_BOOL.

◆ postgres_fdw_get_connections()

Datum postgres_fdw_get_connections ( PG_FUNCTION_ARGS  )

Definition at line 2325 of file connection.c.

2326{
2328
2330}
static void postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo, enum pgfdwVersion api_version)
Definition: connection.c:2165
#define PG_RETURN_VOID()
Definition: fmgr.h:349

References PG_RETURN_VOID, PGFDW_V1_1, and postgres_fdw_get_connections_internal().

◆ postgres_fdw_get_connections_1_2()

Datum postgres_fdw_get_connections_1_2 ( PG_FUNCTION_ARGS  )

Definition at line 2317 of file connection.c.

2318{
2320
2322}

References PG_RETURN_VOID, PGFDW_V1_2, and postgres_fdw_get_connections_internal().

◆ postgres_fdw_get_connections_internal()

static void postgres_fdw_get_connections_internal ( FunctionCallInfo  fcinfo,
enum pgfdwVersion  api_version 
)
static

Definition at line 2165 of file connection.c.

2167{
2168 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
2169 HASH_SEQ_STATUS scan;
2170 ConnCacheEntry *entry;
2171
2172 InitMaterializedSRF(fcinfo, 0);
2173
2174 /* If cache doesn't exist, we return no records */
2175 if (!ConnectionHash)
2176 return;
2177
2178 /* Check we have the expected number of output arguments */
2179 switch (rsinfo->setDesc->natts)
2180 {
2182 if (api_version != PGFDW_V1_1)
2183 elog(ERROR, "incorrect number of output arguments");
2184 break;
2186 if (api_version != PGFDW_V1_2)
2187 elog(ERROR, "incorrect number of output arguments");
2188 break;
2189 default:
2190 elog(ERROR, "incorrect number of output arguments");
2191 }
2192
2194 while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
2195 {
2196 ForeignServer *server;
2198 bool nulls[POSTGRES_FDW_GET_CONNECTIONS_COLS] = {0};
2199 int i = 0;
2200
2201 /* We only look for open remote connections */
2202 if (!entry->conn)
2203 continue;
2204
2205 server = GetForeignServerExtended(entry->serverid, FSV_MISSING_OK);
2206
2207 /*
2208 * The foreign server may have been dropped in current explicit
2209 * transaction. It is not possible to drop the server from another
2210 * session when the connection associated with it is in use in the
2211 * current transaction, if tried so, the drop query in another session
2212 * blocks until the current transaction finishes.
2213 *
2214 * Even though the server is dropped in the current transaction, the
2215 * cache can still have associated active connection entry, say we
2216 * call such connections dangling. Since we can not fetch the server
2217 * name from system catalogs for dangling connections, instead we show
2218 * NULL value for server name in output.
2219 *
2220 * We could have done better by storing the server name in the cache
2221 * entry instead of server oid so that it could be used in the output.
2222 * But the server name in each cache entry requires 64 bytes of
2223 * memory, which is huge, when there are many cached connections and
2224 * the use case i.e. dropping the foreign server within the explicit
2225 * current transaction seems rare. So, we chose to show NULL value for
2226 * server name in output.
2227 *
2228 * Such dangling connections get closed either in next use or at the
2229 * end of current explicit transaction in pgfdw_xact_callback.
2230 */
2231 if (!server)
2232 {
2233 /*
2234 * If the server has been dropped in the current explicit
2235 * transaction, then this entry would have been invalidated in
2236 * pgfdw_inval_callback at the end of drop server command. Note
2237 * that this connection would not have been closed in
2238 * pgfdw_inval_callback because it is still being used in the
2239 * current explicit transaction. So, assert that here.
2240 */
2241 Assert(entry->conn && entry->xact_depth > 0 && entry->invalidated);
2242
2243 /* Show null, if no server name was found */
2244 nulls[i++] = true;
2245 }
2246 else
2247 values[i++] = CStringGetTextDatum(server->servername);
2248
2249 if (api_version >= PGFDW_V1_2)
2250 {
2251 HeapTuple tp;
2252
2253 /* Use the system cache to obtain the user mapping */
2254 tp = SearchSysCache1(USERMAPPINGOID, ObjectIdGetDatum(entry->key));
2255
2256 /*
2257 * Just like in the foreign server case, user mappings can also be
2258 * dropped in the current explicit transaction. Therefore, the
2259 * similar check as in the server case is required.
2260 */
2261 if (!HeapTupleIsValid(tp))
2262 {
2263 /*
2264 * If we reach here, this entry must have been invalidated in
2265 * pgfdw_inval_callback, same as in the server case.
2266 */
2267 Assert(entry->conn && entry->xact_depth > 0 &&
2268 entry->invalidated);
2269
2270 nulls[i++] = true;
2271 }
2272 else
2273 {
2274 Oid userid;
2275
2276 userid = ((Form_pg_user_mapping) GETSTRUCT(tp))->umuser;
2278 ReleaseSysCache(tp);
2279 }
2280 }
2281
2282 values[i++] = BoolGetDatum(!entry->invalidated);
2283
2284 if (api_version >= PGFDW_V1_2)
2285 {
2286 bool check_conn = PG_GETARG_BOOL(0);
2287
2288 /* Is this connection used in the current transaction? */
2289 values[i++] = BoolGetDatum(entry->xact_depth > 0);
2290
2291 /*
2292 * If a connection status check is requested and supported, return
2293 * whether the connection is closed. Otherwise, return NULL.
2294 */
2295 if (check_conn && pgfdw_conn_checkable())
2296 values[i++] = BoolGetDatum(pgfdw_conn_check(entry->conn) != 0);
2297 else
2298 nulls[i++] = true;
2299
2300 /* Return process ID of remote backend */
2301 values[i++] = Int32GetDatum(PQbackendPID(entry->conn));
2302 }
2303
2304 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
2305 }
2306}
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_2
Definition: connection.c:2135
static int pgfdw_conn_check(PGconn *conn)
Definition: connection.c:2466
#define POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_1
Definition: connection.c:2134
#define POSTGRES_FDW_GET_CONNECTIONS_COLS
Definition: connection.c:2136
static bool pgfdw_conn_checkable(void)
Definition: connection.c:2503
int PQbackendPID(const PGconn *conn)
Definition: fe-connect.c:7655
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
#define MappingUserName(userid)
Definition: foreign.h:20
void InitMaterializedSRF(FunctionCallInfo fcinfo, bits32 flags)
Definition: funcapi.c:76
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
FormData_pg_user_mapping * Form_pg_user_mapping
static Datum BoolGetDatum(bool X)
Definition: postgres.h:107
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:217
unsigned int Oid
Definition: postgres_ext.h:30
fmNodePtr resultinfo
Definition: fmgr.h:89
TupleDesc setDesc
Definition: execnodes.h:359
Tuplestorestate * setResult
Definition: execnodes.h:358
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition: tuplestore.c:784

References Assert(), BoolGetDatum(), ConnectionHash, CStringGetTextDatum, elog, ERROR, FSV_MISSING_OK, GetForeignServerExtended(), GETSTRUCT(), hash_seq_init(), hash_seq_search(), HeapTupleIsValid, i, InitMaterializedSRF(), Int32GetDatum(), MappingUserName, TupleDescData::natts, ObjectIdGetDatum(), PG_GETARG_BOOL, pgfdw_conn_check(), pgfdw_conn_checkable(), PGFDW_V1_1, PGFDW_V1_2, POSTGRES_FDW_GET_CONNECTIONS_COLS, POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_1, POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_2, PQbackendPID(), ReleaseSysCache(), FunctionCallInfoBaseData::resultinfo, SearchSysCache1(), ForeignServer::servername, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, tuplestore_putvalues(), and values.

Referenced by postgres_fdw_get_connections(), and postgres_fdw_get_connections_1_2().

◆ ReleaseConnection()

void ReleaseConnection ( PGconn conn)

Definition at line 891 of file connection.c.

892{
893 /*
894 * Currently, we don't actually track connection references because all
895 * cleanup is managed on a transaction or subtransaction basis instead. So
896 * there's nothing to do here.
897 */
898}

Referenced by estimate_path_cost_size(), finish_foreign_modify(), postgresAcquireSampleRowsFunc(), postgresAnalyzeForeignTable(), postgresEndDirectModify(), postgresEndForeignScan(), postgresGetAnalyzeInfoForForeignTable(), and postgresImportForeignSchema().

◆ UserMappingPasswordRequired()

static bool UserMappingPasswordRequired ( UserMapping user)
static

Definition at line 667 of file connection.c.

668{
669 ListCell *cell;
670
671 foreach(cell, user->options)
672 {
673 DefElem *def = (DefElem *) lfirst(cell);
674
675 if (strcmp(def->defname, "password_required") == 0)
676 return defGetBoolean(def);
677 }
678
679 return true;
680}

References defGetBoolean(), DefElem::defname, lfirst, and user.

Referenced by check_conn_params(), and pgfdw_security_check().

◆ UseScramPassthrough()

static bool UseScramPassthrough ( ForeignServer server,
UserMapping user 
)
static

Definition at line 683 of file connection.c.

684{
685 ListCell *cell;
686
687 foreach(cell, server->options)
688 {
689 DefElem *def = (DefElem *) lfirst(cell);
690
691 if (strcmp(def->defname, "use_scram_passthrough") == 0)
692 return defGetBoolean(def);
693 }
694
695 foreach(cell, user->options)
696 {
697 DefElem *def = (DefElem *) lfirst(cell);
698
699 if (strcmp(def->defname, "use_scram_passthrough") == 0)
700 return defGetBoolean(def);
701 }
702
703 return false;
704}

References defGetBoolean(), DefElem::defname, lfirst, ForeignServer::options, and user.

Referenced by connect_pg_server().

Variable Documentation

◆ ConnectionHash

◆ cursor_number

unsigned int cursor_number = 0
static

◆ pgfdw_we_cleanup_result

uint32 pgfdw_we_cleanup_result = 0
static

Definition at line 88 of file connection.c.

Referenced by pgfdw_get_cleanup_result().

◆ pgfdw_we_connect

uint32 pgfdw_we_connect = 0
static

Definition at line 89 of file connection.c.

Referenced by connect_pg_server().

◆ pgfdw_we_get_result

uint32 pgfdw_we_get_result = 0
static

Definition at line 90 of file connection.c.

Referenced by GetConnection(), and pgfdw_get_result().

◆ prep_stmt_number

unsigned int prep_stmt_number = 0
static

Definition at line 82 of file connection.c.

Referenced by GetPrepStmtNumber().

◆ xact_got_connection

bool xact_got_connection = false
static

Definition at line 85 of file connection.c.

Referenced by GetConnection(), pgfdw_subxact_callback(), and pgfdw_xact_callback().