PostgreSQL Source Code git master
Loading...
Searching...
No Matches
queryjumblefuncs.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * queryjumblefuncs.c
4 * Query normalization and fingerprinting.
5 *
6 * Normalization is a process whereby similar queries, typically differing only
7 * in their constants (though the exact rules are somewhat more subtle than
8 * that) are recognized as equivalent, and are tracked as a single entry. This
9 * is particularly useful for non-prepared queries.
10 *
11 * Normalization is implemented by fingerprinting queries, selectively
12 * serializing those fields of each query tree's nodes that are judged to be
13 * essential to the query. This is referred to as a query jumble. This is
14 * distinct from a regular serialization in that various extraneous
15 * information is ignored as irrelevant or not essential to the query, such
16 * as the collations of Vars and, most notably, the values of constants.
17 *
18 * This jumble is acquired at the end of parse analysis of each query, and
19 * a 64-bit hash of it is stored into the query's Query.queryId field.
20 * The server then copies this value around, making it available in plan
21 * tree(s) generated from the query. The executor can then use this value
22 * to blame query costs on the proper queryId.
23 *
24 * Arrays of two or more constants and PARAM_EXTERN parameters are "squashed"
25 * and contribute only once to the jumble. This has the effect that queries
26 * that differ only on the length of such lists have the same queryId.
27 *
28 *
29 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
30 * Portions Copyright (c) 1994, Regents of the University of California
31 *
32 *
33 * IDENTIFICATION
34 * src/backend/nodes/queryjumblefuncs.c
35 *
36 *-------------------------------------------------------------------------
37 */
38#include "postgres.h"
39
40#include "access/transam.h"
41#include "catalog/pg_proc.h"
42#include "common/hashfn.h"
43#include "common/int.h"
44#include "miscadmin.h"
45#include "nodes/nodeFuncs.h"
46#include "nodes/queryjumble.h"
47#include "utils/lsyscache.h"
48#include "parser/scanner.h"
49#include "parser/scansup.h"
50
51#define JUMBLE_SIZE 1024 /* query serialization buffer size */
52
53/* GUC parameters */
55
56/*
57 * True when compute_query_id is ON or AUTO, and a module requests them.
58 *
59 * Note that IsQueryIdEnabled() should be used instead of checking
60 * query_id_enabled or compute_query_id directly when we want to know
61 * whether query identifiers are computed in the core or not.
62 */
63bool query_id_enabled = false;
64
65static JumbleState *InitJumble(void);
66static int64 DoJumble(JumbleState *jstate, Node *node);
68 const unsigned char *value, Size size);
71 bool extern_param,
72 int location, int len);
73static void _jumbleNode(JumbleState *jstate, Node *node);
74static void _jumbleList(JumbleState *jstate, Node *node);
75static void _jumbleElements(JumbleState *jstate, List *elements, Node *node);
76static void _jumbleParam(JumbleState *jstate, Node *node);
77static void _jumbleA_Const(JumbleState *jstate, Node *node);
81 Alias *expr);
82
83/*
84 * Given a possibly multi-statement source string, confine our attention to the
85 * relevant part of the string.
86 */
87const char *
88CleanQuerytext(const char *query, int *location, int *len)
89{
90 int query_location = *location;
91 int query_len = *len;
92
93 /* First apply starting offset, unless it's -1 (unknown). */
94 if (query_location >= 0)
95 {
96 Assert(query_location <= strlen(query));
97 query += query_location;
98 /* Length of 0 (or -1) means "rest of string" */
99 if (query_len <= 0)
100 query_len = strlen(query);
101 else
102 Assert(query_len <= strlen(query));
103 }
104 else
105 {
106 /* If query location is unknown, distrust query_len as well */
107 query_location = 0;
108 query_len = strlen(query);
109 }
110
111 /*
112 * Discard leading and trailing whitespace, too. Use scanner_isspace()
113 * not libc's isspace(), because we want to match the lexer's behavior.
114 *
115 * Note: the parser now strips leading comments and whitespace from the
116 * reported stmt_location, so this first loop will only iterate in the
117 * unusual case that the location didn't propagate to here. But the
118 * statement length will extend to the end-of-string or terminating
119 * semicolon, so the second loop often does something useful.
120 */
121 while (query_len > 0 && scanner_isspace(query[0]))
122 query++, query_location++, query_len--;
123 while (query_len > 0 && scanner_isspace(query[query_len - 1]))
124 query_len--;
125
126 *location = query_location;
127 *len = query_len;
128
129 return query;
130}
131
132/*
133 * JumbleQuery
134 * Recursively process the given Query producing a 64-bit hash value by
135 * hashing the relevant fields and record that value in the Query's queryId
136 * field. Return the JumbleState object used for jumbling the query.
137 */
140{
142
144
145 jstate = InitJumble();
146
147 query->queryId = DoJumble(jstate, (Node *) query);
148
149 /*
150 * If we are unlucky enough to get a hash of zero, use 1 instead for
151 * normal statements and 2 for utility queries.
152 */
153 if (query->queryId == INT64CONST(0))
154 {
155 if (query->utilityStmt)
156 query->queryId = INT64CONST(2);
157 else
158 query->queryId = INT64CONST(1);
159 }
160
161 return jstate;
162}
163
164/*
165 * Enables query identifier computation.
166 *
167 * Third-party plugins can use this function to inform core that they require
168 * a query identifier to be computed.
169 */
170void
176
177/*
178 * InitJumble
179 * Allocate a JumbleState object and make it ready to jumble.
180 */
181static JumbleState *
183{
185
187
188 /* Set up workspace for query jumbling */
189 jstate->jumble = (unsigned char *) palloc(JUMBLE_SIZE);
190 jstate->jumble_len = 0;
191 jstate->clocations_buf_size = 32;
192 jstate->clocations = (LocationLen *) palloc(jstate->clocations_buf_size *
193 sizeof(LocationLen));
194 jstate->clocations_count = 0;
195 jstate->highest_extern_param_id = 0;
196 jstate->pending_nulls = 0;
197 jstate->has_squashed_lists = false;
198#ifdef USE_ASSERT_CHECKING
199 jstate->total_jumble_len = 0;
200#endif
201
202 return jstate;
203}
204
205/*
206 * DoJumble
207 * Jumble the given Node using the given JumbleState and return the resulting
208 * jumble hash.
209 */
210static int64
212{
213 /* Jumble the given node */
214 _jumbleNode(jstate, node);
215
216 /* Flush any pending NULLs before doing the final hash */
217 if (jstate->pending_nulls > 0)
219
220 /* Squashed list found, reset highest_extern_param_id */
221 if (jstate->has_squashed_lists)
222 jstate->highest_extern_param_id = 0;
223
224 /* Process the jumble buffer and produce the hash value */
226 jstate->jumble_len,
227 0));
228}
229
230/*
231 * AppendJumbleInternal: Internal function for appending to the jumble buffer
232 *
233 * Note: Callers must ensure that size > 0.
234 */
236AppendJumbleInternal(JumbleState *jstate, const unsigned char *item,
237 Size size)
238{
239 unsigned char *jumble = jstate->jumble;
240 Size jumble_len = jstate->jumble_len;
241
242 /* Ensure the caller didn't mess up */
243 Assert(size > 0);
244
245 /*
246 * Fast path for when there's enough space left in the buffer. This is
247 * worthwhile as means the memcpy can be inlined into very efficient code
248 * when 'size' is a compile-time constant.
249 */
250 if (likely(size <= JUMBLE_SIZE - jumble_len))
251 {
252 memcpy(jumble + jumble_len, item, size);
253 jstate->jumble_len += size;
254
255#ifdef USE_ASSERT_CHECKING
256 jstate->total_jumble_len += size;
257#endif
258
259 return;
260 }
261
262 /*
263 * Whenever the jumble buffer is full, we hash the current contents and
264 * reset the buffer to contain just that hash value, thus relying on the
265 * hash to summarize everything so far.
266 */
267 do
268 {
270
271 if (unlikely(jumble_len >= JUMBLE_SIZE))
272 {
274
276 JUMBLE_SIZE, 0));
277 memcpy(jumble, &start_hash, sizeof(start_hash));
278 jumble_len = sizeof(start_hash);
279 }
280 part_size = Min(size, JUMBLE_SIZE - jumble_len);
281 memcpy(jumble + jumble_len, item, part_size);
282 jumble_len += part_size;
283 item += part_size;
284 size -= part_size;
285
286#ifdef USE_ASSERT_CHECKING
287 jstate->total_jumble_len += part_size;
288#endif
289 } while (size > 0);
290
291 jstate->jumble_len = jumble_len;
292}
293
294/*
295 * AppendJumble
296 * Add 'size' bytes of the given jumble 'value' to the jumble state
297 */
298static pg_noinline void
299AppendJumble(JumbleState *jstate, const unsigned char *value, Size size)
300{
301 if (jstate->pending_nulls > 0)
303
305}
306
307/*
308 * AppendJumbleNull
309 * For jumbling NULL pointers
310 */
313{
314 jstate->pending_nulls++;
315}
316
317/*
318 * AppendJumble8
319 * Add the first byte from the given 'value' pointer to the jumble state
320 */
321static pg_noinline void
322AppendJumble8(JumbleState *jstate, const unsigned char *value)
323{
324 if (jstate->pending_nulls > 0)
326
328}
329
330/*
331 * AppendJumble16
332 * Add the first 2 bytes from the given 'value' pointer to the jumble
333 * state.
334 */
335static pg_noinline void
336AppendJumble16(JumbleState *jstate, const unsigned char *value)
337{
338 if (jstate->pending_nulls > 0)
340
342}
343
344/*
345 * AppendJumble32
346 * Add the first 4 bytes from the given 'value' pointer to the jumble
347 * state.
348 */
349static pg_noinline void
350AppendJumble32(JumbleState *jstate, const unsigned char *value)
351{
352 if (jstate->pending_nulls > 0)
354
356}
357
358/*
359 * AppendJumble64
360 * Add the first 8 bytes from the given 'value' pointer to the jumble
361 * state.
362 */
363static pg_noinline void
364AppendJumble64(JumbleState *jstate, const unsigned char *value)
365{
366 if (jstate->pending_nulls > 0)
368
370}
371
372/*
373 * FlushPendingNulls
374 * Incorporate the pending_nulls value into the jumble buffer.
375 *
376 * Note: Callers must ensure that there's at least 1 pending NULL.
377 */
380{
381 Assert(jstate->pending_nulls > 0);
382
384 (const unsigned char *) &jstate->pending_nulls, 4);
385 jstate->pending_nulls = 0;
386}
387
388
389/*
390 * Record the location of some kind of constant within a query string.
391 * These are not only bare constants but also expressions that ultimately
392 * constitute a constant, such as those inside casts and simple function
393 * calls; if extern_param, then it corresponds to a PARAM_EXTERN Param.
394 *
395 * If length is -1, it indicates a single such constant element. If
396 * it's a positive integer, it indicates the length of a squashable
397 * list of them.
398 */
399static void
400RecordConstLocation(JumbleState *jstate, bool extern_param, int location, int len)
401{
402 /* -1 indicates unknown or undefined location */
403 if (location >= 0)
404 {
405 /* enlarge array if needed */
406 if (jstate->clocations_count >= jstate->clocations_buf_size)
407 {
408 jstate->clocations_buf_size *= 2;
409 jstate->clocations = (LocationLen *)
410 repalloc(jstate->clocations,
411 jstate->clocations_buf_size *
412 sizeof(LocationLen));
413 }
414 jstate->clocations[jstate->clocations_count].location = location;
415
416 /*
417 * Lengths are either positive integers (indicating a squashable
418 * list), or -1.
419 */
420 Assert(len > -1 || len == -1);
421 jstate->clocations[jstate->clocations_count].length = len;
422 jstate->clocations[jstate->clocations_count].squashed = (len > -1);
423 jstate->clocations[jstate->clocations_count].extern_param = extern_param;
424 jstate->clocations_count++;
425 }
426}
427
428/*
429 * Subroutine for _jumbleElements: Verify a few simple cases where we can
430 * deduce that the expression is a constant:
431 *
432 * - See through any wrapping RelabelType and CoerceViaIO layers.
433 * - If it's a FuncExpr, check that the function is a builtin
434 * cast and its arguments are Const.
435 * - Otherwise test if the expression is a simple Const or a
436 * PARAM_EXTERN param.
437 */
438static bool
440{
441restart:
442 switch (nodeTag(element))
443 {
444 case T_RelabelType:
445 /* Unwrap RelabelType */
446 element = (Node *) ((RelabelType *) element)->arg;
447 goto restart;
448
449 case T_CoerceViaIO:
450 /* Unwrap CoerceViaIO */
451 element = (Node *) ((CoerceViaIO *) element)->arg;
452 goto restart;
453
454 case T_Const:
455 return true;
456
457 case T_Param:
458 return castNode(Param, element)->paramkind == PARAM_EXTERN;
459
460 case T_FuncExpr:
461 {
462 FuncExpr *func = (FuncExpr *) element;
463 ListCell *temp;
464
465 if (func->funcformat != COERCE_IMPLICIT_CAST &&
466 func->funcformat != COERCE_EXPLICIT_CAST)
467 return false;
468
469 if (func->funcid > FirstGenbkiObjectId)
470 return false;
471
472 /*
473 * We can check function arguments recursively, being careful
474 * about recursing too deep. At each recursion level it's
475 * enough to test the stack on the first element. (Note that
476 * I wasn't able to hit this without bloating the stack
477 * artificially in this function: the parser errors out before
478 * stack size becomes a problem here.)
479 */
480 foreach(temp, func->args)
481 {
482 Node *arg = lfirst(temp);
483
484 if (!IsA(arg, Const))
485 {
486 if (foreach_current_index(temp) == 0 &&
488 return false;
489 else if (!IsSquashableConstant(arg))
490 return false;
491 }
492 }
493
494 return true;
495 }
496
497 default:
498 return false;
499 }
500}
501
502/*
503 * Subroutine for _jumbleElements: Verify whether the provided list
504 * can be squashed, meaning it contains only constant expressions.
505 *
506 * Return value indicates if squashing is possible.
507 *
508 * Note that this function searches only for explicit Const nodes with
509 * possibly very simple decorations on top and PARAM_EXTERN parameters,
510 * and does not try to simplify expressions.
511 */
512static bool
514{
515 ListCell *temp;
516
517 /* If the list is too short, we don't try to squash it. */
518 if (list_length(elements) < 2)
519 return false;
520
521 foreach(temp, elements)
522 {
524 return false;
525 }
526
527 return true;
528}
529
530#define JUMBLE_NODE(item) \
531 _jumbleNode(jstate, (Node *) expr->item)
532#define JUMBLE_ELEMENTS(list, node) \
533 _jumbleElements(jstate, (List *) expr->list, node)
534#define JUMBLE_LOCATION(location) \
535 RecordConstLocation(jstate, false, expr->location, -1)
536#define JUMBLE_FIELD(item) \
537do { \
538 if (sizeof(expr->item) == 8) \
539 AppendJumble64(jstate, (const unsigned char *) &(expr->item)); \
540 else if (sizeof(expr->item) == 4) \
541 AppendJumble32(jstate, (const unsigned char *) &(expr->item)); \
542 else if (sizeof(expr->item) == 2) \
543 AppendJumble16(jstate, (const unsigned char *) &(expr->item)); \
544 else if (sizeof(expr->item) == 1) \
545 AppendJumble8(jstate, (const unsigned char *) &(expr->item)); \
546 else \
547 AppendJumble(jstate, (const unsigned char *) &(expr->item), sizeof(expr->item)); \
548} while (0)
549#define JUMBLE_STRING(str) \
550do { \
551 if (expr->str) \
552 AppendJumble(jstate, (const unsigned char *) (expr->str), strlen(expr->str) + 1); \
553 else \
554 AppendJumbleNull(jstate); \
555} while(0)
556/* Function name used for the node field attribute custom_query_jumble. */
557#define JUMBLE_CUSTOM(nodetype, item) \
558 _jumble##nodetype##_##item(jstate, expr, expr->item)
559
560#include "queryjumblefuncs.funcs.c"
561
562static void
564{
565 Node *expr = node;
566#ifdef USE_ASSERT_CHECKING
567 Size prev_jumble_len = jstate->total_jumble_len;
568#endif
569
570 if (expr == NULL)
571 {
573 return;
574 }
575
576 /* Guard against stack overflow due to overly complex expressions */
578
579 /*
580 * We always emit the node's NodeTag, then any additional fields that are
581 * considered significant, and then we recurse to any child nodes.
582 */
584
585 switch (nodeTag(expr))
586 {
587#include "queryjumblefuncs.switch.c"
588
589 case T_List:
590 case T_IntList:
591 case T_OidList:
592 case T_XidList:
593 _jumbleList(jstate, expr);
594 break;
595
596 default:
597 /* Only a warning, since we can stumble along anyway */
598 elog(WARNING, "unrecognized node type: %d",
599 (int) nodeTag(expr));
600 break;
601 }
602
603 /* Ensure we added something to the jumble buffer */
604 Assert(jstate->total_jumble_len > prev_jumble_len);
605}
606
607static void
609{
610 List *expr = (List *) node;
611 ListCell *l;
612
613 switch (expr->type)
614 {
615 case T_List:
616 foreach(l, expr)
618 break;
619 case T_IntList:
620 foreach(l, expr)
621 AppendJumble32(jstate, (const unsigned char *) &lfirst_int(l));
622 break;
623 case T_OidList:
624 foreach(l, expr)
625 AppendJumble32(jstate, (const unsigned char *) &lfirst_oid(l));
626 break;
627 case T_XidList:
628 foreach(l, expr)
629 AppendJumble32(jstate, (const unsigned char *) &lfirst_xid(l));
630 break;
631 default:
632 elog(ERROR, "unrecognized list node type: %d",
633 (int) expr->type);
634 return;
635 }
636}
637
638/*
639 * We try to jumble lists of expressions as one individual item regardless
640 * of how many elements are in the list. This is know as squashing, which
641 * results in different queries jumbling to the same query_id, if the only
642 * difference is the number of elements in the list.
643 *
644 * We allow constants and PARAM_EXTERN parameters to be squashed. To normalize
645 * such queries, we use the start and end locations of the list of elements in
646 * a list.
647 */
648static void
650{
651 bool normalize_list = false;
652
653 if (IsSquashableConstantList(elements))
654 {
655 if (IsA(node, ArrayExpr))
656 {
657 ArrayExpr *aexpr = (ArrayExpr *) node;
658
659 if (aexpr->list_start > 0 && aexpr->list_end > 0)
660 {
662 false,
663 aexpr->list_start + 1,
664 (aexpr->list_end - aexpr->list_start) - 1);
665 normalize_list = true;
666 jstate->has_squashed_lists = true;
667 }
668 }
669 }
670
671 if (!normalize_list)
672 {
673 _jumbleNode(jstate, (Node *) elements);
674 }
675}
676
677/*
678 * We store the highest param ID of extern params. This can later be used
679 * to start the numbering of the placeholder for squashed lists.
680 */
681static void
683{
684 Param *expr = (Param *) node;
685
686 JUMBLE_FIELD(paramkind);
687 JUMBLE_FIELD(paramid);
688 JUMBLE_FIELD(paramtype);
689 /* paramtypmod and paramcollid are ignored */
690
691 if (expr->paramkind == PARAM_EXTERN)
692 {
693 /*
694 * At this point, only external parameter locations outside of
695 * squashable lists will be recorded.
696 */
697 RecordConstLocation(jstate, true, expr->location, -1);
698
699 /*
700 * Update the highest Param id seen, in order to start normalization
701 * correctly.
702 *
703 * Note: This value is reset at the end of jumbling if there exists a
704 * squashable list. See the comment in the definition of JumbleState.
705 */
706 if (expr->paramid > jstate->highest_extern_param_id)
707 jstate->highest_extern_param_id = expr->paramid;
708 }
709}
710
711static void
713{
714 A_Const *expr = (A_Const *) node;
715
716 JUMBLE_FIELD(isnull);
717 if (!expr->isnull)
718 {
719 JUMBLE_FIELD(val.node.type);
720 switch (nodeTag(&expr->val))
721 {
722 case T_Integer:
723 JUMBLE_FIELD(val.ival.ival);
724 break;
725 case T_Float:
726 JUMBLE_STRING(val.fval.fval);
727 break;
728 case T_Boolean:
729 JUMBLE_FIELD(val.boolval.boolval);
730 break;
731 case T_String:
732 JUMBLE_STRING(val.sval.sval);
733 break;
734 case T_BitString:
735 JUMBLE_STRING(val.bsval.bsval);
736 break;
737 default:
738 elog(ERROR, "unrecognized node type: %d",
739 (int) nodeTag(&expr->val));
740 break;
741 }
742 }
743}
744
745static void
747{
748 VariableSetStmt *expr = (VariableSetStmt *) node;
749
750 JUMBLE_FIELD(kind);
752
753 /*
754 * Account for the list of arguments in query jumbling only if told by the
755 * parser.
756 */
757 if (expr->jumble_args)
758 JUMBLE_NODE(args);
759 JUMBLE_FIELD(is_local);
760 JUMBLE_LOCATION(location);
761}
762
763/*
764 * Custom query jumble function for RangeTblEntry.eref.
765 */
766static void
769 Alias *expr)
770{
772
773 /*
774 * This includes only the table name, the list of column names is ignored.
775 */
776 JUMBLE_STRING(aliasname);
777}
778
779/*
780 * CompLocation: comparator for qsorting LocationLen structs by location
781 */
782static int
783CompLocation(const void *a, const void *b)
784{
785 int l = ((const LocationLen *) a)->location;
786 int r = ((const LocationLen *) b)->location;
787
788 return pg_cmp_s32(l, r);
789}
790
791/*
792 * Given a valid SQL string and an array of constant-location records, return
793 * the textual lengths of those constants in a newly allocated LocationLen
794 * array, or NULL if there are no constants.
795 *
796 * The constants may use any allowed constant syntax, such as float literals,
797 * bit-strings, single-quoted strings and dollar-quoted strings. This is
798 * accomplished by using the public API for the core scanner.
799 *
800 * It is the caller's job to ensure that the string is a valid SQL statement
801 * with constants at the indicated locations. Since in practice the string
802 * has already been parsed, and the locations that the caller provides will
803 * have originated from within the authoritative parser, this should not be
804 * a problem.
805 *
806 * Multiple constants can have the same location. We reset lengths of those
807 * past the first to -1 so that they can later be ignored.
808 *
809 * If query_loc > 0, then "query" has been advanced by that much compared to
810 * the original string start, as is the case with multi-statement strings, so
811 * we need to translate the provided locations to compensate. (This lets us
812 * avoid re-scanning statements before the one of interest, so it's worth
813 * doing.)
814 *
815 * N.B. There is an assumption that a '-' character at a Const location begins
816 * a negative numeric constant. This precludes there ever being another
817 * reason for a constant to start with a '-'.
818 *
819 * It is the caller's responsibility to free the result, if necessary.
820 */
822ComputeConstantLengths(const JumbleState *jstate, const char *query,
823 int query_loc)
824{
826 core_yyscan_t yyscanner;
830
831 if (jstate->clocations_count == 0)
832 return NULL;
833
834 /* Copy constant locations to avoid modifying jstate */
835 locs = palloc_array(LocationLen, jstate->clocations_count);
836 memcpy(locs, jstate->clocations, jstate->clocations_count * sizeof(LocationLen));
837
838 /*
839 * Sort the records by location so that we can process them in order while
840 * scanning the query text.
841 */
842 if (jstate->clocations_count > 1)
843 qsort(locs, jstate->clocations_count,
844 sizeof(LocationLen), CompLocation);
845
846 /* initialize the flex scanner --- should match raw_parser() */
847 yyscanner = scanner_init(query,
848 &yyextra,
851
852 /* Search for each constant, in sequence */
853 for (int i = 0; i < jstate->clocations_count; i++)
854 {
855 int loc;
856 int tok;
857
858 /* Ignore constants after the first one in the same location */
859 if (i > 0 && locs[i].location == locs[i - 1].location)
860 {
861 locs[i].length = -1;
862 continue;
863 }
864
865 if (locs[i].squashed)
866 continue; /* squashable list, ignore */
867
868 /*
869 * Adjust the constant's location using the provided starting location
870 * of the current statement. This allows us to avoid scanning a
871 * multi-statement string from the beginning.
872 */
873 loc = locs[i].location - query_loc;
874 Assert(loc >= 0);
875
876 /*
877 * We have a valid location for a constant that's not a dupe. Lex
878 * tokens until we find the desired constant.
879 */
880 for (;;)
881 {
882 tok = core_yylex(&yylval, &yylloc, yyscanner);
883
884 /* We should not hit end-of-string, but if we do, behave sanely */
885 if (tok == 0)
886 break; /* out of inner for-loop */
887
888 /*
889 * We should find the token position exactly, but if we somehow
890 * run past it, work with that.
891 */
892 if (yylloc >= loc)
893 {
894 if (query[loc] == '-')
895 {
896 /*
897 * It's a negative value - this is the one and only case
898 * where we replace more than a single token.
899 *
900 * Do not compensate for the special-case adjustment of
901 * location to that of the leading '-' operator in the
902 * event of a negative constant (see doNegate() in
903 * gram.y). It is also useful for our purposes to start
904 * from the minus symbol. In this way, queries like
905 * "select * from foo where bar = 1" and "select * from
906 * foo where bar = -2" can be treated similarly.
907 */
908 tok = core_yylex(&yylval, &yylloc, yyscanner);
909 if (tok == 0)
910 break; /* out of inner for-loop */
911 }
912
913 /*
914 * We now rely on the assumption that flex has placed a zero
915 * byte after the text of the current token in scanbuf.
916 */
917 locs[i].length = strlen(yyextra.scanbuf + loc);
918 break; /* out of inner for-loop */
919 }
920 }
921
922 /* If we hit end-of-string, give up, leaving remaining lengths -1 */
923 if (tok == 0)
924 break;
925 }
926
927 scanner_finish(yyscanner);
928
929 return locs;
930}
#define INT64CONST(x)
Definition c.h:630
#define pg_noinline
Definition c.h:321
#define Min(x, y)
Definition c.h:1091
#define likely(x)
Definition c.h:437
#define Assert(condition)
Definition c.h:943
int64_t int64
Definition c.h:621
#define pg_attribute_always_inline
Definition c.h:305
#define unlikely(x)
Definition c.h:438
size_t Size
Definition c.h:689
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
Datum arg
Definition elog.c:1322
#define WARNING
Definition elog.h:37
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc_array(type, count)
Definition fe_memutils.h:76
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition hashfn.h:37
long val
Definition informix.c:689
static struct @177 value
static int pg_cmp_s32(int32 a, int32 b)
Definition int.h:713
int b
Definition isn.c:74
int a
Definition isn.c:73
int i
Definition isn.c:77
PGDLLIMPORT const ScanKeywordList ScanKeywords
void * repalloc(void *pointer, Size size)
Definition mcxt.c:1632
void * palloc(Size size)
Definition mcxt.c:1387
#define IsA(nodeptr, _type_)
Definition nodes.h:164
#define nodeTag(nodeptr)
Definition nodes.h:139
#define castNode(_type_, nodeptr)
Definition nodes.h:182
const void size_t len
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
#define foreach_current_index(var_or_cell)
Definition pg_list.h:435
#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
#define qsort(a, b, c, d)
Definition port.h:495
static int64 DatumGetInt64(Datum X)
Definition postgres.h:403
static int fb(int x)
const char * YYLTYPE
@ PARAM_EXTERN
Definition primnodes.h:385
@ COERCE_IMPLICIT_CAST
Definition primnodes.h:769
@ COERCE_EXPLICIT_CAST
Definition primnodes.h:768
@ COMPUTE_QUERY_ID_AUTO
Definition queryjumble.h:85
@ COMPUTE_QUERY_ID_OFF
Definition queryjumble.h:83
static bool IsQueryIdEnabled(void)
#define JUMBLE_NODE(item)
JumbleState * JumbleQuery(Query *query)
bool query_id_enabled
static void _jumbleNode(JumbleState *jstate, Node *node)
static void _jumbleVariableSetStmt(JumbleState *jstate, Node *node)
#define JUMBLE_SIZE
static bool IsSquashableConstantList(List *elements)
static int CompLocation(const void *a, const void *b)
int compute_query_id
static bool IsSquashableConstant(Node *element)
static pg_attribute_always_inline void AppendJumbleInternal(JumbleState *jstate, const unsigned char *item, Size size)
static pg_attribute_always_inline void AppendJumbleNull(JumbleState *jstate)
static pg_noinline void AppendJumble8(JumbleState *jstate, const unsigned char *value)
#define JUMBLE_LOCATION(location)
static void _jumbleParam(JumbleState *jstate, Node *node)
const char * CleanQuerytext(const char *query, int *location, int *len)
static void _jumbleList(JumbleState *jstate, Node *node)
static void FlushPendingNulls(JumbleState *jstate)
static void RecordConstLocation(JumbleState *jstate, bool extern_param, int location, int len)
#define JUMBLE_STRING(str)
static pg_noinline void AppendJumble32(JumbleState *jstate, const unsigned char *value)
static pg_noinline void AppendJumble64(JumbleState *jstate, const unsigned char *value)
static void _jumbleA_Const(JumbleState *jstate, Node *node)
static void AppendJumble(JumbleState *jstate, const unsigned char *value, Size size)
void EnableQueryId(void)
static void _jumbleElements(JumbleState *jstate, List *elements, Node *node)
static pg_noinline void AppendJumble16(JumbleState *jstate, const unsigned char *value)
static int64 DoJumble(JumbleState *jstate, Node *node)
LocationLen * ComputeConstantLengths(const JumbleState *jstate, const char *query, int query_loc)
static void _jumbleRangeTblEntry_eref(JumbleState *jstate, RangeTblEntry *rte, Alias *expr)
static JumbleState * InitJumble(void)
#define JUMBLE_FIELD(item)
static chr element(struct vars *v, const chr *startp, const chr *endp)
core_yyscan_t scanner_init(const char *str, core_yy_extra_type *yyext, const ScanKeywordList *keywordlist, const uint16 *keyword_tokens)
Definition scan.l:1233
#define yylloc
Definition scan.l:1106
void scanner_finish(core_yyscan_t yyscanner)
Definition scan.l:1273
#define yyextra
Definition scan.l:1102
const uint16 ScanKeywordTokens[]
Definition scan.l:80
void * core_yyscan_t
Definition scanner.h:118
int core_yylex(core_YYSTYPE *yylval_param, YYLTYPE *yylloc_param, core_yyscan_t yyscanner)
bool scanner_isspace(char ch)
Definition scansup.c:105
bool stack_is_too_deep(void)
void check_stack_depth(void)
Definition stack_depth.c:95
bool isnull
Definition parsenodes.h:391
union ValUnion val
Definition parsenodes.h:390
Oid funcid
Definition primnodes.h:783
List * args
Definition primnodes.h:801
Definition pg_list.h:54
NodeTag type
Definition pg_list.h:55
Definition nodes.h:135
ParseLoc location
Definition primnodes.h:404
int paramid
Definition primnodes.h:397
ParamKind paramkind
Definition primnodes.h:396
Node * utilityStmt
Definition parsenodes.h:141
#define FirstGenbkiObjectId
Definition transam.h:195
const char * type
const char * name