From e4602483e95bebd4da31e4ea95dc5c7f715c1e4f Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Mon, 23 Jan 2023 19:25:23 -0800 Subject: dblink, postgres_fdw: Handle interrupts during connection establishment Until now dblink and postgres_fdw did not process interrupts during connection establishment. Besides preventing query cancellations etc, this can lead to undetected deadlocks, as global barriers are not processed. These aforementioned undetected deadlocks are the reason for the spate of CI test failures in the FreeBSD 'test_running' step. Fix the bug by using the helper from libpq-be-fe-helpers.h, introduced in a prior commit. Besides fixing the bug, this also removes duplicated code around reserving file descriptors. As the change is relatively large and there are no field reports of the problem, don't backpatch for now. Reviewed-by: Thomas Munro Discussion: https://postgr.es/m/20220925232237.p6uskba2dw6fnwj2@awork3.anarazel.de Backpatch: --- contrib/postgres_fdw/connection.c | 42 ++++++--------------------------------- 1 file changed, 6 insertions(+), 36 deletions(-) (limited to 'contrib/postgres_fdw/connection.c') diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c index ed75ce3f79c..7760380f00d 100644 --- a/contrib/postgres_fdw/connection.c +++ b/contrib/postgres_fdw/connection.c @@ -17,6 +17,7 @@ #include "catalog/pg_user_mapping.h" #include "commands/defrem.h" #include "funcapi.h" +#include "libpq/libpq-be-fe-helpers.h" #include "mb/pg_wchar.h" #include "miscadmin.h" #include "pgstat.h" @@ -446,35 +447,10 @@ connect_pg_server(ForeignServer *server, UserMapping *user) /* verify the set of connection parameters */ check_conn_params(keywords, values, user); - /* - * We must obey fd.c's limit on non-virtual file descriptors. Assume - * that a PGconn represents one long-lived FD. (Doing this here also - * ensures that VFDs are closed if needed to make room.) - */ - if (!AcquireExternalFD()) - { -#ifndef WIN32 /* can't write #if within ereport() macro */ - ereport(ERROR, - (errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION), - errmsg("could not connect to server \"%s\"", - server->servername), - errdetail("There are too many open files on the local server."), - errhint("Raise the server's max_files_per_process and/or \"ulimit -n\" limits."))); -#else - ereport(ERROR, - (errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION), - errmsg("could not connect to server \"%s\"", - server->servername), - errdetail("There are too many open files on the local server."), - errhint("Raise the server's max_files_per_process setting."))); -#endif - } - /* OK to make connection */ - conn = PQconnectdbParams(keywords, values, false); - - if (!conn) - ReleaseExternalFD(); /* because the PG_CATCH block won't */ + conn = libpqsrv_connect_params(keywords, values, + /* expand_dbname = */ false, + PG_WAIT_EXTENSION); if (!conn || PQstatus(conn) != CONNECTION_OK) ereport(ERROR, @@ -507,12 +483,7 @@ connect_pg_server(ForeignServer *server, UserMapping *user) } PG_CATCH(); { - /* Release PGconn data structure if we managed to create one */ - if (conn) - { - PQfinish(conn); - ReleaseExternalFD(); - } + libpqsrv_disconnect(conn); PG_RE_THROW(); } PG_END_TRY(); @@ -528,9 +499,8 @@ disconnect_pg_server(ConnCacheEntry *entry) { if (entry->conn != NULL) { - PQfinish(entry->conn); + libpqsrv_disconnect(entry->conn); entry->conn = NULL; - ReleaseExternalFD(); } } -- cgit v1.2.3