PostgreSQL Source Code  git master
queryjumble.h File Reference
#include "nodes/parsenodes.h"
Include dependency graph for queryjumble.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  LocationLen
 
struct  JumbleState
 

Typedefs

typedef struct LocationLen LocationLen
 
typedef struct JumbleState JumbleState
 

Enumerations

enum  ComputeQueryIdType { COMPUTE_QUERY_ID_OFF , COMPUTE_QUERY_ID_ON , COMPUTE_QUERY_ID_AUTO , COMPUTE_QUERY_ID_REGRESS }
 

Functions

const char * CleanQuerytext (const char *query, int *location, int *len)
 
JumbleStateJumbleQuery (Query *query, const char *querytext)
 
void EnableQueryId (void)
 
static bool IsQueryIdEnabled (void)
 

Variables

PGDLLIMPORT int compute_query_id
 
PGDLLIMPORT bool query_id_enabled
 

Typedef Documentation

◆ JumbleState

typedef struct JumbleState JumbleState

◆ LocationLen

typedef struct LocationLen LocationLen

Enumeration Type Documentation

◆ ComputeQueryIdType

Enumerator
COMPUTE_QUERY_ID_OFF 
COMPUTE_QUERY_ID_ON 
COMPUTE_QUERY_ID_AUTO 
COMPUTE_QUERY_ID_REGRESS 

Definition at line 54 of file queryjumble.h.

55 {
60 };
@ COMPUTE_QUERY_ID_AUTO
Definition: queryjumble.h:58
@ COMPUTE_QUERY_ID_REGRESS
Definition: queryjumble.h:59
@ COMPUTE_QUERY_ID_ON
Definition: queryjumble.h:57
@ COMPUTE_QUERY_ID_OFF
Definition: queryjumble.h:56

Function Documentation

◆ CleanQuerytext()

const char* CleanQuerytext ( const char *  query,
int *  location,
int *  len 
)

Definition at line 63 of file queryjumble.c.

64 {
65  int query_location = *location;
66  int query_len = *len;
67 
68  /* First apply starting offset, unless it's -1 (unknown). */
69  if (query_location >= 0)
70  {
71  Assert(query_location <= strlen(query));
72  query += query_location;
73  /* Length of 0 (or -1) means "rest of string" */
74  if (query_len <= 0)
75  query_len = strlen(query);
76  else
77  Assert(query_len <= strlen(query));
78  }
79  else
80  {
81  /* If query location is unknown, distrust query_len as well */
82  query_location = 0;
83  query_len = strlen(query);
84  }
85 
86  /*
87  * Discard leading and trailing whitespace, too. Use scanner_isspace()
88  * not libc's isspace(), because we want to match the lexer's behavior.
89  */
90  while (query_len > 0 && scanner_isspace(query[0]))
91  query++, query_location++, query_len--;
92  while (query_len > 0 && scanner_isspace(query[query_len - 1]))
93  query_len--;
94 
95  *location = query_location;
96  *len = query_len;
97 
98  return query;
99 }
Assert(fmt[strlen(fmt) - 1] !='\n')
const void size_t len
bool scanner_isspace(char ch)
Definition: scansup.c:117

References Assert(), len, and scanner_isspace().

Referenced by compute_utility_query_id(), and pgss_store().

◆ EnableQueryId()

void EnableQueryId ( void  )

Definition at line 151 of file queryjumble.c.

152 {
154  query_id_enabled = true;
155 }
bool query_id_enabled
Definition: queryjumble.c:46
int compute_query_id
Definition: queryjumble.c:43

References compute_query_id, COMPUTE_QUERY_ID_OFF, and query_id_enabled.

Referenced by _PG_init().

◆ IsQueryIdEnabled()

static bool IsQueryIdEnabled ( void  )
inlinestatic

Definition at line 77 of file queryjumble.h.

78 {
80  return false;
82  return true;
83  return query_id_enabled;
84 }
PGDLLIMPORT bool query_id_enabled
Definition: queryjumble.c:46
PGDLLIMPORT int compute_query_id
Definition: queryjumble.c:43

