PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgpa_trove.c File Reference
#include "postgres.h"
#include "pg_plan_advice.h"
#include "pgpa_trove.h"
#include "common/hashfn_unstable.h"
#include "lib/simplehash.h"
Include dependency graph for pgpa_trove.c:

Go to the source code of this file.

Data Structures

struct  pgpa_trove_slice
 
struct  pgpa_trove
 
struct  pgpa_trove_entry_key
 
struct  pgpa_trove_entry_element
 

Macros

#define SH_PREFIX   pgpa_trove_entry
 
#define SH_ELEMENT_TYPE   pgpa_trove_entry_element
 
#define SH_KEY_TYPE   pgpa_trove_entry_key
 
#define SH_KEY   key
 
#define SH_HASH_KEY(tb, key)   pgpa_trove_entry_hash_key(key)
 
#define SH_EQUAL(tb, a, b)   pgpa_trove_entry_compare_key(a, b)
 
#define SH_SCOPE   static inline
 
#define SH_DECLARE
 
#define SH_DEFINE
 

Typedefs

typedef struct pgpa_trove_slice pgpa_trove_slice
 

Functions

static uint32 pgpa_trove_entry_hash_key (pgpa_trove_entry_key key)
 
static bool pgpa_trove_entry_compare_key (pgpa_trove_entry_key a, pgpa_trove_entry_key b)
 
static void pgpa_init_trove_slice (pgpa_trove_slice *tslice)
 
static void pgpa_trove_add_to_slice (pgpa_trove_slice *tslice, pgpa_advice_tag_type tag, pgpa_advice_target *target)
 
static void pgpa_trove_add_to_hash (pgpa_trove_entry_hash *hash, pgpa_advice_target *target, int index)
 
static Bitmapsetpgpa_trove_slice_lookup (pgpa_trove_slice *tslice, pgpa_identifier *rid)
 
pgpa_trovepgpa_build_trove (List *advice_items)
 
void pgpa_trove_lookup (pgpa_trove *trove, pgpa_trove_lookup_type type, int nrids, pgpa_identifier *rids, pgpa_trove_result *result)
 
void pgpa_trove_lookup_all (pgpa_trove *trove, pgpa_trove_lookup_type type, pgpa_trove_entry **entries, int *nentries)
 
charpgpa_cstring_trove_entry (pgpa_trove_entry *entry)
 
void pgpa_trove_set_flags (pgpa_trove_entry *entries, Bitmapset *indexes, int flags)
 
void pgpa_trove_append_flags (StringInfo buf, int flags)
 

Macro Definition Documentation

◆ SH_DECLARE

#define SH_DECLARE

Definition at line 112 of file pgpa_trove.c.

◆ SH_DEFINE

#define SH_DEFINE

Definition at line 113 of file pgpa_trove.c.

◆ SH_ELEMENT_TYPE

#define SH_ELEMENT_TYPE   pgpa_trove_entry_element

Definition at line 106 of file pgpa_trove.c.

◆ SH_EQUAL

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

Definition at line 110 of file pgpa_trove.c.

◆ SH_HASH_KEY

#define SH_HASH_KEY (   tb,
  key 
)    pgpa_trove_entry_hash_key(key)

Definition at line 109 of file pgpa_trove.c.

◆ SH_KEY

#define SH_KEY   key

Definition at line 108 of file pgpa_trove.c.

◆ SH_KEY_TYPE

#define SH_KEY_TYPE   pgpa_trove_entry_key

Definition at line 107 of file pgpa_trove.c.

◆ SH_PREFIX

#define SH_PREFIX   pgpa_trove_entry

Definition at line 105 of file pgpa_trove.c.

◆ SH_SCOPE

#define SH_SCOPE   static inline

Definition at line 111 of file pgpa_trove.c.

Typedef Documentation

◆ pgpa_trove_slice

Function Documentation

◆ pgpa_build_trove()

pgpa_trove * pgpa_build_trove ( List advice_items)

Definition at line 133 of file pgpa_trove.c.

