Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5b1572e

Browse files
committed
Add PQconnectdb_safe function which filters out some parameters incompatible with libpq.
1 parent 313dd35 commit 5b1572e

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

contrib/mmts/multimaster.c

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,8 +1850,6 @@ void MtmUpdateNodeConnectionInfo(MtmConnectionInfo* conn, char const* connStr)
18501850
elog(ERROR, "Invalid raftable port: %s", port+9);
18511851
}
18521852
n += 9;
1853-
memmove(port, port+n, connStrLen - n + 1);
1854-
connStrLen -= n;
18551853
} else {
18561854
conn->raftablePort = 0;
18571855
}
@@ -1863,8 +1861,6 @@ void MtmUpdateNodeConnectionInfo(MtmConnectionInfo* conn, char const* connStr)
18631861
elog(ERROR, "Invalid arbiter port: %s", port+12);
18641862
}
18651863
n += 12;
1866-
memmove(port, port+n, connStrLen - n + 1);
1867-
connStrLen -= n;
18681864
} else {
18691865
conn->arbiterPort = 0;
18701866
}
@@ -2787,6 +2783,32 @@ typedef struct
27872783
int nodeId;
27882784
} MtmGetClusterInfoCtx;
27892785

2786+
static void erase_option_from_connstr(const char *option, char *connstr)
2787+
{
2788+
char *needle = psprintf("%s=", option);
2789+
while (1) {
2790+
char *found = strstr(connstr, needle);
2791+
if (found == NULL) break;
2792+
while (*found != '\0' && *found != ' ') {
2793+
*found = ' ';
2794+
found++;
2795+
}
2796+
}
2797+
pfree(needle);
2798+
}
2799+
2800+
PGconn *PQconnectdb_safe(const char *conninfo)
2801+
{
2802+
PGconn *conn;
2803+
char *safe_connstr = pstrdup(conninfo);
2804+
erase_option_from_connstr("raftport", safe_connstr);
2805+
erase_option_from_connstr("arbiterport", safe_connstr);
2806+
2807+
conn = PQconnectdb(safe_connstr);
2808+
2809+
pfree(safe_connstr);
2810+
return conn;
2811+
}
27902812

27912813
Datum
27922814
mtm_get_cluster_info(PG_FUNCTION_ARGS)
@@ -2819,9 +2841,9 @@ mtm_get_cluster_info(PG_FUNCTION_ARGS)
28192841
if (usrfctx->nodeId > Mtm->nAllNodes) {
28202842
SRF_RETURN_DONE(funcctx);
28212843
}
2822-
conn = PQconnectdb(Mtm->nodes[usrfctx->nodeId-1].con.connStr);
2844+
conn = PQconnectdb_safe(Mtm->nodes[usrfctx->nodeId-1].con.connStr);
28232845
if (PQstatus(conn) != CONNECTION_OK) {
2824-
elog(ERROR, "Failed to establish connection '%s' to node %d", Mtm->nodes[usrfctx->nodeId-1].con.connStr, usrfctx->nodeId);
2846+
elog(ERROR, "Failed to establish connection '%s' to node %d: error = %s", Mtm->nodes[usrfctx->nodeId-1].con.connStr, usrfctx->nodeId, PQerrorMessage(conn));
28252847
}
28262848
result = PQexec(conn, "select * from mtm.get_cluster_state()");
28272849

@@ -2996,7 +3018,7 @@ static void MtmBroadcastUtilityStmt(char const* sql, bool ignoreError)
29963018
{
29973019
if (!BIT_CHECK(disabledNodeMask, i))
29983020
{
2999-
conns[i] = PQconnectdb(psprintf("%s application_name=%s", Mtm->nodes[i].con.connStr, MULTIMASTER_BROADCAST_SERVICE));
3021+
conns[i] = PQconnectdb_safe(psprintf("%s application_name=%s", Mtm->nodes[i].con.connStr, MULTIMASTER_BROADCAST_SERVICE));
30003022
if (PQstatus(conns[i]) != CONNECTION_OK)
30013023
{
30023024
if (ignoreError)
@@ -3008,7 +3030,7 @@ static void MtmBroadcastUtilityStmt(char const* sql, bool ignoreError)
30083030
do {
30093031
PQfinish(conns[i]);
30103032
} while (--i >= 0);
3011-
elog(ERROR, "Failed to establish connection '%s' to node %d", Mtm->nodes[failedNode].con.connStr, failedNode+1);
3033+
elog(ERROR, "Failed to establish connection '%s' to node %d, error = %s", Mtm->nodes[failedNode].con.connStr, failedNode+1, PQerrorMessage(conns[i]));
30123034
}
30133035
}
30143036
PQsetNoticeReceiver(conns[i], MtmNoticeReceiver, &i);

contrib/mmts/multimaster.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "access/clog.h"
99
#include "pglogical_output/hooks.h"
10+
#include "libpq-fe.h"
1011

1112
#define DEBUG_LEVEL 0
1213

@@ -283,7 +284,7 @@ extern void MtmUpdateLsnMapping(int nodeId, XLogRecPtr endLsn);
283284
extern XLogRecPtr MtmGetFlushPosition(int nodeId);
284285
extern bool MtmWatchdog(timestamp_t now);
285286
extern void MtmCheckHeartbeat(void);
286-
287+
extern PGconn *PQconnectdb_safe(const char *conninfo);
287288

288289

289290
#endif

contrib/mmts/pglogical_receiver.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@ pglogical_receiver_main(Datum main_arg)
217217
char *copybuf = NULL;
218218
int spill_file = -1;
219219
StringInfoData spill_info;
220+
char *slotName;
220221
char* connString = psprintf("replication=database %s", Mtm->nodes[nodeId-1].con.connStr);
221-
char* slotName = psprintf(MULTIMASTER_SLOT_PATTERN, MtmNodeId);
222+
slotName = psprintf(MULTIMASTER_SLOT_PATTERN, MtmNodeId);
222223

223224
initStringInfo(&spill_info);
224225

@@ -261,7 +262,7 @@ pglogical_receiver_main(Datum main_arg)
261262
count = Mtm->recoveryCount;
262263

263264
/* Establish connection to remote server */
264-
conn = PQconnectdb(connString);
265+
conn = PQconnectdb_safe(connString);
265266
status = PQstatus(conn);
266267
if (status != CONNECTION_OK)
267268
{

0 commit comments

Comments
 (0)