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 "pgpa_trove.h"
27
29
30/*
31 * An advice trove is organized into a series of "slices", each of which
32 * contains information about one topic e.g. scan methods. Each slice consists
33 * of an array of trove entries plus a hash table that we can use to determine
34 * which ones are relevant to a particular part of the query.
35 */
43
44/*
45 * Scan advice is stored into 'scan'; join advice is stored into 'join'; and
46 * advice that can apply to both cases is stored into 'rel'. This lets callers
47 * ask just for what's relevant. These slices correspond to the possible values
48 * of pgpa_trove_lookup_type.
49 */
56
57/*
58 * We're going to build a hash table to allow clients of this module to find
59 * relevant advice for a given part of the query quickly. However, we're going
60 * to use only three of the five key fields as hash keys. There are two reasons
61 * for this.
62 *
63 * First, it's allowable to set partition_schema to NULL to match a partition
64 * with the correct name in any schema.
65 *
66 * Second, we expect the "occurrence" and "partition_schema" portions of the
67 * relation identifiers to be mostly uninteresting. Most of the time, the
68 * occurrence field will be 1 and the partition_schema values will all be the
69 * same. Even when there is some variation, the absolute number of entries
70 * that have the same values for all three of these key fields should be
71 * quite small.
72 */
73typedef struct
74{
75 const char *alias_name;
76 const char *partition_name;
77 const char *plan_name;
79
86
88
89static inline bool
91{
92 if (strcmp(a.alias_name, b.alias_name) != 0)
93 return false;
94
95 if (!strings_equal_or_both_null(a.partition_name, b.partition_name))
96 return false;
97
98 if (!strings_equal_or_both_null(a.plan_name, b.plan_name))
99 return false;
100
101 return true;
102}
103
104#define SH_PREFIX pgpa_trove_entry
105#define SH_ELEMENT_TYPE pgpa_trove_entry_element
106#define SH_KEY_TYPE pgpa_trove_entry_key
107#define SH_KEY key
108#define SH_HASH_KEY(tb, key) pgpa_trove_entry_hash_key(key)
109#define SH_EQUAL(tb, a, b) pgpa_trove_entry_compare_key(a, b)
110#define SH_SCOPE static inline
111#define SH_DECLARE
112#define SH_DEFINE
113#include "lib/simplehash.h"
114
118 pgpa_advice_target *target);
120 pgpa_advice_target *target,
121 int index);
123 pgpa_identifier *rid);
124
125/*
126 * Build a trove of advice from a list of advice items.
127 *
128 * Caller can obtain a list of advice items to pass to this function by
129 * calling pgpa_parse().
130 */
133{
135
137 pgpa_init_trove_slice(&trove->rel);
139
141 {
142 switch (item->tag)
143 {
145 {
146 pgpa_advice_target *target;
147
148 /*
149 * For most advice types, each element in the top-level
150 * list is a separate target, but it's most convenient to
151 * regard the entirety of a JOIN_ORDER specification as a
152 * single target. Since it wasn't represented that way
153 * during parsing, build a surrogate object now.
154 */
157 target->children = item->targets;
158
160 item->tag, target);
161 }
162 break;
163
169
170 /*
171 * Scan advice.
172 */
173 foreach_ptr(pgpa_advice_target, target, item->targets)
174 {
175 /*
176 * For now, all of our scan types target single relations,
177 * but in the future this might not be true, e.g. a custom
178 * scan could replace a join.
179 */
180 Assert(target->ttype == PGPA_TARGET_IDENTIFIER);
182 item->tag, target);
183 }
184 break;
185
195
196 /*
197 * Join strategy advice.
198 */
199 foreach_ptr(pgpa_advice_target, target, item->targets)
200 {
202 item->tag, target);
203 }
204 break;
205
207 case PGPA_TAG_GATHER:
210
211 /*
212 * Advice about a RelOptInfo relevant to both scans and joins.
213 */
214 foreach_ptr(pgpa_advice_target, target, item->targets)
215 {
217 item->tag, target);
218 }
219 break;
220 }
221 }
222
223 return trove;
224}
225
226/*
227 * Search a trove of advice for relevant entries.
228 *
229 * All parameters are input parameters except for *result, which is an output
230 * parameter used to return results to the caller.
231 */
232void
234 int nrids, pgpa_identifier *rids, pgpa_trove_result *result)
235{
237 Bitmapset *indexes;
238
239 Assert(nrids > 0);
240
242 tslice = &trove->scan;
243 else if (type == PGPA_TROVE_LOOKUP_JOIN)
244 tslice = &trove->join;
245 else
246 tslice = &trove->rel;
247
248 indexes = pgpa_trove_slice_lookup(tslice, &rids[0]);
249 for (int i = 1; i < nrids; ++i)
250 {
252
253 /*
254 * If the caller is asking about two relations that aren't part of the
255 * same subquery, they've messed up.
256 */
257 Assert(strings_equal_or_both_null(rids[0].plan_name,
258 rids[i].plan_name));
259
261 indexes = bms_union(indexes, other_indexes);
262 }
263
264 result->entries = tslice->entries;
265 result->indexes = indexes;
266}
267
268/*
269 * Return all entries in a trove slice to the caller.
270 *
271 * The first two arguments are input arguments, and the remainder are output
272 * arguments.
273 */
274void
276 pgpa_trove_entry **entries, int *nentries)
277{
279
281 tslice = &trove->scan;
282 else if (type == PGPA_TROVE_LOOKUP_JOIN)
283 tslice = &trove->join;
284 else
285 tslice = &trove->rel;
286
287 *entries = tslice->entries;
288 *nentries = tslice->nused;
289}
290
291/*
292 * Convert a trove entry to an item of plan advice that would produce it.
293 */
294char *
296{
298
301
302 /* JOIN_ORDER tags are transformed by pgpa_build_trove; undo that here */
303 if (entry->tag != PGPA_TAG_JOIN_ORDER)
305 else
307
309
310 if (entry->target->itarget != NULL)
311 {
314 }
315
316 if (entry->tag != PGPA_TAG_JOIN_ORDER)
318
319 return buf.data;
320}
321
322/*
323 * Set PGPA_TE_* flags on a set of trove entries.
324 */
325void
326pgpa_trove_set_flags(pgpa_trove_entry *entries, Bitmapset *indexes, int flags)
327{
328 int i = -1;
329
330 while ((i = bms_next_member(indexes, i)) >= 0)
331 {
332 pgpa_trove_entry *entry = &entries[i];
333
334 entry->flags |= flags;
335 }
336}
337
338/*
339 * Append a string representation of the specified PGPA_TE_* flags to the
340 * given StringInfo.
341 */
342void
344{
345 if ((flags & PGPA_TE_MATCH_FULL) != 0)
346 {
347 Assert((flags & PGPA_TE_MATCH_PARTIAL) != 0);
348 appendStringInfo(buf, "matched");
349 }
350 else if ((flags & PGPA_TE_MATCH_PARTIAL) != 0)
351 appendStringInfo(buf, "partially matched");
352 else
353 appendStringInfo(buf, "not matched");
354 if ((flags & PGPA_TE_INAPPLICABLE) != 0)
355 appendStringInfo(buf, ", inapplicable");
356 if ((flags & PGPA_TE_CONFLICTING) != 0)
357 appendStringInfo(buf, ", conflicting");
358 if ((flags & PGPA_TE_FAILED) != 0)
359 appendStringInfo(buf, ", failed");
360}
361
362/*
363 * Add a new advice target to an existing pgpa_trove_slice object.
364 */
365static void
368 pgpa_advice_target *target)
369{
370 pgpa_trove_entry *entry;
371
372 if (tslice->nused >= tslice->nallocated)
373 {
374 int new_allocated;
375
376 new_allocated = tslice->nallocated * 2;
377 tslice->entries = repalloc_array(tslice->entries, pgpa_trove_entry,
379 tslice->nallocated = new_allocated;
380 }
381
382 entry = &tslice->entries[tslice->nused];
383 entry->tag = tag;
384 entry->target = target;
385 entry->flags = 0;
386
387 pgpa_trove_add_to_hash(tslice->hash, target, tslice->nused);
388
389 tslice->nused++;
390}
391
392/*
393 * Update the hash table for a newly-added advice target.
394 */
395static void
397 int index)
398{
401 bool found;
402
403 /* For non-identifiers, add entries for all descendants. */
404 if (target->ttype != PGPA_TARGET_IDENTIFIER)
405 {
407 {
409 }
410 return;
411 }
412
413 /* Sanity checks. */
414 Assert(target->rid.occurrence > 0);
415 Assert(target->rid.alias_name != NULL);
416
417 /* Add an entry for this relation identifier. */
418 key.alias_name = target->rid.alias_name;
419 key.partition_name = target->rid.partrel;
420 key.plan_name = target->rid.plan_name;
421 element = pgpa_trove_entry_insert(hash, key, &found);
422 if (!found)
423 element->indexes = NULL;
424 element->indexes = bms_add_member(element->indexes, index);
425}
426
427/*
428 * Create and initialize a new pgpa_trove_slice object.
429 */
430static void
432{
433 /*
434 * In an ideal world, we'll make tslice->nallocated big enough that the
435 * array and hash table will be large enough to contain the number of
436 * advice items in this trove slice, but a generous default value is not
437 * good for performance, because pgpa_init_trove_slice() has to zero an
438 * amount of memory proportional to tslice->nallocated. Hence, we keep the
439 * starting value quite small, on the theory that advice strings will
440 * often be relatively short.
441 */
442 tslice->nallocated = 16;
443 tslice->nused = 0;
444 tslice->entries = palloc_array(pgpa_trove_entry, tslice->nallocated);
446 tslice->nallocated, NULL);
447}
448
449/*
450 * Fast hash function for a key consisting of alias_name, partition_name,
451 * and plan_name.
452 */
453static uint32
455{
457 int sp_len;
458
459 fasthash_init(&hs, 0);
460
461 /* alias_name may not be NULL */
462 sp_len = fasthash_accum_cstring(&hs, key.alias_name);
463
464 /* partition_name and plan_name, however, can be NULL */
465 if (key.partition_name != NULL)
466 sp_len += fasthash_accum_cstring(&hs, key.partition_name);
467 if (key.plan_name != NULL)
468 sp_len += fasthash_accum_cstring(&hs, key.plan_name);
469
470 /*
471 * hashfn_unstable.h recommends using string length as tweak. It's not
472 * clear to me what to do if there are multiple strings, so for now I'm
473 * just using the total of all of the lengths.
474 */
475 return fasthash_final32(&hs, sp_len);
476}
477
478/*
479 * Look for matching entries.
480 */
481static Bitmapset *
483{
486 Bitmapset *result = NULL;
487
488 Assert(rid->occurrence >= 1);
489
490 key.alias_name = rid->alias_name;
491 key.partition_name = rid->partrel;
492 key.plan_name = rid->plan_name;
493
495
496 if (element != NULL)
497 {
498 int i = -1;
499
500 while ((i = bms_next_member(element->indexes, i)) >= 0)
501 {
502 pgpa_trove_entry *entry = &tslice->entries[i];
503
504 /*
505 * We know that this target or one of its descendants matches the
506 * identifier on the three key fields above, but we don't know
507 * which descendant or whether the occurrence and schema also
508 * match.
509 */
510 if (pgpa_identifier_matches_target(rid, entry->target))
511 result = bms_add_member(result, i);
512 }
513 }
514
515 return result;
516}
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:927
uint32_t uint32
Definition c.h:600
#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:469
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:229
void pgpa_format_advice_target(StringInfo str, pgpa_advice_target *target)
Definition pgpa_ast.c:164
void pgpa_format_index_target(StringInfo str, pgpa_index_target *itarget)
Definition pgpa_ast.c:200
pgpa_advice_tag_type
Definition pgpa_ast.h:81
@ PGPA_TAG_INDEX_SCAN
Definition pgpa_ast.h:88
@ PGPA_TAG_NESTED_LOOP_MATERIALIZE
Definition pgpa_ast.h:92
@ PGPA_TAG_MERGE_JOIN_PLAIN
Definition pgpa_ast.h:91
@ PGPA_TAG_GATHER_MERGE
Definition pgpa_ast.h:85
@ PGPA_TAG_GATHER
Definition pgpa_ast.h:84
@ PGPA_TAG_NESTED_LOOP_MEMOIZE
Definition pgpa_ast.h:93
@ PGPA_TAG_SEMIJOIN_NON_UNIQUE
Definition pgpa_ast.h:97
@ PGPA_TAG_BITMAP_HEAP_SCAN
Definition pgpa_ast.h:82
@ PGPA_TAG_PARTITIONWISE
Definition pgpa_ast.h:96
@ PGPA_TAG_NO_GATHER
Definition pgpa_ast.h:95
@ PGPA_TAG_INDEX_ONLY_SCAN
Definition pgpa_ast.h:87
@ PGPA_TAG_SEQ_SCAN
Definition pgpa_ast.h:99
@ PGPA_TAG_HASH_JOIN
Definition pgpa_ast.h:86
@ PGPA_TAG_SEMIJOIN_UNIQUE
Definition pgpa_ast.h:98
@ PGPA_TAG_JOIN_ORDER
Definition pgpa_ast.h:89
@ PGPA_TAG_TID_SCAN
Definition pgpa_ast.h:100
@ PGPA_TAG_FOREIGN_JOIN
Definition pgpa_ast.h:83
@ PGPA_TAG_NESTED_LOOP_PLAIN
Definition pgpa_ast.h:94
@ PGPA_TAG_MERGE_JOIN_MATERIALIZE
Definition pgpa_ast.h:90
@ 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:90
static Bitmapset * pgpa_trove_slice_lookup(pgpa_trove_slice *tslice, pgpa_identifier *rid)
Definition pgpa_trove.c:482
void pgpa_trove_set_flags(pgpa_trove_entry *entries, Bitmapset *indexes, int flags)
Definition pgpa_trove.c:326
static void pgpa_init_trove_slice(pgpa_trove_slice *tslice)
Definition pgpa_trove.c:431
pgpa_trove * pgpa_build_trove(List *advice_items)
Definition pgpa_trove.c:132
void pgpa_trove_append_flags(StringInfo buf, int flags)
Definition pgpa_trove.c:343
static void pgpa_trove_add_to_slice(pgpa_trove_slice *tslice, pgpa_advice_tag_type tag, pgpa_advice_target *target)
Definition pgpa_trove.c:366
void pgpa_trove_lookup_all(pgpa_trove *trove, pgpa_trove_lookup_type type, pgpa_trove_entry **entries, int *nentries)
Definition pgpa_trove.c:275
char * pgpa_cstring_trove_entry(pgpa_trove_entry *entry)
Definition pgpa_trove.c:295
static void pgpa_trove_add_to_hash(pgpa_trove_entry_hash *hash, pgpa_advice_target *target, int index)
Definition pgpa_trove.c:396
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:233
static uint32 pgpa_trove_entry_hash_key(pgpa_trove_entry_key key)
Definition pgpa_trove.c:454
pgpa_trove_lookup_type
Definition pgpa_trove.h:82
@ PGPA_TROVE_LOOKUP_SCAN
Definition pgpa_trove.h:85
@ PGPA_TROVE_LOOKUP_JOIN
Definition pgpa_trove.h:83
#define PGPA_TE_INAPPLICABLE
Definition pgpa_trove.h:48
#define PGPA_TE_MATCH_FULL
Definition pgpa_trove.h:47
#define PGPA_TE_MATCH_PARTIAL
Definition pgpa_trove.h:46
#define PGPA_TE_CONFLICTING
Definition pgpa_trove.h:49
#define PGPA_TE_FAILED
Definition pgpa_trove.h:50
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 appendStringInfo(StringInfo str, const char *fmt,...)
Definition stringinfo.c:145
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:81
int status
Definition pgpa_trove.c:83
Bitmapset * indexes
Definition pgpa_trove.c:84
pgpa_trove_entry_key key
Definition pgpa_trove.c:82
Definition pgpa_trove.c:74
const char * partition_name
Definition pgpa_trove.c:76
const char * alias_name
Definition pgpa_trove.c:75
const char * plan_name
Definition pgpa_trove.c:77
Definition pgpa_trove.h:57
pgpa_advice_target * target
Definition pgpa_trove.h:59
pgpa_advice_tag_type tag
Definition pgpa_trove.h:58
int flags
Definition pgpa_trove.h:60
pgpa_trove_entry * entries
Definition pgpa_trove.h:95
Bitmapset * indexes
Definition pgpa_trove.h:96
struct pgpa_trove_entry_hash * hash
Definition pgpa_trove.c:41
pgpa_trove_entry * entries
Definition pgpa_trove.c:40
unsigned nallocated
Definition pgpa_trove.c:38
pgpa_trove_slice rel
Definition pgpa_trove.c:53
pgpa_trove_slice join
Definition pgpa_trove.c:52
pgpa_trove_slice scan
Definition pgpa_trove.c:54
const char * type