134{
136
138 pgpa_init_trove_slice(&trove->rel);
140
142 {
143 switch (item->tag)
144 {
146 {
147 pgpa_advice_target *target;
148
149 /*
150 * For most advice types, each element in the top-level
151 * list is a separate target, but it's most convenient to
152 * regard the entirety of a JOIN_ORDER specification as a
153 * single target. Since it wasn't represented that way
154 * during parsing, build a surrogate object now.
155 */
158 target->children = item->targets;
159
161 item->tag, target);
162 }
163 break;
164
171
172 /*
173 * Scan advice.
174 */
175 foreach_ptr(pgpa_advice_target, target, item->targets)
176 {
177 /*
178 * For now, all of our scan types target single relations,
179 * but in the future this might not be true, e.g. a custom
180 * scan could replace a join.
181 */
182 Assert(target->ttype == PGPA_TARGET_IDENTIFIER);
184 item->tag, target);
185 }
186 break;
187
197
198 /*
199 * Join strategy advice.
200 */
201 foreach_ptr(pgpa_advice_target, target, item->targets)
202 {
204 item->tag, target);
205 }
206 break;
207
209 case PGPA_TAG_GATHER:
212
213 /*
214 * Advice about a RelOptInfo relevant to both scans and joins.
215 */
216 foreach_ptr(pgpa_advice_target, target, item->targets)
217 {
219 item->tag, target);
220 }
221 break;
222 }
223 }
224
225 return trove;
226}
#define Assert(condition)
Definition c.h:943
#define palloc_object(type)
Definition fe_memutils.h:74
#define palloc0_object(type)
Definition fe_memutils.h:75
#define foreach_ptr(type, var, lst)
Definition pg_list.h:501
@ PGPA_TAG_INDEX_SCAN
Definition pgpa_ast.h:89
@ PGPA_TAG_NESTED_LOOP_MATERIALIZE
Definition pgpa_ast.h:93
@ PGPA_TAG_MERGE_JOIN_PLAIN
Definition pgpa_ast.h:92
@ PGPA_TAG_GATHER_MERGE
Definition pgpa_ast.h:86
@ PGPA_TAG_GATHER
Definition pgpa_ast.h:85
@ PGPA_TAG_NESTED_LOOP_MEMOIZE
Definition pgpa_ast.h:94
@ PGPA_TAG_SEMIJOIN_NON_UNIQUE
Definition pgpa_ast.h:98
@ PGPA_TAG_BITMAP_HEAP_SCAN
Definition pgpa_ast.h:82
@ PGPA_TAG_PARTITIONWISE
Definition pgpa_ast.h:97
@ PGPA_TAG_NO_GATHER
Definition pgpa_ast.h:96
@ PGPA_TAG_INDEX_ONLY_SCAN
Definition pgpa_ast.h:88
@ PGPA_TAG_SEQ_SCAN
Definition pgpa_ast.h:100
@ PGPA_TAG_HASH_JOIN
Definition pgpa_ast.h:87
@ PGPA_TAG_SEMIJOIN_UNIQUE
Definition pgpa_ast.h:99
@ PGPA_TAG_DO_NOT_SCAN
Definition pgpa_ast.h:83
@ PGPA_TAG_JOIN_ORDER
Definition pgpa_ast.h:90
@ PGPA_TAG_TID_SCAN
Definition pgpa_ast.h:101
@ PGPA_TAG_FOREIGN_JOIN
Definition pgpa_ast.h:84
@ PGPA_TAG_NESTED_LOOP_PLAIN
Definition pgpa_ast.h:95
@ PGPA_TAG_MERGE_JOIN_MATERIALIZE
Definition pgpa_ast.h:91
@ PGPA_TARGET_IDENTIFIER
Definition pgpa_ast.h:27
@ PGPA_TARGET_ORDERED_LIST
Definition pgpa_ast.h:28
static void pgpa_init_trove_slice(pgpa_trove_slice *tslice)
Definition pgpa_trove.c:433
static void pgpa_trove_add_to_slice(pgpa_trove_slice *tslice, pgpa_advice_tag_type tag, pgpa_advice_target *target)
Definition pgpa_trove.c:368
static int fb(int x)
pgpa_target_type ttype
Definition pgpa_ast.h:49
pgpa_trove_slice rel
Definition pgpa_trove.c:54
pgpa_trove_slice join
Definition pgpa_trove.c:53
pgpa_trove_slice scan
Definition pgpa_trove.c:55

