Skip to content

Commit c3eda50

Browse files
committed
Change internal queryid type from uint64 to int64
uint64 was perhaps chosen in cff440d as the type was uint32 prior to that widening work. Having this as uint64 doesn't make much sense and just adds the overhead of having to remember that we always output this in its signed form. Let's remove that overhead. The signed form output is seemingly required since we have no way to represent the full range of uint64 in an SQL type. We use BIGINT in places like pg_stat_statements, which maps directly to int64. The release notes "Source Code" section may want to mention this adjustment as some extensions may wish to adjust their code. Author: David Rowley <dgrowleyml@gmail.com> Suggested-by: Peter Eisentraut <peter@eisentraut.org> Reviewed-by: Sami Imseih <samimseih@gmail.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Discussion: https://postgr.es/m/50cb0c8b-994b-48f9-a1c4-13039eb3536b@eisentraut.org
1 parent 03c53a7 commit c3eda50

File tree

16 files changed

+73
-55
lines changed

16 files changed

+73
-55
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ typedef struct pgssHashKey
144144
{
145145
Oid userid; /* user OID */
146146
Oid dbid; /* database OID */
147-
uint64 queryid; /* query identifier */
147+
int64 queryid; /* query identifier */
148148
bool toplevel; /* query executed at top level */
149149
} pgssHashKey;
150150

