PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ginscan.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * ginscan.c
4 * routines to manage scans of inverted index relations
5 *
6 *
7 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
9 *
10 * IDENTIFICATION
11 * src/backend/access/gin/ginscan.c
12 *-------------------------------------------------------------------------
13 */
14
15#include "postgres.h"
16
17#include "access/gin_private.h"
18#include "access/relscan.h"
19#include "pgstat.h"
20#include "utils/memutils.h"
21#include "utils/rel.h"
22
23
25ginbeginscan(Relation rel, int nkeys, int norderbys)
26{
27 IndexScanDesc scan;
29
30 /* no order by operators allowed */
31 Assert(norderbys == 0);
32
33 scan = RelationGetIndexScan(rel, nkeys, norderbys);
34
35 /* allocate private workspace */
37 so->keys = NULL;
38 so->nkeys = 0;
40 "Gin scan temporary context",
43 "Gin scan key context",
46
47 scan->opaque = so;
48
49 return scan;
50}
51
52/*
53 * Create a new GinScanEntry, unless an equivalent one already exists,
54 * in which case just return it
55 */
56static GinScanEntry
58 StrategyNumber strategy, int32 searchMode,
59 Datum queryKey, GinNullCategory queryCategory,
60 bool isPartialMatch, Pointer extra_data)
61{
62 GinState *ginstate = &so->ginstate;
63 GinScanEntry scanEntry;
64 uint32 i;
65
66 /*
67 * Look for an existing equivalent entry.
68 *
69 * Entries with non-null extra_data are never considered identical, since
70 * we can't know exactly what the opclass might be doing with that.
71 *
72 * Also, give up de-duplication once we have 100 entries. That avoids
73 * spending O(N^2) time on probably-fruitless de-duplication of large
74 * search-key sets. The threshold of 100 is arbitrary but matches
75 * predtest.c's threshold for what's a large array.
76 */
77 if (extra_data == NULL && so->totalentries < 100)
78 {
79 for (i = 0; i < so->totalentries; i++)
80 {
81 GinScanEntry prevEntry = so->entries[i];
82
83 if (prevEntry->extra_data == NULL &&
84 prevEntry->isPartialMatch == isPartialMatch &&
85 prevEntry->strategy == strategy &&
86 prevEntry->searchMode == searchMode &&
87 prevEntry->attnum == attnum &&
88 ginCompareEntries(ginstate, attnum,
89 prevEntry->queryKey,
90 prevEntry->queryCategory,
91 queryKey,
92 queryCategory) == 0)
93 {
94 /* Successful match */
95 return prevEntry;
96 }
97 }
98 }
99
100 /* Nope, create a new entry */
101 scanEntry = (GinScanEntry) palloc(sizeof(GinScanEntryData));
102 scanEntry->queryKey = queryKey;
103 scanEntry->queryCategory = queryCategory;
104 scanEntry->isPartialMatch = isPartialMatch;
105 scanEntry->extra_data = extra_data;
106 scanEntry->strategy = strategy;
107 scanEntry->searchMode = searchMode;
108 scanEntry->attnum = attnum;
109
110 scanEntry->buffer = InvalidBuffer;
111 ItemPointerSetMin(&scanEntry->curItem);
112 scanEntry->matchBitmap = NULL;
113 scanEntry->matchIterator = NULL;
115 scanEntry->matchNtuples = -1;
116 scanEntry->list = NULL;
117 scanEntry->nlist = 0;
118 scanEntry->offset = InvalidOffsetNumber;
119 scanEntry->isFinished = false;
120 scanEntry->reduceResult = false;
121
122 /* Add it to so's array */
123 if (so->totalentries >= so->allocentries)
124 {
125 so->allocentries *= 2;
126 so->entries = (GinScanEntry *)
127 repalloc(so->entries, so->allocentries * sizeof(GinScanEntry));
128 }
129 so->entries[so->totalentries++] = scanEntry;
130
131 return scanEntry;
132}
133
134/*
135 * Append hidden scan entry of given category to the scan key.
136 *
137 * NB: this had better be called at most once per scan key, since
138 * ginFillScanKey leaves room for only one hidden entry. Currently,
139 * it seems sufficiently clear that this is true that we don't bother
140 * with any cross-check logic.
141 */
142static void
144 GinNullCategory queryCategory)
145{
146 int i = key->nentries++;
147
148 /* strategy is of no interest because this is not a partial-match item */
149 key->scanEntry[i] = ginFillScanEntry(so, key->attnum,
150 InvalidStrategy, key->searchMode,
151 (Datum) 0, queryCategory,
152 false, NULL);
153}
154
155/*
156 * Initialize the next GinScanKey using the output from the extractQueryFn
157 */
158static void
160 StrategyNumber strategy, int32 searchMode,
161 Datum query, uint32 nQueryValues,
162 Datum *queryValues, GinNullCategory *queryCategories,
163 bool *partial_matches, Pointer *extra_data)
164{
165 GinScanKey key = &(so->keys[so->nkeys++]);
166 GinState *ginstate = &so->ginstate;
167 uint32 i;
168
169 key->nentries = nQueryValues;
170 key->nuserentries = nQueryValues;
171
172 /* Allocate one extra array slot for possible "hidden" entry */
173 key->scanEntry = (GinScanEntry *) palloc(sizeof(GinScanEntry) *
174 (nQueryValues + 1));
175 key->entryRes = (GinTernaryValue *) palloc0(sizeof(GinTernaryValue) *
176 (nQueryValues + 1));
177
178 key->query = query;
179 key->queryValues = queryValues;
180 key->queryCategories = queryCategories;
181 key->extra_data = extra_data;
182 key->strategy = strategy;
183 key->searchMode = searchMode;
184 key->attnum = attnum;
185
186 /*
187 * Initially, scan keys of GIN_SEARCH_MODE_ALL mode are marked
188 * excludeOnly. This might get changed later.
189 */
190 key->excludeOnly = (searchMode == GIN_SEARCH_MODE_ALL);
191
192 ItemPointerSetMin(&key->curItem);
193 key->curItemMatches = false;
194 key->recheckCurItem = false;
195 key->isFinished = false;
196 key->nrequired = 0;
197 key->nadditional = 0;
198 key->requiredEntries = NULL;
199 key->additionalEntries = NULL;
200
202
203 /* Set up normal scan entries using extractQueryFn's outputs */
204 for (i = 0; i < nQueryValues; i++)
205 {
206 Datum queryKey;
207 GinNullCategory queryCategory;
208 bool isPartialMatch;
209 Pointer this_extra;
210
211 queryKey = queryValues[i];
212 queryCategory = queryCategories[i];
213 isPartialMatch =
214 (ginstate->canPartialMatch[attnum - 1] && partial_matches)
215 ? partial_matches[i] : false;
216 this_extra = (extra_data) ? extra_data[i] : NULL;
217
218 key->scanEntry[i] = ginFillScanEntry(so, attnum,
219 strategy, searchMode,
220 queryKey, queryCategory,
221 isPartialMatch, this_extra);
222 }
223
224 /*
225 * For GIN_SEARCH_MODE_INCLUDE_EMPTY and GIN_SEARCH_MODE_EVERYTHING search
226 * modes, we add the "hidden" entry immediately. GIN_SEARCH_MODE_ALL is
227 * handled later, since we might be able to omit the hidden entry for it.
228 */
229 if (searchMode == GIN_SEARCH_MODE_INCLUDE_EMPTY)
231 else if (searchMode == GIN_SEARCH_MODE_EVERYTHING)
233}
234
235/*
236 * Release current scan keys, if any.
237 */
238void
240{
241 uint32 i;
242
243 if (so->keys == NULL)
244 return;
245
246 for (i = 0; i < so->totalentries; i++)
247 {
248 GinScanEntry entry = so->entries[i];
249
250 if (entry->buffer != InvalidBuffer)
251 ReleaseBuffer(entry->buffer);
252 if (entry->list)
253 pfree(entry->list);
254 if (entry->matchIterator)
256 if (entry->matchBitmap)
257 tbm_free(entry->matchBitmap);
258 }
259
261
262 so->keys = NULL;
263 so->nkeys = 0;
264 so->entries = NULL;
265 so->totalentries = 0;
266}
267
268void
270{
271 ScanKey scankey = scan->keyData;
273 int i;
274 bool hasNullQuery = false;
275 bool attrHasNormalScan[INDEX_MAX_KEYS] = {false};
276 MemoryContext oldCtx;
277
278 /*
279 * Allocate all the scan key information in the key context. (If
280 * extractQuery leaks anything there, it won't be reset until the end of
281 * scan or rescan, but that's OK.)
282 */
283 oldCtx = MemoryContextSwitchTo(so->keyCtx);
284
285 /* if no scan keys provided, allocate extra EVERYTHING GinScanKey */
286 so->keys = (GinScanKey)
287 palloc(Max(scan->numberOfKeys, 1) * sizeof(GinScanKeyData));
288 so->nkeys = 0;
289
290 /* initialize expansible array of GinScanEntry pointers */
291 so->totalentries = 0;
292 so->allocentries = 32;
293 so->entries = (GinScanEntry *)
294 palloc(so->allocentries * sizeof(GinScanEntry));
295
296 so->isVoidRes = false;
297
298 for (i = 0; i < scan->numberOfKeys; i++)
299 {
300 ScanKey skey = &scankey[i];
301 Datum *queryValues;
302 int32 nQueryValues = 0;
303 bool *partial_matches = NULL;
304 Pointer *extra_data = NULL;
305 bool *nullFlags = NULL;
306 GinNullCategory *categories;
307 int32 searchMode = GIN_SEARCH_MODE_DEFAULT;
308
309 /*
310 * We assume that GIN-indexable operators are strict, so a null query
311 * argument means an unsatisfiable query.
312 */
313 if (skey->sk_flags & SK_ISNULL)
314 {
315 so->isVoidRes = true;
316 break;
317 }
318
319 /* OK to call the extractQueryFn */
320 queryValues = (Datum *)
321 DatumGetPointer(FunctionCall7Coll(&so->ginstate.extractQueryFn[skey->sk_attno - 1],
322 so->ginstate.supportCollation[skey->sk_attno - 1],
323 skey->sk_argument,
324 PointerGetDatum(&nQueryValues),
326 PointerGetDatum(&partial_matches),
327 PointerGetDatum(&extra_data),
328 PointerGetDatum(&nullFlags),
329 PointerGetDatum(&searchMode)));
330
331 /*
332 * If bogus searchMode is returned, treat as GIN_SEARCH_MODE_ALL; note
333 * in particular we don't allow extractQueryFn to select
334 * GIN_SEARCH_MODE_EVERYTHING.
335 */
336 if (searchMode < GIN_SEARCH_MODE_DEFAULT ||
337 searchMode > GIN_SEARCH_MODE_ALL)
338 searchMode = GIN_SEARCH_MODE_ALL;
339
340 /* Non-default modes require the index to have placeholders */
341 if (searchMode != GIN_SEARCH_MODE_DEFAULT)
342 hasNullQuery = true;
343
344 /*
345 * In default mode, no keys means an unsatisfiable query.
346 */
347 if (queryValues == NULL || nQueryValues <= 0)
348 {
349 if (searchMode == GIN_SEARCH_MODE_DEFAULT)
350 {
351 so->isVoidRes = true;
352 break;
353 }
354 nQueryValues = 0; /* ensure sane value */
355 }
356
357 /*
358 * Create GinNullCategory representation. If the extractQueryFn
359 * didn't create a nullFlags array, we assume everything is non-null.
360 * While at it, detect whether any null keys are present.
361 */
362 categories = (GinNullCategory *) palloc0(nQueryValues * sizeof(GinNullCategory));
363 if (nullFlags)
364 {
365 int32 j;
366
367 for (j = 0; j < nQueryValues; j++)
368 {
369 if (nullFlags[j])
370 {
371 categories[j] = GIN_CAT_NULL_KEY;
372 hasNullQuery = true;
373 }
374 }
375 }
376
377 ginFillScanKey(so, skey->sk_attno,
378 skey->sk_strategy, searchMode,
379 skey->sk_argument, nQueryValues,
380 queryValues, categories,
381 partial_matches, extra_data);
382
383 /* Remember if we had any non-excludeOnly keys */
384 if (searchMode != GIN_SEARCH_MODE_ALL)
385 attrHasNormalScan[skey->sk_attno - 1] = true;
386 }
387
388 /*
389 * Processing GIN_SEARCH_MODE_ALL scan keys requires us to make a second
390 * pass over the scan keys. Above we marked each such scan key as
391 * excludeOnly. If the involved column has any normal (not excludeOnly)
392 * scan key as well, then we can leave it like that. Otherwise, one
393 * excludeOnly scan key must receive a GIN_CAT_EMPTY_QUERY hidden entry
394 * and be set to normal (excludeOnly = false).
395 */
396 for (i = 0; i < so->nkeys; i++)
397 {
398 GinScanKey key = &so->keys[i];
399
400 if (key->searchMode != GIN_SEARCH_MODE_ALL)
401 continue;
402
403 if (!attrHasNormalScan[key->attnum - 1])
404 {
405 key->excludeOnly = false;
407 attrHasNormalScan[key->attnum - 1] = true;
408 }
409 }
410
411 /*
412 * If there are no regular scan keys, generate an EVERYTHING scankey to
413 * drive a full-index scan.
414 */
415 if (so->nkeys == 0 && !so->isVoidRes)
416 {
417 hasNullQuery = true;
420 (Datum) 0, 0,
421 NULL, NULL, NULL, NULL);
422 }
423
424 /*
425 * If the index is version 0, it may be missing null and placeholder
426 * entries, which would render searches for nulls and full-index scans
427 * unreliable. Throw an error if so.
428 */
429 if (hasNullQuery && !so->isVoidRes)
430 {
431 GinStatsData ginStats;
432
433 ginGetStats(scan->indexRelation, &ginStats);
434 if (ginStats.ginVersion < 1)
436 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
437 errmsg("old GIN indexes do not support whole-index scans nor searches for nulls"),
438 errhint("To fix this, do REINDEX INDEX \"%s\".",
439 RelationGetRelationName(scan->indexRelation))));
440 }
441
442 MemoryContextSwitchTo(oldCtx);
443
444 pgstat_count_index_scan(scan->indexRelation);
445 if (scan->instrument)
446 scan->instrument->nsearches++;
447}
448
449void
450ginrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys,
451 ScanKey orderbys, int norderbys)
452{
454
455 ginFreeScanKeys(so);
456
457 if (scankey && scan->numberOfKeys > 0)
458 memcpy(scan->keyData, scankey, scan->numberOfKeys * sizeof(ScanKeyData));
459}
460
461
462void
464{
466
467 ginFreeScanKeys(so);
468
471
472 pfree(so);
473}
#define InvalidBlockNumber
Definition: block.h:33
#define InvalidBuffer
Definition: buf.h:25
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:5373
#define Max(x, y)
Definition: c.h:969
char * Pointer
Definition: c.h:493
int32_t int32
Definition: c.h:498
uint32_t uint32
Definition: c.h:502
int errhint(const char *fmt,...)
Definition: elog.c:1318
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
Datum FunctionCall7Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2, Datum arg3, Datum arg4, Datum arg5, Datum arg6, Datum arg7)
Definition: fmgr.c:1284
IndexScanDesc RelationGetIndexScan(Relation indexRelation, int nkeys, int norderbys)
Definition: genam.c:80
#define GIN_SEARCH_MODE_ALL
Definition: gin.h:38
#define GIN_SEARCH_MODE_EVERYTHING
Definition: gin.h:39
#define GIN_SEARCH_MODE_DEFAULT
Definition: gin.h:36
char GinTernaryValue
Definition: gin.h:71
#define GIN_SEARCH_MODE_INCLUDE_EMPTY
Definition: gin.h:37
struct GinScanKeyData GinScanKeyData
GinScanOpaqueData * GinScanOpaque
Definition: gin_private.h:394
struct GinScanEntryData * GinScanEntry
Definition: gin_private.h:267
struct GinScanKeyData * GinScanKey
Definition: gin_private.h:265
#define GIN_CAT_EMPTY_ITEM
Definition: ginblock.h:210
signed char GinNullCategory
Definition: ginblock.h:206
#define GIN_CAT_NULL_KEY
Definition: ginblock.h:209
#define ItemPointerSetMin(p)
Definition: ginblock.h:166
#define GIN_CAT_EMPTY_QUERY
Definition: ginblock.h:212
void ginInitConsistentFunction(GinState *ginstate, GinScanKey key)
Definition: ginlogic.c:227
IndexScanDesc ginbeginscan(Relation rel, int nkeys, int norderbys)
Definition: ginscan.c:25
void ginFreeScanKeys(GinScanOpaque so)
Definition: ginscan.c:239
static void ginFillScanKey(GinScanOpaque so, OffsetNumber attnum, StrategyNumber strategy, int32 searchMode, Datum query, uint32 nQueryValues, Datum *queryValues, GinNullCategory *queryCategories, bool *partial_matches, Pointer *extra_data)
Definition: ginscan.c:159
void ginendscan(IndexScanDesc scan)
Definition: ginscan.c:463
void ginNewScanKey(IndexScanDesc scan)
Definition: ginscan.c:269
void ginrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, ScanKey orderbys, int norderbys)
Definition: ginscan.c:450
static void ginScanKeyAddHiddenEntry(GinScanOpaque so, GinScanKey key, GinNullCategory queryCategory)
Definition: ginscan.c:143
static GinScanEntry ginFillScanEntry(GinScanOpaque so, OffsetNumber attnum, StrategyNumber strategy, int32 searchMode, Datum queryKey, GinNullCategory queryCategory, bool isPartialMatch, Pointer extra_data)
Definition: ginscan.c:57
void ginGetStats(Relation index, GinStatsData *stats)
Definition: ginutil.c:628
void initGinState(GinState *state, Relation index)
Definition: ginutil.c:102
int ginCompareEntries(GinState *ginstate, OffsetNumber attnum, Datum a, GinNullCategory categorya, Datum b, GinNullCategory categoryb)
Definition: ginutil.c:393
Assert(PointerIsAligned(start, uint64))
int j
Definition: isn.c:78
int i
Definition: isn.c:77
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:414
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:2166
void pfree(void *pointer)
Definition: mcxt.c:2146
void * palloc0(Size size)
Definition: mcxt.c:1969
void * palloc(Size size)
Definition: mcxt.c:1939
MemoryContext CurrentMemoryContext
Definition: mcxt.c:159
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:485
#define AllocSetContextCreate
Definition: memutils.h:149
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:180
#define InvalidOffsetNumber
Definition: off.h:26
uint16 OffsetNumber
Definition: off.h:24
#define FirstOffsetNumber
Definition: off.h:27
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
int16 attnum
Definition: pg_attribute.h:74
#define INDEX_MAX_KEYS
#define pgstat_count_index_scan(rel)
Definition: pgstat.h:694
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:327
uintptr_t Datum
Definition: postgres.h:69
static Datum UInt16GetDatum(uint16 X)
Definition: postgres.h:197
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:317
#define RelationGetRelationName(relation)
Definition: rel.h:550
#define SK_ISNULL
Definition: skey.h:115
uint16 StrategyNumber
Definition: stratnum.h:22
#define InvalidStrategy
Definition: stratnum.h:24
ItemPointerData curItem
Definition: gin_private.h:352
TBMIterateResult matchResult
Definition: gin_private.h:362
TIDBitmap * matchBitmap
Definition: gin_private.h:355
ItemPointerData * list
Definition: gin_private.h:367
TBMPrivateIterator * matchIterator
Definition: gin_private.h:356
GinNullCategory queryCategory
Definition: gin_private.h:341
StrategyNumber strategy
Definition: gin_private.h:344
Pointer extra_data
Definition: gin_private.h:343
OffsetNumber offset
Definition: gin_private.h:369
OffsetNumber attnum
Definition: gin_private.h:346
GinScanKey keys
Definition: gin_private.h:382
MemoryContext keyCtx
Definition: gin_private.h:389
GinScanEntry * entries
Definition: gin_private.h:385
MemoryContext tempCtx
Definition: gin_private.h:379
bool canPartialMatch[INDEX_MAX_KEYS]
Definition: gin_private.h:86
int32 ginVersion
Definition: gin.h:62
struct ScanKeyData * keyData
Definition: relscan.h:141
Relation indexRelation
Definition: relscan.h:137
int sk_flags
Definition: skey.h:66
Datum sk_argument
Definition: skey.h:72
StrategyNumber sk_strategy
Definition: skey.h:68
AttrNumber sk_attno
Definition: skey.h:67
BlockNumber blockno
Definition: tidbitmap.h:64
void tbm_free(TIDBitmap *tbm)
Definition: tidbitmap.c:311
void tbm_end_private_iterate(TBMPrivateIterator *iterator)
Definition: tidbitmap.c:1150