References Assert, pgpa_advice_target::children, fb(), foreach_ptr, pgpa_trove::join, palloc0_object, palloc_object, pgpa_init_trove_slice(), PGPA_TAG_BITMAP_HEAP_SCAN, PGPA_TAG_DO_NOT_SCAN, PGPA_TAG_FOREIGN_JOIN, PGPA_TAG_GATHER, PGPA_TAG_GATHER_MERGE, PGPA_TAG_HASH_JOIN, PGPA_TAG_INDEX_ONLY_SCAN, PGPA_TAG_INDEX_SCAN, PGPA_TAG_JOIN_ORDER, PGPA_TAG_MERGE_JOIN_MATERIALIZE, PGPA_TAG_MERGE_JOIN_PLAIN, PGPA_TAG_NESTED_LOOP_MATERIALIZE, PGPA_TAG_NESTED_LOOP_MEMOIZE, PGPA_TAG_NESTED_LOOP_PLAIN, PGPA_TAG_NO_GATHER, PGPA_TAG_PARTITIONWISE, PGPA_TAG_SEMIJOIN_NON_UNIQUE, PGPA_TAG_SEMIJOIN_UNIQUE, PGPA_TAG_SEQ_SCAN, PGPA_TAG_TID_SCAN, PGPA_TARGET_IDENTIFIER, PGPA_TARGET_ORDERED_LIST, pgpa_trove_add_to_slice(), pgpa_trove::rel, pgpa_trove::scan, and pgpa_advice_target::ttype.

Referenced by pgpa_planner_setup().

◆ pgpa_cstring_trove_entry()

char * pgpa_cstring_trove_entry ( pgpa_trove_entry entry)

Definition at line 297 of file pgpa_trove.c.

298{
300
303
304 /* JOIN_ORDER tags are transformed by pgpa_build_trove; undo that here */
305 if (entry->tag != PGPA_TAG_JOIN_ORDER)
307 else
309
311
312 if (entry->target->itarget != NULL)
313 {
316 }
317
318 if (entry->tag != PGPA_TAG_JOIN_ORDER)
320
321 return buf.data;
322}
static char buf[DEFAULT_XLOG_SEG_SIZE]
char * pgpa_cstring_advice_tag(pgpa_advice_tag_type advice_tag)
Definition pgpa_ast.c:29
void pgpa_format_advice_target(StringInfo str, pgpa_advice_target *target)
Definition pgpa_ast.c:170
void pgpa_format_index_target(StringInfo str, pgpa_index_target *itarget)
Definition pgpa_ast.c:206
void appendStringInfoString(StringInfo str, const char *s)
Definition stringinfo.c:230
void appendStringInfoChar(StringInfo str, char ch)
Definition stringinfo.c:242
void initStringInfo(StringInfo str)
Definition stringinfo.c:97
pgpa_index_target * itarget
Definition pgpa_ast.h:64
pgpa_advice_target * target
Definition pgpa_trove.h:29
pgpa_advice_tag_type tag
Definition pgpa_trove.h:28

References appendStringInfoChar(), appendStringInfoString(), Assert, buf, fb(), initStringInfo(), pgpa_advice_target::itarget, pgpa_cstring_advice_tag(), pgpa_format_advice_target(), pgpa_format_index_target(), PGPA_TAG_JOIN_ORDER, PGPA_TARGET_ORDERED_LIST, pgpa_trove_entry::tag, pgpa_trove_entry::target, and pgpa_advice_target::ttype.

Referenced by pgpa_planner_append_feedback().

◆ pgpa_init_trove_slice()

static void pgpa_init_trove_slice ( pgpa_trove_slice tslice)
static

Definition at line 433 of file pgpa_trove.c.