References compute_query_id, COMPUTE_QUERY_ID_OFF, COMPUTE_QUERY_ID_ON, and query_id_enabled.

Referenced by ExplainQuery(), JumbleQuery(), parse_analyze_fixedparams(), parse_analyze_varparams(), and parse_analyze_withcb().

◆ JumbleQuery()

JumbleState* JumbleQuery ( Query query,
const char *  querytext 
)

Definition at line 102 of file queryjumble.c.

103 {
104  JumbleState *jstate = NULL;
105 
107 
108  if (query->utilityStmt)
109  {
110  query->queryId = compute_utility_query_id(querytext,
111  query->stmt_location,
112  query->stmt_len);
113  }
114  else
115  {
116  jstate = (JumbleState *) palloc(sizeof(JumbleState));
117 
118  /* Set up workspace for query jumbling */
119  jstate->jumble = (unsigned char *) palloc(JUMBLE_SIZE);
120  jstate->jumble_len = 0;
121  jstate->clocations_buf_size = 32;
122  jstate->clocations = (LocationLen *)
123  palloc(jstate->clocations_buf_size * sizeof(LocationLen));
124  jstate->clocations_count = 0;
125  jstate->highest_extern_param_id = 0;
126 
127  /* Compute query ID and mark the Query node with it */
128  JumbleQueryInternal(jstate, query);
129  query->queryId = DatumGetUInt64(hash_any_extended(jstate->jumble,
130  jstate->jumble_len,
131  0));
132 
133  /*
134  * If we are unlucky enough to get a hash of zero, use 1 instead, to
135  * prevent confusion with the utility-statement case.
136  */
137  if (query->queryId == UINT64CONST(0))
138  query->queryId = UINT64CONST(1);
139  }
140 
141  return jstate;
142 }
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37
void * palloc(Size size)
Definition: mcxt.c:1199
static uint64 DatumGetUInt64(Datum X)
Definition: postgres.h:767
#define JUMBLE_SIZE
Definition: queryjumble.c:40
static uint64 compute_utility_query_id(const char *query_text, int query_location, int query_len)
Definition: queryjumble.c:161
static void JumbleQueryInternal(JumbleState *jstate, Query *query)
Definition: queryjumble.c:241
static bool IsQueryIdEnabled(void)
Definition: queryjumble.h:77
unsigned char * jumble
Definition: queryjumble.h:35
int clocations_buf_size
Definition: queryjumble.h:44
Size jumble_len
Definition: queryjumble.h:38
int highest_extern_param_id
Definition: queryjumble.h:50
LocationLen * clocations
Definition: queryjumble.h:41
int clocations_count
Definition: queryjumble.h:47
int stmt_location
Definition: parsenodes.h:206
int stmt_len
Definition: parsenodes.h:207
Node * utilityStmt
Definition: parsenodes.h:136

References Assert(), JumbleState::clocations, JumbleState::clocations_buf_size, JumbleState::clocations_count, compute_utility_query_id(), DatumGetUInt64(), hash_any_extended(), JumbleState::highest_extern_param_id, IsQueryIdEnabled(), JumbleState::jumble, JumbleState::jumble_len, JUMBLE_SIZE, JumbleQueryInternal(), palloc(), Query::stmt_len, Query::stmt_location, and Query::utilityStmt.

Referenced by ExplainQuery(), parse_analyze_fixedparams(), parse_analyze_varparams(), and parse_analyze_withcb().

Variable Documentation

◆ compute_query_id

PGDLLIMPORT int compute_query_id
extern

Definition at line 43 of file queryjumble.c.

Referenced by EnableQueryId(), ExplainOnePlan(), and IsQueryIdEnabled().

◆ query_id_enabled

PGDLLIMPORT bool query_id_enabled
extern

Definition at line 46 of file queryjumble.c.

Referenced by EnableQueryId(), and IsQueryIdEnabled().