PostgreSQL Source Code  git master
queryjumblefuncs.c File Reference
#include "postgres.h"
#include "common/hashfn.h"
#include "miscadmin.h"
#include "nodes/queryjumble.h"
#include "parser/scansup.h"
#include "queryjumblefuncs.funcs.c"
#include "queryjumblefuncs.switch.c"
Include dependency graph for queryjumblefuncs.c:

Go to the source code of this file.

Macros

#define JUMBLE_SIZE   1024 /* query serialization buffer size */
 
#define JUMBLE_NODE(item)    _jumbleNode(jstate, (Node *) expr->item)
 
#define JUMBLE_LOCATION(location)    RecordConstLocation(jstate, expr->location)
 
#define JUMBLE_FIELD(item)    AppendJumble(jstate, (const unsigned char *) &(expr->item), sizeof(expr->item))
 
#define JUMBLE_FIELD_SINGLE(item)    AppendJumble(jstate, (const unsigned char *) &(item), sizeof(item))
 
#define JUMBLE_STRING(str)
 

Functions

static void AppendJumble (JumbleState *jstate, const unsigned char *item, Size size)
 
static void RecordConstLocation (JumbleState *jstate, int location)
 
static void _jumbleNode (JumbleState *jstate, Node *node)
 
static void _jumbleA_Const (JumbleState *jstate, Node *node)
 
static void _jumbleList (JumbleState *jstate, Node *node)
 
const char * CleanQuerytext (const char *query, int *location, int *len)
 
JumbleStateJumbleQuery (Query *query)
 
void EnableQueryId (void)
 

Variables

int compute_query_id = COMPUTE_QUERY_ID_AUTO
 
bool query_id_enabled = false
 

Macro Definition Documentation

◆ JUMBLE_FIELD

#define JUMBLE_FIELD (   item)     AppendJumble(jstate, (const unsigned char *) &(expr->item), sizeof(expr->item))

Definition at line 223 of file queryjumblefuncs.c.

◆ JUMBLE_FIELD_SINGLE

#define JUMBLE_FIELD_SINGLE (   item)     AppendJumble(jstate, (const unsigned char *) &(item), sizeof(item))

Definition at line 225 of file queryjumblefuncs.c.

◆ JUMBLE_LOCATION

#define JUMBLE_LOCATION (   location)     RecordConstLocation(jstate, expr->location)

Definition at line 221 of file queryjumblefuncs.c.

◆ JUMBLE_NODE

#define JUMBLE_NODE (   item)     _jumbleNode(jstate, (Node *) expr->item)

Definition at line 219 of file queryjumblefuncs.c.

◆ JUMBLE_SIZE

#define JUMBLE_SIZE   1024 /* query serialization buffer size */

Definition at line 40 of file queryjumblefuncs.c.

◆ JUMBLE_STRING

#define JUMBLE_STRING (   str)
Value:
do { \
if (expr->str) \
AppendJumble(jstate, (const unsigned char *) (expr->str), strlen(expr->str) + 1); \
} while(0)

Definition at line 227 of file queryjumblefuncs.c.

Function Documentation

◆ _jumbleA_Const()

static void _jumbleA_Const ( JumbleState jstate,
Node node 
)
static

Definition at line 323 of file queryjumblefuncs.c.

324 {
325  A_Const *expr = (A_Const *) node;
326 
327  JUMBLE_FIELD(isnull);
328  if (!expr->isnull)
329  {
330  JUMBLE_FIELD(val.node.type);
331  switch (nodeTag(&expr->val))
332  {
333  case T_Integer:
334  JUMBLE_FIELD(val.ival.ival);
335  break;
336  case T_Float:
337  JUMBLE_STRING(val.fval.fval);
338  break;
339  case T_Boolean:
340  JUMBLE_FIELD(val.boolval.boolval);
341  break;
342  case T_String:
343  JUMBLE_STRING(val.sval.sval);
344  break;
345  case T_BitString:
346  JUMBLE_STRING(val.bsval.bsval);
347  break;
348  default:
349  elog(ERROR, "unrecognized node type: %d",
350  (int) nodeTag(&expr->val));
351  break;
352  }
353  }
354 }
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
long val
Definition: informix.c:670
#define nodeTag(nodeptr)
Definition: nodes.h:133
#define JUMBLE_STRING(str)
#define JUMBLE_FIELD(item)
bool isnull
Definition: parsenodes.h:363
union ValUnion val
Definition: parsenodes.h:362

References elog, ERROR, A_Const::isnull, JUMBLE_FIELD, JUMBLE_STRING, nodeTag, A_Const::val, and val.