434{
435 /*
436 * In an ideal world, we'll make tslice->nallocated big enough that the
437 * array and hash table will be large enough to contain the number of
438 * advice items in this trove slice, but a generous default value is not
439 * good for performance, because pgpa_init_trove_slice() has to zero an
440 * amount of memory proportional to tslice->nallocated. Hence, we keep the
441 * starting value quite small, on the theory that advice strings will
442 * often be relatively short.
443 */
444 tslice->nallocated = 16;
445 tslice->nused = 0;
446 tslice->entries = palloc_array(pgpa_trove_entry, tslice->nallocated);
448 tslice->nallocated, NULL);
449}
#define palloc_array(type, count)
Definition fe_memutils.h:76
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
Definition pgpa_trove.h:27

References CurrentMemoryContext, fb(), and palloc_array.

Referenced by pgpa_build_trove().

◆ pgpa_trove_add_to_hash()

static void pgpa_trove_add_to_hash ( pgpa_trove_entry_hash hash,
pgpa_advice_target target,
int  index 
)
static

Definition at line 398 of file pgpa_trove.c.

400{
403 bool found;
404
405 /* For non-identifiers, add entries for all descendants. */
406 if (target->ttype != PGPA_TARGET_IDENTIFIER)
407 {
409 {
411 }
412 return;
413 }
414
415 /* Sanity checks. */
416 Assert(target->rid.occurrence > 0);
417 Assert(target->rid.alias_name != NULL);
418
419 /* Add an entry for this relation identifier. */
420 key.alias_name = target->rid.alias_name;
421 key.partition_name = target->rid.partrel;
422 key.plan_name = target->rid.plan_name;
423 element = pgpa_trove_entry_insert(hash, key, &found);
424 if (!found)
425 element->indexes = NULL;
426 element->indexes = bms_add_member(element->indexes, index);
427}
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
static void pgpa_trove_add_to_hash(pgpa_trove_entry_hash *hash, pgpa_advice_target *target, int index)
Definition pgpa_trove.c:398
static chr element(struct vars *v, const chr *startp, const chr *endp)
static unsigned hash(unsigned *uv, int n)
Definition rege_dfa.c:715
Definition type.h:96
pgpa_identifier rid
Definition pgpa_ast.h:58
const char * alias_name
const char * partrel
const char * plan_name
Definition pgpa_trove.c:82
Definition pgpa_trove.c:75

References pgpa_identifier::alias_name, Assert, bms_add_member(), pgpa_advice_target::children, element(), fb(), foreach_ptr, hash(), pgpa_identifier::occurrence, pgpa_identifier::partrel, PGPA_TARGET_IDENTIFIER, pgpa_trove_add_to_hash(), pgpa_identifier::plan_name, pgpa_advice_target::rid, and pgpa_advice_target::ttype.

Referenced by pgpa_trove_add_to_hash(), and pgpa_trove_add_to_slice().

◆ pgpa_trove_add_to_slice()

static void pgpa_trove_add_to_slice ( pgpa_trove_slice tslice,
pgpa_advice_tag_type  tag,
pgpa_advice_target target 
)
static

Definition at line 368 of file pgpa_trove.c.

