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)
 
static void _jumbleRangeTblEntry (JumbleState *jstate, Node *node)
 
const char * CleanQuerytext (const char *query, int *location, int *len)
 
JumbleStateJumbleQuery (Query *query, const char *querytext)
 
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 218 of file queryjumblefuncs.c.

◆ JUMBLE_FIELD_SINGLE

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

Definition at line 220 of file queryjumblefuncs.c.

◆ JUMBLE_LOCATION

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

Definition at line 216 of file queryjumblefuncs.c.

◆ JUMBLE_NODE

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

Definition at line 214 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 222 of file queryjumblefuncs.c.

Function Documentation

◆ _jumbleA_Const()

static void _jumbleA_Const ( JumbleState jstate,
Node node 
)
static

Definition at line 318 of file queryjumblefuncs.c.

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

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 287 of file queryjumblefuncs.c.

288 {
289  List *expr = (List *) node;
290  ListCell *l;
291 
292  switch (expr->type)
293  {
294  case T_List:
295  foreach(l, expr)
296  _jumbleNode(jstate, lfirst(l));
297  break;
298  case T_IntList:
299  foreach(l, expr)
301  break;
302  case T_OidList:
303  foreach(l, expr)
305  break;
306  case T_XidList:
307  foreach(l, expr)
309  break;
310  default:
311  elog(ERROR, "unrecognized list node type: %d",
312  (int) expr->type);
313  return;
314  }
315 }
#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 231 of file queryjumblefuncs.c.

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

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

Referenced by _jumbleList(), and JumbleQuery().

◆ _jumbleRangeTblEntry()

static void _jumbleRangeTblEntry ( JumbleState jstate,
Node node 
)
static

Definition at line 352 of file queryjumblefuncs.c.

353 {
354  RangeTblEntry *expr = (RangeTblEntry *) node;
355 
356  JUMBLE_FIELD(rtekind);
357  switch (expr->rtekind)
358  {
359  case RTE_RELATION:
360  JUMBLE_FIELD(relid);
361  JUMBLE_NODE(tablesample);
362  JUMBLE_FIELD(inh);
363  break;
364  case RTE_SUBQUERY:
365  JUMBLE_NODE(subquery);
366  break;
367  case RTE_JOIN:
368  JUMBLE_FIELD(jointype);
369  break;
370  case RTE_FUNCTION:
372  break;
373  case RTE_TABLEFUNC:
374  JUMBLE_NODE(tablefunc);
375  break;
376  case RTE_VALUES:
377  JUMBLE_NODE(values_lists);
378  break;
379  case RTE_CTE:
380 
381  /*
382  * Depending on the CTE name here isn't ideal, but it's the only
383  * info we have to identify the referenced WITH item.
384  */
385  JUMBLE_STRING(ctename);
386  JUMBLE_FIELD(ctelevelsup);
387  break;
388  case RTE_NAMEDTUPLESTORE:
389  JUMBLE_STRING(enrname);
390  break;
391  case RTE_RESULT:
392  break;
393  default:
394  elog(ERROR, "unrecognized RTE kind: %d", (int) expr->rtekind);
395  break;
396  }
397 }
@ RTE_JOIN
Definition: parsenodes.h:1016
@ RTE_CTE
Definition: parsenodes.h:1020
@ RTE_NAMEDTUPLESTORE
Definition: parsenodes.h:1021
@ RTE_VALUES
Definition: parsenodes.h:1019
@ RTE_SUBQUERY
Definition: parsenodes.h:1015
@ RTE_RESULT
Definition: parsenodes.h:1022
@ RTE_FUNCTION
Definition: parsenodes.h:1017
@ RTE_TABLEFUNC
Definition: parsenodes.h:1018
@ RTE_RELATION
Definition: parsenodes.h:1014
#define JUMBLE_NODE(item)
static const struct fns functions
Definition: regcomp.c:357
RTEKind rtekind
Definition: parsenodes.h:1033

References elog(), ERROR, functions, JUMBLE_FIELD, JUMBLE_NODE, JUMBLE_STRING, RTE_CTE, RTE_FUNCTION, RTE_JOIN, RTE_NAMEDTUPLESTORE, RTE_RELATION, RTE_RESULT, RTE_SUBQUERY, RTE_TABLEFUNC, RTE_VALUES, and RangeTblEntry::rtekind.

◆ AppendJumble()

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

Definition at line 156 of file queryjumblefuncs.c.

157 {
158  unsigned char *jumble = jstate->jumble;
159  Size jumble_len = jstate->jumble_len;
160 
161  /*
162  * Whenever the jumble buffer is full, we hash the current contents and
163  * reset the buffer to contain just that hash value, thus relying on the
164  * hash to summarize everything so far.
165  */
166  while (size > 0)
167  {
168  Size part_size;
169 
170  if (jumble_len >= JUMBLE_SIZE)
171  {
172  uint64 start_hash;
173 
174  start_hash = DatumGetUInt64(hash_any_extended(jumble,
175  JUMBLE_SIZE, 0));
176  memcpy(jumble, &start_hash, sizeof(start_hash));
177  jumble_len = sizeof(start_hash);
178  }
179  part_size = Min(size, JUMBLE_SIZE - jumble_len);
180  memcpy(jumble + jumble_len, item, part_size);
181  jumble_len += part_size;
182  item += part_size;
183  size -= part_size;
184  }
185  jstate->jumble_len = jumble_len;
186 }
#define Min(x, y)
Definition: c.h:988
size_t Size
Definition: c.h:589
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
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, and Min.

◆ CleanQuerytext()

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

Definition at line 61 of file queryjumblefuncs.c.

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

◆ EnableQueryId()

void EnableQueryId ( void  )

Definition at line 145 of file queryjumblefuncs.c.

146 {
148  query_id_enabled = true;
149 }
@ 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,
const char *  querytext 
)

Definition at line 100 of file queryjumblefuncs.c.

101 {
102  JumbleState *jstate = NULL;
103 
105 
106  jstate = (JumbleState *) palloc(sizeof(JumbleState));
107 
108  /* Set up workspace for query jumbling */
109  jstate->jumble = (unsigned char *) palloc(JUMBLE_SIZE);
110  jstate->jumble_len = 0;
111  jstate->clocations_buf_size = 32;
112  jstate->clocations = (LocationLen *)
113  palloc(jstate->clocations_buf_size * sizeof(LocationLen));
114  jstate->clocations_count = 0;
115  jstate->highest_extern_param_id = 0;
116 
117  /* Compute query ID and mark the Query node with it */
118  _jumbleNode(jstate, (Node *) query);
119  query->queryId = DatumGetUInt64(hash_any_extended(jstate->jumble,
120  jstate->jumble_len,
121  0));
122 
123  /*
124  * If we are unlucky enough to get a hash of zero, use 1 instead for
125  * normal statements and 2 for utility queries.
126  */
127  if (query->queryId == UINT64CONST(0))
128  {
129  if (query->utilityStmt)
130  query->queryId = UINT64CONST(2);
131  else
132  query->queryId = UINT64CONST(1);
133  }
134 
135  return jstate;
136 }
void * palloc(Size size)
Definition: mcxt.c:1210
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:143

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 193 of file queryjumblefuncs.c.

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

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 46 of file queryjumblefuncs.c.

Referenced by EnableQueryId(), and IsQueryIdEnabled().