Skip to content

Commit 91e9b87

Browse files
committed
Move query metadata handling from pgsm_merge_counters to pgsm_store
1 parent 27b136a commit 91e9b87

1 file changed

Lines changed: 49 additions & 53 deletions

File tree

src/pg_stat_monitor.c

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ static void pgsm_update_counters(Counters *counters,
246246
const struct JitInstrumentation *jitusage,
247247
int parallel_workers_to_launch,
248248
int parallel_workers_launched);
249-
static void pgsm_merge_counters(Counters *dst, const Counters *src,
250-
const char *comments, int64 parentid);
249+
static void pgsm_merge_counters(Counters *dst, const Counters *src);
251250
static void pgsm_store(pgsmQueryStats *stats);
252251

253252
static void pg_stat_monitor_internal(FunctionCallInfo fcinfo,
@@ -1345,8 +1344,7 @@ pgsm_update_counters(Counters *counters,
13451344
* Merges source counters into destination counters.
13461345
*/
13471346
static void
1348-
pgsm_merge_counters(Counters *dst, const Counters *src,
1349-
const char *comments, int64 parentid)
1347+
pgsm_merge_counters(Counters *dst, const Counters *src)
13501348
{
13511349
int index;
13521350

@@ -1405,52 +1403,6 @@ pgsm_merge_counters(Counters *dst, const Counters *src,
14051403
strlcpy(dst->planinfo.plan_text, src->planinfo.plan_text, PLAN_TEXT_LEN);
14061404
}
14071405

1408-
/* copy the query metadata once */
1409-
if (pgsm_extract_comments && comments && comments[0] && !dst->info.comments[0])
1410-
strlcpy(dst->info.comments, comments, COMMENTS_LEN);
1411-
1412-
if (pgsm_track_application_names && app_name[0] != '\0' && !dst->info.application_name[0])
1413-
strlcpy(dst->info.application_name, app_name, APPLICATIONNAME_LEN);
1414-
1415-
dst->info.num_relations = num_relations;
1416-
for (int i = 0; i < num_relations; i++)
1417-
strlcpy(dst->info.relations[i], relations[i], REL_LEN);
1418-
1419-
if (nesting_level > 0 && nesting_level < max_nesting_level && parentid != 0 && pgsm_track == PGSM_TRACK_ALL)
1420-
{
1421-
if (!DsaPointerIsValid(dst->info.parent_query))
1422-
{
1423-
int parent_query_len = nested_query_txts[nesting_level - 1] ?
1424-
strlen(nested_query_txts[nesting_level - 1]) : 0;
1425-
1426-
/* If we have a parent query, store it in the raw dsa area */
1427-
if (parent_query_len > 0)
1428-
{
1429-
dsa_area *query_dsa_area = get_dsa_area_for_query_text();
1430-
1431-
/*
1432-
* Use dsa_allocate_extended with DSA_ALLOC_NO_OOM flag, as we
1433-
* don't want to get an error if memory allocation fails.
1434-
*/
1435-
dsa_pointer qry = dsa_allocate_extended(query_dsa_area, parent_query_len + 1, DSA_ALLOC_NO_OOM | DSA_ALLOC_ZERO);
1436-
1437-
if (DsaPointerIsValid(qry))
1438-
{
1439-
char *qry_buff = dsa_get_address(query_dsa_area, qry);
1440-
1441-
memcpy(qry_buff, nested_query_txts[nesting_level - 1], parent_query_len);
1442-
qry_buff[parent_query_len] = 0;
1443-
/* store the dsa pointer for parent query text */
1444-
dst->info.parent_query = qry;
1445-
}
1446-
}
1447-
}
1448-
}
1449-
else
1450-
{
1451-
Assert(!DsaPointerIsValid(dst->info.parent_query));
1452-
}
1453-
14541406
/* error info */
14551407
dst->error.elevel = src->error.elevel;
14561408
strlcpy(dst->error.sqlcode, src->error.sqlcode, SQLCODE_LEN);
@@ -1852,9 +1804,53 @@ pgsm_store(pgsmQueryStats *stats)
18521804
entry->minmax_stats_since = reset_timestamp;
18531805
}
18541806

1855-
pgsm_merge_counters(&entry->counters, &stats->counters,
1856-
pgsm_extract_comments ? comments : NULL,
1857-
entry->key.parentid);
1807+
pgsm_merge_counters(&entry->counters, &stats->counters);
1808+
1809+
/* copy the query metadata once */
1810+
if (pgsm_extract_comments && comments[0] && !entry->counters.info.comments[0])
1811+
strlcpy(entry->counters.info.comments, comments, COMMENTS_LEN);
1812+
1813+
if (pgsm_track_application_names && app_name[0] != '\0' && !entry->counters.info.application_name[0])
1814+
strlcpy(entry->counters.info.application_name, app_name, APPLICATIONNAME_LEN);
1815+
1816+
entry->counters.info.num_relations = num_relations;
1817+
for (int i = 0; i < num_relations; i++)
1818+
strlcpy(entry->counters.info.relations[i], relations[i], REL_LEN);
1819+
1820+
if (nesting_level > 0 && nesting_level < max_nesting_level && entry->key.parentid != 0 && pgsm_track == PGSM_TRACK_ALL)
1821+
{
1822+
if (!DsaPointerIsValid(entry->counters.info.parent_query))
1823+
{
1824+
int parent_query_len = nested_query_txts[nesting_level - 1] ?
1825+
strlen(nested_query_txts[nesting_level - 1]) : 0;
1826+
1827+
/* If we have a parent query, store it in the raw dsa area */
1828+
if (parent_query_len > 0)
1829+
{
1830+
dsa_area *query_dsa_area = get_dsa_area_for_query_text();
1831+
1832+
/*
1833+
* Use dsa_allocate_extended with DSA_ALLOC_NO_OOM flag, as we
1834+
* don't want to get an error if memory allocation fails.
1835+
*/
1836+
dsa_pointer qry = dsa_allocate_extended(query_dsa_area, parent_query_len + 1, DSA_ALLOC_NO_OOM | DSA_ALLOC_ZERO);
1837+
1838+
if (DsaPointerIsValid(qry))
1839+
{
1840+
char *qry_buff = dsa_get_address(query_dsa_area, qry);
1841+
1842+
memcpy(qry_buff, nested_query_txts[nesting_level - 1], parent_query_len);
1843+
qry_buff[parent_query_len] = 0;
1844+
/* store the dsa pointer for parent query text */
1845+
entry->counters.info.parent_query = qry;
1846+
}
1847+
}
1848+
}
1849+
}
1850+
else
1851+
{
1852+
Assert(!DsaPointerIsValid(entry->counters.info.parent_query));
1853+
}
18581854

18591855
SpinLockRelease(&entry->mutex);
18601856

0 commit comments

Comments
 (0)