371{
372 pgpa_trove_entry *entry;
373
374 if (tslice->nused >= tslice->nallocated)
375 {
376 int new_allocated;
377
378 new_allocated = tslice->nallocated * 2;
379 tslice->entries = repalloc_array(tslice->entries, pgpa_trove_entry,
381 tslice->nallocated = new_allocated;
382 }
383
384 entry = &tslice->entries[tslice->nused];
385 entry->tag = tag;
386 entry->target = target;
387 entry->flags = 0;
388
389 pgpa_trove_add_to_hash(tslice->hash, target, tslice->nused);
390
391 tslice->nused++;
392}
#define repalloc_array(pointer, type, count)
Definition fe_memutils.h:78
int flags
Definition pgpa_trove.h:30

References fb(), pgpa_trove_entry::flags, pgpa_trove_add_to_hash(), repalloc_array, pgpa_trove_entry::tag, and pgpa_trove_entry::target.

Referenced by pgpa_build_trove().

◆ pgpa_trove_append_flags()

void pgpa_trove_append_flags ( StringInfo  buf,
int  flags 
)

Definition at line 345 of file pgpa_trove.c.

346{
347 if ((flags & PGPA_FB_MATCH_FULL) != 0)
348 {
349 Assert((flags & PGPA_FB_MATCH_PARTIAL) != 0);
350 appendStringInfoString(buf, "matched");
351 }
352 else if ((flags & PGPA_FB_MATCH_PARTIAL) != 0)
353 appendStringInfoString(buf, "partially matched");
354 else
355 appendStringInfoString(buf, "not matched");
356 if ((flags & PGPA_FB_INAPPLICABLE) != 0)
357 appendStringInfoString(buf, ", inapplicable");
358 if ((flags & PGPA_FB_CONFLICTING) != 0)
359 appendStringInfoString(buf, ", conflicting");
360 if ((flags & PGPA_FB_FAILED) != 0)
361 appendStringInfoString(buf, ", failed");
362}
#define PGPA_FB_INAPPLICABLE
#define PGPA_FB_CONFLICTING
#define PGPA_FB_MATCH_FULL
#define PGPA_FB_FAILED
#define PGPA_FB_MATCH_PARTIAL

References appendStringInfoString(), Assert, buf, PGPA_FB_CONFLICTING, PGPA_FB_FAILED, PGPA_FB_INAPPLICABLE, PGPA_FB_MATCH_FULL, and PGPA_FB_MATCH_PARTIAL.

Referenced by pg_plan_advice_explain_feedback(), and pgpa_planner_feedback_warning().

◆ pgpa_trove_entry_compare_key()

static bool pgpa_trove_entry_compare_key ( pgpa_trove_entry_key  a,
pgpa_trove_entry_key  b 
)
inlinestatic

Definition at line 91 of file pgpa_trove.c.

92{
93 if (strcmp(a.alias_name, b.alias_name) != 0)
94 return false;
95
96 if (!strings_equal_or_both_null(a.partition_name, b.partition_name))
97 return false;
98
99 if (!strings_equal_or_both_null(a.plan_name, b.plan_name))
100 return false;
101
102 return true;
103}
int b
Definition isn.c:74
int a
Definition isn.c:73
static bool strings_equal_or_both_null(const char *a, const char *b)

References a, b, fb(), and strings_equal_or_both_null().

◆ pgpa_trove_entry_hash_key()

static uint32 pgpa_trove_entry_hash_key ( pgpa_trove_entry_key  key)
static

Definition at line 456 of file pgpa_trove.c.

457{
459 int sp_len;
460
461 fasthash_init(&hs, 0);
462
463 /* alias_name may not be NULL */
464 sp_len = fasthash_accum_cstring(&hs, key.alias_name);
465
466 /* partition_name and plan_name, however, can be NULL */
467 if (key.partition_name != NULL)
468 sp_len += fasthash_accum_cstring(&hs, key.partition_name);
469 if (key.plan_name != NULL)
470 sp_len += fasthash_accum_cstring(&hs, key.plan_name);
471
472 /*
473 * hashfn_unstable.h recommends using string length as tweak. It's not
474 * clear to me what to do if there are multiple strings, so for now I'm
475 * just using the total of all of the lengths.
476 */
477 return fasthash_final32(&hs, sp_len);
478}
static size_t fasthash_accum_cstring(fasthash_state *hs, const char *str)
static uint32 fasthash_final32(fasthash_state *hs, uint64 tweak)
static void fasthash_init(fasthash_state *hs, uint64 seed)

References fasthash_accum_cstring(), fasthash_final32(), fasthash_init(), and fb().

◆ pgpa_trove_lookup()

void pgpa_trove_lookup ( pgpa_trove trove,
pgpa_trove_lookup_type  type,
int  nrids,
pgpa_identifier rids,
pgpa_trove_result result 
)

Definition at line 235 of file pgpa_trove.c.

237{
239 Bitmapset *indexes;
240
241 Assert(nrids > 0);
242
244 tslice = &trove->scan;
245 else if (type == PGPA_TROVE_LOOKUP_JOIN)
246 tslice = &trove->join;
247 else
248 tslice = &trove->rel;
249
250 indexes = pgpa_trove_slice_lookup(tslice, &rids[0]);
251 for (int i = 1; i < nrids; ++i)
252 {
254
255 /*
256 * If the caller is asking about two relations that aren't part of the
257 * same subquery, they've messed up.
258 */
259 Assert(strings_equal_or_both_null(rids[0].plan_name,
260 rids[i].plan_name));
261
263 indexes = bms_union(indexes, other_indexes);
264 }
265
266 result->entries = tslice->entries;
267 result->indexes = indexes;
268}
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:251
uint32 result
int i
Definition isn.c:77
static Bitmapset * pgpa_trove_slice_lookup(pgpa_trove_slice *tslice, pgpa_identifier *rid)
Definition pgpa_trove.c:484
@ PGPA_TROVE_LOOKUP_SCAN
Definition pgpa_trove.h:55
@ PGPA_TROVE_LOOKUP_JOIN
Definition pgpa_trove.h:53
const char * type

References Assert, bms_union(), fb(), i, pgpa_trove::join, PGPA_TROVE_LOOKUP_JOIN, PGPA_TROVE_LOOKUP_SCAN, pgpa_trove_slice_lookup(), pgpa_trove::rel, result, pgpa_trove::scan, strings_equal_or_both_null(), and type.

Referenced by pgpa_build_simple_rel(), and pgpa_get_join_state().

◆ pgpa_trove_lookup_all()

void pgpa_trove_lookup_all ( pgpa_trove trove,
pgpa_trove_lookup_type  type,
pgpa_trove_entry **  entries,
int nentries 
)

Definition at line 277 of file pgpa_trove.c.

279{
281
283 tslice = &trove->scan;
284 else if (type == PGPA_TROVE_LOOKUP_JOIN)
285 tslice = &trove->join;
286 else
287 tslice = &trove->rel;
288
289 *entries = tslice->entries;
290 *nentries = tslice->nused;
291}

References fb(), pgpa_trove::join, PGPA_TROVE_LOOKUP_JOIN, PGPA_TROVE_LOOKUP_SCAN, pgpa_trove::rel, pgpa_trove::scan, and type.

Referenced by pgpa_planner_append_feedback().

◆ pgpa_trove_set_flags()

void pgpa_trove_set_flags ( pgpa_trove_entry entries,
Bitmapset indexes,
int  flags 
)

Definition at line 328 of file pgpa_trove.c.

329{
330 int i = -1;
331
332 while ((i = bms_next_member(indexes, i)) >= 0)
333 {
334 pgpa_trove_entry *entry = &entries[i];
335
336 entry->flags |= flags;
337 }
338}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290

References bms_next_member(), pgpa_trove_entry::flags, and i.

Referenced by pgpa_planner_apply_join_path_advice(), pgpa_planner_apply_joinrel_advice(), and pgpa_planner_apply_scan_advice().

◆ pgpa_trove_slice_lookup()

static Bitmapset * pgpa_trove_slice_lookup ( pgpa_trove_slice tslice,
pgpa_identifier rid 
)
static

Definition at line 484 of file pgpa_trove.c.

485{
489
490 Assert(rid->occurrence >= 1);
491
492 key.alias_name = rid->alias_name;
493 key.partition_name = rid->partrel;
494 key.plan_name = rid->plan_name;
495
497
498 if (element != NULL)
499 {
500 int i = -1;
501
502 while ((i = bms_next_member(element->indexes, i)) >= 0)
503 {
504 pgpa_trove_entry *entry = &tslice->entries[i];
505
506 /*
507 * We know that this target or one of its descendants matches the
508 * identifier on the three key fields above, but we don't know
509 * which descendant or whether the occurrence and schema also
510 * match.
511 */
512 if (pgpa_identifier_matches_target(rid, entry->target))
514 }
515 }
516
517 return result;
518}
bool pgpa_identifier_matches_target(pgpa_identifier *rid, pgpa_advice_target *target)
Definition pgpa_ast.c:235

References pgpa_identifier::alias_name, Assert, bms_add_member(), bms_next_member(), element(), fb(), i, pgpa_identifier::occurrence, pgpa_identifier::partrel, pgpa_identifier_matches_target(), pgpa_identifier::plan_name, result, and pgpa_trove_entry::target.

Referenced by pgpa_trove_lookup().