48
48
#include "funcapi.h"
49
49
#include "lib/stringinfo.h"
50
50
#include "libpq-fe.h"
51
+ #include "libpq/libpq-be-fe-helpers.h"
51
52
#include "mb/pg_wchar.h"
52
53
#include "miscadmin.h"
53
54
#include "parser/scansup.h"
@@ -199,37 +200,14 @@ dblink_get_conn(char *conname_or_str,
199
200
connstr = conname_or_str;
200
201
dblink_connstr_check(connstr);
201
202
202
- /*
203
- * We must obey fd.c's limit on non-virtual file descriptors. Assume
204
- * that a PGconn represents one long-lived FD. (Doing this here also
205
- * ensures that VFDs are closed if needed to make room.)
206
- */
207
- if (!AcquireExternalFD())
208
- {
209
- #ifndef WIN32 /* can't write #if within ereport() macro */
210
- ereport(ERROR,
211
- (errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
212
- errmsg("could not establish connection"),
213
- errdetail("There are too many open files on the local server."),
214
- errhint("Raise the server's max_files_per_process and/or \"ulimit -n\" limits.")));
215
- #else
216
- ereport(ERROR,
217
- (errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
218
- errmsg("could not establish connection"),
219
- errdetail("There are too many open files on the local server."),
220
- errhint("Raise the server's max_files_per_process setting.")));
221
- #endif
222
- }
223
-
224
203
/* OK to make connection */
225
- conn = PQconnectdb (connstr);
204
+ conn = libpqsrv_connect (connstr, PG_WAIT_EXTENSION );
226
205
227
206
if (PQstatus(conn) == CONNECTION_BAD)
228
207
{
229
208
char *msg = pchomp(PQerrorMessage(conn));
230
209
231
- PQfinish(conn);
232
- ReleaseExternalFD();
210
+ libpqsrv_disconnect(conn);
233
211
ereport(ERROR,
234
212
(errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
235
213
errmsg("could not establish connection"),
@@ -312,36 +290,13 @@ dblink_connect(PG_FUNCTION_ARGS)
312
290
/* check password in connection string if not superuser */
313
291
dblink_connstr_check(connstr);
314
292
315
- /*
316
- * We must obey fd.c's limit on non-virtual file descriptors. Assume that
317
- * a PGconn represents one long-lived FD. (Doing this here also ensures
318
- * that VFDs are closed if needed to make room.)
319
- */
320
- if (!AcquireExternalFD())
321
- {
322
- #ifndef WIN32 /* can't write #if within ereport() macro */
323
- ereport(ERROR,
324
- (errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
325
- errmsg("could not establish connection"),
326
- errdetail("There are too many open files on the local server."),
327
- errhint("Raise the server's max_files_per_process and/or \"ulimit -n\" limits.")));
328
- #else
329
- ereport(ERROR,
330
- (errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION),
331
- errmsg("could not establish connection"),
332
- errdetail("There are too many open files on the local server."),
333
- errhint("Raise the server's max_files_per_process setting.")));
334
- #endif
335
- }
336
-
337
293
/* OK to make connection */
338
- conn = PQconnectdb (connstr);
294
+ conn = libpqsrv_connect (connstr, PG_WAIT_EXTENSION );
339
295
340
296
if (PQstatus(conn) == CONNECTION_BAD)
341
297
{
342
298
msg = pchomp(PQerrorMessage(conn));
343
- PQfinish(conn);
344
- ReleaseExternalFD();
299
+ libpqsrv_disconnect(conn);
345
300
if (rconn)
346
301
pfree(rconn);
347
302
@@ -366,10 +321,7 @@ dblink_connect(PG_FUNCTION_ARGS)
366
321
else
367
322
{
368
323
if (pconn->conn)
369
- {
370
- PQfinish(pconn->conn);
371
- ReleaseExternalFD();
372
- }
324
+ libpqsrv_disconnect(conn);
373
325
pconn->conn = conn;
374
326
}
375
327
@@ -402,8 +354,7 @@ dblink_disconnect(PG_FUNCTION_ARGS)
402
354
if (!conn)
403
355
dblink_conn_not_avail(conname);
404
356
405
- PQfinish(conn);
406
- ReleaseExternalFD();
357
+ libpqsrv_disconnect(conn);
407
358
if (rconn)
408
359
{
409
360
deleteConnection(conname);
@@ -838,10 +789,7 @@ dblink_record_internal(FunctionCallInfo fcinfo, bool is_async)
838
789
{
839
790
/* if needed, close the connection to the database */
840
791
if (freeconn)
841
- {
842
- PQfinish(conn);
843
- ReleaseExternalFD();
844
- }
792
+ libpqsrv_disconnect(conn);
845
793
}
846
794
PG_END_TRY();
847
795
@@ -1516,10 +1464,7 @@ dblink_exec(PG_FUNCTION_ARGS)
1516
1464
{
1517
1465
/* if needed, close the connection to the database */
1518
1466
if (freeconn)
1519
- {
1520
- PQfinish(conn);
1521
- ReleaseExternalFD();
1522
- }
1467
+ libpqsrv_disconnect(conn);
1523
1468
}
1524
1469
PG_END_TRY();
1525
1470
@@ -2606,8 +2551,7 @@ createNewConnection(const char *name, remoteConn *rconn)
2606
2551
2607
2552
if (found)
2608
2553
{
2609
- PQfinish(rconn->conn);
2610
- ReleaseExternalFD();
2554
+ libpqsrv_disconnect(rconn->conn);
2611
2555
pfree(rconn);
2612
2556
2613
2557
ereport(ERROR,
@@ -2647,8 +2591,7 @@ dblink_security_check(PGconn *conn, remoteConn *rconn)
2647
2591
{
2648
2592
if (!PQconnectionUsedPassword(conn))
2649
2593
{
2650
- PQfinish(conn);
2651
- ReleaseExternalFD();
2594
+ libpqsrv_disconnect(conn);
2652
2595
if (rconn)
2653
2596
pfree(rconn);
2654
2597
0 commit comments