1 /*-------------------------------------------------------------------------
4 * utilities routines for the postgres inverted index access method.
7 * Portions Copyright (c) 1996-2014, 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);
71 * Check opclass capability to do tri-state or binary logic consistent
74 if (index_getprocid(index, i + 1, GIN_TRICONSISTENT_PROC) != InvalidOid)
76 fmgr_info_copy(&(state->triConsistentFn[i]),
77 index_getprocinfo(index, i + 1, GIN_TRICONSISTENT_PROC),
78 CurrentMemoryContext);
81 if (index_getprocid(index, i + 1, GIN_CONSISTENT_PROC) != InvalidOid)
83 fmgr_info_copy(&(state->consistentFn[i]),
84 index_getprocinfo(index, i + 1, GIN_CONSISTENT_PROC),
85 CurrentMemoryContext);
88 if (state->consistentFn[i].fn_oid == InvalidOid &&
89 state->triConsistentFn[i].fn_oid == InvalidOid)
91 elog(ERROR, "missing GIN support function (%d or %d) for attribute %d of index \"%s\"",
92 GIN_CONSISTENT_PROC, GIN_TRICONSISTENT_PROC,
93 i + 1, RelationGetRelationName(index));
97 * Check opclass capability to do partial match.
99 if (index_getprocid(index, i + 1, GIN_COMPARE_PARTIAL_PROC) != InvalidOid)
101 fmgr_info_copy(&(state->comparePartialFn[i]),
102 index_getprocinfo(index, i + 1, GIN_COMPARE_PARTIAL_PROC),
103 CurrentMemoryContext);
104 state->canPartialMatch[i] = true;
108 state->canPartialMatch[i] = false;
112 * If the index column has a specified collation, we should honor that
113 * while doing comparisons. However, we may have a collatable storage
114 * type for a noncollatable indexed data type (for instance, hstore
115 * uses text index entries). If there's no index collation then
116 * specify default collation in case the support functions need
117 * collation. This is harmless if the support functions don't care
118 * about collation, so we just do it unconditionally. (We could
119 * alternatively call get_typcollation, but that seems like expensive
120 * overkill --- there aren't going to be any cases where a GIN storage
121 * type has a nondefault collation.)
123 if (OidIsValid(index->rd_indcollation[i]))
124 state->supportCollation[i] = index->rd_indcollation[i];
126 state->supportCollation[i] = DEFAULT_COLLATION_OID;
131 * Extract attribute (column) number of stored entry from GIN tuple
134 gintuple_get_attrnum(GinState *ginstate, IndexTuple tuple)
138 if (ginstate->oneCol)
140 /* column number is not stored explicitly */
141 colN = FirstOffsetNumber;
149 * First attribute is always int16, so we can safely use any tuple
150 * descriptor to obtain first attribute of tuple
152 res = index_getattr(tuple, FirstOffsetNumber, ginstate->tupdesc[0],
156 colN = DatumGetUInt16(res);
157 Assert(colN >= FirstOffsetNumber && colN <= ginstate->origTupdesc->natts);
164 * Extract stored datum (and possible null category) from GIN tuple
167 gintuple_get_key(GinState *ginstate, IndexTuple tuple,
168 GinNullCategory *category)
173 if (ginstate->oneCol)
176 * Single column index doesn't store attribute numbers in tuples
178 res = index_getattr(tuple, FirstOffsetNumber, ginstate->origTupdesc,
184 * Since the datum type depends on which index column it's from, we
185 * must be careful to use the right tuple descriptor here.
187 OffsetNumber colN = gintuple_get_attrnum(ginstate, tuple);
189 res = index_getattr(tuple, OffsetNumberNext(FirstOffsetNumber),
190 ginstate->tupdesc[colN - 1],
195 *category = GinGetNullCategory(tuple, ginstate);
197 *category = GIN_CAT_NORM_KEY;
203 * Allocate a new page (either by recycling, or by extending the index file)
204 * The returned buffer is already pinned and exclusive-locked
205 * Caller is responsible for initializing the page by calling GinInitBuffer
208 GinNewBuffer(Relation index)
213 /* First, try to get a page from FSM */
216 BlockNumber blkno = GetFreeIndexPage(index);
218 if (blkno == InvalidBlockNumber)
221 buffer = ReadBuffer(index, blkno);
224 * We have to guard against the possibility that someone else already
225 * recycled this page; the buffer may be locked if so.
227 if (ConditionalLockBuffer(buffer))
229 Page page = BufferGetPage(buffer);
232 return buffer; /* OK to use, if never initialized */
234 if (GinPageIsDeleted(page))
235 return buffer; /* OK to use */
237 LockBuffer(buffer, GIN_UNLOCK);
240 /* Can't use it, so release buffer and try again */
241 ReleaseBuffer(buffer);
244 /* Must extend the file */
245 needLock = !RELATION_IS_LOCAL(index);
247 LockRelationForExtension(index, ExclusiveLock);
249 buffer = ReadBuffer(index, P_NEW);
250 LockBuffer(buffer, GIN_EXCLUSIVE);
253 UnlockRelationForExtension(index, ExclusiveLock);
259 GinInitPage(Page page, uint32 f, Size pageSize)
261 GinPageOpaque opaque;
263 PageInit(page, pageSize, sizeof(GinPageOpaqueData));
265 opaque = GinPageGetOpaque(page);
266 memset(opaque, 0, sizeof(GinPageOpaqueData));
268 opaque->rightlink = InvalidBlockNumber;
272 GinInitBuffer(Buffer b, uint32 f)
274 GinInitPage(BufferGetPage(b), f, BufferGetPageSize(b));
278 GinInitMetabuffer(Buffer b)
280 GinMetaPageData *metadata;
281 Page page = BufferGetPage(b);
283 GinInitPage(page, GIN_META, BufferGetPageSize(b));
285 metadata = GinPageGetMeta(page);
287 metadata->head = metadata->tail = InvalidBlockNumber;
288 metadata->tailFreeSize = 0;
289 metadata->nPendingPages = 0;
290 metadata->nPendingHeapTuples = 0;
291 metadata->nTotalPages = 0;
292 metadata->nEntryPages = 0;
293 metadata->nDataPages = 0;
294 metadata->nEntries = 0;
295 metadata->ginVersion = GIN_CURRENT_VERSION;
299 * Compare two keys of the same index column
302 ginCompareEntries(GinState *ginstate, OffsetNumber attnum,
303 Datum a, GinNullCategory categorya,
304 Datum b, GinNullCategory categoryb)
306 /* if not of same null category, sort by that first */
307 if (categorya != categoryb)
308 return (categorya < categoryb) ? -1 : 1;
310 /* all null items in same category are equal */
311 if (categorya != GIN_CAT_NORM_KEY)
314 /* both not null, so safe to call the compareFn */
315 return DatumGetInt32(FunctionCall2Coll(&ginstate->compareFn[attnum - 1],
316 ginstate->supportCollation[attnum - 1],
321 * Compare two keys of possibly different index columns
324 ginCompareAttEntries(GinState *ginstate,
325 OffsetNumber attnuma, Datum a, GinNullCategory categorya,
326 OffsetNumber attnumb, Datum b, GinNullCategory categoryb)
328 /* attribute number is the first sort key */
329 if (attnuma != attnumb)
330 return (attnuma < attnumb) ? -1 : 1;
332 return ginCompareEntries(ginstate, attnuma, a, categorya, b, categoryb);
337 * Support for sorting key datums in ginExtractEntries
339 * Note: we only have to worry about null and not-null keys here;
340 * ginExtractEntries never generates more than one placeholder null,
341 * so it doesn't have to sort those.
351 FmgrInfo *cmpDatumFunc;
357 cmpEntries(const void *a, const void *b, void *arg)
359 const keyEntryData *aa = (const keyEntryData *) a;
360 const keyEntryData *bb = (const keyEntryData *) b;
361 cmpEntriesArg *data = (cmpEntriesArg *) arg;
367 res = 0; /* NULL "=" NULL */
369 res = 1; /* NULL ">" not-NULL */
372 res = -1; /* not-NULL "<" NULL */
374 res = DatumGetInt32(FunctionCall2Coll(data->cmpDatumFunc,
376 aa->datum, bb->datum));
379 * Detect if we have any duplicates. If there are equal keys, qsort must
380 * compare them at some point, else it wouldn't know whether one should go
381 * before or after the other.
384 data->haveDups = true;
391 * Extract the index key values from an indexable item
393 * The resulting key values are sorted, and any duplicates are removed.
394 * This avoids generating redundant index entries.
397 ginExtractEntries(GinState *ginstate, OffsetNumber attnum,
398 Datum value, bool isNull,
399 int32 *nentries, GinNullCategory **categories)
406 * We don't call the extractValueFn on a null item. Instead generate a
412 entries = (Datum *) palloc(sizeof(Datum));
413 entries[0] = (Datum) 0;
414 *categories = (GinNullCategory *) palloc(sizeof(GinNullCategory));
415 (*categories)[0] = GIN_CAT_NULL_ITEM;
419 /* OK, call the opclass's extractValueFn */
420 nullFlags = NULL; /* in case extractValue doesn't set it */
422 DatumGetPointer(FunctionCall3Coll(&ginstate->extractValueFn[attnum - 1],
423 ginstate->supportCollation[attnum - 1],
425 PointerGetDatum(nentries),
426 PointerGetDatum(&nullFlags)));
429 * Generate a placeholder if the item contained no keys.
431 if (entries == NULL || *nentries <= 0)
434 entries = (Datum *) palloc(sizeof(Datum));
435 entries[0] = (Datum) 0;
436 *categories = (GinNullCategory *) palloc(sizeof(GinNullCategory));
437 (*categories)[0] = GIN_CAT_EMPTY_ITEM;
442 * If the extractValueFn didn't create a nullFlags array, create one,
443 * assuming that everything's non-null. Otherwise, run through the array
444 * and make sure each value is exactly 0 or 1; this ensures binary
445 * compatibility with the GinNullCategory representation.
447 if (nullFlags == NULL)
448 nullFlags = (bool *) palloc0(*nentries * sizeof(bool));
451 for (i = 0; i < *nentries; i++)
452 nullFlags[i] = (nullFlags[i] ? true : false);
454 /* now we can use the nullFlags as category codes */
455 *categories = (GinNullCategory *) nullFlags;
458 * If there's more than one key, sort and unique-ify.
460 * XXX Using qsort here is notationally painful, and the overhead is
461 * pretty bad too. For small numbers of keys it'd likely be better to use
462 * a simple insertion sort.
466 keyEntryData *keydata;
469 keydata = (keyEntryData *) palloc(*nentries * sizeof(keyEntryData));
470 for (i = 0; i < *nentries; i++)
472 keydata[i].datum = entries[i];
473 keydata[i].isnull = nullFlags[i];
476 arg.cmpDatumFunc = &ginstate->compareFn[attnum - 1];
477 arg.collation = ginstate->supportCollation[attnum - 1];
478 arg.haveDups = false;
479 qsort_arg(keydata, *nentries, sizeof(keyEntryData),
480 cmpEntries, (void *) &arg);
484 /* there are duplicates, must get rid of 'em */
487 entries[0] = keydata[0].datum;
488 nullFlags[0] = keydata[0].isnull;
490 for (i = 1; i < *nentries; i++)
492 if (cmpEntries(&keydata[i - 1], &keydata[i], &arg) != 0)
494 entries[j] = keydata[i].datum;
495 nullFlags[j] = keydata[i].isnull;
503 /* easy, no duplicates */
504 for (i = 0; i < *nentries; i++)
506 entries[i] = keydata[i].datum;
507 nullFlags[i] = keydata[i].isnull;
518 ginoptions(PG_FUNCTION_ARGS)
520 Datum reloptions = PG_GETARG_DATUM(0);
521 bool validate = PG_GETARG_BOOL(1);
522 relopt_value *options;
525 static const relopt_parse_elt tab[] = {
526 {"fastupdate", RELOPT_TYPE_BOOL, offsetof(GinOptions, useFastUpdate)}
529 options = parseRelOptions(reloptions, validate, RELOPT_KIND_GIN,
532 /* if none set, we're done */
536 rdopts = allocateReloptStruct(sizeof(GinOptions), options, numoptions);
538 fillRelOptions((void *) rdopts, sizeof(GinOptions), options, numoptions,
539 validate, tab, lengthof(tab));
543 PG_RETURN_BYTEA_P(rdopts);
547 * Fetch index's statistical data into *stats
549 * Note: in the result, nPendingPages can be trusted to be up-to-date,
550 * as can ginVersion; but the other fields are as of the last VACUUM.
553 ginGetStats(Relation index, GinStatsData *stats)
557 GinMetaPageData *metadata;
559 metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
560 LockBuffer(metabuffer, GIN_SHARE);
561 metapage = BufferGetPage(metabuffer);
562 metadata = GinPageGetMeta(metapage);
564 stats->nPendingPages = metadata->nPendingPages;
565 stats->nTotalPages = metadata->nTotalPages;
566 stats->nEntryPages = metadata->nEntryPages;
567 stats->nDataPages = metadata->nDataPages;
568 stats->nEntries = metadata->nEntries;
569 stats->ginVersion = metadata->ginVersion;
571 UnlockReleaseBuffer(metabuffer);
575 * Write the given statistics to the index's metapage
577 * Note: nPendingPages and ginVersion are *not* copied over
580 ginUpdateStats(Relation index, const GinStatsData *stats)
584 GinMetaPageData *metadata;
586 metabuffer = ReadBuffer(index, GIN_METAPAGE_BLKNO);
587 LockBuffer(metabuffer, GIN_EXCLUSIVE);
588 metapage = BufferGetPage(metabuffer);
589 metadata = GinPageGetMeta(metapage);
591 START_CRIT_SECTION();
593 metadata->nTotalPages = stats->nTotalPages;
594 metadata->nEntryPages = stats->nEntryPages;
595 metadata->nDataPages = stats->nDataPages;
596 metadata->nEntries = stats->nEntries;
598 MarkBufferDirty(metabuffer);
600 if (RelationNeedsWAL(index))
603 ginxlogUpdateMeta data;
606 data.node = index->rd_node;
608 data.newRightlink = data.prevTail = InvalidBlockNumber;
609 memcpy(&data.metadata, metadata, sizeof(GinMetaPageData));
611 rdata.buffer = InvalidBuffer;
612 rdata.data = (char *) &data;
613 rdata.len = sizeof(ginxlogUpdateMeta);
616 recptr = XLogInsert(RM_GIN_ID, XLOG_GIN_UPDATE_META_PAGE, &rdata);
617 PageSetLSN(metapage, recptr);
620 UnlockReleaseBuffer(metabuffer);