◆ _jumbleList()

static void _jumbleList ( JumbleState jstate,
Node node 
)
static

Definition at line 292 of file queryjumblefuncs.c.

293 {
294  List *expr = (List *) node;
295  ListCell *l;
296 
297  switch (expr->type)
298  {
299  case T_List:
300  foreach(l, expr)
301  _jumbleNode(jstate, lfirst(l));
302  break;
303  case T_IntList:
304  foreach(l, expr)
306  break;
307  case T_OidList:
308  foreach(l, expr)
310  break;
311  case T_XidList:
312  foreach(l, expr)
314  break;
315  default:
316  elog(ERROR, "unrecognized list node type: %d",
317  (int) expr->type);
318  return;
319  }
320 }
#define lfirst(lc)
Definition: pg_list.h:172
#define lfirst_int(lc)
Definition: pg_list.h:173
#define lfirst_oid(lc)
Definition: pg_list.h:174
#define lfirst_xid(lc)
Definition: pg_list.h:175
static void _jumbleNode(JumbleState *jstate, Node *node)
#define JUMBLE_FIELD_SINGLE(item)
Definition: pg_list.h:54
NodeTag type
Definition: pg_list.h:55

References _jumbleNode(), elog, ERROR, JUMBLE_FIELD_SINGLE, lfirst, lfirst_int, lfirst_oid, lfirst_xid, and List::type.

Referenced by _jumbleNode().

◆ _jumbleNode()

static void _jumbleNode ( JumbleState jstate,
Node node 
)
static

Definition at line 236 of file queryjumblefuncs.c.

237 {
238  Node *expr = node;
239 
240  if (expr == NULL)
241  return;
242 
243  /* Guard against stack overflow due to overly complex expressions */
245 
246  /*
247  * We always emit the node's NodeTag, then any additional fields that are
248  * considered significant, and then we recurse to any child nodes.
249  */
251 
252  switch (nodeTag(expr))
253  {
254 #include "queryjumblefuncs.switch.c"
255 
256  case T_List:
257  case T_IntList:
258  case T_OidList:
259  case T_XidList:
260  _jumbleList(jstate, expr);
261  break;
262 
263  default:
264  /* Only a warning, since we can stumble along anyway */
265  elog(WARNING, "unrecognized node type: %d",
266  (int) nodeTag(expr));
267  break;
268  }
269 
270  /* Special cases to handle outside the automated code */
271  switch (nodeTag(expr))
272  {
273  case T_Param:
274  {
275  Param *p = (Param *) node;
276 
277  /*
278  * Update the highest Param id seen, in order to start
279  * normalization correctly.
280  */
281  if (p->paramkind == PARAM_EXTERN &&
282  p->paramid > jstate->highest_extern_param_id)
283  jstate->highest_extern_param_id = p->paramid;
284  }
285  break;
286  default:
287  break;
288  }
289 }
#define WARNING
Definition: elog.h:36
void check_stack_depth(void)
Definition: postgres.c:3530
@ PARAM_EXTERN
Definition: primnodes.h:367
static void _jumbleList(JumbleState *jstate, Node *node)
int highest_extern_param_id
Definition: queryjumble.h:50
Definition: nodes.h:129
int paramid
Definition: primnodes.h:377
ParamKind paramkind
Definition: primnodes.h:376
const char * type

References _jumbleList(), check_stack_depth(), elog, JumbleState::highest_extern_param_id, JUMBLE_FIELD, nodeTag, PARAM_EXTERN, Param::paramid, Param::paramkind, type, and WARNING.

Referenced by _jumbleList(), and JumbleQuery().

◆ AppendJumble()

static void AppendJumble ( JumbleState jstate,
const unsigned char *  item,
Size  size 
)
static

Definition at line 161 of file queryjumblefuncs.c.

162 {
163  unsigned char *jumble = jstate->jumble;
164  Size jumble_len = jstate->jumble_len;
165 
166  /*
167  * Whenever the jumble buffer is full, we hash the current contents and
168  * reset the buffer to contain just that hash value, thus relying on the
169  * hash to summarize everything so far.
170  */
171  while (size > 0)
172  {
173  Size part_size;
174 
175  if (jumble_len >= JUMBLE_SIZE)
176  {
177  uint64 start_hash;
178 
179  start_hash = DatumGetUInt64(hash_any_extended(jumble,
180  JUMBLE_SIZE, 0));
181  memcpy(jumble, &start_hash, sizeof(start_hash));
182  jumble_len = sizeof(start_hash);
183  }
184  part_size = Min(size, JUMBLE_SIZE - jumble_len);
185  memcpy(jumble + jumble_len, item, part_size);
186  jumble_len += part_size;
187  item += part_size;
188  size -= part_size;
189  }
190  jstate->jumble_len = jumble_len;
191 }
#define Min(x, y)
Definition: c.h:1004
size_t Size
Definition: c.h:605
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37
static uint64 DatumGetUInt64(Datum X)
Definition: postgres.h:419
#define JUMBLE_SIZE
static pg_noinline void Size size
Definition: slab.c:607
unsigned char * jumble
Definition: queryjumble.h:35
Size jumble_len
Definition: queryjumble.h:38

