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
 

Enumerations

enum  ComputeQueryIdType { COMPUTE_QUERY_ID_OFF, COMPUTE_QUERY_ID_ON, COMPUTE_QUERY_ID_AUTO }
 

Functions

const char * CleanQuerytext (const char *query, int *location, int *len)
 
JumbleStateJumbleQuery (Query *query, const char *querytext)
 
void EnableQueryId (void)
 
static bool IsQueryIdEnabled (void)
 

Variables

int compute_query_id
 
bool query_id_enabled
 

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

Enumeration Type Documentation

◆ ComputeQueryIdType

Enumerator
COMPUTE_QUERY_ID_OFF 
COMPUTE_QUERY_ID_ON 
COMPUTE_QUERY_ID_AUTO 

Definition at line 56 of file queryjumble.h.

Function Documentation

◆ CleanQuerytext()

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

Definition at line 62 of file queryjumble.c.

References Assert, and scanner_isspace().

Referenced by compute_utility_query_id(), and pgss_store().

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

◆ EnableQueryId()

void EnableQueryId ( void  )

Definition at line 150 of file queryjumble.c.

References compute_query_id, COMPUTE_QUERY_ID_OFF, and query_id_enabled.

Referenced by _PG_init().

151 {
153  query_id_enabled = true;
154 }
int compute_query_id
Definition: queryjumble.c:43
bool query_id_enabled
Definition: queryjumble.c:46

◆ IsQueryIdEnabled()

static bool IsQueryIdEnabled ( void  )
inlinestatic

◆ JumbleQuery()

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

Definition at line 101 of file queryjumble.c.

References Assert, JumbleState::clocations, JumbleState::clocations_buf_size, JumbleState::clocations_count, compute_utility_query_id(), DatumGetUInt64, hash_any_extended(), JumbleState::highest_extern_param_id, IsQueryIdEnabled(), 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().

102 {
103  JumbleState *jstate = NULL;
104 
106 
107  if (query->utilityStmt)
108  {
109  query->queryId = compute_utility_query_id(querytext,
110  query->stmt_location,
111  query->stmt_len);
112  }
113  else
114  {
115  jstate = (JumbleState *) palloc(sizeof(JumbleState));
116 
117  /* Set up workspace for query jumbling */
118  jstate->jumble = (unsigned char *) palloc(JUMBLE_SIZE);
119  jstate->jumble_len = 0;
120  jstate->clocations_buf_size = 32;
121  jstate->clocations = (LocationLen *)
122  palloc(jstate->clocations_buf_size * sizeof(LocationLen));
123  jstate->clocations_count = 0;
124  jstate->highest_extern_param_id = 0;
125 
126  /* Compute query ID and mark the Query node with it */
127  JumbleQueryInternal(jstate, query);
129  jstate->jumble_len,
130  0));
131 
132  /*
133  * If we are unlucky enough to get a hash of zero, use 1 instead, to
134  * prevent confusion with the utility-statement case.
135  */
136  if (query->queryId == UINT64CONST(0))
137  query->queryId = UINT64CONST(1);
138  }
139 
140  return jstate;
141 }
Size jumble_len
Definition: queryjumble.h:40
static bool IsQueryIdEnabled(void)
Definition: queryjumble.h:78
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:160
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:240
uint64 queryId
Definition: parsenodes.h:124
#define DatumGetUInt64(X)
Definition: postgres.h:678
unsigned char * jumble
Definition: queryjumble.h:37
#define Assert(condition)
Definition: c.h:804
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

Variable Documentation

◆ compute_query_id

int compute_query_id

Definition at line 43 of file queryjumble.c.

Referenced by EnableQueryId(), and IsQueryIdEnabled().

◆ query_id_enabled

bool query_id_enabled

Definition at line 46 of file queryjumble.c.

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