| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #ifndef LIBPQ_BE_FE_HELPERS_H |
| #define LIBPQ_BE_FE_HELPERS_H |
|
|
| |
| |
| |
| |
| |
| |
| #ifdef BUILDING_DLL |
| #error "libpq may not be used code directly built into the backend" |
| #endif |
|
|
| #include "libpq-fe.h" |
| #include "miscadmin.h" |
| #include "storage/fd.h" |
| #include "storage/latch.h" |
| #include "utils/timestamp.h" |
| #include "utils/wait_event.h" |
|
|
|
|
| static inline void libpqsrv_connect_prepare(void); |
| static inline void libpqsrv_connect_internal(PGconn *conn, uint32 wait_event_info); |
| static inline PGresult *libpqsrv_get_result_last(PGconn *conn, uint32 wait_event_info); |
| static inline PGresult *libpqsrv_get_result(PGconn *conn, uint32 wait_event_info); |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| static inline PGconn * |
| libpqsrv_connect(const char *conninfo, uint32 wait_event_info) |
| { |
| PGconn *conn = NULL; |
|
|
| libpqsrv_connect_prepare(); |
|
|
| conn = PQconnectStart(conninfo); |
|
|
| libpqsrv_connect_internal(conn, wait_event_info); |
|
|
| return conn; |
| } |
|
|
| |
| |
| |
| |
| static inline PGconn * |
| libpqsrv_connect_params(const char *const *keywords, |
| const char *const *values, |
| int expand_dbname, |
| uint32 wait_event_info) |
| { |
| PGconn *conn = NULL; |
|
|
| libpqsrv_connect_prepare(); |
|
|
| conn = PQconnectStartParams(keywords, values, expand_dbname); |
|
|
| libpqsrv_connect_internal(conn, wait_event_info); |
|
|
| return conn; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| static inline void |
| libpqsrv_disconnect(PGconn *conn) |
| { |
| |
| |
| |
| |
| |
| |
| |
| if (conn == NULL) |
| return; |
|
|
| ReleaseExternalFD(); |
| PQfinish(conn); |
| } |
|
|
|
|
| |
|
|
|
|
| |
| |
| |
| static inline void |
| libpqsrv_connect_prepare(void) |
| { |
| |
| |
| |
| |
| |
| if (!AcquireExternalFD()) |
| { |
| #ifndef WIN32 |
| ereport(ERROR, |
| (errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION), |
| errmsg("could not establish connection"), |
| 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 establish connection"), |
| errdetail("There are too many open files on the local server."), |
| errhint("Raise the server's max_files_per_process setting."))); |
| #endif |
| } |
| } |
|
|
| |
| |
| |
| static inline void |
| libpqsrv_connect_internal(PGconn *conn, uint32 wait_event_info) |
| { |
| |
| |
| |
| |
| if (conn == NULL) |
| { |
| ReleaseExternalFD(); |
| return; |
| } |
|
|
| |
| |
| |
| |
| if (PQstatus(conn) == CONNECTION_BAD) |
| return; |
|
|
| |
| |
| |
| |
| PG_TRY(); |
| { |
| PostgresPollingStatusType status; |
|
|
| |
| |
| |
| |
| |
| status = PGRES_POLLING_WRITING; |
| while (status != PGRES_POLLING_OK && status != PGRES_POLLING_FAILED) |
| { |
| int io_flag; |
| int rc; |
|
|
| if (status == PGRES_POLLING_READING) |
| io_flag = WL_SOCKET_READABLE; |
| #ifdef WIN32 |
|
|
| |
| |
| |
| |
| else if (PQstatus(conn) == CONNECTION_STARTED) |
| io_flag = WL_SOCKET_CONNECTED; |
| #endif |
| else |
| io_flag = WL_SOCKET_WRITEABLE; |
|
|
| rc = WaitLatchOrSocket(MyLatch, |
| WL_EXIT_ON_PM_DEATH | WL_LATCH_SET | io_flag, |
| PQsocket(conn), |
| 0, |
| wait_event_info); |
|
|
| |
| if (rc & WL_LATCH_SET) |
| { |
| ResetLatch(MyLatch); |
| CHECK_FOR_INTERRUPTS(); |
| } |
|
|
| |
| if (rc & io_flag) |
| status = PQconnectPoll(conn); |
| } |
| } |
| PG_CATCH(); |
| { |
| |
| |
| |
| |
| |
| ReleaseExternalFD(); |
| PQfinish(conn); |
|
|
| PG_RE_THROW(); |
| } |
| PG_END_TRY(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static inline PGresult * |
| libpqsrv_exec(PGconn *conn, const char *query, uint32 wait_event_info) |
| { |
| if (!PQsendQuery(conn, query)) |
| return NULL; |
| return libpqsrv_get_result_last(conn, wait_event_info); |
| } |
|
|
| |
| |
| |
| |
| |
| static inline PGresult * |
| libpqsrv_exec_params(PGconn *conn, |
| const char *command, |
| int nParams, |
| const Oid *paramTypes, |
| const char *const *paramValues, |
| const int *paramLengths, |
| const int *paramFormats, |
| int resultFormat, |
| uint32 wait_event_info) |
| { |
| if (!PQsendQueryParams(conn, command, nParams, paramTypes, paramValues, |
| paramLengths, paramFormats, resultFormat)) |
| return NULL; |
| return libpqsrv_get_result_last(conn, wait_event_info); |
| } |
|
|
| |
| |
| |
| |
| static inline PGresult * |
| libpqsrv_get_result_last(PGconn *conn, uint32 wait_event_info) |
| { |
| PGresult *volatile lastResult = NULL; |
|
|
| |
| PG_TRY(); |
| { |
| for (;;) |
| { |
| |
| PGresult *result; |
|
|
| result = libpqsrv_get_result(conn, wait_event_info); |
| if (result == NULL) |
| break; |
|
|
| |
| |
| |
| |
| PQclear(lastResult); |
| lastResult = result; |
|
|
| if (PQresultStatus(lastResult) == PGRES_COPY_IN || |
| PQresultStatus(lastResult) == PGRES_COPY_OUT || |
| PQresultStatus(lastResult) == PGRES_COPY_BOTH || |
| PQstatus(conn) == CONNECTION_BAD) |
| break; |
| } |
| } |
| PG_CATCH(); |
| { |
| PQclear(lastResult); |
| PG_RE_THROW(); |
| } |
| PG_END_TRY(); |
|
|
| return lastResult; |
| } |
|
|
| |
| |
| |
| static inline PGresult * |
| libpqsrv_get_result(PGconn *conn, uint32 wait_event_info) |
| { |
| |
| |
| |
| |
| while (PQisBusy(conn)) |
| { |
| int rc; |
|
|
| rc = WaitLatchOrSocket(MyLatch, |
| WL_EXIT_ON_PM_DEATH | WL_LATCH_SET | |
| WL_SOCKET_READABLE, |
| PQsocket(conn), |
| 0, |
| wait_event_info); |
|
|
| |
| if (rc & WL_LATCH_SET) |
| { |
| ResetLatch(MyLatch); |
| CHECK_FOR_INTERRUPTS(); |
| } |
|
|
| |
| if (PQconsumeInput(conn) == 0) |
| { |
| |
| break; |
| } |
| } |
|
|
| |
| return PQgetResult(conn); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static inline const char * |
| libpqsrv_cancel(PGconn *conn, TimestampTz endtime) |
| { |
| PGcancelConn *cancel_conn; |
| const char *error = NULL; |
|
|
| cancel_conn = PQcancelCreate(conn); |
| if (cancel_conn == NULL) |
| return "out of memory"; |
|
|
| |
|
|
| PG_TRY(); |
| { |
| if (!PQcancelStart(cancel_conn)) |
| { |
| error = pchomp(PQcancelErrorMessage(cancel_conn)); |
| goto exit; |
| } |
|
|
| for (;;) |
| { |
| PostgresPollingStatusType pollres; |
| TimestampTz now; |
| long cur_timeout; |
| int waitEvents = WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH; |
|
|
| pollres = PQcancelPoll(cancel_conn); |
| if (pollres == PGRES_POLLING_OK) |
| break; |
|
|
| |
| now = GetCurrentTimestamp(); |
| cur_timeout = TimestampDifferenceMilliseconds(now, endtime); |
| if (cur_timeout <= 0) |
| { |
| error = "cancel request timed out"; |
| break; |
| } |
|
|
| switch (pollres) |
| { |
| case PGRES_POLLING_READING: |
| waitEvents |= WL_SOCKET_READABLE; |
| break; |
| case PGRES_POLLING_WRITING: |
| waitEvents |= WL_SOCKET_WRITEABLE; |
| break; |
| default: |
| error = pchomp(PQcancelErrorMessage(cancel_conn)); |
| goto exit; |
| } |
|
|
| |
| WaitLatchOrSocket(MyLatch, waitEvents, PQcancelSocket(cancel_conn), |
| cur_timeout, PG_WAIT_CLIENT); |
|
|
| ResetLatch(MyLatch); |
|
|
| CHECK_FOR_INTERRUPTS(); |
| } |
| exit: ; |
| } |
| PG_FINALLY(); |
| { |
| PQcancelFinish(cancel_conn); |
| } |
| PG_END_TRY(); |
|
|
| return error; |
| } |
|
|
| #endif |
|
|