PostgreSQL Source Code git master
Loading...
Searching...
No Matches
stashfuncs.c File Reference
#include "postgres.h"
#include "common/hashfn.h"
#include "fmgr.h"
#include "funcapi.h"
#include "pg_stash_advice.h"
#include "utils/builtins.h"
#include "utils/tuplestore.h"
#include "lib/simplehash.h"
Include dependency graph for stashfuncs.c:

Go to the source code of this file.

Data Structures

struct  pgsa_stash_count
 

Macros

#define SH_PREFIX   pgsa_stash_count_table
 
#define SH_ELEMENT_TYPE   pgsa_stash_count
 
#define SH_KEY_TYPE   uint64
 
#define SH_KEY   pgsa_stash_id
 
#define SH_HASH_KEY(tb, key)   hash_bytes((const unsigned char *) &(key), sizeof(uint64))
 
#define SH_EQUAL(tb, a, b)   (a == b)
 
#define SH_SCOPE   static inline
 
#define SH_DEFINE
 
#define SH_DECLARE
 

Typedefs

typedef struct pgsa_stash_count pgsa_stash_count
 

Functions

 PG_FUNCTION_INFO_V1 (pg_create_advice_stash)
 
 PG_FUNCTION_INFO_V1 (pg_drop_advice_stash)
 
 PG_FUNCTION_INFO_V1 (pg_get_advice_stash_contents)
 
 PG_FUNCTION_INFO_V1 (pg_get_advice_stashes)
 
 PG_FUNCTION_INFO_V1 (pg_set_stashed_advice)
 
Datum pg_create_advice_stash (PG_FUNCTION_ARGS)
 
Datum pg_drop_advice_stash (PG_FUNCTION_ARGS)
 
Datum pg_get_advice_stashes (PG_FUNCTION_ARGS)
 
Datum pg_get_advice_stash_contents (PG_FUNCTION_ARGS)
 
Datum pg_set_stashed_advice (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 42 of file stashfuncs.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 41 of file stashfuncs.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   pgsa_stash_count

Definition at line 35 of file stashfuncs.c.

◆ SH_EQUAL

#define SH_EQUAL (   tb,
  a,
  b 
)    (a == b)

Definition at line 39 of file stashfuncs.c.

◆ SH_HASH_KEY

#define SH_HASH_KEY (   tb,
  key 
)    hash_bytes((const unsigned char *) &(key), sizeof(uint64))

Definition at line 38 of file stashfuncs.c.

◆ SH_KEY

#define SH_KEY   pgsa_stash_id

Definition at line 37 of file stashfuncs.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   uint64

Definition at line 36 of file stashfuncs.c.

◆ SH_PREFIX

#define SH_PREFIX   pgsa_stash_count_table

Definition at line 34 of file stashfuncs.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 40 of file stashfuncs.c.

Typedef Documentation

◆ pgsa_stash_count

Function Documentation

◆ pg_create_advice_stash()

Datum pg_create_advice_stash ( PG_FUNCTION_ARGS  )

Definition at line 49 of file stashfuncs.c.

50{
52
60}
#define unlikely(x)
Definition c.h:438
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_GETARG_TEXT_PP(n)
Definition fmgr.h:310
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
@ LW_EXCLUSIVE
Definition lwlock.h:104
dshash_table * pgsa_entry_dshash
void pgsa_create_stash(char *stash_name)
void pgsa_attach(void)
void pgsa_check_stash_name(char *stash_name)
pgsa_shared_state * pgsa_state
static int fb(int x)
char * text_to_cstring(const text *t)
Definition varlena.c:217

References fb(), pgsa_shared_state::lock, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), PG_GETARG_TEXT_PP, PG_RETURN_VOID, pgsa_attach(), pgsa_check_stash_name(), pgsa_create_stash(), pgsa_entry_dshash, pgsa_state, text_to_cstring(), and unlikely.

◆ pg_drop_advice_stash()

◆ PG_FUNCTION_INFO_V1() [1/5]

PG_FUNCTION_INFO_V1 ( pg_create_advice_stash  )

◆ PG_FUNCTION_INFO_V1() [2/5]

PG_FUNCTION_INFO_V1 ( pg_drop_advice_stash  )

◆ PG_FUNCTION_INFO_V1() [3/5]

PG_FUNCTION_INFO_V1 ( pg_get_advice_stash_contents  )

◆ PG_FUNCTION_INFO_V1() [4/5]

