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