Skip to content

Commit 53ecbca

Browse files
committed
TMP
1 parent e36038a commit 53ecbca

2 files changed

Lines changed: 55 additions & 47 deletions

File tree

pg_stat_monitor.c

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ static int hist_bucket_count_total;
9797
static uint32 pgsm_client_ip = PGSM_INVALID_IP_MASK;
9898

9999
/* The array to store outer layer query id*/
100-
uint64 *nested_queryids;
100+
int64 *nested_queryids;
101101
char **nested_query_txts;
102102
List *lentries = NIL;
103103

@@ -195,13 +195,13 @@ DECLARE_HOOK(void pgsm_ProcessUtility, PlannedStmt *pstmt, const char *queryStri
195195
DestReceiver *dest,
196196
QueryCompletion *qc);
197197
#endif
198-
static uint64 pgsm_hash_string(const char *str, int len);
198+
static int64 pgsm_hash_string(const char *str, int len);
199199
char *unpack_sql_state(int sql_state);
200200

201-
static pgsmEntry *pgsm_create_hash_entry(uint64 bucket_id, uint64 queryid, PlanInfo *plan_info);
201+
static pgsmEntry *pgsm_create_hash_entry(uint64 bucket_id, int64 queryid, PlanInfo *plan_info);
202202
static void pgsm_add_to_list(pgsmEntry *entry, char *query_text, int query_len);
203-
static pgsmEntry *pgsm_get_entry_for_query(uint64 queryid, PlanInfo *plan_info, const char *query_text, int query_len, bool create, CmdType cmd_type);
204-
static uint64 get_pgsm_query_id_hash(const char *norm_query, int len);
203+
static pgsmEntry *pgsm_get_entry_for_query(int64 queryid, PlanInfo *plan_info, const char *query_text, int query_len, bool create, CmdType cmd_type);
204+
static int64 get_pgsm_query_id_hash(const char *norm_query, int len);
205205

206206
static void pgsm_cleanup_callback(void *arg);
207207
static void pgsm_store_error(const char *query, ErrorData *edata);
@@ -248,7 +248,7 @@ static void RecordConstLocation(JumbleState *jstate, int location);
248248
* relevant part of the string.
249249
*/
250250
static const char *CleanQuerytext(const char *query, int *location, int *len);
251-
static uint64 get_query_id(JumbleState *jstate, Query *query);
251+
static int64 get_query_id(JumbleState *jstate, Query *query);
252252
#endif
253253