References DatumGetUInt64(), hash_any_extended(), JumbleState::jumble, JumbleState::jumble_len, JUMBLE_SIZE, Min, and size.

◆ CleanQuerytext()

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

Definition at line 66 of file queryjumblefuncs.c.

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

References Assert, len, and scanner_isspace().

Referenced by pgss_store().

◆ EnableQueryId()

void EnableQueryId ( void  )

Definition at line 150 of file queryjumblefuncs.c.

151 {
153  query_id_enabled = true;
154 }
@ COMPUTE_QUERY_ID_OFF
Definition: queryjumble.h:56
bool query_id_enabled
int compute_query_id

References compute_query_id, COMPUTE_QUERY_ID_OFF, and query_id_enabled.

Referenced by _PG_init().

◆ JumbleQuery()

JumbleState* JumbleQuery ( Query query)

Definition at line 105 of file queryjumblefuncs.c.

106 {
107  JumbleState *jstate = NULL;
108 
110 
111  jstate = (JumbleState *) palloc(sizeof(JumbleState));
112 
113  /* Set up workspace for query jumbling */
114  jstate->jumble = (unsigned char *) palloc(JUMBLE_SIZE);
115  jstate->jumble_len = 0;
116  jstate->clocations_buf_size = 32;
117  jstate->clocations = (LocationLen *)
118  palloc(jstate->clocations_buf_size * sizeof(LocationLen));
119  jstate->clocations_count = 0;
120  jstate->highest_extern_param_id = 0;
121 
122  /* Compute query ID and mark the Query node with it */
123  _jumbleNode(jstate, (Node *) query);
124  query->queryId = DatumGetUInt64(hash_any_extended(jstate->jumble,
125  jstate->jumble_len,
126  0));
127 
128  /*
129  * If we are unlucky enough to get a hash of zero, use 1 instead for
130  * normal statements and 2 for utility queries.
131  */
132  if (query->queryId == UINT64CONST(0))
133  {
134  if (query->utilityStmt)
135  query->queryId = UINT64CONST(2);
136  else
137  query->queryId = UINT64CONST(1);
138  }
139 
140  return jstate;
141 }
void * palloc(Size size)
Definition: mcxt.c:1317
static bool IsQueryIdEnabled(void)
Definition: queryjumble.h:77
int clocations_buf_size
Definition: queryjumble.h:44
LocationLen * clocations
Definition: queryjumble.h:41
int clocations_count
Definition: queryjumble.h:47
Node * utilityStmt
Definition: parsenodes.h:136

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

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

◆ RecordConstLocation()

static void RecordConstLocation ( JumbleState jstate,
int  location 
)
static

Definition at line 198 of file queryjumblefuncs.c.

199 {
200  /* -1 indicates unknown or undefined location */
201  if (location >= 0)
202  {
203  /* enlarge array if needed */
204  if (jstate->clocations_count >= jstate->clocations_buf_size)
205  {
206  jstate->clocations_buf_size *= 2;
207  jstate->clocations = (LocationLen *)
208  repalloc(jstate->clocations,
209  jstate->clocations_buf_size *
210  sizeof(LocationLen));
211  }
212  jstate->clocations[jstate->clocations_count].location = location;
213  /* initialize lengths to -1 to simplify third-party module usage */
214  jstate->clocations[jstate->clocations_count].length = -1;
215  jstate->clocations_count++;
216  }
217 }
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1541

References JumbleState::clocations, JumbleState::clocations_buf_size, JumbleState::clocations_count, LocationLen::length, LocationLen::location, and repalloc().

Variable Documentation

◆ compute_query_id

int compute_query_id = COMPUTE_QUERY_ID_AUTO

Definition at line 43 of file queryjumblefuncs.c.

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

◆ query_id_enabled

bool query_id_enabled = false

Definition at line 52 of file queryjumblefuncs.c.

Referenced by EnableQueryId(), and IsQueryIdEnabled().