1 /*-------------------------------------------------------------------------
2  *
3  * queryjumble.h
4  *	  Query normalization and fingerprinting.
5  *
6  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * IDENTIFICATION
10  *	  src/include/utils/queryjumble.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef QUERYJUBLE_H
15 #define QUERYJUBLE_H
16 
17 #include "nodes/parsenodes.h"
18 
19 #define JUMBLE_SIZE				1024	/* query serialization buffer size */
20 
21 /*
22  * Struct for tracking locations/lengths of constants during normalization
23  */
24 typedef struct LocationLen
25 {
26 	int			location;		/* start offset in query text */
27 	int			length;			/* length in bytes, or -1 to ignore */
28 } LocationLen;
29 
30 /*
31  * Working state for computing a query jumble and producing a normalized
32  * query string
33  */
34 typedef struct JumbleState
35 {
36 	/* Jumble of current query tree */
37 	unsigned char *jumble;
38 
39 	/* Number of bytes used in jumble[] */
40 	Size		jumble_len;
41 
42 	/* Array of locations of constants that should be removed */
43 	LocationLen *clocations;
44 
45 	/* Allocated length of clocations array */
46 	int			clocations_buf_size;
47 
48 	/* Current number of valid entries in clocations array */
49 	int			clocations_count;
50 
51 	/* highest Param id we've seen, in order to start normalization correctly */
52 	int			highest_extern_param_id;
53 } JumbleState;
54 
55 /* Values for the compute_query_id GUC */
56 enum ComputeQueryIdType
57 {
58 	COMPUTE_QUERY_ID_OFF,
59 	COMPUTE_QUERY_ID_ON,
60 	COMPUTE_QUERY_ID_AUTO
61 };
62 
63 /* GUC parameters */
64 extern int	compute_query_id;
65 
66 
67 extern const char *CleanQuerytext(const char *query, int *location, int *len);
68 extern JumbleState *JumbleQuery(Query *query, const char *querytext);
69 extern void EnableQueryId(void);
70 
71 extern bool query_id_enabled;
72 
73 /*
74  * Returns whether query identifier computation has been enabled, either
75  * directly in the GUC or by a module when the setting is 'auto'.
76  */
77 static inline bool
IsQueryIdEnabled(void)78 IsQueryIdEnabled(void)
79 {
80 	if (compute_query_id == COMPUTE_QUERY_ID_OFF)
81 		return false;
82 	if (compute_query_id == COMPUTE_QUERY_ID_ON)
83 		return true;
84 	return query_id_enabled;
85 }
86 
87 #endif							/* QUERYJUMBLE_H */
88