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
 

Macros

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

Typedefs

typedef struct LocationLen LocationLen
 
typedef struct JumbleState JumbleState
 

Functions

const char * CleanQuerytext (const char *query, int *location, int *len)
 
JumbleStateJumbleQuery (Query *query, const char *querytext)
 

Macro Definition Documentation

◆ JUMBLE_SIZE

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

Definition at line 19 of file queryjumble.h.

Typedef Documentation

◆ JumbleState

typedef struct JumbleState JumbleState

◆ LocationLen

typedef struct LocationLen LocationLen

Function Documentation

◆ CleanQuerytext()

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

Definition at line 56 of file queryjumble.c.

References Assert, and scanner_isspace().

Referenced by compute_utility_query_id(), and pgss_store().

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

◆ JumbleQuery()

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

Definition at line 95 of file queryjumble.c.

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

Referenced by ExplainQuery(), parse_analyze(), parse_analyze_varparams(), and pg_analyze_and_rewrite_params().

96 {
97  JumbleState *jstate = NULL;
98 
99  if (query->utilityStmt)
100  {
101  query->queryId = compute_utility_query_id(querytext,
102  query->stmt_location,
103  query->stmt_len);
104  }
105  else
106  {
107  jstate = (JumbleState *) palloc(sizeof(JumbleState));
108 
109  /* Set up workspace for query jumbling */
110  jstate->jumble = (unsigned char *) palloc(JUMBLE_SIZE);
111  jstate->jumble_len = 0;
112  jstate->clocations_buf_size = 32;
113  jstate->clocations = (LocationLen *)
114  palloc(jstate->clocations_buf_size * sizeof(LocationLen));
115  jstate->clocations_count = 0;
116  jstate->highest_extern_param_id = 0;
117 
118  /* Compute query ID and mark the Query node with it */
119  JumbleQueryInternal(jstate, query);
121  jstate->jumble_len,
122  0));
123 
124  /*
125  * If we are unlucky enough to get a hash of zero, use 1 instead, to
126  * prevent confusion with the utility-statement case.
127  */
128  if (query->queryId == UINT64CONST(0))
129  query->queryId = UINT64CONST(1);
130  }
131 
132  return jstate;
133 }
Size jumble_len
Definition: queryjumble.h:40
int stmt_location
Definition: parsenodes.h:192
static uint64 compute_utility_query_id(const char *str, int query_location, int query_len)
Definition: queryjumble.c:139
int clocations_buf_size
Definition: queryjumble.h:46
Node * utilityStmt
Definition: parsenodes.h:128
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37
int clocations_count
Definition: queryjumble.h:49
int highest_extern_param_id
Definition: queryjumble.h:52
static void JumbleQueryInternal(JumbleState *jstate, Query *query)
Definition: queryjumble.c:219
uint64 queryId
Definition: parsenodes.h:124
#define DatumGetUInt64(X)
Definition: postgres.h:678
unsigned char * jumble
Definition: queryjumble.h:37
LocationLen * clocations
Definition: queryjumble.h:43
void * palloc(Size size)
Definition: mcxt.c:1062
int stmt_len
Definition: parsenodes.h:193
#define JUMBLE_SIZE
Definition: queryjumble.c:40