PG_FUNCTION_INFO_V1 ( pg_get_advice_stashes  )

◆ PG_FUNCTION_INFO_V1() [5/5]

PG_FUNCTION_INFO_V1 ( pg_set_stashed_advice  )

◆ pg_get_advice_stash_contents()

Datum pg_get_advice_stash_contents ( PG_FUNCTION_ARGS  )

Definition at line 142 of file stashfuncs.c.

143{
144 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
146 char *stash_name = NULL;
148 uint64 stash_id = 0;
149 pgsa_entry *entry;
150
151 InitMaterializedSRF(fcinfo, 0);
152
153 /* Attach to dynamic shared memory if not already done. */
155 pgsa_attach();
156
157 /* User can pass NULL for all stashes, or the name of a specific stash. */
158 if (!PG_ARGISNULL(0))
159 {
163
164 /* If the user specified a stash name, it should exist. */
165 if (stash_id == 0)
168 errmsg("advice stash \"%s\" does not exist", stash_name));
169 }
170 else
171 {
173
174 /*
175 * If we're dumping data about all stashes, we need an ID->name lookup
176 * table.
177 */
180 while ((stash = dshash_seq_next(&iterator)) != NULL)
181 {
183 bool found;
184
186 stash->pgsa_stash_id,
187 &found);
188 Assert(!found);
189 n->name = pstrdup(stash->name);
190 }
192 }
193
194 /* Now iterate over all the entries. */
196 while ((entry = dshash_seq_next(&iterator)) != NULL)
197 {
198 Datum values[3];
199 bool nulls[3];
200 char *this_stash_name;
201 char *advice_string;
202
203 /* Skip incomplete entries where the advice string was never set. */
204 if (entry->advice_string == InvalidDsaPointer)
205 continue;
206
207 if (stash_id != 0)
208 {
209 /*
210 * We're only dumping data for one particular stash, so skip
211 * entries for any other stash and use the stash name specified by
212 * the user.
213 */
214 if (stash_id != entry->key.pgsa_stash_id)
215 continue;
217 }
218 else
219 {
221
222 /*
223 * We're dumping data for all stashes, so look up the correct name
224 * to use in the hash table. If nothing is found, which is
225 * possible due to race conditions, make up a string to use.
226 */
228 if (n != NULL)
230 else
231 this_stash_name = psprintf("<stash %" PRIu64 ">",
232 entry->key.pgsa_stash_id);
233 }
234
235 /* Work out tuple values. */
237 nulls[0] = false;
238 values[1] = Int64GetDatum(entry->key.queryId);
239 nulls[1] = false;
240 advice_string = dsa_get_address(pgsa_dsa_area, entry->advice_string);
241 values[2] = CStringGetTextDatum(advice_string);
242 nulls[2] = false;
243
244 /* Emit the tuple. */
245 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values,
246 nulls);
247 }
249
250 return (Datum) 0;
251}
static Datum values[MAXATTR]
Definition bootstrap.c:190
#define CStringGetTextDatum(s)
Definition builtins.h:98
#define Assert(condition)
Definition c.h:943
uint64_t uint64
Definition c.h:625
void * dsa_get_address(dsa_area *area, dsa_pointer dp)
Definition dsa.c:957
#define InvalidDsaPointer
Definition dsa.h:78
void dshash_seq_init(dshash_seq_status *status, dshash_table *hash_table, bool exclusive)
Definition dshash.c:659
void dshash_seq_term(dshash_seq_status *status)
Definition dshash.c:768
void * dshash_seq_next(dshash_seq_status *status)
Definition dshash.c:678
int errcode(int sqlerrcode)
Definition elog.c:874
#define ERROR
Definition elog.h:39
#define ereport(elevel,...)
Definition elog.h:151
#define PG_ARGISNULL(n)
Definition fmgr.h:209
void InitMaterializedSRF(FunctionCallInfo fcinfo, uint32 flags)
Definition funcapi.c:76
char * pstrdup(const char *in)
Definition mcxt.c:1781
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
static char * errmsg
dshash_table * pgsa_stash_dshash
dsa_area * pgsa_dsa_area
uint64 pgsa_lookup_stash_id(char *stash_name)
static Datum Int64GetDatum(int64 X)
Definition postgres.h:413
uint64_t Datum
Definition postgres.h:70
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
int64 queryId
uint64 pgsa_stash_id
pgsa_entry_key key
dsa_pointer advice_string
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, const Datum *values, const bool *isnull)
Definition tuplestore.c:785