@@ -346,7 +346,7 @@ static void pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
346346
ProcessUtilityContext context, ParamListInfo params,
347347
QueryEnvironment *queryEnv,
348348
DestReceiver *dest, QueryCompletion *qc);
349-
static void pgss_store(const char *query, uint64 queryId,
349+
static void pgss_store(const char *query, int64 queryId,
350350
int query_location, int query_len,
351351
pgssStoreKind kind,
352352
double total_time, uint64 rows,
@@ -370,7 +370,7 @@ static char *qtext_fetch(Size query_offset, int query_len,
370370
char *buffer, Size buffer_size);
371371
static bool need_gc_qtexts(void);
372372
static void gc_qtexts(void);
373-
static TimestampTz entry_reset(Oid userid, Oid dbid, uint64 queryid, bool minmax_only);
373+
static TimestampTz entry_reset(Oid userid, Oid dbid, int64 queryid, bool minmax_only);
374374
static char *generate_normalized_query(JumbleState *jstate, const char *query,
375375
int query_loc, int *query_len_p);
376376
static void fill_in_constant_lengths(JumbleState *jstate, const char *query,
@@ -852,7 +852,7 @@ pgss_post_parse_analyze(ParseState *pstate, Query *query, JumbleState *jstate)
852852
{
853853
if (pgss_track_utility && IsA(query->utilityStmt, ExecuteStmt))
854854
{
855-
query->queryId = UINT64CONST(0);
855+
query->queryId = INT64CONST(0);
856856
return;
857857
}
858858
}
@@ -899,7 +899,7 @@ pgss_planner(Query *parse,
899899
*/
900900
if (pgss_enabled(nesting_level)
901901
&& pgss_track_planning && query_string
902-
&& parse->queryId != UINT64CONST(0))
902+
&& parse->queryId != INT64CONST(0))
903903
{
904904
instr_time start;
905905
instr_time duration;
@@ -1002,7 +1002,7 @@ pgss_ExecutorStart(QueryDesc *queryDesc, int eflags)
10021002
* counting of optimizable statements that are directly contained in
10031003
* utility statements.
10041004
*/
1005-
if (pgss_enabled(nesting_level) && queryDesc->plannedstmt->queryId != UINT64CONST(0))
1005+
if (pgss_enabled(nesting_level) && queryDesc->plannedstmt->queryId != INT64CONST(0))
10061006
{
10071007
/*
10081008
* Set up to track total elapsed time in ExecutorRun. Make sure the
@@ -1068,9 +1068,9 @@ pgss_ExecutorFinish(QueryDesc *queryDesc)
10681068
static void
10691069
pgss_ExecutorEnd(QueryDesc *queryDesc)
10701070
{
1071-
uint64 queryId = queryDesc->plannedstmt->queryId;
1071+
int64 queryId = queryDesc->plannedstmt->queryId;
10721072

1073-
if (queryId != UINT64CONST(0) && queryDesc->totaltime &&
1073+
if (queryId != INT64CONST(0) && queryDesc->totaltime &&
10741074
pgss_enabled(nesting_level))
10751075
{
10761076
/*
@@ -1111,7 +1111,7 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
11111111
DestReceiver *dest, QueryCompletion *qc)
11121112
{
11131113
Node *parsetree = pstmt->utilityStmt;
1114-
uint64 saved_queryId = pstmt->queryId;
1114+
int64 saved_queryId = pstmt->queryId;
11151115
int saved_stmt_location = pstmt->stmt_location;
11161116
int saved_stmt_len = pstmt->stmt_len;
11171117
bool enabled = pgss_track_utility && pgss_enabled(nesting_level);
@@ -1131,7 +1131,7 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
11311131
* only.
11321132
*/
11331133
if (enabled)
1134-
pstmt->queryId = UINT64CONST(0);
1134+
pstmt->queryId = INT64CONST(0);
11351135

11361136
/*
11371137
* If it's an EXECUTE statement, we don't track it and don't increment the
@@ -1278,7 +1278,7 @@ pgss_ProcessUtility(PlannedStmt *pstmt, const char *queryString,
12781278
* for the arrays in the Counters field.
12791279
*/
12801280
static void
1281-
pgss_store(const char *query, uint64 queryId,
1281+
pgss_store(const char *query, int64 queryId,
12821282
int query_location, int query_len,
12831283
pgssStoreKind kind,
12841284
double total_time, uint64 rows,
@@ -1304,7 +1304,7 @@ pgss_store(const char *query, uint64 queryId,
13041304
* Nothing to do if compute_query_id isn't enabled and no other module
13051305
* computed a query identifier.
13061306
*/
1307-
if (queryId == UINT64CONST(0))
1307+
if (queryId == INT64CONST(0))
13081308
return;
13091309

13101310
/*
@@ -1514,11 +1514,11 @@ pg_stat_statements_reset_1_7(PG_FUNCTION_ARGS)
15141514
{
15151515
Oid userid;
15161516
Oid dbid;
1517-
uint64 queryid;
1517+
int64 queryid;
15181518

15191519
userid = PG_GETARG_OID(0);
15201520
dbid = PG_GETARG_OID(1);
1521-
queryid = (uint64) PG_GETARG_INT64(2);
1521+
queryid = PG_GETARG_INT64(2);
15221522

15231523
entry_reset(userid, dbid, queryid, false);
15241524

@@ -1530,12 +1530,12 @@ pg_stat_statements_reset_1_11(PG_FUNCTION_ARGS)
15301530
{
15311531
Oid userid;
15321532
Oid dbid;
1533-
uint64 queryid;
1533+
int64 queryid;
15341534
bool minmax_only;
15351535

15361536
userid = PG_GETARG_OID(0);
15371537
dbid = PG_GETARG_OID(1);
1538-
queryid = (uint64) PG_GETARG_INT64(2);
1538+
queryid = PG_GETARG_INT64(2);
15391539
minmax_only = PG_GETARG_BOOL(3);
15401540

15411541
PG_RETURN_TIMESTAMPTZ(entry_reset(userid, dbid, queryid, minmax_only));
@@ -2671,7 +2671,7 @@ if (e) { \
26712671
* Reset entries corresponding to parameters passed.
26722672
*/
26732673
static TimestampTz
2674-
entry_reset(Oid userid, Oid dbid, uint64 queryid, bool minmax_only)
2674+
entry_reset(Oid userid, Oid dbid, int64 queryid, bool minmax_only)
26752675
{
26762676
HASH_SEQ_STATUS hash_seq;
26772677
pgssEntry *entry;
@@ -2691,7 +2691,7 @@ entry_reset(Oid userid, Oid dbid, uint64 queryid, bool minmax_only)
26912691

26922692
stats_reset = GetCurrentTimestamp();
26932693

2694-
if (userid != 0 && dbid != 0 && queryid != UINT64CONST(0))
2694+
if (userid != 0 && dbid != 0 && queryid != INT64CONST(0))
26952695
{
26962696
/* If all the parameters are available, use the fast path. */
26972697
memset(&key, 0, sizeof(pgssHashKey));
@@ -2714,7 +2714,7 @@ entry_reset(Oid userid, Oid dbid, uint64 queryid, bool minmax_only)
27142714

27152715
SINGLE_ENTRY_RESET(entry);
27162716
}
2717-
else if (userid != 0 || dbid != 0 || queryid != UINT64CONST(0))
2717+
else if (userid != 0 || dbid != 0 || queryid != INT64CONST(0))
27182718
{
27192719
/* Reset entries corresponding to valid parameters. */
27202720
hash_seq_init(&hash_seq, pgss_hash);

src/backend/access/brin/brin.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ typedef struct BrinShared
6868
int scantuplesortstates;
6969

7070
/* Query ID, for report in worker processes */
71-
uint64 queryid;
71+
int64 queryid;
7272

7373
/*
7474
* workersdonecv is used to monitor the progress of workers. All parallel

src/backend/access/nbtree/nbtsort.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ typedef struct BTShared
105105
int scantuplesortstates;
106106

107107
/* Query ID, for report in worker processes */
108-
uint64 queryid;
108+
int64 queryid;
109109

110110
/*
111111
* workersdonecv is used to monitor the progress of workers. All parallel

src/backend/commands/explain.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -811,14 +811,10 @@ ExplainPrintPlan(ExplainState *es, QueryDesc *queryDesc)
811811
* the queryid in any of the EXPLAIN plans to keep stable the results
812812
* generated by regression test suites.
813813
*/
814-
if (es->verbose && queryDesc->plannedstmt->queryId != UINT64CONST(0) &&
814+
if (es->verbose && queryDesc->plannedstmt->queryId != INT64CONST(0) &&
815815
compute_query_id != COMPUTE_QUERY_ID_REGRESS)
816816
{
817-
/*
818-
* Output the queryid as an int64 rather than a uint64 so we match
819-
* what would be seen in the BIGINT pg_stat_statements.queryid column.
820-
*/
821-
ExplainPropertyInteger("Query Identifier", NULL, (int64)
817+
ExplainPropertyInteger("Query Identifier", NULL,
822818
queryDesc->plannedstmt->queryId, es);
823819
}
824820
}

src/backend/commands/vacuumparallel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ typedef struct PVShared
6363
*/
6464
Oid relid;
6565
int elevel;
66-
uint64 queryid;
66+
int64 queryid;
6767

6868
/*
6969
* Fields for both index vacuum and cleanup.

src/backend/nodes/gen_node_support.pl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,11 @@ sub elem
10391039
print $off "\tWRITE_UINT_FIELD($f);\n";
10401040
print $rff "\tREAD_UINT_FIELD($f);\n" unless $no_read;
10411041
}
1042+
elsif ($t eq 'int64')
1043+
{
1044+
print $off "\tWRITE_INT64_FIELD($f);\n";
1045+
print $rff "\tREAD_INT64_FIELD($f);\n" unless $no_read;
1046+
}
10421047
elsif ($t eq 'uint64'
10431048
|| $t eq 'AclMode')
10441049
{

src/backend/nodes/outfuncs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ static void outDouble(StringInfo str, double d);
5151
#define WRITE_UINT_FIELD(fldname) \
5252
appendStringInfo(str, " :" CppAsString(fldname) " %u", node->fldname)
5353

54+
/* Write a signed integer field (anything written with INT64_FORMAT) */
55+
#define WRITE_INT64_FIELD(fldname) \
56+
appendStringInfo(str, \
57+
" :" CppAsString(fldname) " " INT64_FORMAT, \
58+
node->fldname)
59+
5460
/* Write an unsigned integer field (anything written with UINT64_FORMAT) */
5561
#define WRITE_UINT64_FIELD(fldname) \
5662
appendStringInfo(str, " :" CppAsString(fldname) " " UINT64_FORMAT, \

src/backend/nodes/queryjumblefuncs.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ int compute_query_id = COMPUTE_QUERY_ID_AUTO;
5656
bool query_id_enabled = false;
5757

5858
static JumbleState *InitJumble(void);
59-
static uint64 DoJumble(JumbleState *jstate, Node *node);
59+
static int64 DoJumble(JumbleState *jstate, Node *node);
6060
static void AppendJumble(JumbleState *jstate,
6161
const unsigned char *value, Size size);
6262
static void FlushPendingNulls(JumbleState *jstate);
@@ -141,12 +141,12 @@ JumbleQuery(Query *query)
141141
* If we are unlucky enough to get a hash of zero, use 1 instead for
142142
* normal statements and 2 for utility queries.
143143
*/
144-
if (query->queryId == UINT64CONST(0))
144+
if (query->queryId == INT64CONST(0))
145145
{
146146
if (query->utilityStmt)
147-
query->queryId = UINT64CONST(2);
147+
query->queryId = INT64CONST(2);
148148
else
149-
query->queryId = UINT64CONST(1);
149+
query->queryId = INT64CONST(1);
150150
}
151151

152152
return jstate;
@@ -197,7 +197,7 @@ InitJumble(void)
197197
* Jumble the given Node using the given JumbleState and return the resulting
198198
* jumble hash.
199199
*/
200-
static uint64
200+
static int64
201201
DoJumble(JumbleState *jstate, Node *node)
202202
{
203203
/* Jumble the given node */
@@ -208,9 +208,9 @@ DoJumble(JumbleState *jstate, Node *node)
208208
FlushPendingNulls(jstate);
209209

210210
/* Process the jumble buffer and produce the hash value */
211-
return DatumGetUInt64(hash_any_extended(jstate->jumble,
212-
jstate->jumble_len,
213-
0));
211+
return DatumGetInt64(hash_any_extended(jstate->jumble,
212+
jstate->jumble_len,
213+
0));
214214
}
215215

216216
/*
@@ -256,10 +256,10 @@ AppendJumbleInternal(JumbleState *jstate, const unsigned char *item,
256256

257257
if (unlikely(jumble_len >= JUMBLE_SIZE))
258258
{
259-
uint64 start_hash;
259+
int64 start_hash;
260260

261-
start_hash = DatumGetUInt64(hash_any_extended(jumble,
262-
JUMBLE_SIZE, 0));
261+
start_hash = DatumGetInt64(hash_any_extended(jumble,
262+
JUMBLE_SIZE, 0));
263263
memcpy(jumble, &start_hash, sizeof(start_hash));
264264
jumble_len = sizeof(start_hash);
265265
}

src/backend/nodes/readfuncs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
token = pg_strtok(&length); /* get field value */ \
6969
local_node->fldname = atoui(token)
7070

71+
/* Read a signed integer field (anything written using INT64_FORMAT) */
72+
#define READ_INT64_FIELD(fldname) \
73+
token = pg_strtok(&length); /* skip :fldname */ \
74+
token = pg_strtok(&length); /* get field value */ \
75+
local_node->fldname = strtoi64(token, NULL, 10)
76+
7177
/* Read an unsigned integer field (anything written using UINT64_FORMAT) */
7278
#define READ_UINT64_FIELD(fldname) \
7379
token = pg_strtok(&length); /* skip :fldname */ \

src/backend/rewrite/rewriteHandler.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4544,7 +4544,7 @@ build_generation_expression(Relation rel, int attrno)
45444544
List *
45454545
QueryRewrite(Query *parsetree)
45464546
{
4547-
uint64 input_query_id = parsetree->queryId;
4547+
int64 input_query_id = parsetree->queryId;
45484548
List *querylist;
45494549
List *results;
45504550
ListCell *l;

0 commit comments

Comments
 (0)