@@ -107,6 +107,7 @@ PG_FUNCTION_INFO_V1(mtm_recover_node);
107
107
PG_FUNCTION_INFO_V1 (mtm_get_snapshot );
108
108
PG_FUNCTION_INFO_V1 (mtm_get_nodes_state );
109
109
PG_FUNCTION_INFO_V1 (mtm_get_cluster_state );
110
+ PG_FUNCTION_INFO_V1 (mtm_get_cluster_info );
110
111
PG_FUNCTION_INFO_V1 (mtm_make_table_local );
111
112
PG_FUNCTION_INFO_V1 (mtm_dump_lock_graph );
112
113
@@ -1608,7 +1609,7 @@ _PG_init(void)
1608
1609
"Minamal amount of time (milliseconds) to wait 2PC confirmation from all nodes" ,
1609
1610
"Timeout for 2PC is calculated as MAX(prepare_time*2pc_prepare_ratio/100,2pc_min_timeout)" ,
1610
1611
& Mtm2PCMinTimeout ,
1611
- 10000 ,
1612
+ 100000 , /* 100 seconds */
1612
1613
0 ,
1613
1614
INT_MAX ,
1614
1615
PGC_BACKEND ,
@@ -1623,7 +1624,7 @@ _PG_init(void)
1623
1624
"Percent of prepare time for maximal time of second phase of two-pahse commit" ,
1624
1625
"Timeout for 2PC is calculated as MAX(prepare_time*2pc_prepare_ratio/100,2pc_min_timeout)" ,
1625
1626
& Mtm2PCPrepareRatio ,
1626
- 100 ,
1627
+ 1000 , /* 10 times */
1627
1628
0 ,
1628
1629
INT_MAX ,
1629
1630
PGC_BACKEND ,
@@ -2177,10 +2178,9 @@ mtm_get_snapshot(PG_FUNCTION_ARGS)
2177
2178
typedef struct
2178
2179
{
2179
2180
int nodeId ;
2180
- char * connStrPtr ;
2181
2181
TupleDesc desc ;
2182
- Datum values [8 ];
2183
- bool nulls [8 ];
2182
+ Datum values [Natts_mtm_nodes_state ];
2183
+ bool nulls [Natts_mtm_nodes_state ];
2184
2184
} MtmGetNodeStateCtx ;
2185
2185
2186
2186
Datum
@@ -2189,7 +2189,6 @@ mtm_get_nodes_state(PG_FUNCTION_ARGS)
2189
2189
FuncCallContext * funcctx ;
2190
2190
MtmGetNodeStateCtx * usrfctx ;
2191
2191
MemoryContext oldcontext ;
2192
- char * p ;
2193
2192
int64 lag ;
2194
2193
bool is_first_call = SRF_IS_FIRSTCALL ();
2195
2194
@@ -2199,7 +2198,6 @@ mtm_get_nodes_state(PG_FUNCTION_ARGS)
2199
2198
usrfctx = (MtmGetNodeStateCtx * )palloc (sizeof (MtmGetNodeStateCtx ));
2200
2199
get_call_result_type (fcinfo , NULL , & usrfctx -> desc );
2201
2200
usrfctx -> nodeId = 1 ;
2202
- usrfctx -> connStrPtr = pstrdup (MtmConnStrs );
2203
2201
memset (usrfctx -> nulls , false, sizeof (usrfctx -> nulls ));
2204
2202
funcctx -> user_fctx = usrfctx ;
2205
2203
MemoryContextSwitchTo (oldcontext );
@@ -2218,23 +2216,19 @@ mtm_get_nodes_state(PG_FUNCTION_ARGS)
2218
2216
usrfctx -> nulls [4 ] = lag < 0 ;
2219
2217
usrfctx -> values [5 ] = Int64GetDatum (Mtm -> transCount ? Mtm -> nodes [usrfctx -> nodeId - 1 ].transDelay /Mtm -> transCount : 0 );
2220
2218
usrfctx -> values [6 ] = TimestampTzGetDatum (time_t_to_timestamptz (Mtm -> nodes [usrfctx -> nodeId - 1 ].lastStatusChangeTime ));
2221
- p = strchr (usrfctx -> connStrPtr , ',' );
2222
- if (p != NULL ) {
2223
- * p ++ = '\0' ;
2224
- }
2225
- usrfctx -> values [7 ] = CStringGetTextDatum (usrfctx -> connStrPtr );
2226
- usrfctx -> connStrPtr = p ;
2219
+ usrfctx -> values [7 ] = CStringGetTextDatum (Mtm -> nodes [usrfctx -> nodeId - 1 ].con .connStr );
2227
2220
usrfctx -> nodeId += 1 ;
2228
2221
2229
2222
SRF_RETURN_NEXT (funcctx , HeapTupleGetDatum (heap_form_tuple (usrfctx -> desc , usrfctx -> values , usrfctx -> nulls )));
2230
2223
}
2231
2224
2225
+
2232
2226
Datum
2233
2227
mtm_get_cluster_state (PG_FUNCTION_ARGS )
2234
2228
{
2235
2229
TupleDesc desc ;
2236
- Datum values [10 ];
2237
- bool nulls [10 ] = {false};
2230
+ Datum values [Natts_mtm_cluster_state ];
2231
+ bool nulls [Natts_mtm_cluster_state ] = {false};
2238
2232
get_call_result_type (fcinfo , NULL , & desc );
2239
2233
2240
2234
values [0 ] = CStringGetTextDatum (MtmNodeStatusMnem [Mtm -> status ]);
@@ -2243,16 +2237,73 @@ mtm_get_cluster_state(PG_FUNCTION_ARGS)
2243
2237
values [3 ] = Int64GetDatum (Mtm -> nodeLockerMask );
2244
2238
values [4 ] = Int32GetDatum (Mtm -> nNodes );
2245
2239
values [5 ] = Int32GetDatum ((int )Mtm -> pool .active );
2246
- values [6 ] = Int64GetDatum ( BgwPoolGetQueueSize ( & Mtm -> pool ) );
2247
- values [7 ] = Int64GetDatum (Mtm -> transCount );
2248
- values [8 ] = Int64GetDatum (Mtm -> timeShift );
2249
- values [9 ] = Int32GetDatum (Mtm -> recoverySlot );
2250
- nulls [ 9 ] = Mtm -> recoverySlot == 0 ;
2240
+ values [6 ] = Int32GetDatum (( int ) Mtm -> pool . pending );
2241
+ values [7 ] = Int64GetDatum (BgwPoolGetQueueSize ( & Mtm -> pool ) );
2242
+ values [8 ] = Int64GetDatum (Mtm -> transCount );
2243
+ values [9 ] = Int64GetDatum (Mtm -> timeShift );
2244
+ values [ 10 ] = Int32GetDatum ( Mtm -> recoverySlot ) ;
2251
2245
2252
2246
PG_RETURN_DATUM (HeapTupleGetDatum (heap_form_tuple (desc , values , nulls )));
2253
2247
}
2254
2248
2255
2249
2250
+ typedef struct
2251
+ {
2252
+ int nodeId ;
2253
+ } MtmGetClusterInfoCtx ;
2254
+
2255
+
2256
+ Datum
2257
+ mtm_get_cluster_info (PG_FUNCTION_ARGS )
2258
+ {
2259
+
2260
+ FuncCallContext * funcctx ;
2261
+ MtmGetClusterInfoCtx * usrfctx ;
2262
+ MemoryContext oldcontext ;
2263
+ TupleDesc desc ;
2264
+ bool is_first_call = SRF_IS_FIRSTCALL ();
2265
+ int i ;
2266
+ PGconn * conn ;
2267
+ PGresult * result ;
2268
+ char * values [Natts_mtm_cluster_state ];
2269
+ HeapTuple tuple ;
2270
+
2271
+ if (is_first_call ) {
2272
+ funcctx = SRF_FIRSTCALL_INIT ();
2273
+ oldcontext = MemoryContextSwitchTo (funcctx -> multi_call_memory_ctx );
2274
+ usrfctx = (MtmGetClusterInfoCtx * )palloc (sizeof (MtmGetNodeStateCtx ));
2275
+ get_call_result_type (fcinfo , NULL , & desc );
2276
+ funcctx -> attinmeta = TupleDescGetAttInMetadata (desc );
2277
+ usrfctx -> nodeId = 1 ;
2278
+ funcctx -> user_fctx = usrfctx ;
2279
+ MemoryContextSwitchTo (oldcontext );
2280
+ }
2281
+ funcctx = SRF_PERCALL_SETUP ();
2282
+ usrfctx = (MtmGetClusterInfoCtx * )funcctx -> user_fctx ;
2283
+ if (usrfctx -> nodeId > MtmNodes ) {
2284
+ SRF_RETURN_DONE (funcctx );
2285
+ }
2286
+ conn = PQconnectdb (Mtm -> nodes [usrfctx -> nodeId - 1 ].con .connStr );
2287
+ if (PQstatus (conn ) != CONNECTION_OK ) {
2288
+ elog (ERROR , "Failed to establish connection '%s' to node %d" , Mtm -> nodes [usrfctx -> nodeId - 1 ].con .connStr , usrfctx -> nodeId );
2289
+ }
2290
+ result = PQexec (conn , "select * from mtm.get_cluster_state()" );
2291
+
2292
+ if (PQresultStatus (result ) != PGRES_TUPLES_OK || PQntuples (result ) != 1 ) {
2293
+ elog (ERROR , "Failed to receive data from %d" , usrfctx -> nodeId );
2294
+ }
2295
+
2296
+ for (i = 0 ; i < Natts_mtm_cluster_state ; i ++ ) {
2297
+ values [i ] = PQgetvalue (result , 0 , i );
2298
+ }
2299
+ tuple = BuildTupleFromCStrings (funcctx -> attinmeta , values );
2300
+ PQclear (result );
2301
+ PQfinish (conn );
2302
+ usrfctx -> nodeId += 1 ;
2303
+ SRF_RETURN_NEXT (funcctx , HeapTupleGetDatum (tuple ));
2304
+ }
2305
+
2306
+
2256
2307
Datum mtm_make_table_local (PG_FUNCTION_ARGS )
2257
2308
{
2258
2309
Oid reloid = PG_GETARG_OID (1 );
0 commit comments