PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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)
 
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 67 of file queryjumblefuncs.c.

68 {
69  int query_location = *location;
70  int query_len = *len;
71 
72  /* First apply starting offset, unless it's -1 (unknown). */
73  if (query_location >= 0)
74  {
75  Assert(query_location <= strlen(query));
76  query += query_location;
77  /* Length of 0 (or -1) means "rest of string" */
78  if (query_len <= 0)
79  query_len = strlen(query);
80  else
81  Assert(query_len <= strlen(query));
82  }
83  else
84  {
85  /* If query location is unknown, distrust query_len as well */
86  query_location = 0;
87  query_len = strlen(query);
88  }
89 
90  /*
91  * Discard leading and trailing whitespace, too. Use scanner_isspace()
92  * not libc's isspace(), because we want to match the lexer's behavior.
93  *
94  * Note: the parser now strips leading comments and whitespace from the
95  * reported stmt_location, so this first loop will only iterate in the
96  * unusual case that the location didn't propagate to here. But the
97  * statement length will extend to the end-of-string or terminating
98  * semicolon, so the second loop often does something useful.
99  */
100  while (query_len > 0 && scanner_isspace(query[0]))
101  query++, query_location++, query_len--;
102  while (query_len > 0 && scanner_isspace(query[query_len - 1]))
103  query_len--;
104 
105  *location = query_location;
106  *len = query_len;
107 
108  return query;
109 }
#define Assert(condition)
Definition: c.h:863
const void size_t len
bool scanner_isspace(char ch)
Definition: scansup.c:117

References Assert, len, and scanner_isspace().

Referenced by pgss_store(), and script_error_callback().

◆ EnableQueryId()

void EnableQueryId ( void  )

Definition at line 157 of file queryjumblefuncs.c.

158 {
160  query_id_enabled = true;
161 }
bool query_id_enabled
int compute_query_id

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
PGDLLIMPORT int compute_query_id

References compute_query_id, COMPUTE_QUERY_ID_OFF, COMPUTE_QUERY_ID_ON, and query_id_enabled.

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

◆ JumbleQuery()

JumbleState* JumbleQuery ( Query query)

Definition at line 112 of file queryjumblefuncs.c.

113 {
114  JumbleState *jstate = NULL;
115 
117 
118  jstate = (JumbleState *) palloc(sizeof(JumbleState));
119 
120  /* Set up workspace for query jumbling */
121  jstate->jumble = (unsigned char *) palloc(JUMBLE_SIZE);
122  jstate->jumble_len = 0;
123  jstate->clocations_buf_size = 32;
124  jstate->clocations = (LocationLen *)
125  palloc(jstate->clocations_buf_size * sizeof(LocationLen));
126  jstate->clocations_count = 0;
127  jstate->highest_extern_param_id = 0;
128 
129  /* Compute query ID and mark the Query node with it */
130  _jumbleNode(jstate, (Node *) query);
131  query->queryId = DatumGetUInt64(hash_any_extended(jstate->jumble,
132  jstate->jumble_len,
133  0));
134 
135  /*
136  * If we are unlucky enough to get a hash of zero, use 1 instead for
137  * normal statements and 2 for utility queries.
138  */
139  if (query->queryId == UINT64CONST(0))
140  {
141  if (query->utilityStmt)
142  query->queryId = UINT64CONST(2);
143  else
144  query->queryId = UINT64CONST(1);
145  }
146 
147  return jstate;
148 }
static Datum hash_any_extended(const unsigned char *k, int keylen, uint64 seed)
Definition: hashfn.h:37
void * palloc(Size size)
Definition: mcxt.c:1317
static uint64 DatumGetUInt64(Datum X)
Definition: postgres.h:419
static bool IsQueryIdEnabled(void)
Definition: queryjumble.h:77
static void _jumbleNode(JumbleState *jstate, Node *node)
#define JUMBLE_SIZE
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
Definition: nodes.h:129
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 ExecCreateTableAs(), ExplainOneUtility(), ExplainQuery(), parse_analyze_fixedparams(), parse_analyze_varparams(), parse_analyze_withcb(), and PerformCursorOpen().

Variable Documentation

◆ compute_query_id

PGDLLIMPORT int compute_query_id
extern

Definition at line 43 of file queryjumblefuncs.c.

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

◆ query_id_enabled

PGDLLIMPORT bool query_id_enabled
extern

Definition at line 52 of file queryjumblefuncs.c.

Referenced by EnableQueryId(), and IsQueryIdEnabled().