References pgsa_entry::advice_string, Assert, CStringGetTextDatum, CurrentMemoryContext, dsa_get_address(), dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), ereport, errcode(), errmsg, ERROR, fb(), InitMaterializedSRF(), Int64GetDatum(), InvalidDsaPointer, pgsa_entry::key, pgsa_stash_name::name, PG_ARGISNULL, PG_GETARG_TEXT_PP, pgsa_attach(), pgsa_check_stash_name(), pgsa_dsa_area, pgsa_entry_dshash, pgsa_lookup_stash_id(), pgsa_stash_dshash, pgsa_entry_key::pgsa_stash_id, psprintf(), pstrdup(), pgsa_entry_key::queryId, text_to_cstring(), tuplestore_putvalues(), unlikely, and values.

◆ pg_get_advice_stashes()

Datum pg_get_advice_stashes ( PG_FUNCTION_ARGS  )

Definition at line 83 of file stashfuncs.c.

84{
85 ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
87 pgsa_entry *entry;
90
91 InitMaterializedSRF(fcinfo, 0);
92
93 /* Attach to dynamic shared memory if not already done. */
96
97 /* Tally up the number of entries per stash. */
100 while ((entry = dshash_seq_next(&iterator)) != NULL)
101 {
103 bool found;
104
106 entry->key.pgsa_stash_id,
107 &found);
108 if (!found)
109 c->num_entries = 1;
110 else
111 c->num_entries++;
112 }
114
115 /* Emit results. */
117 while ((stash = dshash_seq_next(&iterator)) != NULL)
118 {
119 Datum values[2];
120 bool nulls[2];
122
123 values[0] = CStringGetTextDatum(stash->name);
124 nulls[0] = false;
125
126 c = pgsa_stash_count_table_lookup(chash, stash->pgsa_stash_id);
127 values[1] = Int64GetDatum(c == NULL ? 0 : c->num_entries);
128 nulls[1] = false;
129
130 tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values,
131 nulls);
132 }
134
135 return (Datum) 0;
136}
char * c

References CStringGetTextDatum, CurrentMemoryContext, dshash_seq_init(), dshash_seq_next(), dshash_seq_term(), fb(), InitMaterializedSRF(), Int64GetDatum(), pgsa_entry::key, pgsa_attach(), pgsa_entry_dshash, pgsa_stash_dshash, pgsa_entry_key::pgsa_stash_id, tuplestore_putvalues(), unlikely, and values.

◆ pg_set_stashed_advice()

Datum pg_set_stashed_advice ( PG_FUNCTION_ARGS  )

Definition at line 262 of file stashfuncs.c.

263{
264 char *stash_name;
265 int64 queryId;
266
267 if (PG_ARGISNULL(0) || PG_ARGISNULL(1))
269
270 /* Get and check advice stash name. */
273
274 /*
275 * Get and check query ID.
276 *
277 * queryID 0 means no query ID was computed, so reject that.
278 */
279 queryId = PG_GETARG_INT64(1);
280 if (queryId == 0)
283 errmsg("cannot set advice string for query ID 0"));
284
285 /* Attach to dynamic shared memory if not already done. */
287 pgsa_attach();
288
289 /* Now call the appropriate function to do the real work. */
290 if (PG_ARGISNULL(2))
291 {
295 }
296 else
297 {
298 char *advice_string = text_to_cstring(PG_GETARG_TEXT_PP(2));
299
301 pgsa_set_advice_string(stash_name, queryId, advice_string);
303 }
304
306}
int64_t int64
Definition c.h:621
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_GETARG_INT64(n)
Definition fmgr.h:284
@ LW_SHARED
Definition lwlock.h:105
void pgsa_set_advice_string(char *stash_name, int64 queryId, char *advice_string)
void pgsa_clear_advice_string(char *stash_name, int64 queryId)

References ereport, errcode(), errmsg, ERROR, fb(), pgsa_shared_state::lock, LW_SHARED, LWLockAcquire(), LWLockRelease(), PG_ARGISNULL, PG_GETARG_INT64, PG_GETARG_TEXT_PP, PG_RETURN_NULL, PG_RETURN_VOID, pgsa_attach(), pgsa_check_stash_name(), pgsa_clear_advice_string(), pgsa_entry_dshash, pgsa_set_advice_string(), pgsa_state, text_to_cstring(), and unlikely.