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

Commit 2d7675d

Browse files
author
Vladimir Ershov
committed
status fix
1 parent f4d7ed2 commit 2d7675d

File tree

1 file changed

+49
-37
lines changed

1 file changed

+49
-37
lines changed

src/pgpro_scheduler.c

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,8 @@ int _find_stat_database(scheduler_stat_t *list, char *dbname)
615615
{
616616
int i;
617617

618+
if(list->num_db == 0) return -1;
619+
618620
for(i=0; i < list->num_db; i++)
619621
{
620622
if(strcmp(list->dats[i]->name, dbname) == 0) return i;
@@ -1132,7 +1134,7 @@ scheduler_status(PG_FUNCTION_ARGS)
11321134
current = (char *)GetConfigOption("schedule.database", false, true);
11331135
in_use = _split_string_to_char_array(current, true);
11341136

1135-
list.super_pid = InvalidPid;
1137+
list.super_pid = 0;
11361138
list.super_datname[0] = 0;
11371139
list.num_db = in_use ? in_use->n: 0;
11381140
if(list.num_db)
@@ -1146,13 +1148,17 @@ scheduler_status(PG_FUNCTION_ARGS)
11461148
if(!list.dats[i])
11471149
elog(ERROR, "cannot alloc memory for pgpro_scheduler database stat");
11481150
snprintf(list.dats[i]->name, NAMEDATALEN, "%s", in_use->data[i]);
1149-
list.dats[i]->manager_pid = InvalidPid;
1151+
list.dats[i]->manager_pid = 0;
11501152
list.dats[i]->nexecs = 0;
11511153
list.dats[i]->execs = palloc(sizeof(pid_t)*max_worker_processes);
11521154
list.dats[i]->natexecs = 0;
11531155
list.dats[i]->atexecs = palloc(sizeof(pid_t)*max_worker_processes);
11541156
}
11551157
}
1158+
else
1159+
{
1160+
list.dats = NULL;
1161+
}
11561162

11571163
num_backends = pgstat_fetch_stat_numbackends();
11581164
for(i = 1; i <= num_backends; i++)
@@ -1205,53 +1211,57 @@ scheduler_status(PG_FUNCTION_ARGS)
12051211
{
12061212
if(list.dats[i]->manager_pid == 0) nrecords++;
12071213
}
1208-
1209-
records = palloc(sizeof(scheduler_stat_record_t) * nrecords);
1210-
if(list.super_pid)
1214+
1215+
if(nrecords == 0)
12111216
{
1212-
records[nr].pid = list.super_pid;
1213-
records[nr].datname = list.super_datname;
1214-
records[nr].name = names[0];
1215-
nr++;
1217+
funcctx->max_calls = 0;
1218+
funcctx->user_fctx = NULL;
12161219
}
1217-
for(i = 0; i < list.num_db; i++)
1220+
else
12181221
{
1219-
if(list.dats[i]->manager_pid)
1222+
records = palloc(sizeof(scheduler_stat_record_t) * nrecords);
1223+
if(list.super_pid)
12201224
{
1221-
records[nr].pid = list.dats[i]->manager_pid;
1222-
records[nr].datname = list.dats[i]->name;
1223-
records[nr++].name = names[1];
1224-
if(list.dats[i]->nexecs)
1225+
records[nr].pid = list.super_pid;
1226+
records[nr].datname = list.super_datname;
1227+
records[nr++].name = names[0];
1228+
}
1229+
for(i = 0; i < list.num_db; i++)
1230+
{
1231+
if(list.dats[i]->manager_pid)
12251232
{
1226-
for(j=0; j < list.dats[i]->nexecs; j++)
1233+
records[nr].pid = list.dats[i]->manager_pid;
1234+
records[nr].datname = list.dats[i]->name;
1235+
records[nr++].name = names[1];
1236+
if(list.dats[i]->nexecs)
12271237
{
1228-
records[nr].pid = list.dats[i]->execs[j];
1229-
records[nr].datname = list.dats[i]->name;
1230-
records[nr++].name = names[2];
1238+
for(j=0; j < list.dats[i]->nexecs; j++)
1239+
{
1240+
records[nr].pid = list.dats[i]->execs[j];
1241+
records[nr].datname = list.dats[i]->name;
1242+
records[nr++].name = names[2];
1243+
}
12311244
}
1232-
}
1233-
if(list.dats[i]->natexecs)
1234-
{
1235-
for(j=0; j < list.dats[i]->natexecs; j++)
1245+
if(list.dats[i]->natexecs)
12361246
{
1237-
records[nr].pid = list.dats[i]->atexecs[j];
1238-
records[nr].datname = list.dats[i]->name;
1239-
records[nr++].name = names[3];
1247+
for(j=0; j < list.dats[i]->natexecs; j++)
1248+
{
1249+
records[nr].pid = list.dats[i]->atexecs[j];
1250+
records[nr].datname = list.dats[i]->name;
1251+
records[nr++].name = names[3];
1252+
}
12401253
}
12411254
}
1255+
else
1256+
{
1257+
records[nr].pid = 0;
1258+
records[nr].datname = list.dats[i]->name;
1259+
records[nr++].name = names[1];
1260+
}
12421261
}
1262+
funcctx->max_calls = nrecords;
1263+
funcctx->user_fctx = (void *)records;
12431264
}
1244-
for(i=0; i < list.num_db; i++)
1245-
{
1246-
if(list.dats[i]->manager_pid == 0)
1247-
{
1248-
records[nr].pid = 0;
1249-
records[nr].datname = list.dats[i]->name;
1250-
records[nr++].name = names[1];
1251-
}
1252-
}
1253-
funcctx->max_calls = nrecords;
1254-
funcctx->user_fctx = (void *)records;
12551265
MemoryContextSwitchTo(old_ctx);
12561266
}
12571267
funcctx = SRF_PERCALL_SETUP();
@@ -1260,6 +1270,8 @@ scheduler_status(PG_FUNCTION_ARGS)
12601270
tupdesc = funcctx->tuple_desc;
12611271
nrecords = funcctx->max_calls;
12621272

1273+
if(nrecords == 0) SRF_RETURN_DONE(funcctx);
1274+
12631275
records = (scheduler_stat_record_t *)funcctx->user_fctx;
12641276
if(nr < nrecords)
12651277
{

0 commit comments

Comments
 (0)