1 /*-------------------------------------------------------------------------
4 * utilities routines for the postgres inverted index access method.
7 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/access/gin/ginutil.c
12 *-------------------------------------------------------------------------
17 #include "access/gin_private.h"
18 #include "access/reloptions.h"
19 #include "catalog/pg_collation.h"
20 #include "catalog/pg_type.h"
21 #include "miscadmin.h"
22 #include "storage/indexfsm.h"
23 #include "storage/lmgr.h"
27 * initGinState: fill in an empty GinState struct to describe the index
29 * Note: assorted subsidiary data is allocated in the CurrentMemoryContext.
32 initGinState(GinState *state, Relation index)
34 TupleDesc origTupdesc = RelationGetDescr(index);
37 MemSet(state, 0, sizeof(GinState));
40 state->oneCol = (origTupdesc->natts == 1) ? true : false;
41 state->origTupdesc = origTupdesc;
43 for (i = 0; i < origTupdesc->natts; i++)
46 state->tupdesc[i] = state->origTupdesc;
49 state->tupdesc[i] = CreateTemplateTupleDesc(2, false);
51 TupleDescInitEntry(state->tupdesc[i], (AttrNumber) 1, NULL,
53 TupleDescInitEntry(state->tupdesc[i], (AttrNumber) 2, NULL,
54 origTupdesc->attrs[i]->atttypid,
55 origTupdesc->attrs[i]->atttypmod,
56 origTupdesc->attrs[i]->attndims);
57 TupleDescInitEntryCollation(state->tupdesc[i], (AttrNumber) 2,
58 origTupdesc->attrs[i]->attcollation);
61 fmgr_info_copy(&(state->compareFn[i]),
62 index_getprocinfo(index, i + 1, GIN_COMPARE_PROC),
63 CurrentMemoryContext);
64 fmgr_info_copy(&(state->extractValueFn[i]),
65 index_getprocinfo(index, i + 1, GIN_EXTRACTVALUE_PROC),
66 CurrentMemoryContext);
67 fmgr_info_copy(&(state->extractQueryFn[i]),
68 index_getprocinfo(index, i + 1, GIN_EXTRACTQUERY_PROC),
69 CurrentMemoryContext);
70 fmgr_info_copy(&(state->consistentFn[i]),
71 index_getprocinfo(index, i + 1, GIN_CONSISTENT_PROC),
72 CurrentMemoryContext);
75 * Check opclass capability to do partial match.
77 if (index_getprocid(index, i + 1, GIN_COMPARE_PARTIAL_PROC) != InvalidOid)
79 fmgr_info_copy(&(state->comparePartialFn[i]),
80 index_getprocinfo(index, i + 1, GIN_COMPARE_PARTIAL_PROC),
81 CurrentMemoryContext);
82 state->canPartialMatch[i] = true;
86 state->canPartialMatch[i] = false;
90 * If the index column has a specified collation, we should honor that
91 * while doing comparisons. However, we may have a collatable storage
92 * type for a noncollatable indexed data type (for instance, hstore
93 * uses text index entries). If there's no index collation then
94 * specify default collation in case the support functions need
95 * collation. This is harmless if the support functions don't care
96 * about collation, so we just do it unconditionally. (We could
97 * alternatively call get_typcollation, but that seems like expensive
98 * overkill --- there aren't going to be any cases where a GIN storage
99 * type has a nondefault collation.)
101 if (OidIsValid(index->rd_indcollation[i]))
102 state->supportCollation[i] = index->rd_indcollation[i];
104 state->supportCollation[i] = DEFAULT_COLLATION_OID;
109 * Extract attribute (column) number of stored entry from GIN tuple
112 gintuple_get_attrnum(GinState *ginstate, IndexTuple tuple)
116 if (ginstate->oneCol)
118 /* column number is not stored explicitly */
119 colN = FirstOffsetNumber;
127 * First attribute is always int16, so we can safely use any tuple
128 * descriptor to obtain first attribute of tuple
130 res = index_getattr(tuple, FirstOffsetNumber, ginstate->tupdesc[0],
134 colN = DatumGetUInt16(res);
135 Assert(colN >= FirstOffsetNumber && colN <= ginstate->origTupdesc->natts);
142 * Extract stored datum (and possible null category) from GIN tuple
145 gintuple_get_key(GinState *ginstate, IndexTuple tuple,
146 GinNullCategory *category)
151 if (ginstate->oneCol)
154 * Single column index doesn't store attribute numbers in tuples
156 res = index_getattr(tuple, FirstOffsetNumber, ginstate->origTupdesc,
162 * Since the datum type depends on which index column it's from, we
163 * must be careful to use the right tuple descriptor here.
165 OffsetNumber colN = gintuple_get_attrnum(ginstate, tuple);
167 res = index_getattr(tuple, OffsetNumberNext(FirstOffsetNumber),
168 ginstate->tupdesc[colN - 1],
173 *category = GinGetNullCategory(tuple, ginstate);
175 *category = GIN_CAT_NORM_KEY;
181 * Allocate a new page (either by recycling, or by extending the index file)
182 * The returned buffer is already pinned and exclusive-locked
183 * Caller is responsible for initializing the page by calling GinInitBuffer
186 GinNewBuffer(Relation index)
191 /* First, try to get a page from FSM */
194 BlockNumber blkno = GetFreeIndexPage(index);
196 if (blkno == InvalidBlockNumber)
199 buffer = ReadBuffer(index, blkno);
202 * We have to guard against the possibility that someone else already
203 * recycled this page; the buffer may be locked if so.
205 if (ConditionalLockBuffer(buffer))
207 Page page = BufferGetPage(buffer);
210 return buffer; /* OK to use, if never initialized */
212 if (GinPageIsDeleted(page))
213 return buffer; /* OK to use */
215 LockBuffer(buffer, GIN_UNLOCK);
218 /* Can't use it, so release buffer and try again */
219 ReleaseBuffer(buffer);
222 /* Must extend the file */
223 needLock = !RELATION_IS_LOCAL(index);
225 LockRelationForExtension(index, ExclusiveLock);
227 buffer = ReadBuffer(index, P_NEW);
228 LockBuffer(buffer, GIN_EXCLUSIVE);
231 UnlockRelationForExtension(index, ExclusiveLock);
237 GinInitPage(Page page, uint32 f, Size pageSize)
239 GinPageOpaque opaque;
241 PageInit(page, pageSize, sizeof(GinPageOpaqueData));
243 opaque = GinPageGetOpaque(page);
244 memset(opaque, 0, sizeof(GinPageOpaqueData));
246 opaque->rightlink = InvalidBlockNumber;
250 GinInitBuffer(Buffer b, uint32 f)
252 GinInitPage(BufferGetPage(b), f, BufferGetPageSize(b));
256 GinInitMetabuffer(Buffer b)
258 GinMetaPageData *metadata;
259 Page page = BufferGetPage(b);
261 GinInitPage(page, GIN_META, BufferGetPageSize(b));
263 metadata = GinPageGetMeta(page);
265 metadata->head = metadata->tail = InvalidBlockNumber;
266 metadata->tailFreeSize = 0;
267 metadata->nPendingPages = 0;
268 metadata->nPendingHeapTuples = 0;
269 metadata->nTotalPages = 0;
270 metadata->nEntryPages = 0;
271 metadata->nDataPages = 0;
272 metadata->nEntries = 0;
273 metadata->ginVersion = GIN_CURRENT_VERSION;
277 * Compare two keys of the same index column
280 ginCompareEntries(GinState *ginstate, OffsetNumber attnum,
281 Datum a, GinNullCategory categorya,
282 Datum b, GinNullCategory categoryb)
284 /* if not of same null category, sort by that first */
285 if (categorya != categoryb)
286 return (categorya < categoryb) ? -1 : 1;
288 /* all null items in same category are equal */
289 if (categorya != GIN_CAT_NORM_KEY)
292 /* both not null, so safe to call the compareFn */
293 return DatumGetInt32(FunctionCall2Coll(&ginstate->compareFn[attnum - 1],
294 ginstate->supportCollation[attnum - 1],
299 * Compare two keys of possibly different index columns
302 ginCompareAttEntries(GinState *ginstate,
303 OffsetNumber attnuma, Datum a, GinNullCategory categorya,
304 OffsetNumber attnumb, Datum b, GinNullCategory categoryb)
306 /* attribute number is the first sort key */
307 if (attnuma != attnumb)
308 return (attnuma < attnumb) ? -1 : 1;
310 return ginCompareEntries(ginstate, attnuma, a, categorya, b, categoryb);
315 * Support for sorting key datums in ginExtractEntries
317 * Note: we only have to worry about null and not-null keys here;
318 * ginExtractEntries never generates more than one placeholder null,
319 * so it doesn't have to sort those.
329 FmgrInfo *cmpDatumFunc;
335 cmpEntries(const void *a, const void *b, void *arg)
337 const keyEntryData *aa = (const keyEntryData *) a;
338 const keyEntryData *bb = (const keyEntryData *) b;
339 cmpEntriesArg *data = (cmpEntriesArg *) arg;
345 res = 0; /* NULL "=" NULL */
347 res = 1; /* NULL ">" not-NULL */
350 res = -1; /* not-NULL "<" NULL */
352 res = DatumGetInt32(FunctionCall2Coll(data->cmpDatumFunc,
354 aa->datum, bb->datum));
357 * Detect if we have any duplicates. If there are equal keys, qsort must
358 * compare them at some point, else it wouldn't know whether one should go
359 * before or after the other.
362 data->haveDups = true;
369 * Extract the index key values from an indexable item
371 * The resulting key values are sorted, and any duplicates are removed.
372 * This avoids generating redundant index entries.
375 ginExtractEntries(GinState *ginstate, OffsetNumber attnum,
376 Datum value, bool isNull,
377 int32 *nentries, GinNullCategory **categories)
384 * We don't call the extractValueFn on a null item. Instead generate a
390 entries = (Datum *) palloc(sizeof(Datum));
391 entries[0] = (Datum) 0;
392 *categories = (GinNullCategory *) palloc(sizeof(GinNullCategory));
393 (*categories)[0] = GIN_CAT_NULL_ITEM;
397 /* OK, call the opclass's extractValueFn */
398 nullFlags = NULL; /* in case extractValue doesn't set it */
400 DatumGetPointer(FunctionCall3Coll(&ginstate->extractValueFn[attnum - 1],
401 ginstate->supportCollation[attnum - 1],
403 PointerGetDatum(nentries),
404 PointerGetDatum(&nullFlags)));
407 * Generate a placeholder if the item contained no keys.
409 if (entries == NULL || *nentries <= 0)
412 entries = (Datum *) palloc(sizeof(Datum));
413 entries[0] = (Datum) 0;
414 *categories = (GinNullCategory *) palloc(sizeof(GinNullCategory));
415 (*categories)[0] = GIN_CAT_EMPTY_ITEM;
420 * If the extractValueFn didn't create a nullFlags array, create one,
421 * assuming that everything's non-null. Otherwise, run through the array
422 * and make sure each value is exactly 0 or 1; this ensures binary
423 * compatibility with the GinNullCategory representation.
425 if (nullFlags == NULL)
426 nullFlags = (bool *) palloc0(*nentries * sizeof(bool));
429 for (i = 0; i < *nentries; i++)
430 nullFlags[i] = (nullFlags[i] ? true : false);
432 /* now we can use the nullFlags as category codes */
433 *categories = (GinNullCategory *) nullFlags;
436 * If there's more than one key, sort and unique-ify.
438 * XXX Using qsort here is notationally painful, and the overhead is
439 * pretty bad too. For small numbers of keys it'd likely be better to use
440 * a simple insertion sort.
444 keyEntryData *keydata;
447 keydata = (keyEntryData *) palloc(*nentries * sizeof(keyEntryData));
448 for (i = 0; i < *nentries; i++)
450 keydata[i].datum = entries[i];
451 keydata[i].isnull = nullFlags[i];
454 arg.cmpDatumFunc = &ginstate->compareFn[attnum - 1];
455 arg.collation = ginstate->supportCollation[attnum - 1];
456 arg.haveDups = false;
457 qsort_arg(keydata, *nentries, sizeof(keyEntryData),
458 cmpEntries, (void *) &arg);
462 /* there are duplicates, must get rid of 'em */
465 entries[0] = keydata[0].datum;
466 nullFlags[0] = keydata[0].isnull;
468 for (i = 1; i < *nentries; i++)
470 if (cmpEntries(&keydata[i - 1], &keydata[i], &arg) != 0)
472 entries[j] = keydata[i].datum;
473 nullFlags[j] = keydata[i].isnull;
481 /* easy, no duplicates */
482 for (i = 0; i < *nentries; i++)
484 entries[i] = keydata[i].datum;
485 nullFlags[i] = keydata[i].isnull;
496 ginoptions(PG_FUNCTION_ARGS)
498 Datum reloptions = PG_GETARG_DATUM(0);
499 bool validate = PG_GETARG_BOOL(1);
500 relopt_value *options;
503 static const relopt_parse_elt tab[] = {
504 {"fastupdate", RELOPT_TYPE_BOOL, offsetof(GinOptions, useFastUpdate)}
507 options = parseRelOptions(reloptions, validate, RELOPT_KIND_GIN,
510 /* if none set, we're done */
514 rdopts = allocateReloptStruct(sizeof(GinOptions), options, numoptions);
516 fillRelOptions((void *) rdopts, sizeof(GinOptions), options, numoptions,
517 validate, tab, lengthof(tab));
521 PG_RETURN_BYTEA_P(rdopts);
525 * Fetch index's statistical data into *stats
527 * Note: in the result, nPendingPages can be trusted to be up-to-date,
528 * as can ginVersion; but the other fields are as of the last VACUUM.
531 ginGetStats(Relation index, GinStatsData *stats)
535 GinMetaPageData *metadata;
537 metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
538 LockBuffer(metabuffer, GIN_SHARE);
539 metapage = BufferGetPage(metabuffer);
540 metadata = GinPageGetMeta(metapage);
542 stats->nPendingPages = metadata->nPendingPages;
543 stats->nTotalPages = metadata->nTotalPages;
544 stats->nEntryPages = metadata->nEntryPages;
545 stats->nDataPages = metadata->nDataPages;
546 stats->nEntries = metadata->nEntries;
547 stats->ginVersion = metadata->ginVersion;
549 UnlockReleaseBuffer(metabuffer);
553 * Write the given statistics to the index's metapage
555 * Note: nPendingPages and ginVersion are *not* copied over
558 ginUpdateStats(Relation index, const GinStatsData *stats)
562 GinMetaPageData *metadata;
564 metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
565 LockBuffer(metabuffer, GIN_EXCLUSIVE);
566 metapage = BufferGetPage(metabuffer);
567 metadata = GinPageGetMeta(metapage);
569 START_CRIT_SECTION();
571 metadata->nTotalPages = stats->nTotalPages;
572 metadata->nEntryPages = stats->nEntryPages;
573 metadata->nDataPages = stats->nDataPages;
574 metadata->nEntries = stats->nEntries;
576 MarkBufferDirty(metabuffer);
578 if (RelationNeedsWAL(index))
581 ginxlogUpdateMeta data;
584 data.node = index->rd_node;
586 data.newRightlink = data.prevTail = InvalidBlockNumber;
587 memcpy(&data.metadata, metadata, sizeof(GinMetaPageData));
589 rdata.buffer = InvalidBuffer;
590 rdata.data = (char *) &data;
591 rdata.len = sizeof(ginxlogUpdateMeta);
594 recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_UPDATE_META_PAGE, &rdata);
595 PageSetLSN(metapage, recptr);
596 PageSetTLI(metapage, ThisTimeLineID);
599 UnlockReleaseBuffer(metabuffer);