query-id-with-xor.patch
text/x-diff
Filename: query-id-with-xor.patch
Type: text/x-diff
Part: 1
Patch
Format: unified
| File | + | − |
|---|---|---|
| src/backend/utils/misc/queryjumble.c | 12 | 30 |
| src/include/utils/queryjumble.h | 7 | 7 |
diff --git a/src/backend/utils/misc/queryjumble.c b/src/backend/utils/misc/queryjumble.c
index f004a9ce8c..225940d40c 100644
--- a/src/backend/utils/misc/queryjumble.c
+++ b/src/backend/utils/misc/queryjumble.c
@@ -106,9 +106,10 @@ JumbleQuery(Query *query, const char *querytext)
{
jstate = (JumbleState *) palloc(sizeof(JumbleState));
- /* Set up workspace for query jumbling */
- jstate->jumble = (unsigned char *) palloc(JUMBLE_SIZE);
- jstate->jumble_len = 0;
+ /* Initialize state for query jumbling */
+ jstate->j.id = 0;
+ jstate->nextbyte = 0;
+ /* Initialize state for tracking where constants are */
jstate->clocations_buf_size = 32;
jstate->clocations = (LocationLen *)
palloc(jstate->clocations_buf_size * sizeof(LocationLen));
@@ -117,9 +118,7 @@ JumbleQuery(Query *query, const char *querytext)
/* Compute query ID and mark the Query node with it */
JumbleQueryInternal(jstate, query);
- query->queryId = DatumGetUInt64(hash_any_extended(jstate->jumble,
- jstate->jumble_len,
- 0));
+ query->queryId = jstate->j.id;
/*
* If we are unlucky enough to get a hash of zero, use 1 instead, to
@@ -167,34 +166,17 @@ compute_utility_query_id(const char *query_text, int query_location, int query_l
static void
AppendJumble(JumbleState *jstate, const unsigned char *item, Size size)
{
- unsigned char *jumble = jstate->jumble;
- Size jumble_len = jstate->jumble_len;
+ int nextbyte = jstate->nextbyte;
- /*
- * Whenever the jumble buffer is full, we hash the current contents and
- * reset the buffer to contain just that hash value, thus relying on the
- * hash to summarize everything so far.
- */
while (size > 0)
{
- Size part_size;
-
- if (jumble_len >= JUMBLE_SIZE)
- {
- uint64 start_hash;
-
- start_hash = DatumGetUInt64(hash_any_extended(jumble,
- JUMBLE_SIZE, 0));
- memcpy(jumble, &start_hash, sizeof(start_hash));
- jumble_len = sizeof(start_hash);
- }
- part_size = Min(size, JUMBLE_SIZE - jumble_len);
- memcpy(jumble + jumble_len, item, part_size);
- jumble_len += part_size;
- item += part_size;
- size -= part_size;
+ jstate->j.bytes[nextbyte] ^= *item++;
+ nextbyte++;
+ if (nextbyte >= sizeof(uint64))
+ nextbyte = 0;
+ size--;
}
- jstate->jumble_len = jumble_len;
+ jstate->nextbyte = nextbyte;
}
/*
diff --git a/src/include/utils/queryjumble.h b/src/include/utils/queryjumble.h
index 83ba7339fa..d01712af4d 100644
--- a/src/include/utils/queryjumble.h
+++ b/src/include/utils/queryjumble.h
@@ -16,8 +16,6 @@
#include "nodes/parsenodes.h"
-#define JUMBLE_SIZE 1024 /* query serialization buffer size */
-
/*
* Struct for tracking locations/lengths of constants during normalization
*/
@@ -33,11 +31,13 @@ typedef struct LocationLen
*/
typedef struct JumbleState
{
- /* Jumble of current query tree */
- unsigned char *jumble;
-
- /* Number of bytes used in jumble[] */
- Size jumble_len;
+ /* Working state for accumulating a 64-bit query ID */
+ union
+ {
+ unsigned char bytes[sizeof(uint64)];
+ uint64 id;
+ } j;
+ int nextbyte; /* index of next byte to update in j.bytes[] */
/* Array of locations of constants that should be removed */
LocationLen *clocations;