Skip to content

Commit 4458df4

Browse files
committed
Do not copy query texts when listing tracked statements
As already CStringGetTextDatum() copies the strings there is no reason to run pstrdup(). Additionally make the variable name more consistent with other variables and move the dsa reference to the right scope.
1 parent 6a71052 commit 4458df4

1 file changed

Lines changed: 12 additions & 18 deletions

File tree

src/pg_stat_monitor.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2074,22 +2074,20 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
20742074
uint32 ip = entry->key.ip;
20752075
int64 planid = entry->key.planid;
20762076
int64 pgsm_query_id = entry->pgsm_query_id;
2077-
dsa_area *query_dsa_area;
2078-
char *query_ptr;
2079-
char *query_txt;
2080-
char *parent_query_txt = NULL;
2077+
char *query_text;
2078+
char *parent_query_text = NULL;
20812079
bool toplevel = entry->key.toplevel;
20822080

20832081
/* Load the query text from dsa area */
20842082
if (DsaPointerIsValid(entry->query_text.query_pos))
20852083
{
2084+
dsa_area *query_dsa_area;
2085+
20862086
query_dsa_area = get_dsa_area_for_query_text();
2087-
query_ptr = dsa_get_address(query_dsa_area, entry->query_text.query_pos);
2088-
query_txt = pstrdup(query_ptr);
2087+
query_text = dsa_get_address(query_dsa_area, entry->query_text.query_pos);
20892088
}
20902089
else
2091-
query_txt = pstrdup("Query string not available"); /* Should never happen.
2092-
* Just a safety check */
2090+
query_text = "Query string not available"; /* Should never happen */
20932091

20942092
/* copy counters to a local variable to keep locking time short */
20952093
SpinLockAcquire(&entry->mutex);
@@ -2112,12 +2110,13 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
21122110
{
21132111
if (DsaPointerIsValid(tmp.info.parent_query))
21142112
{
2113+
dsa_area *query_dsa_area;
2114+
21152115
query_dsa_area = get_dsa_area_for_query_text();
2116-
query_ptr = dsa_get_address(query_dsa_area, tmp.info.parent_query);
2117-
parent_query_txt = pstrdup(query_ptr);
2116+
parent_query_text = dsa_get_address(query_dsa_area, tmp.info.parent_query);
21182117
}
21192118
else
2120-
parent_query_txt = pstrdup("parent query text not available");
2119+
parent_query_text = "parent query text not available";
21212120
}
21222121

21232122
/* bucketid at column number 0 */
@@ -2158,7 +2157,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
21582157
if (showtext)
21592158
{
21602159
/* query at column number 8 */
2161-
values[i++] = CStringGetTextDatum(query_txt);
2160+
values[i++] = CStringGetTextDatum(query_text);
21622161
/* plan at column number 9 */
21632162
if (planid && tmp.planinfo.plan_text[0])
21642163
values[i++] = CStringGetTextDatum(tmp.planinfo.plan_text);
@@ -2190,7 +2189,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
21902189
if (tmpkey.parentid != INT64CONST(0))
21912190
{
21922191
values[i++] = Int64GetDatum(tmpkey.parentid);
2193-
values[i++] = CStringGetTextDatum(parent_query_txt);
2192+
values[i++] = CStringGetTextDatum(parent_query_text);
21942193
}
21952194
else
21962195
{
@@ -2410,11 +2409,6 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
24102409

24112410
/* clean up and return the tuplestore */
24122411
tuplestore_putvalues(tupstore, tupdesc, values, nulls);
2413-
2414-
if (query_txt)
2415-
pfree(query_txt);
2416-
if (parent_query_txt)
2417-
pfree(parent_query_txt);
24182412
}
24192413
/* clean up and return the tuplestore */
24202414
pgsm_lock_release(pgsm);

0 commit comments

Comments
 (0)