|
| 1 | +/*---------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * mtm_utils.c |
| 4 | + * Utility functions |
| 5 | + * |
| 6 | + * Copyright (c) 2022, Postgres Professional |
| 7 | + * |
| 8 | + *---------------------------------------------------------------------------- |
| 9 | + */ |
| 10 | + |
| 11 | +#include "logger.h" |
| 12 | +#include "mtm_utils.h" |
| 13 | + |
| 14 | +#include "utils/timeout.h" |
| 15 | + |
| 16 | +/* |
| 17 | + * Disables timeouts on a client side: |
| 18 | + * - statement_timeout; |
| 19 | + * - lock_timeout; |
| 20 | + * - idle_in_transaction_session_timeout; |
| 21 | + * - idle_session_timeout. |
| 22 | + * |
| 23 | + * This timeouts, when set in the postgres config file, affect all process. |
| 24 | + * The multimaster needs his sessions not to be interrupted, so we disable |
| 25 | + * these timeouts. |
| 26 | + * |
| 27 | + * This function raises an error on PQExec failed. |
| 28 | + */ |
| 29 | +static void |
| 30 | +disable_client_timeouts(PGconn *conn) |
| 31 | +{ |
| 32 | + PGresult *res; |
| 33 | + |
| 34 | + res = PQexec(conn, "SET statement_timeout = 0"); |
| 35 | + if (PQresultStatus(res) != PGRES_COMMAND_OK) |
| 36 | + { |
| 37 | + char *msg = pchomp(PQerrorMessage(conn)); |
| 38 | + mtm_log(ERROR, "failed to set statement_timeout: %s", msg); |
| 39 | + } |
| 40 | + |
| 41 | + res = PQexec(conn, "SET lock_timeout = 0"); |
| 42 | + if (PQresultStatus(res) != PGRES_COMMAND_OK) |
| 43 | + { |
| 44 | + char *msg = pchomp(PQerrorMessage(conn)); |
| 45 | + mtm_log(ERROR, "failed to set lock_timeout: %s", msg); |
| 46 | + } |
| 47 | + |
| 48 | + res = PQexec(conn, "SET idle_in_transaction_session_timeout = 0"); |
| 49 | + if (PQresultStatus(res) != PGRES_COMMAND_OK) |
| 50 | + { |
| 51 | + char *msg = pchomp(PQerrorMessage(conn)); |
| 52 | + mtm_log(ERROR, "failed to set idle_in_transaction_session_timeout: %s", msg); |
| 53 | + } |
| 54 | + |
| 55 | + res = PQexec(conn, "SET idle_session_timeout = 0"); |
| 56 | + if (PQresultStatus(res) != PGRES_COMMAND_OK) |
| 57 | + { |
| 58 | + char *msg = pchomp(PQerrorMessage(conn)); |
| 59 | + mtm_log(ERROR, "failed to set idle_session_timeout: %s", msg); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/* |
| 64 | + * Disable timeouts for a current process |
| 65 | + * - statement_timeout; |
| 66 | + * - lock_timeout; |
| 67 | + * - idle_in_transaction_session_timeout; |
| 68 | + * - idle_session_timeout. |
| 69 | + * |
| 70 | + * We disable these timeout for the same reason as in the disable_client_timeout() |
| 71 | + */ |
| 72 | +extern void |
| 73 | +MtmDisableTimeouts(void) |
| 74 | +{ |
| 75 | + if (get_timeout_active(STATEMENT_TIMEOUT)) |
| 76 | + { |
| 77 | + disable_timeout(STATEMENT_TIMEOUT, false); |
| 78 | + } |
| 79 | + |
| 80 | + if (get_timeout_active(LOCK_TIMEOUT)) |
| 81 | + { |
| 82 | + disable_timeout(LOCK_TIMEOUT, false); |
| 83 | + } |
| 84 | + |
| 85 | + if (get_timeout_active(IDLE_IN_TRANSACTION_SESSION_TIMEOUT)) |
| 86 | + { |
| 87 | + disable_timeout(IDLE_IN_TRANSACTION_SESSION_TIMEOUT, false); |
| 88 | + } |
| 89 | + |
| 90 | + if (get_timeout_active(IDLE_SESSION_TIMEOUT)) |
| 91 | + { |
| 92 | + disable_timeout(IDLE_SESSION_TIMEOUT, false); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/* |
| 97 | + * Wrapper on PQconnectPoll |
| 98 | + * |
| 99 | + * On connect disables timeouts on a client side |
| 100 | + */ |
| 101 | +PostgresPollingStatusType |
| 102 | +MtmPQconnectPoll(PGconn *conn) |
| 103 | +{ |
| 104 | + PostgresPollingStatusType status; |
| 105 | + |
| 106 | + status = PQconnectPoll(conn); |
| 107 | + if (status != PGRES_POLLING_OK) |
| 108 | + return status; |
| 109 | + |
| 110 | + disable_client_timeouts(conn); |
| 111 | + |
| 112 | + return status; |
| 113 | +} |
| 114 | + |
| 115 | +/* |
| 116 | + * Wrapper on PQconnectdb |
| 117 | + * |
| 118 | + * On connect disables timeouts on a client side |
| 119 | + */ |
| 120 | +PGconn * |
| 121 | +MtmPQconnectdb(const char *conninfo) |
| 122 | +{ |
| 123 | + PGconn *conn; |
| 124 | + |
| 125 | + conn = PQconnectdb(conninfo); |
| 126 | + if (PQstatus(conn) != CONNECTION_OK) |
| 127 | + return conn; |
| 128 | + |
| 129 | + disable_client_timeouts(conn); |
| 130 | + |
| 131 | + return conn; |
| 132 | +} |
| 133 | + |
0 commit comments