2
0

queryjumble.h 2.2 KB

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