LCOV - differential code coverage report
Current view: top level - contrib/postgres_fdw - connection.c (source / functions) Coverage Total Hit UBC GBC GNC CBC DCB
Current: 1a1248818fb8b8b0353c292c632d6a6af77e8209 vs 8e82d6610e7280d7870c0c36ff1c797e3cfb3e09 Lines: 87.3 % 819 715 104 1 6 708 6
Current Date: 2026-10-12 11:56:10 +0200 Functions: 98.2 % 55 54 1 1 53
Baseline: lcov-20261012-baseline Branches: 68.2 % 714 487 227 1 486
Baseline Date: 2026-10-12 10:36:06 +0200 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 88.9 % 18 16 2 16
(30,360] days: 94.0 % 134 126 8 6 120
(360..) days: 85.9 % 667 573 94 1 572
Function coverage date bins:
(30,360] days: 100.0 % 6 6 1 5
(360..) days: 98.0 % 49 48 1 48
Branch coverage date bins:
(1,7] days: 65.6 % 32 21 11 21
(30,360] days: 76.1 % 92 70 22 70
(360..) days: 67.1 % 590 396 194 1 395

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * connection.c
                                  4                 :                :  *        Connection management functions for postgres_fdw
                                  5                 :                :  *
                                  6                 :                :  * Portions Copyright (c) 2012-2026, PostgreSQL Global Development Group
                                  7                 :                :  *
                                  8                 :                :  * IDENTIFICATION
                                  9                 :                :  *        contrib/postgres_fdw/connection.c
                                 10                 :                :  *
                                 11                 :                :  *-------------------------------------------------------------------------
                                 12                 :                :  */
                                 13                 :                : #include "postgres.h"
                                 14                 :                : 
                                 15                 :                : #if HAVE_POLL_H
                                 16                 :                : #include <poll.h>
                                 17                 :                : #endif
                                 18                 :                : 
                                 19                 :                : #include "access/htup_details.h"
                                 20                 :                : #include "access/xact.h"
                                 21                 :                : #include "catalog/pg_user_mapping.h"
                                 22                 :                : #include "commands/defrem.h"
                                 23                 :                : #include "common/base64.h"
                                 24                 :                : #include "funcapi.h"
                                 25                 :                : #include "libpq/libpq-be.h"
                                 26                 :                : #include "libpq/libpq-be-fe-helpers.h"
                                 27                 :                : #include "mb/pg_wchar.h"
                                 28                 :                : #include "miscadmin.h"
                                 29                 :                : #include "pgstat.h"
                                 30                 :                : #include "postgres_fdw.h"
                                 31                 :                : #include "storage/latch.h"
                                 32                 :                : #include "utils/builtins.h"
                                 33                 :                : #include "utils/hsearch.h"
                                 34                 :                : #include "utils/inval.h"
                                 35                 :                : #include "utils/syscache.h"
                                 36                 :                : #include "utils/tuplestore.h"
                                 37                 :                : 
                                 38                 :                : /*
                                 39                 :                :  * Connection cache hash table entry
                                 40                 :                :  *
                                 41                 :                :  * The lookup key in this hash table is the user mapping OID. We use just one
                                 42                 :                :  * connection per user mapping ID, which ensures that all the scans use the
                                 43                 :                :  * same snapshot during a query.  Using the user mapping OID rather than
                                 44                 :                :  * the foreign server OID + user OID avoids creating multiple connections when
                                 45                 :                :  * the public user mapping applies to all user OIDs.
                                 46                 :                :  *
                                 47                 :                :  * The "conn" pointer can be NULL if we don't currently have a live connection.
                                 48                 :                :  * When we do have a connection, xact_depth tracks the current depth of
                                 49                 :                :  * transactions and subtransactions open on the remote side.  We need to issue
                                 50                 :                :  * commands at the same nesting depth on the remote as we're executing at
                                 51                 :                :  * ourselves, so that rolling back a subtransaction will kill the right
                                 52                 :                :  * queries and not the wrong ones.
                                 53                 :                :  */
                                 54                 :                : typedef Oid ConnCacheKey;
                                 55                 :                : 
                                 56                 :                : typedef struct ConnCacheEntry
                                 57                 :                : {
                                 58                 :                :     ConnCacheKey key;           /* hash key (must be first) */
                                 59                 :                :     PGconn     *conn;           /* connection to foreign server, or NULL */
                                 60                 :                :     /* Remaining fields are invalid when conn is NULL: */
                                 61                 :                :     int         xact_depth;     /* 0 = no xact open, 1 = main xact open, 2 =
                                 62                 :                :                                  * one level of subxact open, etc */
                                 63                 :                :     bool        xact_read_only; /* xact r/o state */
                                 64                 :                :     bool        have_prep_stmt; /* have we prepared any stmts in this xact? */
                                 65                 :                :     bool        have_error;     /* have any subxacts aborted in this xact? */
                                 66                 :                :     bool        changing_xact_state;    /* xact state change in process */
                                 67                 :                :     bool        parallel_commit;    /* do we commit (sub)xacts in parallel? */
                                 68                 :                :     bool        parallel_abort; /* do we abort (sub)xacts in parallel? */
                                 69                 :                :     bool        invalidated;    /* true if reconnect is pending */
                                 70                 :                :     bool        keep_connections;   /* setting value of keep_connections
                                 71                 :                :                                      * server option */
                                 72                 :                :     Oid         serverid;       /* foreign server OID used to get server name */
                                 73                 :                :     uint32      server_hashvalue;   /* hash value of foreign server OID */
                                 74                 :                :     uint32      mapping_hashvalue;  /* hash value of user mapping OID */
                                 75                 :                :     PgFdwConnState state;       /* extra per-connection state */
                                 76                 :                : } ConnCacheEntry;
                                 77                 :                : 
                                 78                 :                : /*
                                 79                 :                :  * Connection cache (initialized on first use)
                                 80                 :                :  */
                                 81                 :                : static HTAB *ConnectionHash = NULL;
                                 82                 :                : 
                                 83                 :                : /* for assigning cursor numbers and prepared statement numbers */
                                 84                 :                : static unsigned int cursor_number = 0;
                                 85                 :                : static unsigned int prep_stmt_number = 0;
                                 86                 :                : 
                                 87                 :                : /* tracks whether any work is needed in callback functions */
                                 88                 :                : static bool xact_got_connection = false;
                                 89                 :                : 
                                 90                 :                : /*
                                 91                 :                :  * tracks the topmost read-only local transaction's nesting level determined
                                 92                 :                :  * by GetTopReadOnlyTransactionNestLevel()
                                 93                 :                :  */
                                 94                 :                : static int  read_only_level = 0;
                                 95                 :                : 
                                 96                 :                : /* custom wait event values, retrieved from shared memory */
                                 97                 :                : static uint32 pgfdw_we_cleanup_result = 0;
                                 98                 :                : static uint32 pgfdw_we_connect = 0;
                                 99                 :                : static uint32 pgfdw_we_get_result = 0;
                                100                 :                : 
                                101                 :                : /*
                                102                 :                :  * Milliseconds to wait to cancel an in-progress query or execute a cleanup
                                103                 :                :  * query; if it takes longer than 30 seconds to do these, we assume the
                                104                 :                :  * connection is dead.
                                105                 :                :  */
                                106                 :                : #define CONNECTION_CLEANUP_TIMEOUT  30000
                                107                 :                : 
                                108                 :                : /*
                                109                 :                :  * Milliseconds to wait before issuing another cancel request.  This covers
                                110                 :                :  * the race condition where the remote session ignored our cancel request
                                111                 :                :  * because it arrived while idle.
                                112                 :                :  */
                                113                 :                : #define RETRY_CANCEL_TIMEOUT    1000
                                114                 :                : 
                                115                 :                : /*
                                116                 :                :  * Macro for constructing commit command to be sent
                                117                 :                :  *
                                118                 :                :  * We synchronize the read/write mode before committing remote transactions
                                119                 :                :  * so deferred triggers on remote servers can run in the right mode.
                                120                 :                :  */
                                121                 :                : #define CONSTRUCT_COMMIT_COMMAND(sql, entry) \
                                122                 :                :     do { \
                                123                 :                :         if ((read_only_level > 0) && !(entry)->xact_read_only) \
                                124                 :                :             strcpy((sql), "SET TRANSACTION READ ONLY; COMMIT TRANSACTION"); \
                                125                 :                :         else \
                                126                 :                :             strcpy((sql), "COMMIT TRANSACTION"); \
                                127                 :                :     } while(0)
                                128                 :                : 
                                129                 :                : /* Macro for constructing abort command to be sent */
                                130                 :                : #define CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel) \
                                131                 :                :     do { \
                                132                 :                :         if (toplevel) \
                                133                 :                :             snprintf((sql), sizeof(sql), \
                                134                 :                :                      "ABORT TRANSACTION"); \
                                135                 :                :         else \
                                136                 :                :             snprintf((sql), sizeof(sql), \
                                137                 :                :                      "ROLLBACK TO SAVEPOINT s%d; RELEASE SAVEPOINT s%d", \
                                138                 :                :                      (entry)->xact_depth, (entry)->xact_depth); \
                                139                 :                :     } while(0)
                                140                 :                : 
                                141                 :                : /*
                                142                 :                :  * Extension version number, for supporting older extension versions' objects
                                143                 :                :  */
                                144                 :                : enum pgfdwVersion
                                145                 :                : {
                                146                 :                :     PGFDW_V1_1 = 0,
                                147                 :                :     PGFDW_V1_2,
                                148                 :                : };
                                149                 :                : 
                                150                 :                : /*
                                151                 :                :  * SQL functions
                                152                 :                :  */
 2093 fujii@postgresql.org      153                 :CBC           6 : PG_FUNCTION_INFO_V1(postgres_fdw_get_connections);
  808                           154                 :              7 : PG_FUNCTION_INFO_V1(postgres_fdw_get_connections_1_2);
 2085                           155                 :              7 : PG_FUNCTION_INFO_V1(postgres_fdw_disconnect);
                                156                 :              7 : PG_FUNCTION_INFO_V1(postgres_fdw_disconnect_all);
  220 jdavis@postgresql.or      157                 :             14 : PG_FUNCTION_INFO_V1(postgres_fdw_connection);
                                158                 :                : 
                                159                 :                : /* prototypes of private functions */
                                160                 :                : static void make_new_connection(ConnCacheEntry *entry, UserMapping *user);
                                161                 :                : static PGconn *connect_pg_server(ForeignServer *server, UserMapping *user);
                                162                 :                : static void disconnect_pg_server(ConnCacheEntry *entry);
                                163                 :                : static void check_conn_params(const char **keywords, const char **values, UserMapping *user);
                                164                 :                : static void configure_remote_session(PGconn *conn);
                                165                 :                : static void do_sql_command_begin(PGconn *conn, const char *sql);
                                166                 :                : static void do_sql_command_end(PGconn *conn, const char *sql,
                                167                 :                :                                bool consume_input);
                                168                 :                : static void begin_remote_xact(ConnCacheEntry *entry);
                                169                 :                : static void pgfdw_report_internal(int elevel, PGresult *res, PGconn *conn,
                                170                 :                :                                   const char *sql);
                                171                 :                : static void pgfdw_xact_callback(XactEvent event, void *arg);
                                172                 :                : static void pgfdw_subxact_callback(SubXactEvent event,
                                173                 :                :                                    SubTransactionId mySubid,
                                174                 :                :                                    SubTransactionId parentSubid,
                                175                 :                :                                    void *arg);
                                176                 :                : static void pgfdw_inval_callback(Datum arg, SysCacheIdentifier cacheid,
                                177                 :                :                                  uint32 hashvalue);
                                178                 :                : static void pgfdw_reject_incomplete_xact_state_change(ConnCacheEntry *entry);
                                179                 :                : static void pgfdw_reset_xact_state(ConnCacheEntry *entry, bool toplevel);
                                180                 :                : static bool pgfdw_cancel_query(PGconn *conn);
                                181                 :                : static bool pgfdw_cancel_query_begin(PGconn *conn, TimestampTz endtime);
                                182                 :                : static bool pgfdw_cancel_query_end(PGconn *conn, TimestampTz endtime,
                                183                 :                :                                    TimestampTz retrycanceltime,
                                184                 :                :                                    bool consume_input);
                                185                 :                : static bool pgfdw_exec_cleanup_query(PGconn *conn, const char *query,
                                186                 :                :                                      bool ignore_errors);
                                187                 :                : static bool pgfdw_exec_cleanup_query_begin(PGconn *conn, const char *query);
                                188                 :                : static bool pgfdw_exec_cleanup_query_end(PGconn *conn, const char *query,
                                189                 :                :                                          TimestampTz endtime,
                                190                 :                :                                          bool consume_input,
                                191                 :                :                                          bool ignore_errors);
                                192                 :                : static bool pgfdw_get_cleanup_result(PGconn *conn, TimestampTz endtime,
                                193                 :                :                                      TimestampTz retrycanceltime,
                                194                 :                :                                      PGresult **result, bool *timed_out);
                                195                 :                : static void pgfdw_abort_cleanup(ConnCacheEntry *entry, bool toplevel);
                                196                 :                : static bool pgfdw_abort_cleanup_begin(ConnCacheEntry *entry, bool toplevel,
                                197                 :                :                                       List **pending_entries,
                                198                 :                :                                       List **cancel_requested);
                                199                 :                : static void pgfdw_finish_pre_commit_cleanup(List *pending_entries);
                                200                 :                : static void pgfdw_finish_pre_subcommit_cleanup(List *pending_entries,
                                201                 :                :                                                int curlevel);
                                202                 :                : static void pgfdw_finish_abort_cleanup(List *pending_entries,
                                203                 :                :                                        List *cancel_requested,
                                204                 :                :                                        bool toplevel);
                                205                 :                : static void pgfdw_security_check(const char **keywords, const char **values,
                                206                 :                :                                  UserMapping *user, PGconn *conn);
                                207                 :                : static bool UserMappingPasswordRequired(UserMapping *user);
                                208                 :                : static bool UseScramPassthrough(ForeignServer *server, UserMapping *user);
                                209                 :                : static bool disconnect_cached_connections(Oid serverid);
                                210                 :                : static void postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo,
                                211                 :                :                                                   enum pgfdwVersion api_version);
                                212                 :                : static int  pgfdw_conn_check(PGconn *conn);
                                213                 :                : static bool pgfdw_conn_checkable(void);
                                214                 :                : static bool pgfdw_has_required_scram_options(const char **keywords, const char **values);
                                215                 :                : 
                                216                 :                : /*
                                217                 :                :  * Get a PGconn which can be used to execute queries on the remote PostgreSQL
                                218                 :                :  * server with the user's authorization.  A new connection is established
                                219                 :                :  * if we don't already have a suitable one, and a transaction is opened at
                                220                 :                :  * the right subtransaction nesting depth if we didn't do that already.
                                221                 :                :  *
                                222                 :                :  * will_prep_stmt must be true if caller intends to create any prepared
                                223                 :                :  * statements.  Since those don't go away automatically at transaction end
                                224                 :                :  * (not even on error), we need this flag to cue manual cleanup.
                                225                 :                :  *
                                226                 :                :  * If state is not NULL, *state receives the per-connection state associated
                                227                 :                :  * with the PGconn.
                                228                 :                :  */
                                229                 :                : PGconn *
 2021 efujita@postgresql.o      230                 :           2344 : GetConnection(UserMapping *user, bool will_prep_stmt, PgFdwConnState **state)
                                231                 :                : {
                                232                 :                :     bool        found;
 2187 fujii@postgresql.org      233                 :           2344 :     bool        retry = false;
                                234                 :                :     ConnCacheEntry *entry;
                                235                 :                :     ConnCacheKey key;
                                236                 :           2344 :     MemoryContext ccxt = CurrentMemoryContext;
                                237                 :                : 
                                238                 :                :     /* First time through, initialize connection cache hashtable */
 4981 tgl@sss.pgh.pa.us         239         [ +  + ]:           2344 :     if (ConnectionHash == NULL)
                                240                 :                :     {
                                241                 :                :         HASHCTL     ctl;
                                242                 :                : 
 1008 noah@leadboat.com         243         [ +  - ]:             11 :         if (pgfdw_we_get_result == 0)
                                244                 :             11 :             pgfdw_we_get_result =
                                245                 :             11 :                 WaitEventExtensionNew("PostgresFdwGetResult");
                                246                 :                : 
 4981 tgl@sss.pgh.pa.us         247                 :             11 :         ctl.keysize = sizeof(ConnCacheKey);
                                248                 :             11 :         ctl.entrysize = sizeof(ConnCacheEntry);
                                249                 :             11 :         ConnectionHash = hash_create("postgres_fdw connections", 8,
                                250                 :                :                                      &ctl,
                                251                 :                :                                      HASH_ELEM | HASH_BLOBS);
                                252                 :                : 
                                253                 :                :         /*
                                254                 :                :          * Register some callback functions that manage connection cleanup.
                                255                 :                :          * This should be done just once in each backend.
                                256                 :                :          */
                                257                 :             11 :         RegisterXactCallback(pgfdw_xact_callback, NULL);
                                258                 :             11 :         RegisterSubXactCallback(pgfdw_subxact_callback, NULL);
 3370                           259                 :             11 :         CacheRegisterSyscacheCallback(FOREIGNSERVEROID,
                                260                 :                :                                       pgfdw_inval_callback, (Datum) 0);
                                261                 :             11 :         CacheRegisterSyscacheCallback(USERMAPPINGOID,
                                262                 :                :                                       pgfdw_inval_callback, (Datum) 0);
                                263                 :                :     }
                                264                 :                : 
                                265                 :                :     /* Set flag that we did GetConnection during the current transaction */
 4981                           266                 :           2344 :     xact_got_connection = true;
                                267                 :                : 
                                268                 :                :     /* Create hash key for the entry.  Assume no pad bytes in key struct */
 3910 rhaas@postgresql.org      269                 :           2344 :     key = user->umid;
                                270                 :                : 
                                271                 :                :     /*
                                272                 :                :      * Find or create cached entry for requested connection.
                                273                 :                :      */
 4981 tgl@sss.pgh.pa.us         274                 :           2344 :     entry = hash_search(ConnectionHash, &key, HASH_ENTER, &found);
                                275         [ +  + ]:           2344 :     if (!found)
                                276                 :                :     {
                                277                 :                :         /*
                                278                 :                :          * We need only clear "conn" here; remaining fields will be filled
                                279                 :                :          * later when "conn" is set.
                                280                 :                :          */
                                281                 :             22 :         entry->conn = NULL;
                                282                 :                :     }
                                283                 :                : 
                                284                 :                :     /* Reject further use of connections which failed abort cleanup. */
 3414 rhaas@postgresql.org      285                 :           2344 :     pgfdw_reject_incomplete_xact_state_change(entry);
                                286                 :                : 
                                287                 :                :     /*
                                288                 :                :      * If the connection needs to be remade due to invalidation, disconnect as
                                289                 :                :      * soon as we're out of all transactions.
                                290                 :                :      */
 2187 fujii@postgresql.org      291   [ +  +  -  +  :           2342 :     if (entry->conn != NULL && entry->invalidated && entry->xact_depth == 0)
                                              -  - ]
                                292                 :                :     {
 2187 fujii@postgresql.org      293         [ #  # ]:UBC           0 :         elog(DEBUG3, "closing connection %p for option changes to take effect",
                                294                 :                :              entry->conn);
 3370 tgl@sss.pgh.pa.us         295                 :              0 :         disconnect_pg_server(entry);
                                296                 :                :     }
                                297                 :                : 
                                298                 :                :     /*
                                299                 :                :      * If cache entry doesn't have a connection, we have to establish a new
                                300                 :                :      * connection.  (If connect_pg_server throws an error, the cache entry
                                301                 :                :      * will remain in a valid empty state, ie conn == NULL.)
                                302                 :                :      */
 4981 tgl@sss.pgh.pa.us         303         [ +  + ]:CBC        2342 :     if (entry->conn == NULL)
 2187 fujii@postgresql.org      304                 :             87 :         make_new_connection(entry, user);
                                305                 :                : 
                                306                 :                :     /*
                                307                 :                :      * We check the health of the cached connection here when using it.  In
                                308                 :                :      * cases where we're out of all transactions, if a broken connection is
                                309                 :                :      * detected, we try to reestablish a new connection later.
                                310                 :                :      */
 2197                           311         [ +  + ]:           2335 :     PG_TRY();
                                312                 :                :     {
                                313                 :                :         /* Process a pending asynchronous request if any. */
 2021 efujita@postgresql.o      314         [ -  + ]:           2335 :         if (entry->state.pendingAreq)
 2021 efujita@postgresql.o      315                 :UBC           0 :             process_pending_request(entry->state.pendingAreq);
                                316                 :                :         /* Start a new transaction or subtransaction if needed. */
 2197 fujii@postgresql.org      317                 :CBC        2335 :         begin_remote_xact(entry);
                                318                 :                :     }
                                319                 :              2 :     PG_CATCH();
                                320                 :                :     {
 2187                           321                 :              2 :         MemoryContext ecxt = MemoryContextSwitchTo(ccxt);
                                322                 :              2 :         ErrorData  *errdata = CopyErrorData();
                                323                 :                : 
                                324                 :                :         /*
                                325                 :                :          * Determine whether to try to reestablish the connection.
                                326                 :                :          *
                                327                 :                :          * After a broken connection is detected in libpq, any error other
                                328                 :                :          * than connection failure (e.g., out-of-memory) can be thrown
                                329                 :                :          * somewhere between return from libpq and the expected ereport() call
                                330                 :                :          * in pgfdw_report_error(). In this case, since PQstatus() indicates
                                331                 :                :          * CONNECTION_BAD, checking only PQstatus() causes the false detection
                                332                 :                :          * of connection failure. To avoid this, we also verify that the
                                333                 :                :          * error's sqlstate is ERRCODE_CONNECTION_FAILURE. Note that also
                                334                 :                :          * checking only the sqlstate can cause another false detection
                                335                 :                :          * because pgfdw_report_error() may report ERRCODE_CONNECTION_FAILURE
                                336                 :                :          * for any libpq-originated error condition.
                                337                 :                :          */
                                338         [ +  - ]:              2 :         if (errdata->sqlerrcode != ERRCODE_CONNECTION_FAILURE ||
                                339         [ +  - ]:              2 :             PQstatus(entry->conn) != CONNECTION_BAD ||
                                340         [ +  + ]:              2 :             entry->xact_depth > 0)
                                341                 :                :         {
                                342                 :              1 :             MemoryContextSwitchTo(ecxt);
 2197                           343                 :              1 :             PG_RE_THROW();
                                344                 :                :         }
                                345                 :                : 
                                346                 :                :         /* Clean up the error state */
 2187                           347                 :              1 :         FlushErrorState();
                                348                 :              1 :         FreeErrorData(errdata);
                                349                 :              1 :         errdata = NULL;
                                350                 :                : 
                                351                 :              1 :         retry = true;
                                352                 :                :     }
 2197                           353         [ -  + ]:           2334 :     PG_END_TRY();
                                354                 :                : 
                                355                 :                :     /*
                                356                 :                :      * If a broken connection is detected, disconnect it, reestablish a new
                                357                 :                :      * connection and retry a new remote transaction. If connection failure is
                                358                 :                :      * reported again, we give up getting a connection.
                                359                 :                :      */
 2187                           360         [ +  + ]:           2334 :     if (retry)
                                361                 :                :     {
                                362         [ -  + ]:              1 :         Assert(entry->xact_depth == 0);
                                363                 :                : 
 2197                           364         [ -  + ]:              1 :         ereport(DEBUG3,
                                365                 :                :                 (errmsg_internal("could not start remote transaction on connection %p",
                                366                 :                :                                  entry->conn)),
                                367                 :                :                 errdetail_internal("%s", pchomp(PQerrorMessage(entry->conn))));
                                368                 :                : 
 2187                           369         [ -  + ]:              1 :         elog(DEBUG3, "closing connection %p to reestablish a new one",
                                370                 :                :              entry->conn);
                                371                 :              1 :         disconnect_pg_server(entry);
                                372                 :                : 
 1305 efujita@postgresql.o      373                 :              1 :         make_new_connection(entry, user);
                                374                 :                : 
 2187 fujii@postgresql.org      375                 :              1 :         begin_remote_xact(entry);
                                376                 :                :     }
                                377                 :                : 
                                378                 :                :     /* Remember if caller will prepare statements */
 4964 tgl@sss.pgh.pa.us         379                 :           2334 :     entry->have_prep_stmt |= will_prep_stmt;
                                380                 :                : 
                                381                 :                :     /* If caller needs access to the per-connection state, return it. */
 2021 efujita@postgresql.o      382         [ +  + ]:           2334 :     if (state)
                                383                 :            809 :         *state = &entry->state;
                                384                 :                : 
 4981 tgl@sss.pgh.pa.us         385                 :           2334 :     return entry->conn;
                                386                 :                : }
                                387                 :                : 
                                388                 :                : /*
                                389                 :                :  * Reset all transient state fields in the cached connection entry and
                                390                 :                :  * establish new connection to the remote server.
                                391                 :                :  */
                                392                 :                : static void
 2187 fujii@postgresql.org      393                 :             88 : make_new_connection(ConnCacheEntry *entry, UserMapping *user)
                                394                 :                : {
                                395                 :             88 :     ForeignServer *server = GetForeignServer(user->serverid);
                                396                 :                :     ListCell   *lc;
                                397                 :                : 
                                398         [ -  + ]:             88 :     Assert(entry->conn == NULL);
                                399                 :                : 
                                400                 :                :     /* Reset all transient state fields, to be sure all are clean */
                                401                 :             88 :     entry->xact_depth = 0;
  190 efujita@postgresql.o      402                 :             88 :     entry->xact_read_only = false;
 2187 fujii@postgresql.org      403                 :             88 :     entry->have_prep_stmt = false;
                                404                 :             88 :     entry->have_error = false;
                                405                 :             88 :     entry->changing_xact_state = false;
                                406                 :             88 :     entry->invalidated = false;
 2096                           407                 :             88 :     entry->serverid = server->serverid;
 2187                           408                 :             88 :     entry->server_hashvalue =
                                409                 :             88 :         GetSysCacheHashValue1(FOREIGNSERVEROID,
                                410                 :                :                               ObjectIdGetDatum(server->serverid));
                                411                 :             88 :     entry->mapping_hashvalue =
                                412                 :             88 :         GetSysCacheHashValue1(USERMAPPINGOID,
                                413                 :                :                               ObjectIdGetDatum(user->umid));
 2021 efujita@postgresql.o      414                 :             88 :     memset(&entry->state, 0, sizeof(entry->state));
                                415                 :                : 
                                416                 :                :     /*
                                417                 :                :      * Determine whether to keep the connection that we're about to make here
                                418                 :                :      * open even after the transaction using it ends, so that the subsequent
                                419                 :                :      * transactions can re-use it.
                                420                 :                :      *
                                421                 :                :      * By default, all the connections to any foreign servers are kept open.
                                422                 :                :      *
                                423                 :                :      * Also determine whether to commit/abort (sub)transactions opened on the
                                424                 :                :      * remote server in parallel at (sub)transaction end, which is disabled by
                                425                 :                :      * default.
                                426                 :                :      *
                                427                 :                :      * Note: it's enough to determine these only when making a new connection
                                428                 :                :      * because if these settings for it are changed, it will be closed and
                                429                 :                :      * re-made later.
                                430                 :                :      */
 2019 fujii@postgresql.org      431                 :             88 :     entry->keep_connections = true;
 1691 efujita@postgresql.o      432                 :             88 :     entry->parallel_commit = false;
 1285                           433                 :             88 :     entry->parallel_abort = false;
 2019 fujii@postgresql.org      434   [ +  -  +  +  :            413 :     foreach(lc, server->options)
                                              +  + ]
                                435                 :                :     {
                                436                 :            325 :         DefElem    *def = (DefElem *) lfirst(lc);
                                437                 :                : 
                                438         [ +  + ]:            325 :         if (strcmp(def->defname, "keep_connections") == 0)
                                439                 :             18 :             entry->keep_connections = defGetBoolean(def);
 1691 efujita@postgresql.o      440         [ +  + ]:            307 :         else if (strcmp(def->defname, "parallel_commit") == 0)
                                441                 :              2 :             entry->parallel_commit = defGetBoolean(def);
 1285                           442         [ +  + ]:            305 :         else if (strcmp(def->defname, "parallel_abort") == 0)
                                443                 :              2 :             entry->parallel_abort = defGetBoolean(def);
                                444                 :                :     }
                                445                 :                : 
                                446                 :                :     /* Now try to make the connection */
 2187 fujii@postgresql.org      447                 :             88 :     entry->conn = connect_pg_server(server, user);
                                448                 :                : 
                                449         [ -  + ]:             81 :     elog(DEBUG3, "new postgres_fdw connection %p for server \"%s\" (user mapping oid %u, userid %u)",
                                450                 :                :          entry->conn, server->servername, user->umid, user->userid);
                                451                 :             81 : }
                                452                 :                : 
                                453                 :                : /*
                                454                 :                :  * Check that non-superuser has used password or delegated credentials
                                455                 :                :  * to establish connection; otherwise, he's piggybacking on the
                                456                 :                :  * postgres server's user identity. See also dblink_security_check()
                                457                 :                :  * in contrib/dblink and check_conn_params.
                                458                 :                :  */
                                459                 :                : static void
 1278 sfrost@snowman.net        460                 :             83 : pgfdw_security_check(const char **keywords, const char **values, UserMapping *user, PGconn *conn)
                                461                 :                : {
                                462                 :                :     /* Superusers bypass the check */
                                463         [ +  + ]:             83 :     if (superuser_arg(user->userid))
                                464                 :             75 :         return;
                                465                 :                : 
                                466                 :                : #ifdef ENABLE_GSS
                                467                 :                :     /* Connected via GSSAPI with delegated credentials- all good. */
 1241 bruce@momjian.us          468   [ -  +  -  - ]:              8 :     if (PQconnectionUsedGSSAPI(conn) && be_gssapi_get_delegation(MyProcPort))
 1278 sfrost@snowman.net        469                 :UBC           0 :         return;
                                470                 :                : #endif
                                471                 :                : 
                                472                 :                :     /* Ok if superuser set PW required false. */
 1278 sfrost@snowman.net        473         [ +  + ]:CBC           8 :     if (!UserMappingPasswordRequired(user))
                                474                 :              2 :         return;
                                475                 :                : 
                                476                 :                :     /* Connected via PW, with PW required true, and provided non-empty PW. */
                                477         [ +  + ]:              6 :     if (PQconnectionUsedPassword(conn))
                                478                 :                :     {
                                479                 :                :         /* ok if params contain a non-empty password */
                                480         [ +  + ]:             40 :         for (int i = 0; keywords[i] != NULL; i++)
                                481                 :                :         {
                                482   [ -  +  -  - ]:             36 :             if (strcmp(keywords[i], "password") == 0 && values[i][0] != '\0')
 1278 sfrost@snowman.net        483                 :UBC           0 :                 return;
                                484                 :                :         }
                                485                 :                :     }
                                486                 :                : 
                                487                 :                :     /*
                                488                 :                :      * Ok if SCRAM pass-through is being used and all required SCRAM options
                                489                 :                :      * are set correctly. If pgfdw_has_required_scram_options returns true we
                                490                 :                :      * assume that UseScramPassthrough is also true since SCRAM options are
                                491                 :                :      * only set when UseScramPassthrough is enabled.
                                492                 :                :      */
  430 peter@eisentraut.org      493   [ +  -  +  +  :CBC           6 :     if (MyProcPort != NULL && MyProcPort->has_scram_keys && pgfdw_has_required_scram_options(keywords, values))
                                              +  - ]
  567                           494                 :              4 :         return;
                                495                 :                : 
 1278 sfrost@snowman.net        496         [ +  - ]:              2 :     ereport(ERROR,
                                497                 :                :             (errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
                                498                 :                :              errmsg("password or GSSAPI delegated credentials required"),
                                499                 :                :              errdetail("Non-superuser cannot connect if the server does not request a password or use GSSAPI with delegated credentials."),
                                500                 :                :              errhint("Target server's authentication method must be changed or password_required=false set in the user mapping attributes.")));
                                501                 :                : }
                                502                 :                : 
                                503                 :                : /*
                                504                 :                :  * Construct connection params from generic options of ForeignServer and
                                505                 :                :  * UserMapping.  (Some of them might not be libpq options, in which case we'll
                                506                 :                :  * just waste a few array slots.)
                                507                 :                :  */
                                508                 :                : static void
  220 jdavis@postgresql.or      509                 :             95 : construct_connection_params(ForeignServer *server, UserMapping *user,
                                510                 :                :                             const char ***p_keywords, const char ***p_values,
                                511                 :                :                             char **p_appname)
                                512                 :                : {
                                513                 :                :     const char **keywords;
                                514                 :                :     const char **values;
                                515                 :             95 :     char       *appname = NULL;
                                516                 :                :     int         n;
                                517                 :                : 
                                518                 :                :     /*
                                519                 :                :      * Add 4 extra slots for application_name, fallback_application_name,
                                520                 :                :      * client_encoding, end marker, and 3 extra slots for scram keys and
                                521                 :                :      * required scram pass-through options.
                                522                 :                :      */
                                523                 :             95 :     n = list_length(server->options) + list_length(user->options) + 4 + 3;
   56 michael@paquier.xyz       524                 :GNC          95 :     keywords = palloc_array(const char *, n);
                                525                 :             95 :     values = palloc_array(const char *, n);
                                526                 :                : 
  220 jdavis@postgresql.or      527                 :CBC          95 :     n = 0;
                                528                 :            190 :     n += ExtractConnectionOptions(server->options,
                                529                 :             95 :                                   keywords + n, values + n);
                                530                 :            190 :     n += ExtractConnectionOptions(user->options,
                                531                 :             95 :                                   keywords + n, values + n);
                                532                 :                : 
                                533                 :                :     /*
                                534                 :                :      * Use pgfdw_application_name as application_name if set.
                                535                 :                :      *
                                536                 :                :      * PQconnectdbParams() processes the parameter arrays from start to end.
                                537                 :                :      * If any key word is repeated, the last value is used. Therefore note
                                538                 :                :      * that pgfdw_application_name must be added to the arrays after options
                                539                 :                :      * of ForeignServer are, so that it can override application_name set in
                                540                 :                :      * ForeignServer.
                                541                 :                :      */
                                542   [ +  +  +  - ]:             95 :     if (pgfdw_application_name && *pgfdw_application_name != '\0')
                                543                 :                :     {
                                544                 :              1 :         keywords[n] = "application_name";
                                545                 :              1 :         values[n] = pgfdw_application_name;
                                546                 :              1 :         n++;
                                547                 :                :     }
                                548                 :                : 
                                549                 :                :     /*
                                550                 :                :      * Search the parameter arrays to find application_name setting, and
                                551                 :                :      * replace escape sequences in it with status information if found.  The
                                552                 :                :      * arrays are searched backwards because the last value is used if
                                553                 :                :      * application_name is repeatedly set.
                                554                 :                :      */
                                555         [ +  + ]:            250 :     for (int i = n - 1; i >= 0; i--)
                                556                 :                :     {
                                557         [ +  + ]:            183 :         if (strcmp(keywords[i], "application_name") == 0 &&
                                558         [ +  - ]:             28 :             *(values[i]) != '\0')
                                559                 :                :         {
                                560                 :                :             /*
                                561                 :                :              * Use this application_name setting if it's not empty string even
                                562                 :                :              * after any escape sequences in it are replaced.
                                563                 :                :              */
                                564                 :             28 :             appname = process_pgfdw_appname(values[i]);
                                565         [ +  - ]:             28 :             if (appname[0] != '\0')
                                566                 :                :             {
                                567                 :             28 :                 values[i] = appname;
                                568                 :             28 :                 break;
                                569                 :                :             }
                                570                 :                : 
                                571                 :                :             /*
                                572                 :                :              * This empty application_name is not used, so we set values[i] to
                                573                 :                :              * NULL and keep searching the array to find the next one.
                                574                 :                :              */
  220 jdavis@postgresql.or      575                 :UBC           0 :             values[i] = NULL;
                                576                 :              0 :             pfree(appname);
                                577                 :              0 :             appname = NULL;
                                578                 :                :         }
                                579                 :                :     }
                                580                 :                : 
  220 jdavis@postgresql.or      581                 :CBC          95 :     *p_appname = appname;
                                582                 :                : 
                                583                 :                :     /* Use "postgres_fdw" as fallback_application_name */
                                584                 :             95 :     keywords[n] = "fallback_application_name";
                                585                 :             95 :     values[n] = "postgres_fdw";
                                586                 :             95 :     n++;
                                587                 :                : 
                                588                 :                :     /* Set client_encoding so that libpq can convert encoding properly. */
                                589                 :             95 :     keywords[n] = "client_encoding";
                                590                 :             95 :     values[n] = GetDatabaseEncodingName();
                                591                 :             95 :     n++;
                                592                 :                : 
                                593                 :                :     /* Add required SCRAM pass-through connection options if it's enabled. */
                                594   [ +  +  +  +  :             95 :     if (MyProcPort != NULL && MyProcPort->has_scram_keys && UseScramPassthrough(server, user))
                                              +  + ]
                                595                 :                :     {
                                596                 :                :         int         len;
                                597                 :                :         char       *encoded;
                                598                 :                :         int         encoded_len;
                                599                 :                : 
                                600                 :              4 :         keywords[n] = "scram_client_key";
                                601                 :              4 :         len = pg_b64_enc_len(sizeof(MyProcPort->scram_ClientKey));
                                602                 :                :         /* don't forget the zero-terminator */
   55 peter@eisentraut.org      603                 :GNC           4 :         encoded = palloc0(len + 1);
  220 jdavis@postgresql.or      604                 :CBC           4 :         encoded_len = pg_b64_encode(MyProcPort->scram_ClientKey,
                                605                 :                :                                     sizeof(MyProcPort->scram_ClientKey),
                                606                 :                :                                     encoded, len);
                                607         [ -  + ]:              4 :         if (encoded_len < 0)
  220 jdavis@postgresql.or      608         [ #  # ]:UBC           0 :             elog(ERROR, "could not encode SCRAM client key");
   55 peter@eisentraut.org      609                 :GNC           4 :         values[n] = encoded;
 4981 tgl@sss.pgh.pa.us         610                 :CBC           4 :         n++;
                                611                 :                : 
  220 jdavis@postgresql.or      612                 :              4 :         keywords[n] = "scram_server_key";
                                613                 :              4 :         len = pg_b64_enc_len(sizeof(MyProcPort->scram_ServerKey));
                                614                 :                :         /* don't forget the zero-terminator */
   55 peter@eisentraut.org      615                 :GNC           4 :         encoded = palloc0(len + 1);
  220 jdavis@postgresql.or      616                 :CBC           4 :         encoded_len = pg_b64_encode(MyProcPort->scram_ServerKey,
                                617                 :                :                                     sizeof(MyProcPort->scram_ServerKey),
                                618                 :                :                                     encoded, len);
                                619         [ -  + ]:              4 :         if (encoded_len < 0)
  220 jdavis@postgresql.or      620         [ #  # ]:UBC           0 :             elog(ERROR, "could not encode SCRAM server key");
   55 peter@eisentraut.org      621                 :GNC           4 :         values[n] = encoded;
 4981 tgl@sss.pgh.pa.us         622                 :CBC           4 :         n++;
                                623                 :                : 
                                624                 :                :         /*
                                625                 :                :          * Require scram-sha-256 to ensure that no other auth method is used
                                626                 :                :          * when connecting with foreign server.
                                627                 :                :          */
  220 jdavis@postgresql.or      628                 :              4 :         keywords[n] = "require_auth";
                                629                 :              4 :         values[n] = "scram-sha-256";
                                630                 :              4 :         n++;
                                631                 :                :     }
                                632                 :                : 
                                633                 :             95 :     keywords[n] = values[n] = NULL;
                                634                 :                : 
                                635                 :                :     /* Verify the set of connection parameters. */
                                636                 :             95 :     check_conn_params(keywords, values, user);
                                637                 :                : 
                                638                 :             92 :     *p_keywords = keywords;
                                639                 :             92 :     *p_values = values;
                                640                 :             92 : }
                                641                 :                : 
                                642                 :                : /*
                                643                 :                :  * Connect to remote server using specified server and user mapping properties.
                                644                 :                :  */
                                645                 :                : static PGconn *
                                646                 :             88 : connect_pg_server(ForeignServer *server, UserMapping *user)
                                647                 :                : {
                                648                 :             88 :     PGconn     *volatile conn = NULL;
                                649                 :                : 
                                650                 :                :     /*
                                651                 :                :      * Use PG_TRY block to ensure closing connection on error.
                                652                 :                :      */
                                653         [ +  + ]:             88 :     PG_TRY();
                                654                 :                :     {
                                655                 :                :         const char **keywords;
                                656                 :                :         const char **values;
                                657                 :                :         char       *appname;
                                658                 :                :         PGconn     *start_conn;
                                659                 :                : 
                                660                 :             88 :         construct_connection_params(server, user, &keywords, &values, &appname);
                                661                 :                : 
                                662                 :                :         /* first time, allocate or get the custom wait event */
 1103 michael@paquier.xyz       663         [ +  + ]:             85 :         if (pgfdw_we_connect == 0)
                                664                 :             10 :             pgfdw_we_connect = WaitEventExtensionNew("PostgresFdwConnect");
                                665                 :                : 
                                666                 :                :         /* OK to make connection */
                                667                 :                :         start_conn =
  137 fujii@postgresql.org      668                 :             85 :             libpqsrv_connect_params_start(keywords, values,
                                669                 :                :                                            /* expand_dbname = */ false);
  142                           670                 :             85 :         PQsetNoticeReceiver(start_conn, libpqsrv_notice_receiver,
                                671                 :                :                             "received message via remote connection");
                                672                 :             85 :         libpqsrv_connect_complete(start_conn, pgfdw_we_connect);
                                673                 :             85 :         conn = start_conn;
                                674                 :                : 
 4981 tgl@sss.pgh.pa.us         675   [ +  -  +  + ]:             85 :         if (!conn || PQstatus(conn) != CONNECTION_OK)
                                676         [ +  - ]:              2 :             ereport(ERROR,
                                677                 :                :                     (errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
                                678                 :                :                      errmsg("could not connect to server \"%s\"",
                                679                 :                :                             server->servername),
                                680                 :                :                      errdetail_internal("%s", pchomp(PQerrorMessage(conn)))));
                                681                 :                : 
                                682                 :                :         /* Perform post-connection security checks. */
  567 peter@eisentraut.org      683                 :             83 :         pgfdw_security_check(keywords, values, user, conn);
                                684                 :                : 
                                685                 :                :         /* Prepare new session for use */
 4980 tgl@sss.pgh.pa.us         686                 :             81 :         configure_remote_session(conn);
                                687                 :                : 
 1753 fujii@postgresql.org      688         [ +  + ]:             81 :         if (appname != NULL)
                                689                 :             28 :             pfree(appname);
 4981 tgl@sss.pgh.pa.us         690                 :             81 :         pfree(keywords);
                                691                 :             81 :         pfree(values);
                                692                 :                :     }
                                693                 :              7 :     PG_CATCH();
                                694                 :                :     {
 1358 andres@anarazel.de        695                 :              7 :         libpqsrv_disconnect(conn);
 4981 tgl@sss.pgh.pa.us         696                 :              7 :         PG_RE_THROW();
                                697                 :                :     }
                                698         [ -  + ]:             81 :     PG_END_TRY();
                                699                 :                : 
                                700                 :             81 :     return conn;
                                701                 :                : }
                                702                 :                : 
                                703                 :                : /*
                                704                 :                :  * Disconnect any open connection for a connection cache entry.
                                705                 :                :  */
                                706                 :                : static void
 3370                           707                 :             72 : disconnect_pg_server(ConnCacheEntry *entry)
                                708                 :                : {
                                709         [ +  - ]:             72 :     if (entry->conn != NULL)
                                710                 :                :     {
 1358 andres@anarazel.de        711                 :             72 :         libpqsrv_disconnect(entry->conn);
 3370 tgl@sss.pgh.pa.us         712                 :             72 :         entry->conn = NULL;
                                713                 :                :     }
                                714                 :             72 : }
                                715                 :                : 
                                716                 :                : /*
                                717                 :                :  * Check and return the value of password_required, if defined; otherwise,
                                718                 :                :  * return true, which is the default value of it.  The mapping has been
                                719                 :                :  * pre-validated.
                                720                 :                :  */
                                721                 :                : static bool
 2488 andrew@dunslane.net       722                 :             16 : UserMappingPasswordRequired(UserMapping *user)
                                723                 :                : {
                                724                 :                :     ListCell   *cell;
                                725                 :                : 
                                726   [ +  +  +  +  :             29 :     foreach(cell, user->options)
                                              +  + ]
                                727                 :                :     {
                                728                 :             16 :         DefElem    *def = (DefElem *) lfirst(cell);
                                729                 :                : 
                                730         [ +  + ]:             16 :         if (strcmp(def->defname, "password_required") == 0)
                                731                 :              3 :             return defGetBoolean(def);
                                732                 :                :     }
                                733                 :                : 
                                734                 :             13 :     return true;
                                735                 :                : }
                                736                 :                : 
                                737                 :                : /*
                                738                 :                :  * Return whether SCRAM pass-through is enabled.
                                739                 :                :  *
                                740                 :                :  * If use_scram_passthrough is specified in both the foreign server
                                741                 :                :  * and the user mapping, the user mapping setting takes precedence.
                                742                 :                :  */
                                743                 :                : static bool
  635 peter@eisentraut.org      744                 :             13 : UseScramPassthrough(ForeignServer *server, UserMapping *user)
                                745                 :                : {
                                746                 :                :     ListCell   *cell;
                                747                 :                : 
  139 fujii@postgresql.org      748   [ +  +  +  +  :             18 :     foreach(cell, user->options)
                                              +  + ]
                                749                 :                :     {
  635 peter@eisentraut.org      750                 :              7 :         DefElem    *def = (DefElem *) lfirst(cell);
                                751                 :                : 
                                752         [ +  + ]:              7 :         if (strcmp(def->defname, "use_scram_passthrough") == 0)
                                753                 :              2 :             return defGetBoolean(def);
                                754                 :                :     }
                                755                 :                : 
  139 fujii@postgresql.org      756   [ +  -  +  +  :             44 :     foreach(cell, server->options)
                                              +  + ]
                                757                 :                :     {
  635 peter@eisentraut.org      758                 :             37 :         DefElem    *def = (DefElem *) lfirst(cell);
                                759                 :                : 
                                760         [ +  + ]:             37 :         if (strcmp(def->defname, "use_scram_passthrough") == 0)
                                761                 :              4 :             return defGetBoolean(def);
                                762                 :                :     }
                                763                 :                : 
                                764                 :              7 :     return false;
                                765                 :                : }
                                766                 :                : 
                                767                 :                : /*
                                768                 :                :  * For non-superusers, insist that the connstr specify a password or that the
                                769                 :                :  * user provided their own GSSAPI delegated credentials.  This
                                770                 :                :  * prevents a password from being picked up from .pgpass, a service file, the
                                771                 :                :  * environment, etc.  We don't want the postgres user's passwords,
                                772                 :                :  * certificates, etc to be accessible to non-superusers.  (See also
                                773                 :                :  * dblink_connstr_check in contrib/dblink.)
                                774                 :                :  */
                                775                 :                : static void
 3233 rhaas@postgresql.org      776                 :             95 : check_conn_params(const char **keywords, const char **values, UserMapping *user)
                                777                 :                : {
                                778                 :                :     int         i;
                                779                 :                : 
                                780                 :                :     /* no check required if superuser */
                                781         [ +  + ]:             95 :     if (superuser_arg(user->userid))
 4981 tgl@sss.pgh.pa.us         782                 :             84 :         return;
                                783                 :                : 
                                784                 :                : #ifdef ENABLE_GSS
                                785                 :                :     /* ok if the user provided their own delegated credentials */
 1241 bruce@momjian.us          786         [ -  + ]:             11 :     if (be_gssapi_get_delegation(MyProcPort))
 1278 sfrost@snowman.net        787                 :UBC           0 :         return;
                                788                 :                : #endif
                                789                 :                : 
                                790                 :                :     /* ok if params contain a non-empty password */
 4981 tgl@sss.pgh.pa.us         791         [ +  + ]:CBC          71 :     for (i = 0; keywords[i] != NULL; i++)
                                792                 :                :     {
                                793   [ +  +  +  - ]:             63 :         if (strcmp(keywords[i], "password") == 0 && values[i][0] != '\0')
                                794                 :              3 :             return;
                                795                 :                :     }
                                796                 :                : 
                                797                 :                :     /* ok if the superuser explicitly said so at user mapping creation time */
 2488 andrew@dunslane.net       798         [ +  + ]:              8 :     if (!UserMappingPasswordRequired(user))
                                799                 :              1 :         return;
                                800                 :                : 
                                801                 :                :     /*
                                802                 :                :      * Ok if SCRAM pass-through is being used and all required scram options
                                803                 :                :      * are set correctly. If pgfdw_has_required_scram_options returns true we
                                804                 :                :      * assume that UseScramPassthrough is also true since SCRAM options are
                                805                 :                :      * only set when UseScramPassthrough is enabled.
                                806                 :                :      */
  430 peter@eisentraut.org      807   [ +  -  +  +  :              7 :     if (MyProcPort != NULL && MyProcPort->has_scram_keys && pgfdw_has_required_scram_options(keywords, values))
                                              +  + ]
  567                           808                 :              4 :         return;
                                809                 :                : 
 4981 tgl@sss.pgh.pa.us         810         [ +  - ]:              3 :     ereport(ERROR,
                                811                 :                :             (errcode(ERRCODE_S_R_E_PROHIBITED_SQL_STATEMENT_ATTEMPTED),
                                812                 :                :              errmsg("password or GSSAPI delegated credentials required"),
                                813                 :                :              errdetail("Non-superusers must delegate GSSAPI credentials, provide a password, or enable SCRAM pass-through in user mapping.")));
                                814                 :                : }
                                815                 :                : 
                                816                 :                : /*
                                817                 :                :  * Issue SET commands to make sure remote session is configured properly.
                                818                 :                :  *
                                819                 :                :  * We do this just once at connection, assuming nothing will change the
                                820                 :                :  * values later.  Since we'll never send volatile function calls to the
                                821                 :                :  * remote, there shouldn't be any way to break this assumption from our end.
                                822                 :                :  * It's possible to think of ways to break it at the remote end, eg making
                                823                 :                :  * a foreign table point to a view that includes a set_config call ---
                                824                 :                :  * but once you admit the possibility of a malicious view definition,
                                825                 :                :  * there are any number of ways to break things.
                                826                 :                :  */
                                827                 :                : static void
 4980                           828                 :             81 : configure_remote_session(PGconn *conn)
                                829                 :                : {
 4963                           830                 :             81 :     int         remoteversion = PQserverVersion(conn);
                                831                 :                : 
                                832                 :                :     /* Force the search path to contain only pg_catalog (see deparse.c) */
                                833                 :             81 :     do_sql_command(conn, "SET search_path = pg_catalog");
                                834                 :                : 
                                835                 :                :     /*
                                836                 :                :      * Set remote timezone; this is basically just cosmetic, since all
                                837                 :                :      * transmitted and returned timestamptzs should specify a zone explicitly
                                838                 :                :      * anyway.  However it makes the regression test outputs more predictable.
                                839                 :                :      *
                                840                 :                :      * We don't risk setting remote zone equal to ours, since the remote
                                841                 :                :      * server might use a different timezone database.  Instead, use GMT
                                842                 :                :      * (quoted, because very old servers are picky about case).  That's
                                843                 :                :      * guaranteed to work regardless of the remote's timezone database,
                                844                 :                :      * because pg_tzset() hard-wires it (at least in PG 9.2 and later).
                                845                 :                :      */
  904                           846                 :             81 :     do_sql_command(conn, "SET timezone = 'GMT'");
                                847                 :                : 
                                848                 :                :     /*
                                849                 :                :      * Set values needed to ensure unambiguous data output from remote.  (This
                                850                 :                :      * logic should match what pg_dump does.  See also set_transmission_modes
                                851                 :                :      * in postgres_fdw.c.)
                                852                 :                :      */
 4963                           853                 :             81 :     do_sql_command(conn, "SET datestyle = ISO");
                                854         [ +  - ]:             81 :     if (remoteversion >= 80400)
                                855                 :             81 :         do_sql_command(conn, "SET intervalstyle = postgres");
                                856         [ +  - ]:             81 :     if (remoteversion >= 90000)
                                857                 :             81 :         do_sql_command(conn, "SET extra_float_digits = 3");
                                858                 :                :     else
 4963 tgl@sss.pgh.pa.us         859                 :UBC           0 :         do_sql_command(conn, "SET extra_float_digits = 2");
 4963 tgl@sss.pgh.pa.us         860                 :CBC          81 : }
                                861                 :                : 
                                862                 :                : /*
                                863                 :                :  * Convenience subroutine to issue a non-data-returning SQL command to remote
                                864                 :                :  */
                                865                 :                : void
                                866                 :           1987 : do_sql_command(PGconn *conn, const char *sql)
                                867                 :                : {
 1691 efujita@postgresql.o      868                 :           1987 :     do_sql_command_begin(conn, sql);
                                869                 :           1987 :     do_sql_command_end(conn, sql, false);
                                870                 :           1983 : }
                                871                 :                : 
                                872                 :                : static void
                                873                 :           2005 : do_sql_command_begin(PGconn *conn, const char *sql)
                                874                 :                : {
 3414 rhaas@postgresql.org      875         [ -  + ]:           2005 :     if (!PQsendQuery(conn, sql))
  440 tgl@sss.pgh.pa.us         876                 :UBC           0 :         pgfdw_report_error(NULL, conn, sql);
 1691 efujita@postgresql.o      877                 :CBC        2005 : }
                                878                 :                : 
                                879                 :                : static void
                                880                 :           2005 : do_sql_command_end(PGconn *conn, const char *sql, bool consume_input)
                                881                 :                : {
                                882                 :                :     PGresult   *res;
                                883                 :                : 
                                884                 :                :     /*
                                885                 :                :      * If requested, consume whatever data is available from the socket. (Note
                                886                 :                :      * that if all data is available, this allows pgfdw_get_result to call
                                887                 :                :      * PQgetResult without forcing the overhead of WaitLatchOrSocket, which
                                888                 :                :      * would be large compared to the overhead of PQconsumeInput.)
                                889                 :                :      */
                                890   [ +  +  -  + ]:           2005 :     if (consume_input && !PQconsumeInput(conn))
  440 tgl@sss.pgh.pa.us         891                 :UBC           0 :         pgfdw_report_error(NULL, conn, sql);
 1008 noah@leadboat.com         892                 :CBC        2005 :     res = pgfdw_get_result(conn);
 4980 tgl@sss.pgh.pa.us         893         [ +  + ]:           2005 :     if (PQresultStatus(res) != PGRES_COMMAND_OK)
  440                           894                 :              4 :         pgfdw_report_error(res, conn, sql);
 4980                           895                 :           2001 :     PQclear(res);
                                896                 :           2001 : }
                                897                 :                : 
                                898                 :                : /*
                                899                 :                :  * Start remote transaction or subtransaction, if needed.
                                900                 :                :  *
                                901                 :                :  * Note that we always use at least REPEATABLE READ in the remote session.
                                902                 :                :  * This is so that, if a query initiates multiple scans of the same or
                                903                 :                :  * different foreign tables, we will get snapshot-consistent results from
                                904                 :                :  * those scans.  A disadvantage is that we can't provide sane emulation of
                                905                 :                :  * READ COMMITTED behavior --- it would be nice if we had some other way to
                                906                 :                :  * control which remote queries share a snapshot.
                                907                 :                :  *
                                908                 :                :  * Note also that we always start the remote transaction with the same
                                909                 :                :  * read/write and deferrable properties as the local transaction, and start
                                910                 :                :  * the remote subtransaction with the same read/write property as the local
                                911                 :                :  * subtransaction.
                                912                 :                :  */
                                913                 :                : static void
 4981                           914                 :           2336 : begin_remote_xact(ConnCacheEntry *entry)
                                915                 :                : {
                                916                 :           2336 :     int         curlevel = GetCurrentTransactionNestLevel();
                                917                 :                : 
                                918                 :                :     /*
                                919                 :                :      * If the current local (sub)transaction is read-only, set the topmost
                                920                 :                :      * read-only local transaction's nesting level if we haven't yet.
                                921                 :                :      *
                                922                 :                :      * Note: once it's set, it's retained until the topmost read-only local
                                923                 :                :      * transaction is committed/aborted (see pgfdw_xact_callback and
                                924                 :                :      * pgfdw_subxact_callback).
                                925                 :                :      */
  190 efujita@postgresql.o      926         [ +  + ]:           2336 :     if (XactReadOnly)
                                927                 :                :     {
                                928         [ +  + ]:             10 :         if (read_only_level == 0)
                                929                 :              9 :             read_only_level = GetTopReadOnlyTransactionNestLevel();
                                930         [ -  + ]:             10 :         Assert(read_only_level > 0);
                                931                 :                :     }
                                932                 :                :     else
                                933         [ -  + ]:           2326 :         Assert(read_only_level == 0);
                                934                 :                : 
                                935                 :                :     /*
                                936                 :                :      * Start main transaction if we haven't yet; otherwise, change the current
                                937                 :                :      * remote (sub)transaction's read/write mode if needed.
                                938                 :                :      */
 4981 tgl@sss.pgh.pa.us         939         [ +  + ]:           2336 :     if (entry->xact_depth <= 0)
                                940                 :                :     {
                                941                 :                :         /*
                                942                 :                :          * This is the case when we haven't yet started a main transaction.
                                943                 :                :          */
                                944                 :                :         StringInfoData sql;
  190 efujita@postgresql.o      945                 :            808 :         bool        ro = (read_only_level == 1);
                                946                 :                : 
 4981 tgl@sss.pgh.pa.us         947         [ -  + ]:            808 :         elog(DEBUG3, "starting remote transaction on connection %p",
                                948                 :                :              entry->conn);
                                949                 :                : 
  190 efujita@postgresql.o      950                 :            808 :         initStringInfo(&sql);
                                951                 :            808 :         appendStringInfoString(&sql, "START TRANSACTION ISOLATION LEVEL ");
 4981 tgl@sss.pgh.pa.us         952         [ +  + ]:            808 :         if (IsolationIsSerializable())
  190 efujita@postgresql.o      953                 :              3 :             appendStringInfoString(&sql, "SERIALIZABLE");
                                954                 :                :         else
                                955                 :            805 :             appendStringInfoString(&sql, "REPEATABLE READ");
                                956         [ +  + ]:            808 :         if (ro)
                                957                 :              3 :             appendStringInfoString(&sql, " READ ONLY");
    7                           958   [ +  +  +  - ]:            808 :         if (XactDeferrable && PQserverVersion(entry->conn) >= 90100)
  190                           959                 :              2 :             appendStringInfoString(&sql, " DEFERRABLE");
 3414 rhaas@postgresql.org      960                 :            808 :         entry->changing_xact_state = true;
  190 efujita@postgresql.o      961                 :            808 :         do_sql_command(entry->conn, sql.data);
 4981 tgl@sss.pgh.pa.us         962                 :            807 :         entry->xact_depth = 1;
  190 efujita@postgresql.o      963         [ +  + ]:            807 :         if (ro)
                                964                 :                :         {
                                965         [ -  + ]:              3 :             Assert(!entry->xact_read_only);
                                966                 :              3 :             entry->xact_read_only = true;
                                967                 :                :         }
 3414 rhaas@postgresql.org      968                 :            807 :         entry->changing_xact_state = false;
                                969                 :                :     }
  190 efujita@postgresql.o      970         [ +  + ]:           1528 :     else if (!entry->xact_read_only)
                                971                 :                :     {
                                972                 :                :         /*
                                973                 :                :          * The remote (sub)transaction has been opened in read-write mode.
                                974                 :                :          */
                                975   [ +  +  -  + ]:           1527 :         Assert(read_only_level == 0 ||
                                976                 :                :                entry->xact_depth <= read_only_level);
                                977                 :                : 
                                978                 :                :         /*
                                979                 :                :          * If its nesting depth matches read_only_level, it means that the
                                980                 :                :          * local read-write (sub)transaction that started it has changed to
                                981                 :                :          * read-only after that; in which case change it to read-only as well.
                                982                 :                :          * Otherwise, the local (sub)transaction is still read-write, so there
                                983                 :                :          * is no need to do anything.
                                984                 :                :          */
                                985         [ +  + ]:           1527 :         if (entry->xact_depth == read_only_level)
                                986                 :                :         {
                                987                 :              4 :             entry->changing_xact_state = true;
    7                           988                 :              4 :             do_sql_command(entry->conn, "SET TRANSACTION READ ONLY");
  190                           989                 :              4 :             entry->xact_read_only = true;
                                990                 :              4 :             entry->changing_xact_state = false;
                                991                 :                :         }
                                992                 :                :     }
                                993                 :                :     else
                                994                 :                :     {
                                995                 :                :         /*
                                996                 :                :          * The remote (sub)transaction has been opened in read-only mode.
                                997                 :                :          */
                                998   [ +  -  -  + ]:              1 :         Assert(read_only_level > 0 &&
                                999                 :                :                entry->xact_depth >= read_only_level);
                               1000                 :                : 
                               1001                 :                :         /*
                               1002                 :                :          * The local read-only (sub)transaction that started it is guaranteed
                               1003                 :                :          * to be still read-only (see check_transaction_read_only), so there
                               1004                 :                :          * is no need to do anything.
                               1005                 :                :          */
                               1006                 :                :     }
                               1007                 :                : 
                               1008                 :                :     /*
                               1009                 :                :      * If we're in a subtransaction, stack up savepoints to match our level.
                               1010                 :                :      * This ensures we can rollback just the desired effects when a
                               1011                 :                :      * subtransaction aborts.
                               1012                 :                :      */
 4981 tgl@sss.pgh.pa.us        1013         [ +  + ]:           2356 :     while (entry->xact_depth < curlevel)
                               1014                 :                :     {
                               1015                 :                :         StringInfoData sql;
  190 efujita@postgresql.o     1016                 :             22 :         bool        ro = (entry->xact_depth + 1 == read_only_level);
                               1017                 :                : 
                               1018                 :             22 :         initStringInfo(&sql);
                               1019                 :             22 :         appendStringInfo(&sql, "SAVEPOINT s%d", entry->xact_depth + 1);
                               1020         [ +  + ]:             22 :         if (ro)
    7                          1021                 :              2 :             appendStringInfoString(&sql, "; SET TRANSACTION READ ONLY");
 3414 rhaas@postgresql.org     1022                 :             22 :         entry->changing_xact_state = true;
  190 efujita@postgresql.o     1023                 :             22 :         do_sql_command(entry->conn, sql.data);
 4981 tgl@sss.pgh.pa.us        1024                 :             21 :         entry->xact_depth++;
  190 efujita@postgresql.o     1025         [ +  + ]:             21 :         if (ro)
                               1026                 :                :         {
                               1027         [ -  + ]:              2 :             Assert(!entry->xact_read_only);
                               1028                 :              2 :             entry->xact_read_only = true;
                               1029                 :                :         }
 3414 rhaas@postgresql.org     1030                 :             21 :         entry->changing_xact_state = false;
                               1031                 :                :     }
 4981 tgl@sss.pgh.pa.us        1032                 :           2334 : }
                               1033                 :                : 
                               1034                 :                : /*
                               1035                 :                :  * Release connection reference count created by calling GetConnection.
                               1036                 :                :  */
                               1037                 :                : void
                               1038                 :           2259 : ReleaseConnection(PGconn *conn)
                               1039                 :                : {
                               1040                 :                :     /*
                               1041                 :                :      * Currently, we don't actually track connection references because all
                               1042                 :                :      * cleanup is managed on a transaction or subtransaction basis instead. So
                               1043                 :                :      * there's nothing to do here.
                               1044                 :                :      */
                               1045                 :           2259 : }
                               1046                 :                : 
                               1047                 :                : /*
                               1048                 :                :  * Assign a "unique" number for a cursor.
                               1049                 :                :  *
                               1050                 :                :  * These really only need to be unique per connection within a transaction.
                               1051                 :                :  * For the moment we ignore the per-connection point and assign them across
                               1052                 :                :  * all connections in the transaction, but we ask for the connection to be
                               1053                 :                :  * supplied in case we want to refine that.
                               1054                 :                :  *
                               1055                 :                :  * Note that even if wraparound happens in a very long transaction, actual
                               1056                 :                :  * collisions are highly improbable; just be sure to use %u not %d to print.
                               1057                 :                :  */
                               1058                 :                : unsigned int
                               1059                 :            604 : GetCursorNumber(PGconn *conn)
                               1060                 :                : {
                               1061                 :            604 :     return ++cursor_number;
                               1062                 :                : }
                               1063                 :                : 
                               1064                 :                : /*
                               1065                 :                :  * Assign a "unique" number for a prepared statement.
                               1066                 :                :  *
                               1067                 :                :  * This works much like GetCursorNumber, except that we never reset the counter
                               1068                 :                :  * within a session.  That's because we can't be 100% sure we've gotten rid
                               1069                 :                :  * of all prepared statements on all connections, and it's not really worth
                               1070                 :                :  * increasing the risk of prepared-statement name collisions by resetting.
                               1071                 :                :  */
                               1072                 :                : unsigned int
 4964                          1073                 :            190 : GetPrepStmtNumber(PGconn *conn)
                               1074                 :                : {
                               1075                 :            190 :     return ++prep_stmt_number;
                               1076                 :                : }
                               1077                 :                : 
                               1078                 :                : /*
                               1079                 :                :  * Submit a query and wait for the result.
                               1080                 :                :  *
                               1081                 :                :  * Since we don't use non-blocking mode, this can't process interrupts while
                               1082                 :                :  * pushing the query text to the server.  That risk is relatively small, so we
                               1083                 :                :  * ignore that for now.
                               1084                 :                :  *
                               1085                 :                :  * Caller is responsible for the error handling on the result.
                               1086                 :                :  */
                               1087                 :                : PGresult *
 2021 efujita@postgresql.o     1088                 :           4328 : pgfdw_exec_query(PGconn *conn, const char *query, PgFdwConnState *state)
                               1089                 :                : {
                               1090                 :                :     /* First, process a pending asynchronous request, if any. */
                               1091   [ +  +  +  + ]:           4328 :     if (state && state->pendingAreq)
                               1092                 :              5 :         process_pending_request(state->pendingAreq);
                               1093                 :                : 
 3826 rhaas@postgresql.org     1094         [ +  + ]:           4328 :     if (!PQsendQuery(conn, query))
 1008 noah@leadboat.com        1095                 :              1 :         return NULL;
                               1096                 :           4327 :     return pgfdw_get_result(conn);
                               1097                 :                : }
                               1098                 :                : 
                               1099                 :                : /*
                               1100                 :                :  * Wrap libpqsrv_get_result_last(), adding wait event.
                               1101                 :                :  *
                               1102                 :                :  * Caller is responsible for the error handling on the result.
                               1103                 :                :  */
                               1104                 :                : PGresult *
                               1105                 :           8722 : pgfdw_get_result(PGconn *conn)
                               1106                 :                : {
                               1107                 :           8722 :     return libpqsrv_get_result_last(conn, pgfdw_we_get_result);
                               1108                 :                : }
                               1109                 :                : 
                               1110                 :                : /*
                               1111                 :                :  * Report an error we got from the remote server.
                               1112                 :                :  *
                               1113                 :                :  * Callers should use pgfdw_report_error() to throw an error, or use
                               1114                 :                :  * pgfdw_report() for lesser message levels.  (We make this distinction
                               1115                 :                :  * so that pgfdw_report_error() can be marked noreturn.)
                               1116                 :                :  *
                               1117                 :                :  * res: PGresult containing the error (might be NULL)
                               1118                 :                :  * conn: connection we did the query on
                               1119                 :                :  * sql: NULL, or text of remote command we tried to execute
                               1120                 :                :  *
                               1121                 :                :  * If "res" is not NULL, it'll be PQclear'ed here (unless we throw error,
                               1122                 :                :  * in which case memory context cleanup will clear it eventually).
                               1123                 :                :  *
                               1124                 :                :  * Note: callers that choose not to throw ERROR for a remote error are
                               1125                 :                :  * responsible for making sure that the associated ConnCacheEntry gets
                               1126                 :                :  * marked with have_error = true.
                               1127                 :                :  */
                               1128                 :                : void
  440 tgl@sss.pgh.pa.us        1129                 :             27 : pgfdw_report_error(PGresult *res, PGconn *conn, const char *sql)
                               1130                 :                : {
                               1131                 :             27 :     pgfdw_report_internal(ERROR, res, conn, sql);
  440 tgl@sss.pgh.pa.us        1132                 :UBC           0 :     pg_unreachable();
                               1133                 :                : }
                               1134                 :                : 
                               1135                 :                : void
  440 tgl@sss.pgh.pa.us        1136                 :CBC           3 : pgfdw_report(int elevel, PGresult *res, PGconn *conn, const char *sql)
                               1137                 :                : {
                               1138         [ -  + ]:              3 :     Assert(elevel < ERROR);      /* use pgfdw_report_error for that */
                               1139                 :              3 :     pgfdw_report_internal(elevel, res, conn, sql);
                               1140                 :              3 : }
                               1141                 :                : 
                               1142                 :                : static void
                               1143                 :             30 : pgfdw_report_internal(int elevel, PGresult *res, PGconn *conn,
                               1144                 :                :                       const char *sql)
                               1145                 :                : {
  444                          1146                 :             30 :     char       *diag_sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
                               1147                 :             30 :     char       *message_primary = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
                               1148                 :             30 :     char       *message_detail = PQresultErrorField(res, PG_DIAG_MESSAGE_DETAIL);
                               1149                 :             30 :     char       *message_hint = PQresultErrorField(res, PG_DIAG_MESSAGE_HINT);
                               1150                 :             30 :     char       *message_context = PQresultErrorField(res, PG_DIAG_CONTEXT);
                               1151                 :                :     int         sqlstate;
                               1152                 :                : 
                               1153         [ +  + ]:             30 :     if (diag_sqlstate)
                               1154                 :             23 :         sqlstate = MAKE_SQLSTATE(diag_sqlstate[0],
                               1155                 :                :                                  diag_sqlstate[1],
                               1156                 :                :                                  diag_sqlstate[2],
                               1157                 :                :                                  diag_sqlstate[3],
                               1158                 :                :                                  diag_sqlstate[4]);
                               1159                 :                :     else
                               1160                 :              7 :         sqlstate = ERRCODE_CONNECTION_FAILURE;
                               1161                 :                : 
                               1162                 :                :     /*
                               1163                 :                :      * If we don't get a message from the PGresult, try the PGconn.  This is
                               1164                 :                :      * needed because for connection-level failures, PQgetResult may just
                               1165                 :                :      * return NULL, not a PGresult at all.
                               1166                 :                :      */
                               1167         [ +  + ]:             30 :     if (message_primary == NULL)
                               1168                 :              7 :         message_primary = pchomp(PQerrorMessage(conn));
                               1169                 :                : 
                               1170   [ +  -  +  -  :             30 :     ereport(elevel,
                                     +  -  +  +  +  
                                        +  +  +  +  
                                                 - ]
                               1171                 :                :             (errcode(sqlstate),
                               1172                 :                :              (message_primary != NULL && message_primary[0] != '\0') ?
                               1173                 :                :              errmsg_internal("%s", message_primary) :
                               1174                 :                :              errmsg("could not obtain message string for remote error"),
                               1175                 :                :              message_detail ? errdetail_internal("%s", message_detail) : 0,
                               1176                 :                :              message_hint ? errhint("%s", message_hint) : 0,
                               1177                 :                :              message_context ? errcontext("%s", message_context) : 0,
                               1178                 :                :              sql ? errcontext("remote SQL command: %s", sql) : 0));
                               1179                 :              3 :     PQclear(res);
 4981                          1180                 :              3 : }
                               1181                 :                : 
                               1182                 :                : /*
                               1183                 :                :  * pgfdw_xact_callback --- cleanup at main-transaction end.
                               1184                 :                :  *
                               1185                 :                :  * This runs just late enough that it must not enter user-defined code
                               1186                 :                :  * locally.  (Entering such code on the remote side is fine.  Its remote
                               1187                 :                :  * COMMIT TRANSACTION may run deferred triggers.)
                               1188                 :                :  */
                               1189                 :                : static void
                               1190                 :           4514 : pgfdw_xact_callback(XactEvent event, void *arg)
                               1191                 :                : {
                               1192                 :                :     HASH_SEQ_STATUS scan;
                               1193                 :                :     ConnCacheEntry *entry;
 1691 efujita@postgresql.o     1194                 :           4514 :     List       *pending_entries = NIL;
 1285                          1195                 :           4514 :     List       *cancel_requested = NIL;
                               1196                 :                : 
                               1197                 :                :     /* Quick exit if no connections were touched in this transaction. */
 4981 tgl@sss.pgh.pa.us        1198         [ +  + ]:           4514 :     if (!xact_got_connection)
                               1199                 :           3739 :         return;
                               1200                 :                : 
                               1201                 :                :     /*
                               1202                 :                :      * If we are called for pre-commit cleanup, ensure read_only_level is set
                               1203                 :                :      * for later processing.  Note that we need to do this because the local
                               1204                 :                :      * transaction may have become read-only since the last remote operation.
                               1205                 :                :      */
    7 efujita@postgresql.o     1206   [ +  -  +  + ]:            775 :     if (event == XACT_EVENT_PARALLEL_PRE_COMMIT ||
                               1207                 :                :         event == XACT_EVENT_PRE_COMMIT)
                               1208                 :                :     {
                               1209         [ +  + ]:            710 :         if (XactReadOnly)
                               1210                 :                :         {
                               1211         [ +  + ]:              3 :             if (read_only_level == 0)
                               1212                 :              1 :                 read_only_level = 1;
                               1213         [ -  + ]:              3 :             Assert(read_only_level == 1);
                               1214                 :                :         }
                               1215                 :                :         else
                               1216         [ -  + ]:            707 :             Assert(read_only_level == 0);
                               1217                 :                :     }
                               1218                 :                : 
                               1219                 :                :     /*
                               1220                 :                :      * Scan all connection cache entries to find open remote transactions, and
                               1221                 :                :      * close them.
                               1222                 :                :      */
 4981 tgl@sss.pgh.pa.us        1223                 :            775 :     hash_seq_init(&scan, ConnectionHash);
                               1224         [ +  + ]:           4095 :     while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
                               1225                 :                :     {
                               1226                 :                :         PGresult   *res;
                               1227                 :                : 
                               1228                 :                :         /* Ignore cache entry if no open connection right now */
 4634                          1229         [ +  + ]:           3322 :         if (entry->conn == NULL)
 4981                          1230                 :           1929 :             continue;
                               1231                 :                : 
                               1232                 :                :         /* If it has an open remote transaction, try to close it */
 4634                          1233         [ +  + ]:           1393 :         if (entry->xact_depth > 0)
                               1234                 :                :         {
                               1235                 :                :             char        sql[100];
                               1236                 :                : 
                               1237         [ -  + ]:            809 :             elog(DEBUG3, "closing remote transaction on connection %p",
                               1238                 :                :                  entry->conn);
                               1239                 :                : 
                               1240   [ +  +  -  +  :            809 :             switch (event)
                                                 - ]
                               1241                 :                :             {
 4183 rhaas@postgresql.org     1242                 :            747 :                 case XACT_EVENT_PARALLEL_PRE_COMMIT:
                               1243                 :                :                 case XACT_EVENT_PRE_COMMIT:
                               1244                 :                : 
                               1245                 :                :                     /*
                               1246                 :                :                      * If abort cleanup previously failed for this connection,
                               1247                 :                :                      * we can't issue any more commands against it.
                               1248                 :                :                      */
 3414                          1249                 :            747 :                     pgfdw_reject_incomplete_xact_state_change(entry);
                               1250                 :                : 
                               1251                 :                :                     /* Commit all remote transactions during pre-commit */
    7 efujita@postgresql.o     1252   [ +  +  +  + ]:            747 :                     CONSTRUCT_COMMIT_COMMAND(sql, entry);
 3414 rhaas@postgresql.org     1253                 :            747 :                     entry->changing_xact_state = true;
 1691 efujita@postgresql.o     1254         [ +  + ]:            747 :                     if (entry->parallel_commit)
                               1255                 :                :                     {
    7                          1256                 :             16 :                         do_sql_command_begin(entry->conn, sql);
 1691                          1257                 :             16 :                         pending_entries = lappend(pending_entries, entry);
                               1258                 :             20 :                         continue;
                               1259                 :                :                     }
    7                          1260                 :            731 :                     do_sql_command(entry->conn, sql);
                               1261   [ +  +  -  + ]:            730 :                     if ((read_only_level > 0) && !entry->xact_read_only)
    7 efujita@postgresql.o     1262                 :UBC           0 :                         entry->xact_read_only = true;
 3414 rhaas@postgresql.org     1263                 :CBC         730 :                     entry->changing_xact_state = false;
                               1264                 :                : 
                               1265                 :                :                     /*
                               1266                 :                :                      * If there were any errors in subtransactions, and we
                               1267                 :                :                      * made prepared statements, do a DEALLOCATE ALL to make
                               1268                 :                :                      * sure we get rid of all prepared statements. This is
                               1269                 :                :                      * annoying and not terribly bulletproof, but it's
                               1270                 :                :                      * probably not worth trying harder.
                               1271                 :                :                      *
                               1272                 :                :                      * DEALLOCATE ALL only exists in 8.3 and later, so this
                               1273                 :                :                      * constrains how old a server postgres_fdw can
                               1274                 :                :                      * communicate with.  We intentionally ignore errors in
                               1275                 :                :                      * the DEALLOCATE, so that we can hobble along to some
                               1276                 :                :                      * extent with older servers (leaking prepared statements
                               1277                 :                :                      * as we go; but we don't really support update operations
                               1278                 :                :                      * pre-8.3 anyway).
                               1279                 :                :                      */
 4964 tgl@sss.pgh.pa.us        1280   [ +  +  -  + ]:            730 :                     if (entry->have_prep_stmt && entry->have_error)
                               1281                 :                :                     {
 1008 noah@leadboat.com        1282                 :UBC           0 :                         res = pgfdw_exec_query(entry->conn, "DEALLOCATE ALL",
                               1283                 :                :                                                NULL);
 4964 tgl@sss.pgh.pa.us        1284                 :              0 :                         PQclear(res);
                               1285                 :                :                     }
 4964 tgl@sss.pgh.pa.us        1286                 :CBC         730 :                     entry->have_prep_stmt = false;
                               1287                 :            730 :                     entry->have_error = false;
 4634                          1288                 :            730 :                     break;
                               1289                 :              1 :                 case XACT_EVENT_PRE_PREPARE:
                               1290                 :                : 
                               1291                 :                :                     /*
                               1292                 :                :                      * We disallow any remote transactions, since it's not
                               1293                 :                :                      * very reasonable to hold them open until the prepared
                               1294                 :                :                      * transaction is committed.  For the moment, throw error
                               1295                 :                :                      * unconditionally; later we might allow read-only cases.
                               1296                 :                :                      * Note that the error will cause us to come right back
                               1297                 :                :                      * here with event == XACT_EVENT_ABORT, so we'll clean up
                               1298                 :                :                      * the connection state at that point.
                               1299                 :                :                      */
                               1300         [ +  - ]:              1 :                     ereport(ERROR,
                               1301                 :                :                             (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               1302                 :                :                              errmsg("cannot PREPARE a transaction that has operated on postgres_fdw foreign tables")));
                               1303                 :                :                     break;
 4183 rhaas@postgresql.org     1304                 :UBC           0 :                 case XACT_EVENT_PARALLEL_COMMIT:
                               1305                 :                :                 case XACT_EVENT_COMMIT:
                               1306                 :                :                 case XACT_EVENT_PREPARE:
                               1307                 :                :                     /* Pre-commit should have closed the open transaction */
 4634 tgl@sss.pgh.pa.us        1308         [ #  # ]:              0 :                     elog(ERROR, "missed cleaning up connection during pre-commit");
                               1309                 :                :                     break;
 4183 rhaas@postgresql.org     1310                 :CBC          61 :                 case XACT_EVENT_PARALLEL_ABORT:
                               1311                 :                :                 case XACT_EVENT_ABORT:
                               1312                 :                :                     /* Rollback all remote transactions during abort */
 1285 efujita@postgresql.o     1313         [ +  + ]:             61 :                     if (entry->parallel_abort)
                               1314                 :                :                     {
                               1315         [ +  - ]:              4 :                         if (pgfdw_abort_cleanup_begin(entry, true,
                               1316                 :                :                                                       &pending_entries,
                               1317                 :                :                                                       &cancel_requested))
                               1318                 :              4 :                             continue;
                               1319                 :                :                     }
                               1320                 :                :                     else
                               1321                 :             57 :                         pgfdw_abort_cleanup(entry, true);
 4634 tgl@sss.pgh.pa.us        1322                 :             57 :                     break;
                               1323                 :                :             }
                               1324                 :                :         }
                               1325                 :                : 
                               1326                 :                :         /* Reset state to show we're out of a transaction */
 1691 efujita@postgresql.o     1327                 :           1371 :         pgfdw_reset_xact_state(entry, true);
                               1328                 :                :     }
                               1329                 :                : 
                               1330                 :                :     /* If there are any pending connections, finish cleaning them up */
 1285                          1331   [ +  +  -  + ]:            773 :     if (pending_entries || cancel_requested)
                               1332                 :                :     {
                               1333   [ +  -  +  + ]:             15 :         if (event == XACT_EVENT_PARALLEL_PRE_COMMIT ||
                               1334                 :                :             event == XACT_EVENT_PRE_COMMIT)
                               1335                 :                :         {
                               1336         [ -  + ]:             13 :             Assert(cancel_requested == NIL);
                               1337                 :             13 :             pgfdw_finish_pre_commit_cleanup(pending_entries);
                               1338                 :                :         }
                               1339                 :                :         else
                               1340                 :                :         {
                               1341   [ +  -  -  + ]:              2 :             Assert(event == XACT_EVENT_PARALLEL_ABORT ||
                               1342                 :                :                    event == XACT_EVENT_ABORT);
                               1343                 :              2 :             pgfdw_finish_abort_cleanup(pending_entries, cancel_requested,
                               1344                 :                :                                        true);
                               1345                 :                :         }
                               1346                 :                :     }
                               1347                 :                : 
                               1348                 :                :     /*
                               1349                 :                :      * Regardless of the event type, we can now mark ourselves as out of the
                               1350                 :                :      * transaction.  (Note: if we are here during PRE_COMMIT or PRE_PREPARE,
                               1351                 :                :      * this saves a useless scan of the hashtable during COMMIT or PREPARE.)
                               1352                 :                :      */
 4981 tgl@sss.pgh.pa.us        1353                 :            773 :     xact_got_connection = false;
                               1354                 :                : 
                               1355                 :                :     /* Also reset cursor numbering for next transaction */
                               1356                 :            773 :     cursor_number = 0;
                               1357                 :                : 
                               1358                 :                :     /* Likewise for read_only_level */
  190 efujita@postgresql.o     1359                 :            773 :     read_only_level = 0;
                               1360                 :                : }
                               1361                 :                : 
                               1362                 :                : /*
                               1363                 :                :  * pgfdw_subxact_callback --- cleanup at subtransaction end.
                               1364                 :                :  */
                               1365                 :                : static void
 4981 tgl@sss.pgh.pa.us        1366                 :             72 : pgfdw_subxact_callback(SubXactEvent event, SubTransactionId mySubid,
                               1367                 :                :                        SubTransactionId parentSubid, void *arg)
                               1368                 :                : {
                               1369                 :                :     HASH_SEQ_STATUS scan;
                               1370                 :                :     ConnCacheEntry *entry;
                               1371                 :                :     int         curlevel;
 1691 efujita@postgresql.o     1372                 :             72 :     List       *pending_entries = NIL;
 1285                          1373                 :             72 :     List       *cancel_requested = NIL;
                               1374                 :                : 
                               1375                 :                :     /* Nothing to do at subxact start, nor after commit. */
 4981 tgl@sss.pgh.pa.us        1376   [ +  +  +  + ]:             72 :     if (!(event == SUBXACT_EVENT_PRE_COMMIT_SUB ||
                               1377                 :                :           event == SUBXACT_EVENT_ABORT_SUB))
                               1378                 :             43 :         return;
                               1379                 :                : 
                               1380                 :                :     /* Quick exit if no connections were touched in this transaction. */
                               1381         [ -  + ]:             29 :     if (!xact_got_connection)
 4981 tgl@sss.pgh.pa.us        1382                 :UBC           0 :         return;
                               1383                 :                : 
                               1384                 :                :     /*
                               1385                 :                :      * Scan all connection cache entries to find open remote subtransactions
                               1386                 :                :      * of the current level, and close them.
                               1387                 :                :      */
 4981 tgl@sss.pgh.pa.us        1388                 :CBC          29 :     curlevel = GetCurrentTransactionNestLevel();
                               1389                 :             29 :     hash_seq_init(&scan, ConnectionHash);
                               1390         [ +  + ]:            242 :     while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
                               1391                 :                :     {
                               1392                 :                :         char        sql[100];
                               1393                 :                : 
                               1394                 :                :         /*
                               1395                 :                :          * We only care about connections with open remote subtransactions of
                               1396                 :                :          * the current level.
                               1397                 :                :          */
                               1398   [ +  +  +  + ]:            213 :         if (entry->conn == NULL || entry->xact_depth < curlevel)
                               1399                 :            198 :             continue;
                               1400                 :                : 
                               1401         [ -  + ]:             21 :         if (entry->xact_depth > curlevel)
 4981 tgl@sss.pgh.pa.us        1402         [ #  # ]:UBC           0 :             elog(ERROR, "missed cleaning up remote subtransaction at level %d",
                               1403                 :                :                  entry->xact_depth);
                               1404                 :                : 
 4981 tgl@sss.pgh.pa.us        1405         [ +  + ]:CBC          21 :         if (event == SUBXACT_EVENT_PRE_COMMIT_SUB)
                               1406                 :                :         {
                               1407                 :                :             /*
                               1408                 :                :              * If abort cleanup previously failed for this connection, we
                               1409                 :                :              * can't issue any more commands against it.
                               1410                 :                :              */
 3414 rhaas@postgresql.org     1411                 :              7 :             pgfdw_reject_incomplete_xact_state_change(entry);
                               1412                 :                : 
                               1413                 :                :             /* Commit all remote subtransactions during pre-commit */
 4981 tgl@sss.pgh.pa.us        1414                 :              7 :             snprintf(sql, sizeof(sql), "RELEASE SAVEPOINT s%d", curlevel);
 3414 rhaas@postgresql.org     1415                 :              7 :             entry->changing_xact_state = true;
 1691 efujita@postgresql.o     1416         [ +  + ]:              7 :             if (entry->parallel_commit)
                               1417                 :                :             {
                               1418                 :              2 :                 do_sql_command_begin(entry->conn, sql);
                               1419                 :              2 :                 pending_entries = lappend(pending_entries, entry);
                               1420                 :              2 :                 continue;
                               1421                 :                :             }
 4963 tgl@sss.pgh.pa.us        1422                 :              5 :             do_sql_command(entry->conn, sql);
 3414 rhaas@postgresql.org     1423                 :              5 :             entry->changing_xact_state = false;
                               1424                 :                :         }
                               1425                 :                :         else
                               1426                 :                :         {
                               1427                 :                :             /* Rollback all remote subtransactions during abort */
 1285 efujita@postgresql.o     1428         [ +  + ]:             14 :             if (entry->parallel_abort)
                               1429                 :                :             {
                               1430         [ +  - ]:              4 :                 if (pgfdw_abort_cleanup_begin(entry, false,
                               1431                 :                :                                               &pending_entries,
                               1432                 :                :                                               &cancel_requested))
                               1433                 :              4 :                     continue;
                               1434                 :                :             }
                               1435                 :                :             else
                               1436                 :             10 :                 pgfdw_abort_cleanup(entry, false);
                               1437                 :                :         }
                               1438                 :                : 
                               1439                 :                :         /* OK, we're outta that level of subtransaction */
 1691                          1440                 :             15 :         pgfdw_reset_xact_state(entry, false);
                               1441                 :                :     }
                               1442                 :                : 
                               1443                 :                :     /* If there are any pending connections, finish cleaning them up */
 1285                          1444   [ +  +  -  + ]:             29 :     if (pending_entries || cancel_requested)
                               1445                 :                :     {
                               1446         [ +  + ]:              3 :         if (event == SUBXACT_EVENT_PRE_COMMIT_SUB)
                               1447                 :                :         {
                               1448         [ -  + ]:              1 :             Assert(cancel_requested == NIL);
                               1449                 :              1 :             pgfdw_finish_pre_subcommit_cleanup(pending_entries, curlevel);
                               1450                 :                :         }
                               1451                 :                :         else
                               1452                 :                :         {
                               1453         [ -  + ]:              2 :             Assert(event == SUBXACT_EVENT_ABORT_SUB);
                               1454                 :              2 :             pgfdw_finish_abort_cleanup(pending_entries, cancel_requested,
                               1455                 :                :                                        false);
                               1456                 :                :         }
                               1457                 :                :     }
                               1458                 :                : 
                               1459                 :                :     /* If in read_only_level, reset it */
  190                          1460         [ +  + ]:             29 :     if (curlevel == read_only_level)
                               1461                 :              3 :         read_only_level = 0;
                               1462                 :                : }
                               1463                 :                : 
                               1464                 :                : /*
                               1465                 :                :  * Connection invalidation callback function
                               1466                 :                :  *
                               1467                 :                :  * After a change to a pg_foreign_server or pg_user_mapping catalog entry,
                               1468                 :                :  * close connections depending on that entry immediately if current transaction
                               1469                 :                :  * has not used those connections yet. Otherwise, mark those connections as
                               1470                 :                :  * invalid and then make pgfdw_xact_callback() close them at the end of current
                               1471                 :                :  * transaction, since they cannot be closed in the midst of the transaction
                               1472                 :                :  * using them. Closed connections will be remade at the next opportunity if
                               1473                 :                :  * necessary.
                               1474                 :                :  *
                               1475                 :                :  * Although most cache invalidation callbacks blow away all the related stuff
                               1476                 :                :  * regardless of the given hashvalue, connections are expensive enough that
                               1477                 :                :  * it's worth trying to avoid that.
                               1478                 :                :  *
                               1479                 :                :  * NB: We could avoid unnecessary disconnection more strictly by examining
                               1480                 :                :  * individual option values, but it seems too much effort for the gain.
                               1481                 :                :  */
                               1482                 :                : static void
  236 michael@paquier.xyz      1483                 :            188 : pgfdw_inval_callback(Datum arg, SysCacheIdentifier cacheid, uint32 hashvalue)
                               1484                 :                : {
                               1485                 :                :     HASH_SEQ_STATUS scan;
                               1486                 :                :     ConnCacheEntry *entry;
                               1487                 :                : 
 3370 tgl@sss.pgh.pa.us        1488   [ +  +  -  + ]:            188 :     Assert(cacheid == FOREIGNSERVEROID || cacheid == USERMAPPINGOID);
                               1489                 :                : 
                               1490                 :                :     /* ConnectionHash must exist already, if we're registered */
                               1491                 :            188 :     hash_seq_init(&scan, ConnectionHash);
                               1492         [ +  + ]:           1222 :     while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
                               1493                 :                :     {
                               1494                 :                :         /* Ignore invalid entries */
                               1495         [ +  + ]:           1034 :         if (entry->conn == NULL)
                               1496                 :            838 :             continue;
                               1497                 :                : 
                               1498                 :                :         /* hashvalue == 0 means a cache reset, must clear all state */
                               1499   [ +  -  +  + ]:            196 :         if (hashvalue == 0 ||
                               1500                 :            140 :             (cacheid == FOREIGNSERVEROID &&
                               1501   [ +  +  +  + ]:            196 :              entry->server_hashvalue == hashvalue) ||
                               1502                 :             56 :             (cacheid == USERMAPPINGOID &&
                               1503         [ +  + ]:             56 :              entry->mapping_hashvalue == hashvalue))
                               1504                 :                :         {
                               1505                 :                :             /*
                               1506                 :                :              * Close the connection immediately if it's not used yet in this
                               1507                 :                :              * transaction. Otherwise mark it as invalid so that
                               1508                 :                :              * pgfdw_xact_callback() can close it at the end of this
                               1509                 :                :              * transaction.
                               1510                 :                :              */
 2114 fujii@postgresql.org     1511         [ +  + ]:             59 :             if (entry->xact_depth == 0)
                               1512                 :                :             {
                               1513         [ -  + ]:             56 :                 elog(DEBUG3, "discarding connection %p", entry->conn);
                               1514                 :             56 :                 disconnect_pg_server(entry);
                               1515                 :                :             }
                               1516                 :                :             else
                               1517                 :              3 :                 entry->invalidated = true;
                               1518                 :                :         }
                               1519                 :                :     }
 3370 tgl@sss.pgh.pa.us        1520                 :            188 : }
                               1521                 :                : 
                               1522                 :                : /*
                               1523                 :                :  * Raise an error if the given connection cache entry is marked as being
                               1524                 :                :  * in the middle of an xact state change.  This should be called at which no
                               1525                 :                :  * such change is expected to be in progress; if one is found to be in
                               1526                 :                :  * progress, it means that we aborted in the middle of a previous state change
                               1527                 :                :  * and now don't know what the remote transaction state actually is.
                               1528                 :                :  * Such connections can't safely be further used.  Re-establishing the
                               1529                 :                :  * connection would change the snapshot and roll back any writes already
                               1530                 :                :  * performed, so that's not an option, either. Thus, we must abort.
                               1531                 :                :  *
                               1532                 :                :  * Note: there might be open cursors that use the connection, so even if the
                               1533                 :                :  * connection cache entry is marked as such, we will retain it until abort
                               1534                 :                :  * cleanup of the main transaction, to ensure such open cursors can safely
                               1535                 :                :  * refer to the PGconn for the connection.
                               1536                 :                :  */
                               1537                 :                : static void
 3414 rhaas@postgresql.org     1538                 :           3098 : pgfdw_reject_incomplete_xact_state_change(ConnCacheEntry *entry)
                               1539                 :                : {
                               1540                 :                :     ForeignServer *server;
                               1541                 :                : 
                               1542                 :                :     /* nothing to do for inactive entries and entries of sane state */
 3370 tgl@sss.pgh.pa.us        1543   [ +  +  +  + ]:           3098 :     if (entry->conn == NULL || !entry->changing_xact_state)
 3414 rhaas@postgresql.org     1544                 :           3096 :         return;
                               1545                 :                : 
                               1546                 :                :     /* find server name to be shown in the message below */
 2096 fujii@postgresql.org     1547                 :              2 :     server = GetForeignServer(entry->serverid);
                               1548                 :                : 
 3414 rhaas@postgresql.org     1549         [ +  - ]:              2 :     ereport(ERROR,
                               1550                 :                :             (errcode(ERRCODE_CONNECTION_EXCEPTION),
                               1551                 :                :              errmsg("connection to server \"%s\" cannot be used due to abort cleanup failure",
                               1552                 :                :                     server->servername)));
                               1553                 :                : }
                               1554                 :                : 
                               1555                 :                : /*
                               1556                 :                :  * Reset state to show we're out of a (sub)transaction.
                               1557                 :                :  */
                               1558                 :                : static void
 1691 efujita@postgresql.o     1559                 :           1412 : pgfdw_reset_xact_state(ConnCacheEntry *entry, bool toplevel)
                               1560                 :                : {
                               1561         [ +  + ]:           1412 :     if (toplevel)
                               1562                 :                :     {
                               1563                 :                :         /* Reset state to show we're out of a transaction */
                               1564                 :           1391 :         entry->xact_depth = 0;
                               1565                 :                : 
                               1566                 :                :         /* Reset xact r/o state */
  190                          1567                 :           1391 :         entry->xact_read_only = false;
                               1568                 :                : 
                               1569                 :                :         /*
                               1570                 :                :          * If the connection isn't in a good idle state, it is marked as
                               1571                 :                :          * invalid or keep_connections option of its server is disabled, then
                               1572                 :                :          * discard it to recover. Next GetConnection will open a new
                               1573                 :                :          * connection.
                               1574                 :                :          */
 1691                          1575   [ +  +  +  - ]:           2778 :         if (PQstatus(entry->conn) != CONNECTION_OK ||
                               1576                 :           1387 :             PQtransactionStatus(entry->conn) != PQTRANS_IDLE ||
                               1577         [ +  + ]:           1387 :             entry->changing_xact_state ||
                               1578         [ +  + ]:           1386 :             entry->invalidated ||
                               1579         [ +  + ]:           1384 :             !entry->keep_connections)
                               1580                 :                :         {
                               1581         [ -  + ]:              8 :             elog(DEBUG3, "discarding connection %p", entry->conn);
                               1582                 :              8 :             disconnect_pg_server(entry);
                               1583                 :                :         }
                               1584                 :                :     }
                               1585                 :                :     else
                               1586                 :                :     {
                               1587                 :                :         /* Reset state to show we're out of a subtransaction */
                               1588                 :             21 :         entry->xact_depth--;
                               1589                 :                : 
                               1590                 :                :         /* If in read_only_level, reset xact r/o state */
  190                          1591         [ +  + ]:             21 :         if (entry->xact_depth + 1 == read_only_level)
                               1592                 :              4 :             entry->xact_read_only = false;
                               1593                 :                :     }
 1691                          1594                 :           1412 : }
                               1595                 :                : 
                               1596                 :                : /*
                               1597                 :                :  * Cancel the currently-in-progress query (whose query text we do not have)
                               1598                 :                :  * and ignore the result.  Returns true if we successfully cancel the query
                               1599                 :                :  * and discard any pending result, and false if not.
                               1600                 :                :  *
                               1601                 :                :  * It's not a huge problem if we throw an ERROR here, but if we get into error
                               1602                 :                :  * recursion trouble, we'll end up slamming the connection shut, which will
                               1603                 :                :  * necessitate failing the entire toplevel transaction even if subtransactions
                               1604                 :                :  * were used.  Try to use WARNING where we can.
                               1605                 :                :  *
                               1606                 :                :  * XXX: if the query was one sent by fetch_more_data_begin(), we could get the
                               1607                 :                :  * query text from the pendingAreq saved in the per-connection state, then
                               1608                 :                :  * report the query using it.
                               1609                 :                :  */
                               1610                 :                : static bool
 3414 rhaas@postgresql.org     1611                 :              2 : pgfdw_cancel_query(PGconn *conn)
                               1612                 :                : {
  658 tgl@sss.pgh.pa.us        1613                 :              2 :     TimestampTz now = GetCurrentTimestamp();
                               1614                 :                :     TimestampTz endtime;
                               1615                 :                :     TimestampTz retrycanceltime;
                               1616                 :                : 
                               1617                 :                :     /*
                               1618                 :                :      * If it takes too long to cancel the query and discard the result, assume
                               1619                 :                :      * the connection is dead.
                               1620                 :                :      */
                               1621                 :              2 :     endtime = TimestampTzPlusMilliseconds(now, CONNECTION_CLEANUP_TIMEOUT);
                               1622                 :                : 
                               1623                 :                :     /*
                               1624                 :                :      * Also, lose patience and re-issue the cancel request after a little bit.
                               1625                 :                :      * (This serves to close some race conditions.)
                               1626                 :                :      */
                               1627                 :              2 :     retrycanceltime = TimestampTzPlusMilliseconds(now, RETRY_CANCEL_TIMEOUT);
                               1628                 :                : 
  928 alvherre@alvh.no-ip.     1629         [ -  + ]:              2 :     if (!pgfdw_cancel_query_begin(conn, endtime))
 1285 efujita@postgresql.o     1630                 :UBC           0 :         return false;
  658 tgl@sss.pgh.pa.us        1631                 :CBC           2 :     return pgfdw_cancel_query_end(conn, endtime, retrycanceltime, false);
                               1632                 :                : }
                               1633                 :                : 
                               1634                 :                : /*
                               1635                 :                :  * Submit a cancel request to the given connection, waiting only until
                               1636                 :                :  * the given time.
                               1637                 :                :  *
                               1638                 :                :  * We sleep interruptibly until we receive confirmation that the cancel
                               1639                 :                :  * request has been accepted, and if it is, return true; if the timeout
                               1640                 :                :  * lapses without that, or the request fails for whatever reason, return
                               1641                 :                :  * false.
                               1642                 :                :  */
                               1643                 :                : static bool
  928 alvherre@alvh.no-ip.     1644                 :              2 : pgfdw_cancel_query_begin(PGconn *conn, TimestampTz endtime)
                               1645                 :                : {
  920                          1646                 :              2 :     const char *errormsg = libpqsrv_cancel(conn, endtime);
                               1647                 :                : 
  928                          1648         [ -  + ]:              2 :     if (errormsg != NULL)
  928 alvherre@alvh.no-ip.     1649         [ #  # ]:UBC           0 :         ereport(WARNING,
                               1650                 :                :                 errcode(ERRCODE_CONNECTION_FAILURE),
                               1651                 :                :                 errmsg("could not send cancel request: %s", errormsg));
                               1652                 :                : 
  928 alvherre@alvh.no-ip.     1653                 :CBC           2 :     return errormsg == NULL;
                               1654                 :                : }
                               1655                 :                : 
                               1656                 :                : static bool
  658 tgl@sss.pgh.pa.us        1657                 :              2 : pgfdw_cancel_query_end(PGconn *conn, TimestampTz endtime,
                               1658                 :                :                        TimestampTz retrycanceltime, bool consume_input)
                               1659                 :                : {
                               1660                 :                :     PGresult   *result;
                               1661                 :                :     bool        timed_out;
                               1662                 :                : 
                               1663                 :                :     /*
                               1664                 :                :      * If requested, consume whatever data is available from the socket. (Note
                               1665                 :                :      * that if all data is available, this allows pgfdw_get_cleanup_result to
                               1666                 :                :      * call PQgetResult without forcing the overhead of WaitLatchOrSocket,
                               1667                 :                :      * which would be large compared to the overhead of PQconsumeInput.)
                               1668                 :                :      */
 1285 efujita@postgresql.o     1669   [ -  +  -  - ]:              2 :     if (consume_input && !PQconsumeInput(conn))
                               1670                 :                :     {
 1285 efujita@postgresql.o     1671         [ #  # ]:UBC           0 :         ereport(WARNING,
                               1672                 :                :                 (errcode(ERRCODE_CONNECTION_FAILURE),
                               1673                 :                :                  errmsg("could not get result of cancel request: %s",
                               1674                 :                :                         pchomp(PQerrorMessage(conn)))));
                               1675                 :              0 :         return false;
                               1676                 :                :     }
                               1677                 :                : 
                               1678                 :                :     /* Get and discard the result of the query. */
  658 tgl@sss.pgh.pa.us        1679         [ -  + ]:CBC           2 :     if (pgfdw_get_cleanup_result(conn, endtime, retrycanceltime,
                               1680                 :                :                                  &result, &timed_out))
                               1681                 :                :     {
 1769 fujii@postgresql.org     1682         [ #  # ]:UBC           0 :         if (timed_out)
                               1683         [ #  # ]:              0 :             ereport(WARNING,
                               1684                 :                :                     (errmsg("could not get result of cancel request due to timeout")));
                               1685                 :                :         else
                               1686         [ #  # ]:              0 :             ereport(WARNING,
                               1687                 :                :                     (errcode(ERRCODE_CONNECTION_FAILURE),
                               1688                 :                :                      errmsg("could not get result of cancel request: %s",
                               1689                 :                :                             pchomp(PQerrorMessage(conn)))));
                               1690                 :                : 
 3414 rhaas@postgresql.org     1691                 :              0 :         return false;
                               1692                 :                :     }
 3414 rhaas@postgresql.org     1693                 :CBC           2 :     PQclear(result);
                               1694                 :                : 
                               1695                 :              2 :     return true;
                               1696                 :                : }
                               1697                 :                : 
                               1698                 :                : /*
                               1699                 :                :  * Submit a query during (sub)abort cleanup and wait up to 30 seconds for the
                               1700                 :                :  * result.  If the query is executed without error, the return value is true.
                               1701                 :                :  * If the query is executed successfully but returns an error, the return
                               1702                 :                :  * value is true if and only if ignore_errors is set.  If the query can't be
                               1703                 :                :  * sent or times out, the return value is false.
                               1704                 :                :  *
                               1705                 :                :  * It's not a huge problem if we throw an ERROR here, but if we get into error
                               1706                 :                :  * recursion trouble, we'll end up slamming the connection shut, which will
                               1707                 :                :  * necessitate failing the entire toplevel transaction even if subtransactions
                               1708                 :                :  * were used.  Try to use WARNING where we can.
                               1709                 :                :  */
                               1710                 :                : static bool
                               1711                 :             88 : pgfdw_exec_cleanup_query(PGconn *conn, const char *query, bool ignore_errors)
                               1712                 :                : {
                               1713                 :                :     TimestampTz endtime;
                               1714                 :                : 
                               1715                 :                :     /*
                               1716                 :                :      * If it takes too long to execute a cleanup query, assume the connection
                               1717                 :                :      * is dead.  It's fairly likely that this is why we aborted in the first
                               1718                 :                :      * place (e.g. statement timeout, user cancel), so the timeout shouldn't
                               1719                 :                :      * be too long.
                               1720                 :                :      */
 1285 efujita@postgresql.o     1721                 :             88 :     endtime = TimestampTzPlusMilliseconds(GetCurrentTimestamp(),
                               1722                 :                :                                           CONNECTION_CLEANUP_TIMEOUT);
                               1723                 :                : 
                               1724         [ -  + ]:             88 :     if (!pgfdw_exec_cleanup_query_begin(conn, query))
 1285 efujita@postgresql.o     1725                 :UBC           0 :         return false;
 1285 efujita@postgresql.o     1726                 :CBC          88 :     return pgfdw_exec_cleanup_query_end(conn, query, endtime,
                               1727                 :                :                                         false, ignore_errors);
                               1728                 :                : }
                               1729                 :                : 
                               1730                 :                : static bool
                               1731                 :            100 : pgfdw_exec_cleanup_query_begin(PGconn *conn, const char *query)
                               1732                 :                : {
  921                          1733         [ -  + ]:            100 :     Assert(query != NULL);
                               1734                 :                : 
                               1735                 :                :     /*
                               1736                 :                :      * Submit a query.  Since we don't use non-blocking mode, this also can
                               1737                 :                :      * block.  But its risk is relatively small, so we ignore that for now.
                               1738                 :                :      */
 3414 rhaas@postgresql.org     1739         [ -  + ]:            100 :     if (!PQsendQuery(conn, query))
                               1740                 :                :     {
  440 tgl@sss.pgh.pa.us        1741                 :UBC           0 :         pgfdw_report(WARNING, NULL, conn, query);
 3414 rhaas@postgresql.org     1742                 :              0 :         return false;
                               1743                 :                :     }
                               1744                 :                : 
 1285 efujita@postgresql.o     1745                 :CBC         100 :     return true;
                               1746                 :                : }
                               1747                 :                : 
                               1748                 :                : static bool
                               1749                 :            100 : pgfdw_exec_cleanup_query_end(PGconn *conn, const char *query,
                               1750                 :                :                              TimestampTz endtime, bool consume_input,
                               1751                 :                :                              bool ignore_errors)
                               1752                 :                : {
                               1753                 :                :     PGresult   *result;
                               1754                 :                :     bool        timed_out;
                               1755                 :                : 
  921                          1756         [ -  + ]:            100 :     Assert(query != NULL);
                               1757                 :                : 
                               1758                 :                :     /*
                               1759                 :                :      * If requested, consume whatever data is available from the socket. (Note
                               1760                 :                :      * that if all data is available, this allows pgfdw_get_cleanup_result to
                               1761                 :                :      * call PQgetResult without forcing the overhead of WaitLatchOrSocket,
                               1762                 :                :      * which would be large compared to the overhead of PQconsumeInput.)
                               1763                 :                :      */
 1285                          1764   [ +  +  -  + ]:            100 :     if (consume_input && !PQconsumeInput(conn))
                               1765                 :                :     {
  440 tgl@sss.pgh.pa.us        1766                 :UBC           0 :         pgfdw_report(WARNING, NULL, conn, query);
 1285 efujita@postgresql.o     1767                 :              0 :         return false;
                               1768                 :                :     }
                               1769                 :                : 
                               1770                 :                :     /* Get the result of the query. */
  658 tgl@sss.pgh.pa.us        1771         [ +  + ]:CBC         100 :     if (pgfdw_get_cleanup_result(conn, endtime, endtime, &result, &timed_out))
                               1772                 :                :     {
 1769 fujii@postgresql.org     1773         [ -  + ]:              3 :         if (timed_out)
 1769 fujii@postgresql.org     1774         [ #  # ]:UBC           0 :             ereport(WARNING,
                               1775                 :                :                     (errmsg("could not get query result due to timeout"),
                               1776                 :                :                      errcontext("remote SQL command: %s", query)));
                               1777                 :                :         else
  440 tgl@sss.pgh.pa.us        1778                 :CBC           3 :             pgfdw_report(WARNING, NULL, conn, query);
                               1779                 :                : 
 3414 rhaas@postgresql.org     1780                 :              3 :         return false;
                               1781                 :                :     }
                               1782                 :                : 
                               1783                 :                :     /* Issue a warning if not successful. */
                               1784         [ -  + ]:             97 :     if (PQresultStatus(result) != PGRES_COMMAND_OK)
                               1785                 :                :     {
  440 tgl@sss.pgh.pa.us        1786                 :UBC           0 :         pgfdw_report(WARNING, result, conn, query);
 3414 rhaas@postgresql.org     1787                 :              0 :         return ignore_errors;
                               1788                 :                :     }
 3406 tgl@sss.pgh.pa.us        1789                 :CBC          97 :     PQclear(result);
                               1790                 :                : 
 3414 rhaas@postgresql.org     1791                 :             97 :     return true;
                               1792                 :                : }
                               1793                 :                : 
                               1794                 :                : /*
                               1795                 :                :  * Get, during abort cleanup, the result of a query that is in progress.
                               1796                 :                :  * This might be a query that is being interrupted by a cancel request or by
                               1797                 :                :  * transaction abort, or it might be a query that was initiated as part of
                               1798                 :                :  * transaction abort to get the remote side back to the appropriate state.
                               1799                 :                :  *
                               1800                 :                :  * endtime is the time at which we should give up and assume the remote side
                               1801                 :                :  * is dead.  retrycanceltime is the time at which we should issue a fresh
                               1802                 :                :  * cancel request (pass the same value as endtime if this is not wanted).
                               1803                 :                :  *
                               1804                 :                :  * Returns true if the timeout expired or connection trouble occurred,
                               1805                 :                :  * false otherwise.  Sets *result except in case of a true result.
                               1806                 :                :  * Sets *timed_out to true only when the timeout expired.
                               1807                 :                :  */
                               1808                 :                : static bool
  658 tgl@sss.pgh.pa.us        1809                 :            102 : pgfdw_get_cleanup_result(PGconn *conn, TimestampTz endtime,
                               1810                 :                :                          TimestampTz retrycanceltime,
                               1811                 :                :                          PGresult **result,
                               1812                 :                :                          bool *timed_out)
                               1813                 :                : {
  444                          1814                 :            102 :     bool        failed = false;
                               1815                 :            102 :     PGresult   *last_res = NULL;
                               1816                 :            102 :     int         canceldelta = RETRY_CANCEL_TIMEOUT * 2;
                               1817                 :                : 
  658                          1818                 :            102 :     *result = NULL;
 1769 fujii@postgresql.org     1819                 :            102 :     *timed_out = false;
                               1820                 :                :     for (;;)
  444 tgl@sss.pgh.pa.us        1821                 :            114 :     {
                               1822                 :                :         PGresult   *res;
                               1823                 :                : 
                               1824         [ +  + ]:            306 :         while (PQisBusy(conn))
                               1825                 :                :         {
                               1826                 :                :             int         wc;
                               1827                 :             93 :             TimestampTz now = GetCurrentTimestamp();
                               1828                 :                :             long        cur_timeout;
                               1829                 :                : 
                               1830                 :                :             /* If timeout has expired, give up. */
                               1831         [ -  + ]:             93 :             if (now >= endtime)
                               1832                 :                :             {
  444 tgl@sss.pgh.pa.us        1833                 :UBC           0 :                 *timed_out = true;
                               1834                 :              0 :                 failed = true;
                               1835                 :              0 :                 goto exit;
                               1836                 :                :             }
                               1837                 :                : 
                               1838                 :                :             /* If we need to re-issue the cancel request, do that. */
  444 tgl@sss.pgh.pa.us        1839         [ -  + ]:CBC          93 :             if (now >= retrycanceltime)
                               1840                 :                :             {
                               1841                 :                :                 /* We ignore failure to issue the repeated request. */
  444 tgl@sss.pgh.pa.us        1842                 :UBC           0 :                 (void) libpqsrv_cancel(conn, endtime);
                               1843                 :                : 
                               1844                 :                :                 /* Recompute "now" in case that took measurable time. */
                               1845                 :              0 :                 now = GetCurrentTimestamp();
                               1846                 :                : 
                               1847                 :                :                 /* Adjust re-cancel timeout in increasing steps. */
                               1848                 :              0 :                 retrycanceltime = TimestampTzPlusMilliseconds(now,
                               1849                 :                :                                                               canceldelta);
                               1850                 :              0 :                 canceldelta += canceldelta;
                               1851                 :                :             }
                               1852                 :                : 
                               1853                 :                :             /* If timeout has expired, give up, else get sleep time. */
  444 tgl@sss.pgh.pa.us        1854                 :CBC          93 :             cur_timeout = TimestampDifferenceMilliseconds(now,
                               1855                 :                :                                                           Min(endtime,
                               1856                 :                :                                                               retrycanceltime));
                               1857         [ -  + ]:             93 :             if (cur_timeout <= 0)
                               1858                 :                :             {
  444 tgl@sss.pgh.pa.us        1859                 :UBC           0 :                 *timed_out = true;
                               1860                 :              0 :                 failed = true;
                               1861                 :              0 :                 goto exit;
                               1862                 :                :             }
                               1863                 :                : 
                               1864                 :                :             /* first time, allocate or get the custom wait event */
  444 tgl@sss.pgh.pa.us        1865         [ +  + ]:CBC          93 :             if (pgfdw_we_cleanup_result == 0)
                               1866                 :              2 :                 pgfdw_we_cleanup_result = WaitEventExtensionNew("PostgresFdwCleanupResult");
                               1867                 :                : 
                               1868                 :                :             /* Sleep until there's something to do */
                               1869                 :             93 :             wc = WaitLatchOrSocket(MyLatch,
                               1870                 :                :                                    WL_LATCH_SET | WL_SOCKET_READABLE |
                               1871                 :                :                                    WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
                               1872                 :                :                                    PQsocket(conn),
                               1873                 :                :                                    cur_timeout, pgfdw_we_cleanup_result);
                               1874                 :             93 :             ResetLatch(MyLatch);
                               1875                 :                : 
                               1876         [ -  + ]:             93 :             CHECK_FOR_INTERRUPTS();
                               1877                 :                : 
                               1878                 :                :             /* Data available in socket? */
                               1879         [ +  - ]:             93 :             if (wc & WL_SOCKET_READABLE)
                               1880                 :                :             {
                               1881         [ +  + ]:             93 :                 if (!PQconsumeInput(conn))
                               1882                 :                :                 {
                               1883                 :                :                     /* connection trouble */
                               1884                 :              3 :                     failed = true;
                               1885                 :              3 :                     goto exit;
                               1886                 :                :                 }
                               1887                 :                :             }
                               1888                 :                :         }
                               1889                 :                : 
                               1890                 :            213 :         res = PQgetResult(conn);
                               1891         [ +  + ]:            213 :         if (res == NULL)
                               1892                 :             99 :             break;              /* query is complete */
                               1893                 :                : 
                               1894                 :            114 :         PQclear(last_res);
                               1895                 :            114 :         last_res = res;
                               1896                 :                :     }
                               1897                 :            102 : exit:
 1769 fujii@postgresql.org     1898         [ +  + ]:            102 :     if (failed)
 3406 tgl@sss.pgh.pa.us        1899                 :              3 :         PQclear(last_res);
                               1900                 :                :     else
                               1901                 :             99 :         *result = last_res;
 1769 fujii@postgresql.org     1902                 :            102 :     return failed;
                               1903                 :                : }
                               1904                 :                : 
                               1905                 :                : /*
                               1906                 :                :  * Abort remote transaction or subtransaction.
                               1907                 :                :  *
                               1908                 :                :  * "toplevel" should be set to true if toplevel (main) transaction is
                               1909                 :                :  * rollbacked, false otherwise.
                               1910                 :                :  *
                               1911                 :                :  * Set entry->changing_xact_state to false on success, true on failure.
                               1912                 :                :  */
                               1913                 :                : static void
 1662 efujita@postgresql.o     1914                 :             67 : pgfdw_abort_cleanup(ConnCacheEntry *entry, bool toplevel)
                               1915                 :                : {
                               1916                 :                :     char        sql[100];
                               1917                 :                : 
                               1918                 :                :     /*
                               1919                 :                :      * Don't try to clean up the connection if we're already in error
                               1920                 :                :      * recursion trouble.
                               1921                 :                :      */
 1846 fujii@postgresql.org     1922         [ -  + ]:             67 :     if (in_error_recursion_trouble())
 1846 fujii@postgresql.org     1923                 :UBC           0 :         entry->changing_xact_state = true;
                               1924                 :                : 
                               1925                 :                :     /*
                               1926                 :                :      * If connection is already unsalvageable, don't touch it further.
                               1927                 :                :      */
 1846 fujii@postgresql.org     1928         [ +  + ]:CBC          67 :     if (entry->changing_xact_state)
                               1929                 :              7 :         return;
                               1930                 :                : 
                               1931                 :                :     /*
                               1932                 :                :      * Mark this connection as in the process of changing transaction state.
                               1933                 :                :      */
                               1934                 :             63 :     entry->changing_xact_state = true;
                               1935                 :                : 
                               1936                 :                :     /* Assume we might have lost track of prepared statements */
                               1937                 :             63 :     entry->have_error = true;
                               1938                 :                : 
                               1939                 :                :     /*
                               1940                 :                :      * If a command has been submitted to the remote server by using an
                               1941                 :                :      * asynchronous execution function, the command might not have yet
                               1942                 :                :      * completed.  Check to see if a command is still being processed by the
                               1943                 :                :      * remote server, and if so, request cancellation of the command.
                               1944                 :                :      */
                               1945         [ +  + ]:             63 :     if (PQtransactionStatus(entry->conn) == PQTRANS_ACTIVE &&
                               1946         [ -  + ]:              2 :         !pgfdw_cancel_query(entry->conn))
 1846 fujii@postgresql.org     1947                 :UBC           0 :         return;                 /* Unable to cancel running query */
                               1948                 :                : 
 1285 efujita@postgresql.o     1949         [ +  + ]:CBC          63 :     CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel);
 1846 fujii@postgresql.org     1950         [ +  + ]:             63 :     if (!pgfdw_exec_cleanup_query(entry->conn, sql, false))
 1662 efujita@postgresql.o     1951                 :              3 :         return;                 /* Unable to abort remote (sub)transaction */
                               1952                 :                : 
 1846 fujii@postgresql.org     1953         [ +  + ]:             60 :     if (toplevel)
                               1954                 :                :     {
                               1955   [ +  +  +  - ]:             52 :         if (entry->have_prep_stmt && entry->have_error &&
                               1956         [ -  + ]:             25 :             !pgfdw_exec_cleanup_query(entry->conn,
                               1957                 :                :                                       "DEALLOCATE ALL",
                               1958                 :                :                                       true))
 1846 fujii@postgresql.org     1959                 :UBC           0 :             return;             /* Trouble clearing prepared statements */
                               1960                 :                : 
 1846 fujii@postgresql.org     1961                 :CBC          52 :         entry->have_prep_stmt = false;
                               1962                 :             52 :         entry->have_error = false;
                               1963                 :                :     }
                               1964                 :                : 
                               1965                 :                :     /*
                               1966                 :                :      * If pendingAreq of the per-connection state is not NULL, it means that
                               1967                 :                :      * an asynchronous fetch begun by fetch_more_data_begin() was not done
                               1968                 :                :      * successfully and thus the per-connection state was not reset in
                               1969                 :                :      * fetch_more_data(); in that case reset the per-connection state here.
                               1970                 :                :      */
 1725 efujita@postgresql.o     1971         [ +  + ]:             60 :     if (entry->state.pendingAreq)
 1725 efujita@postgresql.o     1972                 :GBC           1 :         memset(&entry->state, 0, sizeof(entry->state));
                               1973                 :                : 
                               1974                 :                :     /* Disarm changing_xact_state if it all worked */
 1846 fujii@postgresql.org     1975                 :CBC          60 :     entry->changing_xact_state = false;
                               1976                 :                : }
                               1977                 :                : 
                               1978                 :                : /*
                               1979                 :                :  * Like pgfdw_abort_cleanup, submit an abort command or cancel request, but
                               1980                 :                :  * don't wait for the result.
                               1981                 :                :  *
                               1982                 :                :  * Returns true if the abort command or cancel request is successfully issued,
                               1983                 :                :  * false otherwise.  If the abort command is successfully issued, the given
                               1984                 :                :  * connection cache entry is appended to *pending_entries.  Otherwise, if the
                               1985                 :                :  * cancel request is successfully issued, it is appended to *cancel_requested.
                               1986                 :                :  */
                               1987                 :                : static bool
 1285 efujita@postgresql.o     1988                 :              8 : pgfdw_abort_cleanup_begin(ConnCacheEntry *entry, bool toplevel,
                               1989                 :                :                           List **pending_entries, List **cancel_requested)
                               1990                 :                : {
                               1991                 :                :     /*
                               1992                 :                :      * Don't try to clean up the connection if we're already in error
                               1993                 :                :      * recursion trouble.
                               1994                 :                :      */
                               1995         [ -  + ]:              8 :     if (in_error_recursion_trouble())
 1285 efujita@postgresql.o     1996                 :UBC           0 :         entry->changing_xact_state = true;
                               1997                 :                : 
                               1998                 :                :     /*
                               1999                 :                :      * If connection is already unsalvageable, don't touch it further.
                               2000                 :                :      */
 1285 efujita@postgresql.o     2001         [ -  + ]:CBC           8 :     if (entry->changing_xact_state)
 1285 efujita@postgresql.o     2002                 :UBC           0 :         return false;
                               2003                 :                : 
                               2004                 :                :     /*
                               2005                 :                :      * Mark this connection as in the process of changing transaction state.
                               2006                 :                :      */
 1285 efujita@postgresql.o     2007                 :CBC           8 :     entry->changing_xact_state = true;
                               2008                 :                : 
                               2009                 :                :     /* Assume we might have lost track of prepared statements */
                               2010                 :              8 :     entry->have_error = true;
                               2011                 :                : 
                               2012                 :                :     /*
                               2013                 :                :      * If a command has been submitted to the remote server by using an
                               2014                 :                :      * asynchronous execution function, the command might not have yet
                               2015                 :                :      * completed.  Check to see if a command is still being processed by the
                               2016                 :                :      * remote server, and if so, request cancellation of the command.
                               2017                 :                :      */
                               2018         [ -  + ]:              8 :     if (PQtransactionStatus(entry->conn) == PQTRANS_ACTIVE)
                               2019                 :                :     {
                               2020                 :                :         TimestampTz endtime;
                               2021                 :                : 
  928 alvherre@alvh.no-ip.     2022                 :UBC           0 :         endtime = TimestampTzPlusMilliseconds(GetCurrentTimestamp(),
                               2023                 :                :                                               CONNECTION_CLEANUP_TIMEOUT);
                               2024         [ #  # ]:              0 :         if (!pgfdw_cancel_query_begin(entry->conn, endtime))
 1285 efujita@postgresql.o     2025                 :              0 :             return false;       /* Unable to cancel running query */
                               2026                 :              0 :         *cancel_requested = lappend(*cancel_requested, entry);
                               2027                 :                :     }
                               2028                 :                :     else
                               2029                 :                :     {
                               2030                 :                :         char        sql[100];
                               2031                 :                : 
 1285 efujita@postgresql.o     2032         [ +  + ]:CBC           8 :         CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel);
                               2033         [ -  + ]:              8 :         if (!pgfdw_exec_cleanup_query_begin(entry->conn, sql))
 1285 efujita@postgresql.o     2034                 :UBC           0 :             return false;       /* Unable to abort remote transaction */
 1285 efujita@postgresql.o     2035                 :CBC           8 :         *pending_entries = lappend(*pending_entries, entry);
                               2036                 :                :     }
                               2037                 :                : 
                               2038                 :              8 :     return true;
                               2039                 :                : }
                               2040                 :                : 
                               2041                 :                : /*
                               2042                 :                :  * Finish pre-commit cleanup of connections on each of which we've sent a
                               2043                 :                :  * COMMIT command to the remote server.
                               2044                 :                :  */
                               2045                 :                : static void
 1691                          2046                 :             13 : pgfdw_finish_pre_commit_cleanup(List *pending_entries)
                               2047                 :                : {
                               2048                 :                :     ConnCacheEntry *entry;
                               2049                 :             13 :     List       *pending_deallocs = NIL;
                               2050                 :                :     ListCell   *lc;
                               2051                 :                : 
                               2052         [ -  + ]:             13 :     Assert(pending_entries);
                               2053                 :                : 
                               2054                 :                :     /*
                               2055                 :                :      * Get the result of the COMMIT command for each of the pending entries
                               2056                 :                :      */
                               2057   [ +  -  +  +  :             29 :     foreach(lc, pending_entries)
                                              +  + ]
                               2058                 :                :     {
                               2059                 :                :         char        sql[100];
                               2060                 :                : 
                               2061                 :             16 :         entry = (ConnCacheEntry *) lfirst(lc);
                               2062                 :                : 
                               2063         [ -  + ]:             16 :         Assert(entry->changing_xact_state);
                               2064                 :                : 
                               2065                 :                :         /*
                               2066                 :                :          * We might already have received the result on the socket, so pass
                               2067                 :                :          * consume_input=true to try to consume it first
                               2068                 :                :          */
    7                          2069   [ -  +  -  - ]:             16 :         CONSTRUCT_COMMIT_COMMAND(sql, entry);
                               2070                 :             16 :         do_sql_command_end(entry->conn, sql, true);
                               2071   [ -  +  -  - ]:             16 :         if ((read_only_level > 0) && !(entry)->xact_read_only)
    7 efujita@postgresql.o     2072                 :UBC           0 :             entry->xact_read_only = true;
 1691 efujita@postgresql.o     2073                 :CBC          16 :         entry->changing_xact_state = false;
                               2074                 :                : 
                               2075                 :                :         /* Do a DEALLOCATE ALL in parallel if needed */
                               2076   [ +  +  +  + ]:             16 :         if (entry->have_prep_stmt && entry->have_error)
                               2077                 :                :         {
                               2078                 :                :             /* Ignore errors (see notes in pgfdw_xact_callback) */
                               2079         [ +  - ]:              2 :             if (PQsendQuery(entry->conn, "DEALLOCATE ALL"))
                               2080                 :                :             {
                               2081                 :              2 :                 pending_deallocs = lappend(pending_deallocs, entry);
                               2082                 :              2 :                 continue;
                               2083                 :                :             }
                               2084                 :                :         }
                               2085                 :             14 :         entry->have_prep_stmt = false;
                               2086                 :             14 :         entry->have_error = false;
                               2087                 :                : 
                               2088                 :             14 :         pgfdw_reset_xact_state(entry, true);
                               2089                 :                :     }
                               2090                 :                : 
                               2091                 :                :     /* No further work if no pending entries */
                               2092         [ +  + ]:             13 :     if (!pending_deallocs)
                               2093                 :             12 :         return;
                               2094                 :                : 
                               2095                 :                :     /*
                               2096                 :                :      * Get the result of the DEALLOCATE command for each of the pending
                               2097                 :                :      * entries
                               2098                 :                :      */
                               2099   [ +  -  +  +  :              3 :     foreach(lc, pending_deallocs)
                                              +  + ]
                               2100                 :                :     {
                               2101                 :                :         PGresult   *res;
                               2102                 :                : 
                               2103                 :              2 :         entry = (ConnCacheEntry *) lfirst(lc);
                               2104                 :                : 
                               2105                 :                :         /* Ignore errors (see notes in pgfdw_xact_callback) */
                               2106         [ +  + ]:              4 :         while ((res = PQgetResult(entry->conn)) != NULL)
                               2107                 :                :         {
                               2108                 :              2 :             PQclear(res);
                               2109                 :                :             /* Stop if the connection is lost (else we'll loop infinitely) */
                               2110         [ -  + ]:              2 :             if (PQstatus(entry->conn) == CONNECTION_BAD)
 1691 efujita@postgresql.o     2111                 :UBC           0 :                 break;
                               2112                 :                :         }
 1691 efujita@postgresql.o     2113                 :CBC           2 :         entry->have_prep_stmt = false;
                               2114                 :              2 :         entry->have_error = false;
                               2115                 :                : 
                               2116                 :              2 :         pgfdw_reset_xact_state(entry, true);
                               2117                 :                :     }
                               2118                 :                : }
                               2119                 :                : 
                               2120                 :                : /*
                               2121                 :                :  * Finish pre-subcommit cleanup of connections on each of which we've sent a
                               2122                 :                :  * RELEASE command to the remote server.
                               2123                 :                :  */
                               2124                 :                : static void
                               2125                 :              1 : pgfdw_finish_pre_subcommit_cleanup(List *pending_entries, int curlevel)
                               2126                 :                : {
                               2127                 :                :     ConnCacheEntry *entry;
                               2128                 :                :     char        sql[100];
                               2129                 :                :     ListCell   *lc;
                               2130                 :                : 
                               2131         [ -  + ]:              1 :     Assert(pending_entries);
                               2132                 :                : 
                               2133                 :                :     /*
                               2134                 :                :      * Get the result of the RELEASE command for each of the pending entries
                               2135                 :                :      */
                               2136                 :              1 :     snprintf(sql, sizeof(sql), "RELEASE SAVEPOINT s%d", curlevel);
                               2137   [ +  -  +  +  :              3 :     foreach(lc, pending_entries)
                                              +  + ]
                               2138                 :                :     {
                               2139                 :              2 :         entry = (ConnCacheEntry *) lfirst(lc);
                               2140                 :                : 
                               2141         [ -  + ]:              2 :         Assert(entry->changing_xact_state);
                               2142                 :                : 
                               2143                 :                :         /*
                               2144                 :                :          * We might already have received the result on the socket, so pass
                               2145                 :                :          * consume_input=true to try to consume it first
                               2146                 :                :          */
                               2147                 :              2 :         do_sql_command_end(entry->conn, sql, true);
                               2148                 :              2 :         entry->changing_xact_state = false;
                               2149                 :                : 
                               2150                 :              2 :         pgfdw_reset_xact_state(entry, false);
                               2151                 :                :     }
                               2152                 :              1 : }
                               2153                 :                : 
                               2154                 :                : /*
                               2155                 :                :  * Finish abort cleanup of connections on each of which we've sent an abort
                               2156                 :                :  * command or cancel request to the remote server.
                               2157                 :                :  */
                               2158                 :                : static void
 1285                          2159                 :              4 : pgfdw_finish_abort_cleanup(List *pending_entries, List *cancel_requested,
                               2160                 :                :                            bool toplevel)
                               2161                 :                : {
                               2162                 :              4 :     List       *pending_deallocs = NIL;
                               2163                 :                :     ListCell   *lc;
                               2164                 :                : 
                               2165                 :                :     /*
                               2166                 :                :      * For each of the pending cancel requests (if any), get and discard the
                               2167                 :                :      * result of the query, and submit an abort command to the remote server.
                               2168                 :                :      */
                               2169         [ -  + ]:              4 :     if (cancel_requested)
                               2170                 :                :     {
 1285 efujita@postgresql.o     2171   [ #  #  #  #  :UBC           0 :         foreach(lc, cancel_requested)
                                              #  # ]
                               2172                 :                :         {
                               2173                 :              0 :             ConnCacheEntry *entry = (ConnCacheEntry *) lfirst(lc);
  658 tgl@sss.pgh.pa.us        2174                 :              0 :             TimestampTz now = GetCurrentTimestamp();
                               2175                 :                :             TimestampTz endtime;
                               2176                 :                :             TimestampTz retrycanceltime;
                               2177                 :                :             char        sql[100];
                               2178                 :                : 
 1285 efujita@postgresql.o     2179         [ #  # ]:              0 :             Assert(entry->changing_xact_state);
                               2180                 :                : 
                               2181                 :                :             /*
                               2182                 :                :              * Set end time.  You might think we should do this before issuing
                               2183                 :                :              * cancel request like in normal mode, but that is problematic,
                               2184                 :                :              * because if, for example, it took longer than 30 seconds to
                               2185                 :                :              * process the first few entries in the cancel_requested list, it
                               2186                 :                :              * would cause a timeout error when processing each of the
                               2187                 :                :              * remaining entries in the list, leading to slamming that entry's
                               2188                 :                :              * connection shut.
                               2189                 :                :              */
  658 tgl@sss.pgh.pa.us        2190                 :              0 :             endtime = TimestampTzPlusMilliseconds(now,
                               2191                 :                :                                                   CONNECTION_CLEANUP_TIMEOUT);
                               2192                 :              0 :             retrycanceltime = TimestampTzPlusMilliseconds(now,
                               2193                 :                :                                                           RETRY_CANCEL_TIMEOUT);
                               2194                 :                : 
                               2195         [ #  # ]:              0 :             if (!pgfdw_cancel_query_end(entry->conn, endtime,
                               2196                 :                :                                         retrycanceltime, true))
                               2197                 :                :             {
                               2198                 :                :                 /* Unable to cancel running query */
 1285 efujita@postgresql.o     2199                 :              0 :                 pgfdw_reset_xact_state(entry, toplevel);
                               2200                 :              0 :                 continue;
                               2201                 :                :             }
                               2202                 :                : 
                               2203                 :                :             /* Send an abort command in parallel if needed */
                               2204         [ #  # ]:              0 :             CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel);
                               2205         [ #  # ]:              0 :             if (!pgfdw_exec_cleanup_query_begin(entry->conn, sql))
                               2206                 :                :             {
                               2207                 :                :                 /* Unable to abort remote (sub)transaction */
                               2208                 :              0 :                 pgfdw_reset_xact_state(entry, toplevel);
                               2209                 :                :             }
                               2210                 :                :             else
                               2211                 :              0 :                 pending_entries = lappend(pending_entries, entry);
                               2212                 :                :         }
                               2213                 :                :     }
                               2214                 :                : 
                               2215                 :                :     /* No further work if no pending entries */
 1285 efujita@postgresql.o     2216         [ -  + ]:CBC           4 :     if (!pending_entries)
 1285 efujita@postgresql.o     2217                 :UBC           0 :         return;
                               2218                 :                : 
                               2219                 :                :     /*
                               2220                 :                :      * Get the result of the abort command for each of the pending entries
                               2221                 :                :      */
 1285 efujita@postgresql.o     2222   [ +  -  +  +  :CBC          12 :     foreach(lc, pending_entries)
                                              +  + ]
                               2223                 :                :     {
                               2224                 :              8 :         ConnCacheEntry *entry = (ConnCacheEntry *) lfirst(lc);
                               2225                 :                :         TimestampTz endtime;
                               2226                 :                :         char        sql[100];
                               2227                 :                : 
                               2228         [ -  + ]:              8 :         Assert(entry->changing_xact_state);
                               2229                 :                : 
                               2230                 :                :         /*
                               2231                 :                :          * Set end time.  We do this now, not before issuing the command like
                               2232                 :                :          * in normal mode, for the same reason as for the cancel_requested
                               2233                 :                :          * entries.
                               2234                 :                :          */
                               2235                 :              8 :         endtime = TimestampTzPlusMilliseconds(GetCurrentTimestamp(),
                               2236                 :                :                                               CONNECTION_CLEANUP_TIMEOUT);
                               2237                 :                : 
                               2238         [ +  + ]:              8 :         CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel);
                               2239         [ -  + ]:              8 :         if (!pgfdw_exec_cleanup_query_end(entry->conn, sql, endtime,
                               2240                 :                :                                           true, false))
                               2241                 :                :         {
                               2242                 :                :             /* Unable to abort remote (sub)transaction */
 1285 efujita@postgresql.o     2243                 :UBC           0 :             pgfdw_reset_xact_state(entry, toplevel);
 1285 efujita@postgresql.o     2244                 :CBC           4 :             continue;
                               2245                 :                :         }
                               2246                 :                : 
                               2247         [ +  + ]:              8 :         if (toplevel)
                               2248                 :                :         {
                               2249                 :                :             /* Do a DEALLOCATE ALL in parallel if needed */
                               2250   [ +  -  +  - ]:              4 :             if (entry->have_prep_stmt && entry->have_error)
                               2251                 :                :             {
                               2252         [ -  + ]:              4 :                 if (!pgfdw_exec_cleanup_query_begin(entry->conn,
                               2253                 :                :                                                     "DEALLOCATE ALL"))
                               2254                 :                :                 {
                               2255                 :                :                     /* Trouble clearing prepared statements */
 1285 efujita@postgresql.o     2256                 :UBC           0 :                     pgfdw_reset_xact_state(entry, toplevel);
                               2257                 :                :                 }
                               2258                 :                :                 else
 1285 efujita@postgresql.o     2259                 :CBC           4 :                     pending_deallocs = lappend(pending_deallocs, entry);
                               2260                 :              4 :                 continue;
                               2261                 :                :             }
 1285 efujita@postgresql.o     2262                 :UBC           0 :             entry->have_prep_stmt = false;
                               2263                 :              0 :             entry->have_error = false;
                               2264                 :                :         }
                               2265                 :                : 
                               2266                 :                :         /* Reset the per-connection state if needed */
 1285 efujita@postgresql.o     2267         [ -  + ]:CBC           4 :         if (entry->state.pendingAreq)
 1285 efujita@postgresql.o     2268                 :UBC           0 :             memset(&entry->state, 0, sizeof(entry->state));
                               2269                 :                : 
                               2270                 :                :         /* We're done with this entry; unset the changing_xact_state flag */
 1285 efujita@postgresql.o     2271                 :CBC           4 :         entry->changing_xact_state = false;
                               2272                 :              4 :         pgfdw_reset_xact_state(entry, toplevel);
                               2273                 :                :     }
                               2274                 :                : 
                               2275                 :                :     /* No further work if no pending entries */
                               2276         [ +  + ]:              4 :     if (!pending_deallocs)
                               2277                 :              2 :         return;
                               2278         [ -  + ]:              2 :     Assert(toplevel);
                               2279                 :                : 
                               2280                 :                :     /*
                               2281                 :                :      * Get the result of the DEALLOCATE command for each of the pending
                               2282                 :                :      * entries
                               2283                 :                :      */
                               2284   [ +  -  +  +  :              6 :     foreach(lc, pending_deallocs)
                                              +  + ]
                               2285                 :                :     {
                               2286                 :              4 :         ConnCacheEntry *entry = (ConnCacheEntry *) lfirst(lc);
                               2287                 :                :         TimestampTz endtime;
                               2288                 :                : 
                               2289         [ -  + ]:              4 :         Assert(entry->changing_xact_state);
                               2290         [ -  + ]:              4 :         Assert(entry->have_prep_stmt);
                               2291         [ -  + ]:              4 :         Assert(entry->have_error);
                               2292                 :                : 
                               2293                 :                :         /*
                               2294                 :                :          * Set end time.  We do this now, not before issuing the command like
                               2295                 :                :          * in normal mode, for the same reason as for the cancel_requested
                               2296                 :                :          * entries.
                               2297                 :                :          */
                               2298                 :              4 :         endtime = TimestampTzPlusMilliseconds(GetCurrentTimestamp(),
                               2299                 :                :                                               CONNECTION_CLEANUP_TIMEOUT);
                               2300                 :                : 
                               2301         [ -  + ]:              4 :         if (!pgfdw_exec_cleanup_query_end(entry->conn, "DEALLOCATE ALL",
                               2302                 :                :                                           endtime, true, true))
                               2303                 :                :         {
                               2304                 :                :             /* Trouble clearing prepared statements */
 1285 efujita@postgresql.o     2305                 :UBC           0 :             pgfdw_reset_xact_state(entry, toplevel);
                               2306                 :              0 :             continue;
                               2307                 :                :         }
 1285 efujita@postgresql.o     2308                 :CBC           4 :         entry->have_prep_stmt = false;
                               2309                 :              4 :         entry->have_error = false;
                               2310                 :                : 
                               2311                 :                :         /* Reset the per-connection state if needed */
                               2312         [ -  + ]:              4 :         if (entry->state.pendingAreq)
 1285 efujita@postgresql.o     2313                 :UBC           0 :             memset(&entry->state, 0, sizeof(entry->state));
                               2314                 :                : 
                               2315                 :                :         /* We're done with this entry; unset the changing_xact_state flag */
 1285 efujita@postgresql.o     2316                 :CBC           4 :         entry->changing_xact_state = false;
                               2317                 :              4 :         pgfdw_reset_xact_state(entry, toplevel);
                               2318                 :                :     }
                               2319                 :                : }
                               2320                 :                : 
                               2321                 :                : /* Number of output arguments (columns) for various API versions */
                               2322                 :                : #define POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_1  2
                               2323                 :                : #define POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_2  6
                               2324                 :                : #define POSTGRES_FDW_GET_CONNECTIONS_COLS   6   /* maximum of above */
                               2325                 :                : 
                               2326                 :                : /*
                               2327                 :                :  * Internal function used by postgres_fdw_get_connections variants.
                               2328                 :                :  *
                               2329                 :                :  * For API version 1.1, this function takes no input parameter and
                               2330                 :                :  * returns a set of records with the following values:
                               2331                 :                :  *
                               2332                 :                :  * - server_name - server name of active connection. In case the foreign server
                               2333                 :                :  *   is dropped but still the connection is active, then the server name will
                               2334                 :                :  *   be NULL in output.
                               2335                 :                :  * - valid - true/false representing whether the connection is valid or not.
                               2336                 :                :  *   Note that connections can become invalid in pgfdw_inval_callback.
                               2337                 :                :  *
                               2338                 :                :  * For API version 1.2 and later, this function takes an input parameter
                               2339                 :                :  * to check a connection status and returns the following
                               2340                 :                :  * additional values along with the four values from version 1.1:
                               2341                 :                :  *
                               2342                 :                :  * - user_name - the local user name of the active connection. In case the
                               2343                 :                :  *   user mapping is dropped but the connection is still active, then the
                               2344                 :                :  *   user name will be NULL in the output.
                               2345                 :                :  * - used_in_xact - true if the connection is used in the current transaction.
                               2346                 :                :  * - closed - true if the connection is closed.
                               2347                 :                :  * - remote_backend_pid - process ID of the remote backend, on the foreign
                               2348                 :                :  *   server, handling the connection.
                               2349                 :                :  *
                               2350                 :                :  * No records are returned when there are no cached connections at all.
                               2351                 :                :  */
                               2352                 :                : static void
  808 fujii@postgresql.org     2353                 :             14 : postgres_fdw_get_connections_internal(FunctionCallInfo fcinfo,
                               2354                 :                :                                       enum pgfdwVersion api_version)
                               2355                 :                : {
 2093                          2356                 :             14 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
                               2357                 :                :     HASH_SEQ_STATUS scan;
                               2358                 :                :     ConnCacheEntry *entry;
                               2359                 :                : 
 1455 michael@paquier.xyz      2360                 :             14 :     InitMaterializedSRF(fcinfo, 0);
                               2361                 :                : 
                               2362                 :                :     /* If cache doesn't exist, we return no records */
 2093 fujii@postgresql.org     2363         [ -  + ]:             14 :     if (!ConnectionHash)
  808 fujii@postgresql.org     2364                 :UBC           0 :         return;
                               2365                 :                : 
                               2366                 :                :     /* Check we have the expected number of output arguments */
  808 fujii@postgresql.org     2367      [ -  +  - ]:CBC          14 :     switch (rsinfo->setDesc->natts)
                               2368                 :                :     {
  808 fujii@postgresql.org     2369                 :UBC           0 :         case POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_1:
                               2370         [ #  # ]:              0 :             if (api_version != PGFDW_V1_1)
                               2371         [ #  # ]:              0 :                 elog(ERROR, "incorrect number of output arguments");
                               2372                 :              0 :             break;
  808 fujii@postgresql.org     2373                 :CBC          14 :         case POSTGRES_FDW_GET_CONNECTIONS_COLS_V1_2:
                               2374         [ -  + ]:             14 :             if (api_version != PGFDW_V1_2)
  808 fujii@postgresql.org     2375         [ #  # ]:UBC           0 :                 elog(ERROR, "incorrect number of output arguments");
  808 fujii@postgresql.org     2376                 :CBC          14 :             break;
  808 fujii@postgresql.org     2377                 :UBC           0 :         default:
                               2378         [ #  # ]:              0 :             elog(ERROR, "incorrect number of output arguments");
                               2379                 :                :     }
                               2380                 :                : 
 2093 fujii@postgresql.org     2381                 :CBC          14 :     hash_seq_init(&scan, ConnectionHash);
                               2382         [ +  + ]:            123 :     while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
                               2383                 :                :     {
                               2384                 :                :         ForeignServer *server;
 1549 peter@eisentraut.org     2385                 :            109 :         Datum       values[POSTGRES_FDW_GET_CONNECTIONS_COLS] = {0};
                               2386                 :            109 :         bool        nulls[POSTGRES_FDW_GET_CONNECTIONS_COLS] = {0};
  754 fujii@postgresql.org     2387                 :            109 :         int         i = 0;
                               2388                 :                : 
                               2389                 :                :         /* We only look for open remote connections */
 2093                          2390         [ +  + ]:            109 :         if (!entry->conn)
                               2391                 :             95 :             continue;
                               2392                 :                : 
                               2393                 :             14 :         server = GetForeignServerExtended(entry->serverid, FSV_MISSING_OK);
                               2394                 :                : 
                               2395                 :                :         /*
                               2396                 :                :          * The foreign server may have been dropped in current explicit
                               2397                 :                :          * transaction. It is not possible to drop the server from another
                               2398                 :                :          * session when the connection associated with it is in use in the
                               2399                 :                :          * current transaction, if tried so, the drop query in another session
                               2400                 :                :          * blocks until the current transaction finishes.
                               2401                 :                :          *
                               2402                 :                :          * Even though the server is dropped in the current transaction, the
                               2403                 :                :          * cache can still have associated active connection entry, say we
                               2404                 :                :          * call such connections dangling. Since we can not fetch the server
                               2405                 :                :          * name from system catalogs for dangling connections, instead we show
                               2406                 :                :          * NULL value for server name in output.
                               2407                 :                :          *
                               2408                 :                :          * We could have done better by storing the server name in the cache
                               2409                 :                :          * entry instead of server oid so that it could be used in the output.
                               2410                 :                :          * But the server name in each cache entry requires 64 bytes of
                               2411                 :                :          * memory, which is huge, when there are many cached connections and
                               2412                 :                :          * the use case i.e. dropping the foreign server within the explicit
                               2413                 :                :          * current transaction seems rare. So, we chose to show NULL value for
                               2414                 :                :          * server name in output.
                               2415                 :                :          *
                               2416                 :                :          * Such dangling connections get closed either in next use or at the
                               2417                 :                :          * end of current explicit transaction in pgfdw_xact_callback.
                               2418                 :                :          */
                               2419         [ +  + ]:             14 :         if (!server)
                               2420                 :                :         {
                               2421                 :                :             /*
                               2422                 :                :              * If the server has been dropped in the current explicit
                               2423                 :                :              * transaction, then this entry would have been invalidated in
                               2424                 :                :              * pgfdw_inval_callback at the end of drop server command. Note
                               2425                 :                :              * that this connection would not have been closed in
                               2426                 :                :              * pgfdw_inval_callback because it is still being used in the
                               2427                 :                :              * current explicit transaction. So, assert that here.
                               2428                 :                :              */
                               2429   [ +  -  +  -  :              1 :             Assert(entry->conn && entry->xact_depth > 0 && entry->invalidated);
                                              -  + ]
                               2430                 :                : 
                               2431                 :                :             /* Show null, if no server name was found */
  754                          2432                 :              1 :             nulls[i++] = true;
                               2433                 :                :         }
                               2434                 :                :         else
                               2435                 :             13 :             values[i++] = CStringGetTextDatum(server->servername);
                               2436                 :                : 
                               2437         [ +  - ]:             14 :         if (api_version >= PGFDW_V1_2)
                               2438                 :                :         {
                               2439                 :                :             HeapTuple   tp;
                               2440                 :                : 
                               2441                 :                :             /* Use the system cache to obtain the user mapping */
                               2442                 :             14 :             tp = SearchSysCache1(USERMAPPINGOID, ObjectIdGetDatum(entry->key));
                               2443                 :                : 
                               2444                 :                :             /*
                               2445                 :                :              * Just like in the foreign server case, user mappings can also be
                               2446                 :                :              * dropped in the current explicit transaction. Therefore, the
                               2447                 :                :              * similar check as in the server case is required.
                               2448                 :                :              */
                               2449         [ +  + ]:             14 :             if (!HeapTupleIsValid(tp))
                               2450                 :                :             {
                               2451                 :                :                 /*
                               2452                 :                :                  * If we reach here, this entry must have been invalidated in
                               2453                 :                :                  * pgfdw_inval_callback, same as in the server case.
                               2454                 :                :                  */
                               2455   [ +  -  +  -  :              1 :                 Assert(entry->conn && entry->xact_depth > 0 &&
                                              -  + ]
                               2456                 :                :                        entry->invalidated);
                               2457                 :                : 
                               2458                 :              1 :                 nulls[i++] = true;
                               2459                 :                :             }
                               2460                 :                :             else
                               2461                 :                :             {
                               2462                 :                :                 Oid         userid;
                               2463                 :                : 
                               2464                 :             13 :                 userid = ((Form_pg_user_mapping) GETSTRUCT(tp))->umuser;
                               2465         [ +  + ]:             13 :                 values[i++] = CStringGetTextDatum(MappingUserName(userid));
                               2466                 :             13 :                 ReleaseSysCache(tp);
                               2467                 :                :             }
                               2468                 :                :         }
                               2469                 :                : 
                               2470                 :             14 :         values[i++] = BoolGetDatum(!entry->invalidated);
                               2471                 :                : 
  808                          2472         [ +  - ]:             14 :         if (api_version >= PGFDW_V1_2)
                               2473                 :                :         {
                               2474                 :             14 :             bool        check_conn = PG_GETARG_BOOL(0);
                               2475                 :                : 
                               2476                 :                :             /* Is this connection used in the current transaction? */
  754                          2477                 :             14 :             values[i++] = BoolGetDatum(entry->xact_depth > 0);
                               2478                 :                : 
                               2479                 :                :             /*
                               2480                 :                :              * If a connection status check is requested and supported, return
                               2481                 :                :              * whether the connection is closed. Otherwise, return NULL.
                               2482                 :                :              */
  808                          2483   [ +  +  +  - ]:             14 :             if (check_conn && pgfdw_conn_checkable())
  754                          2484                 :              3 :                 values[i++] = BoolGetDatum(pgfdw_conn_check(entry->conn) != 0);
                               2485                 :                :             else
                               2486                 :             11 :                 nulls[i++] = true;
                               2487                 :                : 
                               2488                 :                :             /* Return process ID of remote backend */
  588                          2489                 :             14 :             values[i++] = Int32GetDatum(PQbackendPID(entry->conn));
                               2490                 :                :         }
                               2491                 :                : 
 1679 michael@paquier.xyz      2492                 :             14 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
                               2493                 :                :     }
                               2494                 :                : }
                               2495                 :                : 
                               2496                 :                : /*
                               2497                 :                :  * Values in connection strings must be enclosed in single quotes. Single
                               2498                 :                :  * quotes and backslashes must be escaped with backslash. NB: these rules are
                               2499                 :                :  * different from the rules for escaping a SQL literal.
                               2500                 :                :  */
                               2501                 :                : static void
  220 jdavis@postgresql.or     2502                 :             35 : appendEscapedValue(StringInfo str, const char *val)
                               2503                 :                : {
                               2504                 :             35 :     appendStringInfoChar(str, '\'');
                               2505         [ +  + ]:            343 :     for (int i = 0; val[i] != '\0'; i++)
                               2506                 :                :     {
                               2507   [ +  -  -  + ]:            308 :         if (val[i] == '\\' || val[i] == '\'')
  220 jdavis@postgresql.or     2508                 :UBC           0 :             appendStringInfoChar(str, '\\');
  220 jdavis@postgresql.or     2509                 :CBC         308 :         appendStringInfoChar(str, val[i]);
                               2510                 :                :     }
                               2511                 :             35 :     appendStringInfoChar(str, '\'');
                               2512                 :             35 : }
                               2513                 :                : 
                               2514                 :                : Datum
                               2515                 :              8 : postgres_fdw_connection(PG_FUNCTION_ARGS)
                               2516                 :                : {
                               2517                 :              8 :     Oid         userid = PG_GETARG_OID(0);
                               2518                 :              8 :     Oid         serverid = PG_GETARG_OID(1);
                               2519                 :              8 :     ForeignServer *server = GetForeignServer(serverid);
                               2520                 :              8 :     UserMapping *user = GetUserMapping(userid, serverid);
                               2521                 :                :     StringInfoData str;
                               2522                 :                :     const char **keywords;
                               2523                 :                :     const char **values;
                               2524                 :                :     char       *appname;
                               2525                 :              8 :     char       *sep = "";
                               2526                 :                : 
                               2527                 :                :     /*
                               2528                 :                :      * SCRAM pass-through cannot work for subscriptions because the connection
                               2529                 :                :      * happens in a worker process.
                               2530                 :                :      */
   70                          2531         [ +  + ]:              8 :     if (UseScramPassthrough(server, user))
                               2532         [ +  - ]:              1 :         ereport(ERROR,
                               2533                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                               2534                 :                :                  errmsg("SCRAM pass-through authentication is not supported for subscription connections"),
                               2535                 :                :                  errdetail("The foreign server or user mapping for user \"%s\" has \"use_scram_passthrough\" enabled.",
                               2536                 :                :                            GetUserNameFromId(userid, false)),
                               2537                 :                :                  errhint("Store a password in the user mapping instead.")));
                               2538                 :                : 
  220                          2539                 :              7 :     construct_connection_params(server, user, &keywords, &values, &appname);
                               2540                 :                : 
                               2541                 :              7 :     initStringInfo(&str);
                               2542         [ +  + ]:             42 :     for (int i = 0; keywords[i] != NULL; i++)
                               2543                 :                :     {
                               2544         [ -  + ]:             35 :         if (values[i] == NULL)
  220 jdavis@postgresql.or     2545                 :UBC           0 :             continue;
  220 jdavis@postgresql.or     2546                 :CBC          35 :         appendStringInfo(&str, "%s%s = ", sep, keywords[i]);
                               2547                 :             35 :         appendEscapedValue(&str, values[i]);
                               2548                 :             35 :         sep = " ";
                               2549                 :                :     }
                               2550                 :                : 
                               2551         [ -  + ]:              7 :     if (appname != NULL)
  220 jdavis@postgresql.or     2552                 :UBC           0 :         pfree(appname);
  220 jdavis@postgresql.or     2553                 :CBC           7 :     pfree(keywords);
                               2554                 :              7 :     pfree(values);
                               2555                 :              7 :     PG_RETURN_TEXT_P(cstring_to_text(str.data));
                               2556                 :                : }
                               2557                 :                : 
                               2558                 :                : /*
                               2559                 :                :  * List active foreign server connections.
                               2560                 :                :  *
                               2561                 :                :  * The SQL API of this function has changed multiple times, and will likely
                               2562                 :                :  * do so again in future.  To support the case where a newer version of this
                               2563                 :                :  * loadable module is being used with an old SQL declaration of the function,
                               2564                 :                :  * we continue to support the older API versions.
                               2565                 :                :  */
                               2566                 :                : Datum
  808 fujii@postgresql.org     2567                 :             14 : postgres_fdw_get_connections_1_2(PG_FUNCTION_ARGS)
                               2568                 :                : {
                               2569                 :             14 :     postgres_fdw_get_connections_internal(fcinfo, PGFDW_V1_2);
                               2570                 :                : 
                               2571                 :             14 :     PG_RETURN_VOID();
                               2572                 :                : }
                               2573                 :                : 
                               2574                 :                : Datum
  808 fujii@postgresql.org     2575                 :UBC           0 : postgres_fdw_get_connections(PG_FUNCTION_ARGS)
                               2576                 :                : {
                               2577                 :              0 :     postgres_fdw_get_connections_internal(fcinfo, PGFDW_V1_1);
                               2578                 :                : 
 2093                          2579                 :              0 :     PG_RETURN_VOID();
                               2580                 :                : }
                               2581                 :                : 
                               2582                 :                : /*
                               2583                 :                :  * Disconnect the specified cached connections.
                               2584                 :                :  *
                               2585                 :                :  * This function discards the open connections that are established by
                               2586                 :                :  * postgres_fdw from the local session to the foreign server with
                               2587                 :                :  * the given name. Note that there can be multiple connections to
                               2588                 :                :  * the given server using different user mappings. If the connections
                               2589                 :                :  * are used in the current local transaction, they are not disconnected
                               2590                 :                :  * and warning messages are reported. This function returns true
                               2591                 :                :  * if it disconnects at least one connection, otherwise false. If no
                               2592                 :                :  * foreign server with the given name is found, an error is reported.
                               2593                 :                :  */
                               2594                 :                : Datum
 2085 fujii@postgresql.org     2595                 :CBC           4 : postgres_fdw_disconnect(PG_FUNCTION_ARGS)
                               2596                 :                : {
                               2597                 :                :     ForeignServer *server;
                               2598                 :                :     char       *servername;
                               2599                 :                : 
                               2600                 :              4 :     servername = text_to_cstring(PG_GETARG_TEXT_PP(0));
                               2601                 :              4 :     server = GetForeignServerByName(servername, false);
                               2602                 :                : 
                               2603                 :              3 :     PG_RETURN_BOOL(disconnect_cached_connections(server->serverid));
                               2604                 :                : }
                               2605                 :                : 
                               2606                 :                : /*
                               2607                 :                :  * Disconnect all the cached connections.
                               2608                 :                :  *
                               2609                 :                :  * This function discards all the open connections that are established by
                               2610                 :                :  * postgres_fdw from the local session to the foreign servers.
                               2611                 :                :  * If the connections are used in the current local transaction, they are
                               2612                 :                :  * not disconnected and warning messages are reported. This function
                               2613                 :                :  * returns true if it disconnects at least one connection, otherwise false.
                               2614                 :                :  */
                               2615                 :                : Datum
                               2616                 :              6 : postgres_fdw_disconnect_all(PG_FUNCTION_ARGS)
                               2617                 :                : {
                               2618                 :              6 :     PG_RETURN_BOOL(disconnect_cached_connections(InvalidOid));
                               2619                 :                : }
                               2620                 :                : 
                               2621                 :                : /*
                               2622                 :                :  * Workhorse to disconnect cached connections.
                               2623                 :                :  *
                               2624                 :                :  * This function scans all the connection cache entries and disconnects
                               2625                 :                :  * the open connections whose foreign server OID matches with
                               2626                 :                :  * the specified one. If InvalidOid is specified, it disconnects all
                               2627                 :                :  * the cached connections.
                               2628                 :                :  *
                               2629                 :                :  * This function emits a warning for each connection that's used in
                               2630                 :                :  * the current transaction and doesn't close it. It returns true if
                               2631                 :                :  * it disconnects at least one connection, otherwise false.
                               2632                 :                :  *
                               2633                 :                :  * Note that this function disconnects even the connections that are
                               2634                 :                :  * established by other users in the same local session using different
                               2635                 :                :  * user mappings. This leads even non-superuser to be able to close
                               2636                 :                :  * the connections established by superusers in the same local session.
                               2637                 :                :  *
                               2638                 :                :  * XXX As of now we don't see any security risk doing this. But we should
                               2639                 :                :  * set some restrictions on that, for example, prevent non-superuser
                               2640                 :                :  * from closing the connections established by superusers even
                               2641                 :                :  * in the same session?
                               2642                 :                :  */
                               2643                 :                : static bool
                               2644                 :              9 : disconnect_cached_connections(Oid serverid)
                               2645                 :                : {
                               2646                 :                :     HASH_SEQ_STATUS scan;
                               2647                 :                :     ConnCacheEntry *entry;
                               2648                 :              9 :     bool        all = !OidIsValid(serverid);
                               2649                 :              9 :     bool        result = false;
                               2650                 :                : 
                               2651                 :                :     /*
                               2652                 :                :      * Connection cache hashtable has not been initialized yet in this
                               2653                 :                :      * session, so return false.
                               2654                 :                :      */
                               2655         [ -  + ]:              9 :     if (!ConnectionHash)
 2085 fujii@postgresql.org     2656                 :UBC           0 :         return false;
                               2657                 :                : 
 2085 fujii@postgresql.org     2658                 :CBC           9 :     hash_seq_init(&scan, ConnectionHash);
                               2659         [ +  + ]:             77 :     while ((entry = (ConnCacheEntry *) hash_seq_search(&scan)))
                               2660                 :                :     {
                               2661                 :                :         /* Ignore cache entry if no open connection right now. */
                               2662         [ +  + ]:             68 :         if (!entry->conn)
                               2663                 :             55 :             continue;
                               2664                 :                : 
                               2665   [ +  +  +  + ]:             13 :         if (all || entry->serverid == serverid)
                               2666                 :                :         {
                               2667                 :                :             /*
                               2668                 :                :              * Emit a warning because the connection to close is used in the
                               2669                 :                :              * current transaction and cannot be disconnected right now.
                               2670                 :                :              */
                               2671         [ +  + ]:             10 :             if (entry->xact_depth > 0)
                               2672                 :                :             {
                               2673                 :                :                 ForeignServer *server;
                               2674                 :                : 
                               2675                 :              3 :                 server = GetForeignServerExtended(entry->serverid,
                               2676                 :                :                                                   FSV_MISSING_OK);
                               2677                 :                : 
                               2678         [ -  + ]:              3 :                 if (!server)
                               2679                 :                :                 {
                               2680                 :                :                     /*
                               2681                 :                :                      * If the foreign server was dropped while its connection
                               2682                 :                :                      * was used in the current transaction, the connection
                               2683                 :                :                      * must have been marked as invalid by
                               2684                 :                :                      * pgfdw_inval_callback at the end of DROP SERVER command.
                               2685                 :                :                      */
 2085 fujii@postgresql.org     2686         [ #  # ]:UBC           0 :                     Assert(entry->invalidated);
                               2687                 :                : 
                               2688         [ #  # ]:              0 :                     ereport(WARNING,
                               2689                 :                :                             (errmsg("cannot close dropped server connection because it is still in use")));
                               2690                 :                :                 }
                               2691                 :                :                 else
 2085 fujii@postgresql.org     2692         [ +  - ]:CBC           3 :                     ereport(WARNING,
                               2693                 :                :                             (errmsg("cannot close connection for server \"%s\" because it is still in use",
                               2694                 :                :                                     server->servername)));
                               2695                 :                :             }
                               2696                 :                :             else
                               2697                 :                :             {
                               2698         [ -  + ]:              7 :                 elog(DEBUG3, "discarding connection %p", entry->conn);
                               2699                 :              7 :                 disconnect_pg_server(entry);
                               2700                 :              7 :                 result = true;
                               2701                 :                :             }
                               2702                 :                :         }
                               2703                 :                :     }
                               2704                 :                : 
                               2705                 :              9 :     return result;
                               2706                 :                : }
                               2707                 :                : 
                               2708                 :                : /*
                               2709                 :                :  * Check if the remote server closed the connection.
                               2710                 :                :  *
                               2711                 :                :  * Returns 1 if the connection is closed, -1 if an error occurred,
                               2712                 :                :  * and 0 if it's not closed or if the connection check is unavailable
                               2713                 :                :  * on this platform.
                               2714                 :                :  */
                               2715                 :                : static int
  808                          2716                 :              3 : pgfdw_conn_check(PGconn *conn)
                               2717                 :                : {
                               2718                 :              3 :     int         sock = PQsocket(conn);
                               2719                 :                : 
                               2720   [ +  -  -  + ]:              3 :     if (PQstatus(conn) != CONNECTION_OK || sock == -1)
  808 fujii@postgresql.org     2721                 :UBC           0 :         return -1;
                               2722                 :                : 
                               2723                 :                : #if (defined(HAVE_POLL) && defined(POLLRDHUP))
                               2724                 :                :     {
                               2725                 :                :         struct pollfd input_fd;
                               2726                 :                :         int         result;
                               2727                 :                : 
  808 fujii@postgresql.org     2728                 :CBC           3 :         input_fd.fd = sock;
                               2729                 :              3 :         input_fd.events = POLLRDHUP;
                               2730                 :              3 :         input_fd.revents = 0;
                               2731                 :                : 
                               2732                 :                :         do
                               2733                 :              3 :             result = poll(&input_fd, 1, 0);
                               2734   [ -  +  -  - ]:              3 :         while (result < 0 && errno == EINTR);
                               2735                 :                : 
                               2736         [ -  + ]:              3 :         if (result < 0)
  808 fujii@postgresql.org     2737                 :UBC           0 :             return -1;
                               2738                 :                : 
  807 fujii@postgresql.org     2739                 :CBC           3 :         return (input_fd.revents &
                               2740                 :              3 :                 (POLLRDHUP | POLLHUP | POLLERR | POLLNVAL)) ? 1 : 0;
                               2741                 :                :     }
                               2742                 :                : #else
                               2743                 :                :     return 0;
                               2744                 :                : #endif
                               2745                 :                : }
                               2746                 :                : 
                               2747                 :                : /*
                               2748                 :                :  * Check if connection status checking is available on this platform.
                               2749                 :                :  *
                               2750                 :                :  * Returns true if available, false otherwise.
                               2751                 :                :  */
                               2752                 :                : static bool
  808                          2753                 :              3 : pgfdw_conn_checkable(void)
                               2754                 :                : {
                               2755                 :                : #if (defined(HAVE_POLL) && defined(POLLRDHUP))
                               2756                 :              3 :     return true;
                               2757                 :                : #else
                               2758                 :                :     return false;
                               2759                 :                : #endif
                               2760                 :                : }
                               2761                 :                : 
                               2762                 :                : /*
                               2763                 :                :  * Ensure that require_auth and SCRAM keys are correctly set on values. SCRAM
                               2764                 :                :  * keys used to pass-through are coming from the initial connection from the
                               2765                 :                :  * client with the server.
                               2766                 :                :  *
                               2767                 :                :  * All required SCRAM options are set by postgres_fdw, so we just need to
                               2768                 :                :  * ensure that these options are not overwritten by the user.
                               2769                 :                :  */
                               2770                 :                : static bool
  567 peter@eisentraut.org     2771                 :              9 : pgfdw_has_required_scram_options(const char **keywords, const char **values)
                               2772                 :                : {
                               2773                 :              9 :     bool        has_scram_server_key = false;
                               2774                 :              9 :     bool        has_scram_client_key = false;
                               2775                 :              9 :     bool        has_require_auth = false;
                               2776                 :              9 :     bool        has_scram_keys = false;
                               2777                 :                : 
                               2778                 :                :     /*
                               2779                 :                :      * Continue iterating even if we found the keys that we need to validate
                               2780                 :                :      * to make sure that there is no other declaration of these keys that can
                               2781                 :                :      * overwrite the first.
                               2782                 :                :      */
                               2783         [ +  + ]:             87 :     for (int i = 0; keywords[i] != NULL; i++)
                               2784                 :                :     {
                               2785         [ +  + ]:             78 :         if (strcmp(keywords[i], "scram_client_key") == 0)
                               2786                 :                :         {
                               2787   [ +  -  +  - ]:              8 :             if (values[i] != NULL && values[i][0] != '\0')
                               2788                 :              8 :                 has_scram_client_key = true;
                               2789                 :                :             else
  567 peter@eisentraut.org     2790                 :UBC           0 :                 has_scram_client_key = false;
                               2791                 :                :         }
                               2792                 :                : 
  567 peter@eisentraut.org     2793         [ +  + ]:CBC          78 :         if (strcmp(keywords[i], "scram_server_key") == 0)
                               2794                 :                :         {
                               2795   [ +  -  +  - ]:              8 :             if (values[i] != NULL && values[i][0] != '\0')
                               2796                 :              8 :                 has_scram_server_key = true;
                               2797                 :                :             else
  567 peter@eisentraut.org     2798                 :UBC           0 :                 has_scram_server_key = false;
                               2799                 :                :         }
                               2800                 :                : 
  567 peter@eisentraut.org     2801         [ +  + ]:CBC          78 :         if (strcmp(keywords[i], "require_auth") == 0)
                               2802                 :                :         {
                               2803   [ +  -  +  - ]:              8 :             if (values[i] != NULL && strcmp(values[i], "scram-sha-256") == 0)
                               2804                 :              8 :                 has_require_auth = true;
                               2805                 :                :             else
  567 peter@eisentraut.org     2806                 :UBC           0 :                 has_require_auth = false;
                               2807                 :                :         }
                               2808                 :                :     }
                               2809                 :                : 
  430 peter@eisentraut.org     2810   [ +  +  +  -  :CBC           9 :     has_scram_keys = has_scram_client_key && has_scram_server_key && MyProcPort != NULL && MyProcPort->has_scram_keys;
                                        +  -  +  - ]
                               2811                 :                : 
  567                          2812   [ +  +  +  - ]:              9 :     return (has_scram_keys && has_require_auth);
                               2813                 :                : }
        

Generated by: LCOV version 2.0-1