@@ -1859,8 +1859,6 @@ void MtmUpdateNodeConnectionInfo(MtmConnectionInfo* conn, char const* connStr)
18591859 elog (ERROR , "Invalid raftable port: %s" , port + 9 );
18601860 }
18611861 n += 9 ;
1862- memmove (port , port + n , connStrLen - n + 1 );
1863- connStrLen -= n ;
18641862 } else {
18651863 conn -> raftablePort = 0 ;
18661864 }
@@ -1872,8 +1870,6 @@ void MtmUpdateNodeConnectionInfo(MtmConnectionInfo* conn, char const* connStr)
18721870 elog (ERROR , "Invalid arbiter port: %s" , port + 12 );
18731871 }
18741872 n += 12 ;
1875- memmove (port , port + n , connStrLen - n + 1 );
1876- connStrLen -= n ;
18771873 } else {
18781874 conn -> arbiterPort = 0 ;
18791875 }
@@ -2796,6 +2792,32 @@ typedef struct
27962792 int nodeId ;
27972793} MtmGetClusterInfoCtx ;
27982794
2795+ static void erase_option_from_connstr (const char * option , char * connstr )
2796+ {
2797+ char * needle = psprintf ("%s=" , option );
2798+ while (1 ) {
2799+ char * found = strstr (connstr , needle );
2800+ if (found == NULL ) break ;
2801+ while (* found != '\0' && * found != ' ' ) {
2802+ * found = ' ' ;
2803+ found ++ ;
2804+ }
2805+ }
2806+ pfree (needle );
2807+ }
2808+
2809+ PGconn * PQconnectdb_safe (const char * conninfo )
2810+ {
2811+ PGconn * conn ;
2812+ char * safe_connstr = pstrdup (conninfo );
2813+ erase_option_from_connstr ("raftport" , safe_connstr );
2814+ erase_option_from_connstr ("arbiterport" , safe_connstr );
2815+
2816+ conn = PQconnectdb (safe_connstr );
2817+
2818+ pfree (safe_connstr );
2819+ return conn ;
2820+ }
27992821
28002822Datum
28012823mtm_get_cluster_info (PG_FUNCTION_ARGS )
@@ -2828,9 +2850,9 @@ mtm_get_cluster_info(PG_FUNCTION_ARGS)
28282850 if (usrfctx -> nodeId > Mtm -> nAllNodes ) {
28292851 SRF_RETURN_DONE (funcctx );
28302852 }
2831- conn = PQconnectdb (Mtm -> nodes [usrfctx -> nodeId - 1 ].con .connStr );
2853+ conn = PQconnectdb_safe (Mtm -> nodes [usrfctx -> nodeId - 1 ].con .connStr );
28322854 if (PQstatus (conn ) != CONNECTION_OK ) {
2833- elog (ERROR , "Failed to establish connection '%s' to node %d" , Mtm -> nodes [usrfctx -> nodeId - 1 ].con .connStr , usrfctx -> nodeId );
2855+ elog (ERROR , "Failed to establish connection '%s' to node %d: error = %s " , Mtm -> nodes [usrfctx -> nodeId - 1 ].con .connStr , usrfctx -> nodeId , PQerrorMessage ( conn ) );
28342856 }
28352857 result = PQexec (conn , "select * from mtm.get_cluster_state()" );
28362858
@@ -3004,7 +3026,7 @@ static void MtmBroadcastUtilityStmt(char const* sql, bool ignoreError)
30043026 {
30053027 if (!BIT_CHECK (disabledNodeMask , i ))
30063028 {
3007- conns [i ] = PQconnectdb (psprintf ("%s application_name=%s" , Mtm -> nodes [i ].con .connStr , MULTIMASTER_BROADCAST_SERVICE ));
3029+ conns [i ] = PQconnectdb_safe (psprintf ("%s application_name=%s" , Mtm -> nodes [i ].con .connStr , MULTIMASTER_BROADCAST_SERVICE ));
30083030 if (PQstatus (conns [i ]) != CONNECTION_OK )
30093031 {
30103032 if (ignoreError )
@@ -3016,7 +3038,7 @@ static void MtmBroadcastUtilityStmt(char const* sql, bool ignoreError)
30163038 do {
30173039 PQfinish (conns [i ]);
30183040 } while (-- i >= 0 );
3019- elog (ERROR , "Failed to establish connection '%s' to node %d" , Mtm -> nodes [failedNode ].con .connStr , failedNode + 1 );
3041+ elog (ERROR , "Failed to establish connection '%s' to node %d, error = %s " , Mtm -> nodes [failedNode ].con .connStr , failedNode + 1 , PQerrorMessage ( conns [ i ]) );
30203042 }
30213043 }
30223044 PQsetNoticeReceiver (conns [i ], MtmNoticeReceiver , & i );
0 commit comments