PostgreSQL Source Code git master
Loading...
Searching...
No Matches
pgpa_trove.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * pgpa_trove.c
4 * All of the advice given for a particular query, appropriately
5 * organized for convenient access.
6 *
7 * This name comes from the English expression "trove of advice", which
8 * means a collection of wisdom. This slightly unusual term is chosen
9 * partly because it seems to fit and partly because it's not presently
10 * used for anything else, making it easy to grep. Note that, while we
11 * don't know whether the provided advice is actually wise, it's not our
12 * job to question the user's choices.
13 *
14 * The goal of this module is to make it easy to locate the specific
15 * bits of advice that pertain to any given part of a query, or to
16 * determine that there are none.
17 *
18 * Copyright (c) 2016-2026, PostgreSQL Global Development Group
19 *
20 * contrib/pg_plan_advice/pgpa_trove.c
21 *
22 *-------------------------------------------------------------------------
23 */
24#include "postgres.h"
25
26#include "pg_plan_advice.h"
27#include "pgpa_trove.h"
28
30
31/*
32 * An advice trove is organized into a series of "slices", each of which
33 * contains information about one topic e.g. scan methods. Each slice consists
34 * of an array of trove entries plus a hash table that we can use to determine
35 * which ones are relevant to a particular part of the query.
36 */
44
45/*
46 * Scan advice is stored into 'scan'; join advice is stored into 'join'; and
47 * advice that can apply to both cases is stored into 'rel'. This lets callers
48 * ask just for what's relevant. These slices correspond to the possible values
49 * of pgpa_trove_lookup_type.
50 */
57
58/*
59 * We're going to build a hash table to allow clients of this module to find
60 * relevant advice for a given part of the query quickly. However, we're going
61 * to use only three of the five key fields as hash keys. There are two reasons
62 * for this.
63 *
64 * First, it's allowable to set partition_schema to NULL to match a partition
65 * with the correct name in any schema.
66 *
67 * Second, we expect the "occurrence" and "partition_schema" portions of the
68 * relation identifiers to be mostly uninteresting. Most of the time, the
69 * occurrence field will be 1 and the partition_schema values will all be the
70 * same. Even when there is some variation, the absolute number of entries
71 * that have the same values for all three of these key fields should be
72 * quite small.
73 */
74typedef struct
75{
76 const char *alias_name;
77 const char *partition_name;
78 const char *plan_name;
80
87
89
90static inline bool
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}
104
105#define SH_PREFIX pgpa_trove_entry
106#define SH_ELEMENT_TYPE pgpa_trove_entry_element
107#define SH_KEY_TYPE pgpa_trove_entry_key
108#define SH_KEY key
109#define SH_HASH_KEY(tb, key) pgpa_trove_entry_hash_key(key)
110#define SH_EQUAL(tb, a, b) pgpa_trove_entry_compare_key(a, b)
111#define SH_SCOPE static inline
112#define SH_DECLARE
113#define SH_DEFINE
114#include "lib/simplehash.h"
115
119 pgpa_advice_target *target);
121 pgpa_advice_target *target,
122 int index);
124 pgpa_identifier *rid);
125
126/*
127 * Build a trove of advice from a list of advice items.
128 *
129 * Caller can obtain a list of advice items to pass to this function by
130 * calling pgpa_parse().
131 */
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}
227
228/*
229 * Search a trove of advice for relevant entries.
230 *
231 * All parameters are input parameters except for *result, which is an output
232 * parameter used to return results to the caller.
233 */
234void
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}
269
270/*
271 * Return all entries in a trove slice to the caller.
272 *
273 * The first two arguments are input arguments, and the remainder are output
274 * arguments.
275 */
276void
278 pgpa_trove_entry **entries, int *nentries)
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}
292
293/*
294 * Convert a trove entry to an item of plan advice that would produce it.
295 */
296char *
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}
323
324/*
325 * Set PGPA_FB_* flags on a set of trove entries.
326 */
327void
328pgpa_trove_set_flags(pgpa_trove_entry *entries, Bitmapset *indexes, int flags)
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}
339
340/*
341 * Append a string representation of the specified PGPA_FB_* flags to the
342 * given StringInfo.
343 */
344void
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}
363
364/*
365 * Add a new advice target to an existing pgpa_trove_slice object.
366 */
367static void
370 pgpa_advice_target *target)
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}
393
394/*
395 * Update the hash table for a newly-added advice target.
396 */
397static void
399 int index)
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}
428
429/*
430 * Create and initialize a new pgpa_trove_slice object.
431 */
432static void
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}
450
451/*
452 * Fast hash function for a key consisting of alias_name, partition_name,
453 * and plan_name.
454 */
455static uint32
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}
479
480/*
481 * Look for matching entries.
482 */
483static Bitmapset *
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}
int bms_next_member(const Bitmapset *a, int prevbit)
Definition bitmapset.c:1290
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition bitmapset.c:799
Bitmapset * bms_union(const Bitmapset *a, const Bitmapset *b)
Definition bitmapset.c:251
#define Assert(condition)
Definition c.h:943
uint32_t uint32
Definition c.h:624
uint32 result
#define palloc_object(type)
Definition fe_memutils.h:74
#define repalloc_array(pointer, type, count)
Definition fe_memutils.h:78
#define palloc_array(type, count)
Definition fe_memutils.h:76
#define palloc0_object(type)
Definition fe_memutils.h:75
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)
int b
Definition isn.c:74
int a
Definition isn.c:73
int i
Definition isn.c:77
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
#define foreach_ptr(type, var, lst)
Definition pg_list.h:501
#define PGPA_FB_INAPPLICABLE
#define PGPA_FB_CONFLICTING
#define PGPA_FB_MATCH_FULL
#define PGPA_FB_FAILED
#define PGPA_FB_MATCH_PARTIAL
static char buf[DEFAULT_XLOG_SEG_SIZE]
char * pgpa_cstring_advice_tag(pgpa_advice_tag_type advice_tag)
Definition pgpa_ast.c:29
bool pgpa_identifier_matches_target(pgpa_identifier *rid, pgpa_advice_target *target)
Definition pgpa_ast.c:235
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
pgpa_advice_tag_type
Definition pgpa_ast.h:81
@ 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 bool strings_equal_or_both_null(const char *a, const char *b)
static bool pgpa_trove_entry_compare_key(pgpa_trove_entry_key a, pgpa_trove_entry_key b)
Definition pgpa_trove.c:91
static Bitmapset * pgpa_trove_slice_lookup(pgpa_trove_slice *tslice, pgpa_identifier *rid)
Definition pgpa_trove.c:484
void pgpa_trove_set_flags(pgpa_trove_entry *entries, Bitmapset *indexes, int flags)
Definition pgpa_trove.c:328
static void pgpa_init_trove_slice(pgpa_trove_slice *tslice)
Definition pgpa_trove.c:433
pgpa_trove * pgpa_build_trove(List *advice_items)
Definition pgpa_trove.c:133
void pgpa_trove_append_flags(StringInfo buf, int flags)
Definition pgpa_trove.c:345
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
void pgpa_trove_lookup_all(pgpa_trove *trove, pgpa_trove_lookup_type type, pgpa_trove_entry **entries, int *nentries)
Definition pgpa_trove.c:277
char * pgpa_cstring_trove_entry(pgpa_trove_entry *entry)
Definition pgpa_trove.c:297
static void pgpa_trove_add_to_hash(pgpa_trove_entry_hash *hash, pgpa_advice_target *target, int index)
Definition pgpa_trove.c:398
void pgpa_trove_lookup(pgpa_trove *trove, pgpa_trove_lookup_type type, int nrids, pgpa_identifier *rids, pgpa_trove_result *result)
Definition pgpa_trove.c:235
static uint32 pgpa_trove_entry_hash_key(pgpa_trove_entry_key key)
Definition pgpa_trove.c:456
pgpa_trove_lookup_type
Definition pgpa_trove.h:52
@ PGPA_TROVE_LOOKUP_SCAN
Definition pgpa_trove.h:55
@ PGPA_TROVE_LOOKUP_JOIN
Definition pgpa_trove.h:53
static int fb(int x)
static chr element(struct vars *v, const chr *startp, const chr *endp)
static unsigned hash(unsigned *uv, int n)
Definition rege_dfa.c:715
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
Definition pg_list.h:54
Definition type.h:96
pgpa_identifier rid
Definition pgpa_ast.h:58
pgpa_target_type ttype
Definition pgpa_ast.h:49
pgpa_index_target * itarget
Definition pgpa_ast.h:64
const char * alias_name
const char * partrel
const char * plan_name
Definition pgpa_trove.c:82
int status
Definition pgpa_trove.c:84
Bitmapset * indexes
Definition pgpa_trove.c:85
pgpa_trove_entry_key key
Definition pgpa_trove.c:83
Definition pgpa_trove.c:75
const char * partition_name
Definition pgpa_trove.c:77
const char * alias_name
Definition pgpa_trove.c:76
const char * plan_name
Definition pgpa_trove.c:78
Definition pgpa_trove.h:27
pgpa_advice_target * target
Definition pgpa_trove.h:29
pgpa_advice_tag_type tag
Definition pgpa_trove.h:28
int flags
Definition pgpa_trove.h:30
struct pgpa_trove_entry_hash * hash
Definition pgpa_trove.c:42
pgpa_trove_entry * entries
Definition pgpa_trove.c:41
unsigned nallocated
Definition pgpa_trove.c:39
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
const char * type