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