Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * queryjumble.h
4 : * Query normalization and fingerprinting.
5 : *
6 : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : * IDENTIFICATION
10 : * src/include/nodes/queryjumble.h
11 : *
12 : *-------------------------------------------------------------------------
13 : */
14 : #ifndef QUERYJUMBLE_H
15 : #define QUERYJUMBLE_H
16 :
17 : #include "nodes/parsenodes.h"
18 :
19 : /*
20 : * Struct for tracking locations/lengths of constants during normalization
21 : */
22 : typedef struct LocationLen
23 : {
24 : int location; /* start offset in query text */
25 : int length; /* length in bytes, or -1 to ignore */
26 :
27 : /* Does this location represent a squashed list? */
28 : bool squashed;
29 :
30 : /* Is this location a PARAM_EXTERN parameter? */
31 : bool extern_param;
32 : } LocationLen;
33 :
34 : /*
35 : * Working state for computing a query jumble and producing a normalized
36 : * query string
37 : */
38 : typedef struct JumbleState
39 : {
40 : /* Jumble of current query tree */
41 : unsigned char *jumble;
42 :
43 : /* Number of bytes used in jumble[] */
44 : Size jumble_len;
45 :
46 : /* Array of locations of constants that should be removed */
47 : LocationLen *clocations;
48 :
49 : /* Allocated length of clocations array */
50 : int clocations_buf_size;
51 :
52 : /* Current number of valid entries in clocations array */
53 : int clocations_count;
54 :
55 : /*
56 : * ID of the highest PARAM_EXTERN parameter we've seen in the query; used
57 : * to start normalization correctly. However, if there are any squashed
58 : * lists in the query, we disregard query-supplied parameter numbers and
59 : * renumber everything. This is to avoid possible gaps caused by
60 : * squashing in case any params are in squashed lists.
61 : */
62 : int highest_extern_param_id;
63 :
64 : /* Whether squashable lists are present */
65 : bool has_squashed_lists;
66 :
67 : /*
68 : * Count of the number of NULL nodes seen since last appending a value.
69 : * These are flushed out to the jumble buffer before subsequent appends
70 : * and before performing the final jumble hash.
71 : */
72 : unsigned int pending_nulls;
73 :
74 : #ifdef USE_ASSERT_CHECKING
75 : /* The total number of bytes added to the jumble buffer */
76 : Size total_jumble_len;
77 : #endif
78 : } JumbleState;
79 :
80 : /* Values for the compute_query_id GUC */
81 : enum ComputeQueryIdType
82 : {
83 : COMPUTE_QUERY_ID_OFF,
84 : COMPUTE_QUERY_ID_ON,
85 : COMPUTE_QUERY_ID_AUTO,
86 : COMPUTE_QUERY_ID_REGRESS,
87 : };
88 :
89 : /* GUC parameters */
90 : extern PGDLLIMPORT int compute_query_id;
91 :
92 :
93 : extern const char *CleanQuerytext(const char *query, int *location, int *len);
94 : extern JumbleState *JumbleQuery(Query *query);
95 : extern void EnableQueryId(void);
96 :
97 : extern PGDLLIMPORT bool query_id_enabled;
98 :
99 : /*
100 : * Returns whether query identifier computation has been enabled, either
101 : * directly in the GUC or by a module when the setting is 'auto'.
102 : */
103 : static inline bool
104 444367 : IsQueryIdEnabled(void)
105 : {
106 444367 : if (compute_query_id == COMPUTE_QUERY_ID_OFF)
107 0 : return false;
108 444367 : if (compute_query_id == COMPUTE_QUERY_ID_ON)
109 129 : return true;
110 444238 : return query_id_enabled;
111 : }
112 :
113 : #endif /* QUERYJUMBLE_H */
|