254254
static char *generate_normalized_query(JumbleState *jstate, const char *query,
@@ -336,7 +336,7 @@ _PG_init(void)
336336
prev_ExecutorCheckPerms_hook = ExecutorCheckPerms_hook;
337337
ExecutorCheckPerms_hook = HOOK(pgsm_ExecutorCheckPerms);
338338

339-
nested_queryids = (uint64 *) malloc(sizeof(uint64) * max_stack_depth);
339+
nested_queryids = (int64 *) malloc(sizeof(int64) * max_stack_depth);
340340
nested_query_txts = (char **) malloc(sizeof(char *) * max_stack_depth);
341341

342342
system_init = true;
@@ -431,7 +431,7 @@ pgsm_post_parse_analyze_internal(ParseState *pstate, Query *query, JumbleState *
431431
if (query->utilityStmt)
432432
{
433433
if (pgsm_track_utility && IsA(query->utilityStmt, ExecuteStmt))
434-
query->queryId = UINT64CONST(0);
434+
query->queryId = INT64CONST(0);
435435

436436
return;
437437
}
@@ -448,8 +448,8 @@ pgsm_post_parse_analyze_internal(ParseState *pstate, Query *query, JumbleState *
448448
* If we are unlucky enough to get a hash of zero, use 1 instead, to
449449
* prevent confusion with the utility-statement case.
450450
*/
451-
if (query->queryId == UINT64CONST(0))
452-
query->queryId = UINT64CONST(1);
451+
if (query->queryId == INT64CONST(0))
452+
query->queryId = INT64CONST(1);
453453

454454
/*
455455
* Let's save the normalized query so that we can save the data without in
@@ -564,7 +564,7 @@ pgsm_ExecutorStart(QueryDesc *queryDesc, int eflags)
564564
* utility statements.
565565
*/
566566
if (pgsm_enabled(nesting_level) &&
567-
queryDesc->plannedstmt->queryId != UINT64CONST(0))
567+
queryDesc->plannedstmt->queryId != INT64CONST(0))
568568
{
569569
/*
570570
* Set up to track total elapsed time in ExecutorRun. Make sure the
@@ -626,7 +626,7 @@ pgsm_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
626626
nesting_level--;
627627
if (nesting_level >= 0 && nesting_level < max_stack_depth)
628628
{
629-
nested_queryids[nesting_level] = UINT64CONST(0);
629+
nested_queryids[nesting_level] = INT64CONST(0);
630630
if (nested_query_txts[nesting_level])
631631
free(nested_query_txts[nesting_level]);
632632
nested_query_txts[nesting_level] = NULL;
@@ -637,7 +637,7 @@ pgsm_ExecutorRun(QueryDesc *queryDesc, ScanDirection direction, uint64 count)
637637
nesting_level--;
638638
if (nesting_level >= 0 && nesting_level < max_stack_depth)
639639
{
640-
nested_queryids[nesting_level] = UINT64CONST(0);
640+
nested_queryids[nesting_level] = INT64CONST(0);
641641
if (nested_query_txts[nesting_level])
642642
free(nested_query_txts[nesting_level]);
643643
nested_query_txts[nesting_level] = NULL;
@@ -698,7 +698,11 @@ pgsm_explain(QueryDesc *queryDesc)
698698
static void
699699
pgsm_ExecutorEnd(QueryDesc *queryDesc)
700700
{
701-
uint64 queryId = queryDesc->plannedstmt->queryId;
701+
#if PG_VERSION_NUM < 180000
702+
int64 queryId = queryDesc->plannedstmt->queryId;
703+
#else
704+
int64 queryId = (int64) queryDesc->plannedstmt->queryId;
705+
#endif
702706
SysInfo sys_info;
703707
PlanInfo plan_info;
704708
PlanInfo *plan_ptr = NULL;
@@ -733,12 +737,12 @@ pgsm_ExecutorEnd(QueryDesc *queryDesc)
733737
MemoryContextSwitchTo(oldctx);
734738
}
735739

736-
if (queryId != UINT64CONST(0) && queryDesc->totaltime && pgsm_enabled(nesting_level))
740+
if (queryId != INT64CONST(0) && queryDesc->totaltime && pgsm_enabled(nesting_level))
737741
{
738742
entry = pgsm_get_entry_for_query(queryId, plan_ptr, (char *) queryDesc->sourceText, strlen(queryDesc->sourceText), true, queryDesc->operation);
739743
if (!entry)
740744
{
741-
elog(DEBUG2, "[pg_stat_monitor] pgsm_ExecutorEnd: Failed to find entry for [%lu] %s.", queryId, queryDesc->sourceText);
745+
elog(DEBUG2, "[pg_stat_monitor] pgsm_ExecutorEnd: Failed to find entry for [%ld] %s.", queryId, queryDesc->sourceText);
742746
return;
743747
}
744748

@@ -861,7 +865,11 @@ static PlannedStmt *
861865
pgsm_planner_hook(Query *parse, const char *query_string, int cursorOptions, ParamListInfo boundParams)
862866
{
863867
PlannedStmt *result;
864-
868+
#if PG_VERSION_NUM < 180000
869+
int64 queryId = parse->queryId;
870+
#else
871+
int64 queryId = (int64) parse->queryId;
872+
#endif
865873

866874
/*
867875
* We can't process the query if no query_string is provided, as
@@ -883,7 +891,7 @@ pgsm_planner_hook(Query *parse, const char *query_string, int cursorOptions, Par
883891
enabled = pgsm_enabled(plan_nested_level + nesting_level);
884892
#endif
885893

886-
if (enabled && pgsm_track_planning && query_string && parse->queryId != UINT64CONST(0))
894+
if (enabled && pgsm_track_planning && query_string && queryId != INT64CONST(0))
887895
{
888896
pgsmEntry *entry = NULL;
889897
instr_time start;
@@ -904,7 +912,7 @@ pgsm_planner_hook(Query *parse, const char *query_string, int cursorOptions, Par
904912
INSTR_TIME_SET_CURRENT(start);
905913

906914
if (MemoryContextIsValid(MessageContext))
907-
entry = pgsm_get_entry_for_query(parse->queryId, NULL, query_string, strlen(query_string), true, parse->commandType);
915+
entry = pgsm_get_entry_for_query(queryId, NULL, query_string, strlen(query_string), true, parse->commandType);
908916

909917
#if PG_VERSION_NUM >= 170000
910918
nesting_level++;
@@ -1026,7 +1034,7 @@ pgsm_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
10261034
#endif
10271035
{
10281036
Node *parsetree = pstmt->utilityStmt;
1029-
uint64 queryId = 0;
1037+
int64 queryId = 0;
10301038
bool enabled = pgsm_track_utility && pgsm_enabled(nesting_level);
10311039

10321040
#if PG_VERSION_NUM < 140000
@@ -1046,7 +1054,7 @@ pgsm_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
10461054
* level.
10471055
*/
10481056
if (enabled)
1049-
pstmt->queryId = UINT64CONST(0);
1057+
pstmt->queryId = INT64CONST(0);
10501058
#endif
10511059

10521060
/*
@@ -1265,10 +1273,10 @@ pgsm_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
12651273
* identifying the query, without normalizing constants. Used when hashing
12661274
* utility statements.
12671275
*/
1268-
static uint64
1276+
static int64
12691277
pgsm_hash_string(const char *str, int len)
12701278
{
1271-
return DatumGetUInt64(hash_any_extended((const unsigned char *) str,
1279+
return DatumGetInt64(hash_any_extended((const unsigned char *) str,
12721280
len, 0));
12731281
}
12741282

@@ -1636,7 +1644,7 @@ static void
16361644
pgsm_store_error(const char *query, ErrorData *edata)
16371645
{
16381646
pgsmEntry *entry;
1639-
uint64 queryid = 0;
1647+
int64 queryid = 0;
16401648
int len = strlen(query);
16411649

16421650
if (!query || len == 0)
@@ -1670,7 +1678,7 @@ pgsm_add_to_list(pgsmEntry *entry, char *query_text, int query_len)
16701678
}
16711679

16721680
static pgsmEntry *
1673-
pgsm_get_entry_for_query(uint64 queryid, PlanInfo *plan_info, const char *query_text, int query_len, bool create, CmdType cmd_type)
1681+
pgsm_get_entry_for_query(int64 queryid, PlanInfo *plan_info, const char *query_text, int query_len, bool create, CmdType cmd_type)
16741682
{
16751683
pgsmEntry *entry = NULL;
16761684
ListCell *lc = NULL;
@@ -1729,7 +1737,7 @@ pgsm_cleanup_callback(void *arg)
17291737
* The bucket_id may not be known at this stage. So pass any value that you may wish.
17301738
*/
17311739
static pgsmEntry *
1732-
pgsm_create_hash_entry(uint64 bucket_id, uint64 queryid, PlanInfo *plan_info)
1740+
pgsm_create_hash_entry(uint64 bucket_id, int64 queryid, PlanInfo *plan_info)
17331741
{
17341742
pgsmEntry *entry;
17351743
int sec_ctx;
@@ -1898,7 +1906,7 @@ pgsm_store(pgsmEntry *entry)
18981906
}
18991907
else
19001908
{
1901-
entry->key.parentid = UINT64CONST(0);
1909+
entry->key.parentid = INT64CONST(0);
19021910
}
19031911

19041912
#if PG_VERSION_NUM >= 170000
@@ -2189,13 +2197,13 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
21892197
Counters tmp;
21902198
pgsmHashKey tmpkey;
21912199
double stddev;
2192-
uint64 queryid = entry->key.queryid;
2200+
int64 queryid = entry->key.queryid;
21932201
int64 bucketid = entry->key.bucket_id;
21942202
Oid dbid = entry->key.dbid;
21952203
Oid userid = entry->key.userid;
2196-
uint64 ip = (uint64) entry->key.ip;
2197-
uint64 planid = entry->key.planid;
2198-
uint64 pgsm_query_id = entry->pgsm_query_id;
2204+
uint32 ip = entry->key.ip;
2205+
int64 planid = entry->key.planid;
2206+
int64 pgsm_query_id = entry->pgsm_query_id;
21992207
dsa_area *query_dsa_area;
22002208
char *query_ptr;
22012209
char *query_txt = NULL;
@@ -2241,7 +2249,7 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
22412249
}
22422250

22432251
/* read the parent query text if any */
2244-
if (tmpkey.parentid != UINT64CONST(0))
2252+
if (tmpkey.parentid != INT64CONST(0))
22452253
{
22462254
if (DsaPointerIsValid(tmp.info.parent_query))
22472255
{
@@ -2277,12 +2285,12 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
22772285
nulls[i++] = true;
22782286

22792287
/* queryid at column number 6 */
2280-
values[i++] = UInt64GetDatum(queryid);
2288+
values[i++] = Int64GetDatum(queryid);
22812289

22822290
/* planid at column number 7 */
22832291
if (planid)
22842292
{
2285-
values[i++] = UInt64GetDatum(planid);
2293+
values[i++] = Int64GetDatum(planid);
22862294
}
22872295
else
22882296
{
@@ -2322,14 +2330,14 @@ pg_stat_monitor_internal(FunctionCallInfo fcinfo,
23222330

23232331
/* pgsm_query_id at column number 10 */
23242332
if (pgsm_query_id)
2325-
values[i++] = UInt64GetDatum(pgsm_query_id);
2333+
values[i++] = Int64GetDatum(pgsm_query_id);
23262334
else
23272335
nulls[i++] = true;
23282336

23292337
/* parentid at column number 9 */
2330-
if (tmpkey.parentid != UINT64CONST(0))
2338+
if (tmpkey.parentid != INT64CONST(0))
23312339
{
2332-
values[i++] = UInt64GetDatum(tmpkey.parentid);
2340+
values[i++] = Int64GetDatum(tmpkey.parentid);
23332341
values[i++] = CStringGetTextDatum(parent_query_txt);
23342342
}
23352343
else
@@ -2620,13 +2628,13 @@ get_next_wbucket(pgsmSharedState *pgsm)
26202628
* newline, tab, or any other type. Trailing and leading spaces
26212629
* are also removed before calculating the hash.
26222630
*/
2623-
uint64
2631+
int64
26242632
get_pgsm_query_id_hash(const char *norm_query, int norm_len)
26252633
{
26262634
char *query;
26272635
char *q_iter;
26282636
char *norm_q_iter = (char *) norm_query;
2629-
uint64 pgsm_query_id = 0;
2637+
int64 pgsm_query_id = 0;
26302638

26312639
if (!pgsm_enable_pgsm_query_id)
26322640
return 0;
@@ -3954,10 +3962,10 @@ extract_query_comments(const char *query, char *comments, size_t max_len)
39543962
}
39553963

39563964
#if PG_VERSION_NUM < 140000
3957-
static uint64
3965+
static int64
39583966
get_query_id(JumbleState *jstate, Query *query)
39593967
{
3960-
uint64 queryid;
3968+
int64 queryid;
39613969

39623970
/* Set up workspace for query jumbling */
39633971
jstate->jumble = (unsigned char *) palloc(JUMBLE_SIZE);

pg_stat_monitor.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,22 +184,22 @@ typedef struct CallTime
184184

185185
typedef struct PlanInfo
186186
{
187-
uint64 planid; /* plan identifier */
187+
int64 planid; /* plan identifier */
188188
char plan_text[PLAN_TEXT_LEN]; /* plan text */
189189
size_t plan_len; /* strlen(plan_text) */
190190
} PlanInfo;
191191

192192
typedef struct pgsmHashKey
193193
{
194194
uint64 bucket_id; /* bucket number */
195-
uint64 queryid; /* query identifier */
196-
uint64 planid; /* plan identifier */
197-
uint64 appid; /* hash of application name */
195+
int64 queryid; /* query identifier */
196+
int64 planid; /* plan identifier */
197+
int64 appid; /* hash of application name */
198198
Oid userid; /* user OID */
199199
Oid dbid; /* database OID */
200200
uint32 ip; /* client ip address */
201201
bool toplevel; /* query executed at top level */
202-
uint64 parentid; /* parent queryid of current query */
202+
int64 parentid; /* parent queryId of current query */
203203
} pgsmHashKey;
204204

205205
typedef struct QueryInfo
@@ -337,7 +337,7 @@ typedef struct Counters
337337
typedef struct pgsmEntry
338338
{
339339
pgsmHashKey key; /* hash key of entry - MUST BE FIRST */
340-
uint64 pgsm_query_id; /* pgsm generate normalized query hash */
340+
int64 pgsm_query_id; /* pgsm generate normalized query hash */
341341
char datname[NAMEDATALEN]; /* database name */
342342
char username[NAMEDATALEN]; /* user name */
343343
Counters counters; /* the statistics for this query */

0 commit comments

Comments
 (0)