PostgreSQL Source Code git master
Loading...
Searching...
No Matches
spgist_private.h File Reference
#include "access/itup.h"
#include "access/spgist.h"
#include "catalog/pg_am_d.h"
#include "nodes/tidbitmap.h"
#include "storage/buf.h"
#include "utils/geo_decls.h"
#include "utils/relcache.h"
Include dependency graph for spgist_private.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SpGistOptions
 
struct  SpGistPageOpaqueData
 
struct  SpGistLastUsedPage
 
struct  SpGistLUPCache
 
struct  SpGistMetaPageData
 
struct  SpGistTypeDesc
 
struct  SpGistState
 
struct  SpGistSearchItem
 
struct  SpGistScanOpaqueData
 
struct  SpGistCache
 
struct  SpGistInnerTupleData
 
struct  SpGistLeafTupleData
 
struct  SpGistDeadTupleData
 

Macros

#define SpGistGetFillFactor(relation)
 
#define SpGistGetTargetPageFreeSpace(relation)    (BLCKSZ * (100 - SpGistGetFillFactor(relation)) / 100)
 
#define spgKeyColumn   0
 
#define spgFirstIncludeColumn   1
 
#define SPGIST_METAPAGE_BLKNO   (0) /* metapage */
 
#define SPGIST_ROOT_BLKNO   (1) /* root for normal entries */
 
#define SPGIST_NULL_BLKNO   (2) /* root for null-value entries */
 
#define SPGIST_LAST_FIXED_BLKNO   SPGIST_NULL_BLKNO
 
#define SpGistBlockIsRoot(blkno)    ((blkno) == SPGIST_ROOT_BLKNO || (blkno) == SPGIST_NULL_BLKNO)
 
#define SpGistBlockIsFixed(blkno)    ((BlockNumber) (blkno) <= (BlockNumber) SPGIST_LAST_FIXED_BLKNO)
 
#define SPGIST_META   (1<<0)
 
#define SPGIST_DELETED
 
#define SPGIST_LEAF   (1<<2)
 
#define SPGIST_NULLS   (1<<3)
 
#define SpGistPageGetOpaque(page)   ((SpGistPageOpaque) PageGetSpecialPointer(page))
 
#define SpGistPageIsMeta(page)   (SpGistPageGetOpaque(page)->flags & SPGIST_META)
 
#define SpGistPageIsDeleted(page)   (SpGistPageGetOpaque(page)->flags & SPGIST_DELETED)
 
#define SpGistPageIsLeaf(page)   (SpGistPageGetOpaque(page)->flags & SPGIST_LEAF)
 
#define SpGistPageStoresNulls(page)   (SpGistPageGetOpaque(page)->flags & SPGIST_NULLS)
 
#define SPGIST_PAGE_ID   0xFF82
 
#define SPGIST_CACHED_PAGES   8
 
#define SPGIST_MAGIC_NUMBER   (0xBA0BABEE)
 
#define SpGistPageGetMeta(p)    ((SpGistMetaPageData *) PageGetContents(p))
 
#define SizeOfSpGistSearchItem(n_distances)    (offsetof(SpGistSearchItem, distances) + sizeof(double) * (n_distances))
 
#define SPGIST_LIVE   0 /* normal live tuple (either inner or leaf) */
 
#define SPGIST_REDIRECT   1 /* temporary redirection placeholder */
 
#define SPGIST_DEAD   2 /* dead, cannot be removed because of links */
 
#define SPGIST_PLACEHOLDER   3 /* placeholder, used to preserve offsets */
 
#define SGITMAXNNODES   0x1FFF
 
#define SGITMAXPREFIXSIZE   0xFFFF
 
#define SGITMAXSIZE   0xFFFF
 
#define SGITHDRSZ   MAXALIGN(sizeof(SpGistInnerTupleData))
 
#define _SGITDATA(x)   (((char *) (x)) + SGITHDRSZ)
 
#define SGITDATAPTR(x)   ((x)->prefixSize ? _SGITDATA(x) : NULL)
 
#define SGITDATUM(x, s)
 
#define SGITNODEPTR(x)   ((SpGistNodeTuple) (_SGITDATA(x) + (x)->prefixSize))
 
#define SGITITERATE(x, i, nt)
 
#define SGNTHDRSZ   MAXALIGN(sizeof(SpGistNodeTupleData))
 
#define SGNTDATAPTR(x)   (((char *) (x)) + SGNTHDRSZ)
 
#define SGNTDATUM(x, s)
 
#define SGLT_GET_NEXTOFFSET(spgLeafTuple)    ((spgLeafTuple)->t_info & 0x3FFF)
 
#define SGLT_GET_HASNULLMASK(spgLeafTuple)    (((spgLeafTuple)->t_info & 0x8000) ? true : false)
 
#define SGLT_SET_NEXTOFFSET(spgLeafTuple, offsetNumber)
 
#define SGLT_SET_HASNULLMASK(spgLeafTuple, hasnulls)
 
#define SGLTHDRSZ(hasnulls)
 
#define SGLTDATAPTR(x)   (((char *) (x)) + SGLTHDRSZ(SGLT_GET_HASNULLMASK(x)))
 
#define SGLTDATUM(x, s)
 
#define SGDTSIZE   MAXALIGN(sizeof(SpGistDeadTupleData))
 
#define SPGIST_PAGE_CAPACITY
 
#define SpGistPageGetFreeSpace(p, n)
 
#define STORE_STATE(s, d)
 
#define GBUF_LEAF   0x03
 
#define GBUF_INNER_PARITY(x)   ((x) % 3)
 
#define GBUF_NULLS   0x04
 
#define GBUF_PARITY_MASK   0x03
 
#define GBUF_REQ_LEAF(flags)   (((flags) & GBUF_PARITY_MASK) == GBUF_LEAF)
 
#define GBUF_REQ_NULLS(flags)   ((flags) & GBUF_NULLS)
 
#define SPGIST_MIN_FILLFACTOR   10
 
#define SPGIST_DEFAULT_FILLFACTOR   80
 

Typedefs

typedef struct SpGistOptions SpGistOptions
 
typedef struct SpGistPageOpaqueData SpGistPageOpaqueData
 
typedef SpGistPageOpaqueDataSpGistPageOpaque
 
typedef struct SpGistLastUsedPage SpGistLastUsedPage
 
typedef struct SpGistLUPCache SpGistLUPCache
 
typedef struct SpGistMetaPageData SpGistMetaPageData
 
typedef struct SpGistLeafTupleDataSpGistLeafTuple
 
typedef struct SpGistTypeDesc SpGistTypeDesc
 
typedef struct SpGistState SpGistState
 
typedef struct SpGistSearchItem SpGistSearchItem
 
typedef struct SpGistScanOpaqueData SpGistScanOpaqueData
 
typedef SpGistScanOpaqueDataSpGistScanOpaque
 
typedef struct SpGistCache SpGistCache
 
typedef struct SpGistInnerTupleData SpGistInnerTupleData
 
typedef SpGistInnerTupleDataSpGistInnerTuple
 
typedef IndexTupleData SpGistNodeTupleData
 
typedef SpGistNodeTupleDataSpGistNodeTuple
 
typedef struct SpGistLeafTupleData SpGistLeafTupleData
 
typedef struct SpGistDeadTupleData SpGistDeadTupleData
 
typedef SpGistDeadTupleDataSpGistDeadTuple
 

Functions

SpGistCachespgGetCache (Relation index)
 
TupleDesc getSpGistTupleDesc (Relation index, SpGistTypeDesc *keyType)
 
void initSpGistState (SpGistState *state, Relation index)
 
Buffer SpGistNewBuffer (Relation index)
 
void SpGistUpdateMetaPage (Relation index)
 
Buffer SpGistGetBuffer (Relation index, int flags, int needSpace, bool *isNew)
 
void SpGistSetLastUsedPage (Relation index, Buffer buffer)
 
void SpGistInitPage (Page page, uint16 f)
 
void SpGistInitBuffer (Buffer b, uint16 f)
 
void SpGistInitMetapage (Page page)
 
unsigned int SpGistGetInnerTypeSize (SpGistTypeDesc *att, Datum datum)
 
Size SpGistGetLeafTupleSize (TupleDesc tupleDescriptor, const Datum *datums, const bool *isnulls)
 
SpGistLeafTuple spgFormLeafTuple (SpGistState *state, const ItemPointerData *heapPtr, const Datum *datums, const bool *isnulls)
 
SpGistNodeTuple spgFormNodeTuple (SpGistState *state, Datum label, bool isnull)
 
SpGistInnerTuple spgFormInnerTuple (SpGistState *state, bool hasPrefix, Datum prefix, int nNodes, SpGistNodeTuple *nodes)
 
SpGistDeadTuple spgFormDeadTuple (SpGistState *state, int tupstate, BlockNumber blkno, OffsetNumber offnum)
 
void spgDeformLeafTuple (SpGistLeafTuple tup, TupleDesc tupleDescriptor, Datum *datums, bool *isnulls, bool keyColumnIsNull)
 
DatumspgExtractNodeLabels (SpGistState *state, SpGistInnerTuple innerTuple)
 
OffsetNumber SpGistPageAddNewItem (SpGistState *state, Page page, const void *item, Size size, OffsetNumber *startOffset, bool errorOK)
 
bool spgproperty (Oid index_oid, int attno, IndexAMProperty prop, const char *propname, bool *res, bool *isnull)
 
void spgUpdateNodeLink (SpGistInnerTuple tup, int nodeN, BlockNumber blkno, OffsetNumber offset)
 
void spgPageIndexMultiDelete (SpGistState *state, Page page, OffsetNumber *itemnos, int nitems, int firststate, int reststate, BlockNumber blkno, OffsetNumber offnum)
 
bool spgdoinsert (Relation index, SpGistState *state, const ItemPointerData *heapPtr, const Datum *datums, const bool *isnulls)
 
doublespg_key_orderbys_distances (Datum key, bool isLeaf, ScanKey orderbys, int norderbys)
 
BOXbox_copy (BOX *orig)
 

Macro Definition Documentation

◆ _SGITDATA

#define _SGITDATA (   x)    (((char *) (x)) + SGITHDRSZ)

Definition at line 313 of file spgist_private.h.

◆ GBUF_INNER_PARITY

#define GBUF_INNER_PARITY (   x)    ((x) % 3)

Definition at line 485 of file spgist_private.h.

◆ GBUF_LEAF

#define GBUF_LEAF   0x03

Definition at line 484 of file spgist_private.h.

◆ GBUF_NULLS

#define GBUF_NULLS   0x04

Definition at line 486 of file spgist_private.h.

◆ GBUF_PARITY_MASK

#define GBUF_PARITY_MASK   0x03

Definition at line 488 of file spgist_private.h.

◆ GBUF_REQ_LEAF

#define GBUF_REQ_LEAF (   flags)    (((flags) & GBUF_PARITY_MASK) == GBUF_LEAF)

Definition at line 489 of file spgist_private.h.

◆ GBUF_REQ_NULLS

#define GBUF_REQ_NULLS (   flags)    ((flags) & GBUF_NULLS)

Definition at line 490 of file spgist_private.h.

◆ SGDTSIZE

Definition at line 439 of file spgist_private.h.

◆ SGITDATAPTR

#define SGITDATAPTR (   x)    ((x)->prefixSize ? _SGITDATA(x) : NULL)

Definition at line 314 of file spgist_private.h.

◆ SGITDATUM

#define SGITDATUM (   x,
 
)
Value:
((x)->prefixSize ? \
((s)->attPrefixType.attbyval ? \
*(Datum *) _SGITDATA(x) : \
: (Datum) 0)
int x
Definition isn.c:75
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
#define _SGITDATA(x)

Definition at line 315 of file spgist_private.h.

315 : NULL)
316#define SGITDATUM(x, s) ((x)->prefixSize ? \
317 ((s)->attPrefixType.attbyval ? \
318 *(Datum *) _SGITDATA(x) : \
#define SGITDATUM(x, s)

◆ SGITHDRSZ

Definition at line 312 of file spgist_private.h.

◆ SGITITERATE

#define SGITITERATE (   x,
  i,
  nt 
)
Value:
for ((i) = 0, (nt) = SGITNODEPTR(x); \
(i) < (x)->nNodes; \
(i)++, (nt) = (SpGistNodeTuple) (((char *) (nt)) + IndexTupleSize(nt)))
int i
Definition isn.c:77
static Size IndexTupleSize(const IndexTupleData *itup)
Definition itup.h:71
SpGistNodeTupleData * SpGistNodeTuple
#define SGITNODEPTR(x)

Definition at line 323 of file spgist_private.h.

◆ SGITMAXNNODES

#define SGITMAXNNODES   0x1FFF

Definition at line 308 of file spgist_private.h.

◆ SGITMAXPREFIXSIZE

#define SGITMAXPREFIXSIZE   0xFFFF

Definition at line 309 of file spgist_private.h.

◆ SGITMAXSIZE

#define SGITMAXSIZE   0xFFFF

Definition at line 310 of file spgist_private.h.

◆ SGITNODEPTR

#define SGITNODEPTR (   x)    ((SpGistNodeTuple) (_SGITDATA(x) + (x)->prefixSize))

Definition at line 320 of file spgist_private.h.

◆ SGLT_GET_HASNULLMASK

#define SGLT_GET_HASNULLMASK (   spgLeafTuple)     (((spgLeafTuple)->t_info & 0x8000) ? true : false)

Definition at line 398 of file spgist_private.h.

◆ SGLT_GET_NEXTOFFSET

#define SGLT_GET_NEXTOFFSET (   spgLeafTuple)     ((spgLeafTuple)->t_info & 0x3FFF)

Definition at line 396 of file spgist_private.h.

◆ SGLT_SET_HASNULLMASK

#define SGLT_SET_HASNULLMASK (   spgLeafTuple,
  hasnulls 
)
Value:
((spgLeafTuple)->t_info = \
((spgLeafTuple)->t_info & 0x7FFF) | ((hasnulls) ? 0x8000 : 0))

Definition at line 403 of file spgist_private.h.

◆ SGLT_SET_NEXTOFFSET

#define SGLT_SET_NEXTOFFSET (   spgLeafTuple,
  offsetNumber 
)
Value:
((spgLeafTuple)->t_info = \
((spgLeafTuple)->t_info & 0xC000) | ((offsetNumber) & 0x3FFF))

Definition at line 400 of file spgist_private.h.

400 : false)
402 ((spgLeafTuple)->t_info = \
#define SGLT_SET_NEXTOFFSET(spgLeafTuple, offsetNumber)

◆ SGLTDATAPTR

#define SGLTDATAPTR (   x)    (((char *) (x)) + SGLTHDRSZ(SGLT_GET_HASNULLMASK(x)))

Definition at line 411 of file spgist_private.h.

◆ SGLTDATUM

#define SGLTDATUM (   x,
 
)
Value:
(s)->attLeafType.attbyval, \
(s)->attLeafType.attlen)
#define SGLTDATAPTR(x)
static Datum fetch_att(const void *T, bool attbyval, int attlen)
Definition tupmacs.h:108

Definition at line 412 of file spgist_private.h.

◆ SGLTHDRSZ

#define SGLTHDRSZ (   hasnulls)

◆ SGNTDATAPTR

#define SGNTDATAPTR (   x)    (((char *) (x)) + SGNTHDRSZ)

Definition at line 342 of file spgist_private.h.

◆ SGNTDATUM

#define SGNTDATUM (   x,
 
)
Value:
((s)->attLabelType.attbyval ? \
*(Datum *) SGNTDATAPTR(x) : \
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342
#define SGNTDATAPTR(x)

Definition at line 343 of file spgist_private.h.

345 : \

◆ SGNTHDRSZ

Definition at line 341 of file spgist_private.h.

◆ SizeOfSpGistSearchItem

#define SizeOfSpGistSearchItem (   n_distances)     (offsetof(SpGistSearchItem, distances) + sizeof(double) * (n_distances))

Definition at line 182 of file spgist_private.h.

◆ spgFirstIncludeColumn

#define spgFirstIncludeColumn   1

Definition at line 44 of file spgist_private.h.

◆ SPGIST_CACHED_PAGES

#define SPGIST_CACHED_PAGES   8

Definition at line 105 of file spgist_private.h.

◆ SPGIST_DEAD

#define SPGIST_DEAD   2 /* dead, cannot be removed because of links */

Definition at line 273 of file spgist_private.h.

◆ SPGIST_DEFAULT_FILLFACTOR

#define SPGIST_DEFAULT_FILLFACTOR   80

Definition at line 496 of file spgist_private.h.

◆ SPGIST_DELETED

#define SPGIST_DELETED
Value:
(1<<1) /* never set, but keep for backwards
* compatibility */

Definition at line 73 of file spgist_private.h.

◆ SPGIST_LAST_FIXED_BLKNO

#define SPGIST_LAST_FIXED_BLKNO   SPGIST_NULL_BLKNO

Definition at line 50 of file spgist_private.h.

◆ SPGIST_LEAF

#define SPGIST_LEAF   (1<<2)

Definition at line 74 of file spgist_private.h.

◆ SPGIST_LIVE

#define SPGIST_LIVE   0 /* normal live tuple (either inner or leaf) */

Definition at line 271 of file spgist_private.h.

◆ SPGIST_MAGIC_NUMBER

#define SPGIST_MAGIC_NUMBER   (0xBA0BABEE)

Definition at line 121 of file spgist_private.h.

◆ SPGIST_META

#define SPGIST_META   (1<<0)

Definition at line 72 of file spgist_private.h.

◆ SPGIST_METAPAGE_BLKNO

#define SPGIST_METAPAGE_BLKNO   (0) /* metapage */

Definition at line 47 of file spgist_private.h.

◆ SPGIST_MIN_FILLFACTOR

#define SPGIST_MIN_FILLFACTOR   10

Definition at line 495 of file spgist_private.h.

◆ SPGIST_NULL_BLKNO

#define SPGIST_NULL_BLKNO   (2) /* root for null-value entries */

Definition at line 49 of file spgist_private.h.

◆ SPGIST_NULLS

#define SPGIST_NULLS   (1<<3)

Definition at line 75 of file spgist_private.h.

◆ SPGIST_PAGE_CAPACITY

#define SPGIST_PAGE_CAPACITY
Value:
#define SizeOfPageHeaderData
Definition bufpage.h:242
#define MAXALIGN_DOWN(LEN)
Definition c.h:910

Definition at line 449 of file spgist_private.h.

◆ SPGIST_PAGE_ID

#define SPGIST_PAGE_ID   0xFF82

Definition at line 91 of file spgist_private.h.

◆ SPGIST_PLACEHOLDER

#define SPGIST_PLACEHOLDER   3 /* placeholder, used to preserve offsets */

Definition at line 274 of file spgist_private.h.

◆ SPGIST_REDIRECT

#define SPGIST_REDIRECT   1 /* temporary redirection placeholder */

Definition at line 272 of file spgist_private.h.

◆ SPGIST_ROOT_BLKNO

#define SPGIST_ROOT_BLKNO   (1) /* root for normal entries */

Definition at line 48 of file spgist_private.h.

◆ SpGistBlockIsFixed

#define SpGistBlockIsFixed (   blkno)     ((BlockNumber) (blkno) <= (BlockNumber) SPGIST_LAST_FIXED_BLKNO)

Definition at line 54 of file spgist_private.h.

◆ SpGistBlockIsRoot

#define SpGistBlockIsRoot (   blkno)     ((blkno) == SPGIST_ROOT_BLKNO || (blkno) == SPGIST_NULL_BLKNO)

Definition at line 52 of file spgist_private.h.

◆ SpGistGetFillFactor

#define SpGistGetFillFactor (   relation)
Value:
(AssertMacro(relation->rd_rel->relkind == RELKIND_INDEX && \
relation->rd_rel->relam == SPGIST_AM_OID), \
(relation)->rd_options ? \
((SpGistOptions *) (relation)->rd_options)->fillfactor : \
#define AssertMacro(condition)
Definition c.h:946
#define SPGIST_DEFAULT_FILLFACTOR

Definition at line 32 of file spgist_private.h.

◆ SpGistGetTargetPageFreeSpace

#define SpGistGetTargetPageFreeSpace (   relation)     (BLCKSZ * (100 - SpGistGetFillFactor(relation)) / 100)

Definition at line 38 of file spgist_private.h.

◆ SpGistPageGetFreeSpace

#define SpGistPageGetFreeSpace (   p,
 
)
Value:
Min(SpGistPageGetOpaque(p)->nPlaceholder, n) * \
(SGDTSIZE + sizeof(ItemIdData)))
Size PageGetExactFreeSpace(const PageData *page)
Definition bufpage.c:957
#define SGDTSIZE
#define SpGistPageGetOpaque(page)

Definition at line 458 of file spgist_private.h.

◆ SpGistPageGetMeta

#define SpGistPageGetMeta (   p)     ((SpGistMetaPageData *) PageGetContents(p))

Definition at line 123 of file spgist_private.h.

◆ SpGistPageGetOpaque

#define SpGistPageGetOpaque (   page)    ((SpGistPageOpaque) PageGetSpecialPointer(page))

Definition at line 77 of file spgist_private.h.

◆ SpGistPageIsDeleted

#define SpGistPageIsDeleted (   page)    (SpGistPageGetOpaque(page)->flags & SPGIST_DELETED)

Definition at line 79 of file spgist_private.h.

◆ SpGistPageIsLeaf

#define SpGistPageIsLeaf (   page)    (SpGistPageGetOpaque(page)->flags & SPGIST_LEAF)

Definition at line 80 of file spgist_private.h.

◆ SpGistPageIsMeta

#define SpGistPageIsMeta (   page)    (SpGistPageGetOpaque(page)->flags & SPGIST_META)

Definition at line 78 of file spgist_private.h.

◆ SpGistPageStoresNulls

#define SpGistPageStoresNulls (   page)    (SpGistPageGetOpaque(page)->flags & SPGIST_NULLS)

Definition at line 81 of file spgist_private.h.

◆ spgKeyColumn

#define spgKeyColumn   0

Definition at line 43 of file spgist_private.h.

◆ STORE_STATE

#define STORE_STATE (   s,
 
)
Value:
do { \
(d).redirectXid = (s)->redirectXid; \
(d).isBuild = (s)->isBuild; \
} while(0)

Definition at line 467 of file spgist_private.h.

469 { \
470 (d).redirectXid = (s)->redirectXid; \
471 (d).isBuild = (s)->isBuild; \

Typedef Documentation

◆ SpGistCache

◆ SpGistDeadTuple

◆ SpGistDeadTupleData

◆ SpGistInnerTuple

◆ SpGistInnerTupleData

◆ SpGistLastUsedPage

◆ SpGistLeafTuple

Definition at line 131 of file spgist_private.h.

◆ SpGistLeafTupleData

◆ SpGistLUPCache

◆ SpGistMetaPageData

◆ SpGistNodeTuple

◆ SpGistNodeTupleData

◆ SpGistOptions

◆ SpGistPageOpaque

◆ SpGistPageOpaqueData

◆ SpGistScanOpaque

◆ SpGistScanOpaqueData

◆ SpGistSearchItem

◆ SpGistState

◆ SpGistTypeDesc

Function Documentation

◆ box_copy()

BOX * box_copy ( BOX orig)
extern

Definition at line 82 of file spgproc.c.

83{
84 BOX *result = palloc_object(BOX);
85
86 *result = *orig;
87 return result;
88}
#define palloc_object(type)
Definition fe_memutils.h:74

References fb(), and palloc_object.

Referenced by spg_kd_inner_consistent(), and spg_quad_inner_consistent().

◆ getSpGistTupleDesc()

TupleDesc getSpGistTupleDesc ( Relation  index,
SpGistTypeDesc keyType 
)
extern

Definition at line 316 of file spgutils.c.

317{
320
321 if (keyType->type ==
324 else
325 {
328 /* It's sufficient to update the type-dependent fields of the column */
329 att->atttypid = keyType->type;
330 att->atttypmod = -1;
331 att->attlen = keyType->attlen;
332 att->attbyval = keyType->attbyval;
333 att->attalign = keyType->attalign;
334 att->attstorage = keyType->attstorage;
335 /* We shouldn't need to bother with making these valid: */
336 att->attcompression = InvalidCompressionMethod;
337 att->attcollation = InvalidOid;
338
341 }
342 return outTupDesc;
343}
FormData_pg_attribute * Form_pg_attribute
#define InvalidOid
#define RelationGetDescr(relation)
Definition rel.h:540
#define spgKeyColumn
Definition type.h:96
#define InvalidCompressionMethod
void TupleDescFinalize(TupleDesc tupdesc)
Definition tupdesc.c:508
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition tupdesc.c:242
void populate_compact_attribute(TupleDesc tupdesc, int attnum)
Definition tupdesc.c:100
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178

References CreateTupleDescCopy(), fb(), InvalidCompressionMethod, InvalidOid, populate_compact_attribute(), RelationGetDescr, spgKeyColumn, TupleDescAttr(), and TupleDescFinalize().

Referenced by initSpGistState(), and spgbeginscan().

◆ initSpGistState()

void initSpGistState ( SpGistState state,
Relation  index 
)
extern

Definition at line 347 of file spgutils.c.

348{
349 SpGistCache *cache;
350
351 state->index = index;
352
353 /* Get cached static information about index */
354 cache = spgGetCache(index);
355
356 state->config = cache->config;
357 state->attType = cache->attType;
358 state->attLeafType = cache->attLeafType;
359 state->attPrefixType = cache->attPrefixType;
360 state->attLabelType = cache->attLabelType;
361
362 /* Ensure we have a valid descriptor for leaf tuples */
363 state->leafTupDesc = getSpGistTupleDesc(state->index, &state->attLeafType);
364
365 /* Make workspace for constructing dead tuples */
366 state->deadTupleStorage = palloc0(SGDTSIZE);
367
368 /*
369 * Set horizon XID to use in redirection tuples. Use our own XID if we
370 * have one, else use InvalidTransactionId. The latter case can happen in
371 * VACUUM or REINDEX CONCURRENTLY, and in neither case would it be okay to
372 * force an XID to be assigned. VACUUM won't create any redirection
373 * tuples anyway, but REINDEX CONCURRENTLY can. Fortunately, REINDEX
374 * CONCURRENTLY doesn't mark the index valid until the end, so there could
375 * never be any concurrent scans "in flight" to a redirection tuple it has
376 * inserted. And it locks out VACUUM until the end, too. So it's okay
377 * for VACUUM to immediately expire a redirection tuple that contains an
378 * invalid xid.
379 */
380 state->redirectXid = GetTopTransactionIdIfAny();
381
382 /* Assume we're not in an index build (spgbuild will override) */
383 state->isBuild = false;
384}
void * palloc0(Size size)
Definition mcxt.c:1417
TupleDesc getSpGistTupleDesc(Relation index, SpGistTypeDesc *keyType)
Definition spgutils.c:316
SpGistCache * spgGetCache(Relation index)
Definition spgutils.c:189
SpGistTypeDesc attPrefixType
SpGistTypeDesc attLeafType
SpGistTypeDesc attType
spgConfigOut config
SpGistTypeDesc attLabelType
TransactionId GetTopTransactionIdIfAny(void)
Definition xact.c:443

References SpGistCache::attLabelType, SpGistCache::attLeafType, SpGistCache::attPrefixType, SpGistCache::attType, SpGistCache::config, getSpGistTupleDesc(), GetTopTransactionIdIfAny(), palloc0(), SGDTSIZE, and spgGetCache().

Referenced by spgbeginscan(), spgbuild(), spginsert(), and spgvacuumscan().

◆ spg_key_orderbys_distances()

double * spg_key_orderbys_distances ( Datum  key,
bool  isLeaf,
ScanKey  orderbys,
int  norderbys 
)
extern

Definition at line 63 of file spgproc.c.

65{
66 int sk_num;
67 double *distances = palloc_array(double, norderbys),
68 *distance = distances;
69
70 for (sk_num = 0; sk_num < norderbys; ++sk_num, ++orderbys, ++distance)
71 {
72 Point *point = DatumGetPointP(orderbys->sk_argument);
73
74 *distance = isLeaf ? point_point_distance(point, DatumGetPointP(key))
76 }
77
78 return distances;
79}
#define palloc_array(type, count)
Definition fe_memutils.h:76
static Point * DatumGetPointP(Datum X)
Definition geo_decls.h:175
static BOX * DatumGetBoxP(Datum X)
Definition geo_decls.h:233
#define point_point_distance(p1, p2)
Definition spgproc.c:25
static double point_box_distance(Point *point, BOX *box)
Definition spgproc.c:31

References DatumGetBoxP(), DatumGetPointP(), fb(), palloc_array, point_box_distance(), point_point_distance, and ScanKeyData::sk_argument.

Referenced by spg_box_quad_leaf_consistent(), spg_kd_inner_consistent(), spg_quad_inner_consistent(), and spg_quad_leaf_consistent().

◆ spgDeformLeafTuple()

void spgDeformLeafTuple ( SpGistLeafTuple  tup,
TupleDesc  tupleDescriptor,
Datum datums,
bool isnulls,
bool  keyColumnIsNull 
)
extern

Definition at line 1114 of file spgutils.c.

1116{
1118 char *tp; /* ptr to tuple data */
1119 bits8 *bp; /* ptr to null bitmap in tuple */
1120
1121 if (keyColumnIsNull && tupleDescriptor->natts == 1)
1122 {
1123 /*
1124 * Trivial case: there is only the key attribute and we're in a nulls
1125 * tree. The hasNullsMask bit in the tuple header should not be set
1126 * (and thus we can't use index_deform_tuple_internal), but
1127 * nonetheless the result is NULL.
1128 *
1129 * Note: currently this is dead code, because noplace calls this when
1130 * there is only the key attribute. But we should cover the case.
1131 */
1133
1134 datums[spgKeyColumn] = (Datum) 0;
1135 isnulls[spgKeyColumn] = true;
1136 return;
1137 }
1138
1139 tp = (char *) tup + SGLTHDRSZ(hasNullsMask);
1140 bp = (bits8 *) ((char *) tup + sizeof(SpGistLeafTupleData));
1141
1143 datums, isnulls,
1144 tp, bp, hasNullsMask);
1145
1146 /*
1147 * Key column isnull value from the tuple should be consistent with
1148 * keyColumnIsNull flag from the caller.
1149 */
1151}
#define Assert(condition)
Definition c.h:945
uint8 bits8
Definition c.h:625
void index_deform_tuple_internal(TupleDesc tupleDescriptor, Datum *values, bool *isnull, char *tp, bits8 *bp, int hasnulls)
Definition indextuple.c:387
#define SGLT_GET_HASNULLMASK(spgLeafTuple)
#define SGLTHDRSZ(hasnulls)

References Assert, fb(), index_deform_tuple_internal(), SGLT_GET_HASNULLMASK, SGLTHDRSZ, and spgKeyColumn.

Referenced by doPickSplit(), and storeGettuple().

◆ spgdoinsert()

bool spgdoinsert ( Relation  index,
SpGistState state,
const ItemPointerData heapPtr,
const Datum datums,
const bool isnulls 
)
extern

Definition at line 1909 of file spgdoinsert.c.

1911{
1912 bool result = true;
1913 TupleDesc leafDescriptor = state->leafTupDesc;
1914 bool isnull = isnulls[spgKeyColumn];
1915 int level = 0;
1917 int leafSize;
1918 int bestLeafSize;
1919 int numNoProgressCycles = 0;
1920 SPPageDesc current,
1921 parent;
1923
1924 /*
1925 * Look up FmgrInfo of the user-defined choose function once, to save
1926 * cycles in the loop below.
1927 */
1928 if (!isnull)
1930
1931 /*
1932 * Prepare the leaf datum to insert.
1933 *
1934 * If an optional "compress" method is provided, then call it to form the
1935 * leaf key datum from the input datum. Otherwise, store the input datum
1936 * as is. Since we don't use index_form_tuple in this AM, we have to make
1937 * sure value to be inserted is not toasted; FormIndexDatum doesn't
1938 * guarantee that. But we assume the "compress" method to return an
1939 * untoasted value.
1940 */
1941 if (!isnull)
1942 {
1944 {
1946
1950 index->rd_indcollation[spgKeyColumn],
1951 datums[spgKeyColumn]);
1952 }
1953 else
1954 {
1955 Assert(state->attLeafType.type == state->attType.type);
1956
1957 if (state->attType.attlen == -1)
1960 else
1962 }
1963 }
1964 else
1966
1967 /* Likewise, ensure that any INCLUDE values are not toasted */
1968 for (int i = spgFirstIncludeColumn; i < leafDescriptor->natts; i++)
1969 {
1970 if (!isnulls[i])
1971 {
1974 else
1975 leafDatums[i] = datums[i];
1976 }
1977 else
1978 leafDatums[i] = (Datum) 0;
1979 }
1980
1981 /*
1982 * Compute space needed for a leaf tuple containing the given data.
1983 */
1985 /* Account for an item pointer, too */
1986 leafSize += sizeof(ItemIdData);
1987
1988 /*
1989 * If it isn't gonna fit, and the opclass can't reduce the datum size by
1990 * suffixing, bail out now rather than doing a lot of useless work.
1991 */
1993 (isnull || !state->config.longValuesOK))
1994 ereport(ERROR,
1996 errmsg("index row size %zu exceeds maximum %zu for index \"%s\"",
1997 leafSize - sizeof(ItemIdData),
2000 errhint("Values larger than a buffer page cannot be indexed.")));
2002
2003 /* Initialize "current" to the appropriate root page */
2004 current.blkno = isnull ? SPGIST_NULL_BLKNO : SPGIST_ROOT_BLKNO;
2005 current.buffer = InvalidBuffer;
2006 current.page = NULL;
2007 current.offnum = FirstOffsetNumber;
2008 current.node = -1;
2009
2010 /* "parent" is invalid for the moment */
2011 parent.blkno = InvalidBlockNumber;
2012 parent.buffer = InvalidBuffer;
2013 parent.page = NULL;
2014 parent.offnum = InvalidOffsetNumber;
2015 parent.node = -1;
2016
2017 /*
2018 * Before entering the loop, try to clear any pending interrupt condition.
2019 * If a query cancel is pending, we might as well accept it now not later;
2020 * while if a non-canceling condition is pending, servicing it here avoids
2021 * having to restart the insertion and redo all the work so far.
2022 */
2024
2025 for (;;)
2026 {
2027 bool isNew = false;
2028
2029 /*
2030 * Bail out if query cancel is pending. We must have this somewhere
2031 * in the loop since a broken opclass could produce an infinite
2032 * picksplit loop. However, because we'll be holding buffer lock(s)
2033 * after the first iteration, ProcessInterrupts() wouldn't be able to
2034 * throw a cancel error here. Hence, if we see that an interrupt is
2035 * pending, break out of the loop and deal with the situation below.
2036 * Set result = false because we must restart the insertion if the
2037 * interrupt isn't a query-cancel-or-die case.
2038 */
2040 {
2041 result = false;
2042 break;
2043 }
2044
2045 if (current.blkno == InvalidBlockNumber)
2046 {
2047 /*
2048 * Create a leaf page. If leafSize is too large to fit on a page,
2049 * we won't actually use the page yet, but it simplifies the API
2050 * for doPickSplit to always have a leaf page at hand; so just
2051 * quietly limit our request to a page size.
2052 */
2053 current.buffer =
2055 GBUF_LEAF | (isnull ? GBUF_NULLS : 0),
2057 &isNew);
2058 current.blkno = BufferGetBlockNumber(current.buffer);
2059 }
2060 else if (parent.buffer == InvalidBuffer)
2061 {
2062 /* we hold no parent-page lock, so no deadlock is possible */
2063 current.buffer = ReadBuffer(index, current.blkno);
2065 }
2066 else if (current.blkno != parent.blkno)
2067 {
2068 /* descend to a new child page */
2069 current.buffer = ReadBuffer(index, current.blkno);
2070
2071 /*
2072 * Attempt to acquire lock on child page. We must beware of
2073 * deadlock against another insertion process descending from that
2074 * page to our parent page (see README). If we fail to get lock,
2075 * abandon the insertion and tell our caller to start over.
2076 *
2077 * XXX this could be improved, because failing to get lock on a
2078 * buffer is not proof of a deadlock situation; the lock might be
2079 * held by a reader, or even just background writer/checkpointer
2080 * process. Perhaps it'd be worth retrying after sleeping a bit?
2081 */
2082 if (!ConditionalLockBuffer(current.buffer))
2083 {
2084 ReleaseBuffer(current.buffer);
2086 return false;
2087 }
2088 }
2089 else
2090 {
2091 /* inner tuple can be stored on the same page as parent one */
2092 current.buffer = parent.buffer;
2093 }
2094 current.page = BufferGetPage(current.buffer);
2095
2096 /* should not arrive at a page of the wrong type */
2097 if (isnull ? !SpGistPageStoresNulls(current.page) :
2098 SpGistPageStoresNulls(current.page))
2099 elog(ERROR, "SPGiST index page %u has wrong nulls flag",
2100 current.blkno);
2101
2102 if (SpGistPageIsLeaf(current.page))
2103 {
2104 SpGistLeafTuple leafTuple;
2105 int nToSplit,
2107
2108 leafTuple = spgFormLeafTuple(state, heapPtr, leafDatums, isnulls);
2109 if (leafTuple->size + sizeof(ItemIdData) <=
2110 SpGistPageGetFreeSpace(current.page, 1))
2111 {
2112 /* it fits on page, so insert it and we're done */
2113 addLeafTuple(index, state, leafTuple,
2114 &current, &parent, isnull, isNew);
2115 break;
2116 }
2117 else if ((sizeToSplit =
2118 checkSplitConditions(index, state, &current,
2119 &nToSplit)) < SPGIST_PAGE_CAPACITY / 2 &&
2120 nToSplit < 64 &&
2121 leafTuple->size + sizeof(ItemIdData) + sizeToSplit <= SPGIST_PAGE_CAPACITY)
2122 {
2123 /*
2124 * the amount of data is pretty small, so just move the whole
2125 * chain to another leaf page rather than splitting it.
2126 */
2127 Assert(!isNew);
2128 moveLeafs(index, state, &current, &parent, leafTuple, isnull);
2129 break; /* we're done */
2130 }
2131 else
2132 {
2133 /* picksplit */
2134 if (doPickSplit(index, state, &current, &parent,
2135 leafTuple, level, isnull, isNew))
2136 break; /* doPickSplit installed new tuples */
2137
2138 /* leaf tuple will not be inserted yet */
2139 pfree(leafTuple);
2140
2141 /*
2142 * current now describes new inner tuple, go insert into it
2143 */
2144 Assert(!SpGistPageIsLeaf(current.page));
2146 }
2147 }
2148 else /* non-leaf page */
2149 {
2150 /*
2151 * Apply the opclass choose function to figure out how to insert
2152 * the given datum into the current inner tuple.
2153 */
2155 spgChooseIn in;
2156 spgChooseOut out;
2157
2158 /*
2159 * spgAddNode and spgSplitTuple cases will loop back to here to
2160 * complete the insertion operation. Just in case the choose
2161 * function is broken and produces add or split requests
2162 * repeatedly, check for query cancel (see comments above).
2163 */
2166 {
2167 result = false;
2168 break;
2169 }
2170
2172 PageGetItemId(current.page, current.offnum));
2173
2174 in.datum = datums[spgKeyColumn];
2176 in.level = level;
2177 in.allTheSame = innerTuple->allTheSame;
2178 in.hasPrefix = (innerTuple->prefixSize > 0);
2180 in.nNodes = innerTuple->nNodes;
2182
2183 memset(&out, 0, sizeof(out));
2184
2185 if (!isnull)
2186 {
2187 /* use user-defined choose method */
2189 index->rd_indcollation[0],
2190 PointerGetDatum(&in),
2191 PointerGetDatum(&out));
2192 }
2193 else
2194 {
2195 /* force "match" action (to insert to random subnode) */
2197 }
2198
2199 if (innerTuple->allTheSame)
2200 {
2201 /*
2202 * It's not allowed to do an AddNode at an allTheSame tuple.
2203 * Opclass must say "match", in which case we choose a random
2204 * one of the nodes to descend into, or "split".
2205 */
2206 if (out.resultType == spgAddNode)
2207 elog(ERROR, "cannot add a node to an allTheSame inner tuple");
2208 else if (out.resultType == spgMatchNode)
2209 out.result.matchNode.nodeN =
2211 0, innerTuple->nNodes - 1);
2212 }
2213
2214 switch (out.resultType)
2215 {
2216 case spgMatchNode:
2217 /* Descend to N'th child node */
2219 &current, &parent,
2220 out.result.matchNode.nodeN);
2221 /* Adjust level as per opclass request */
2222 level += out.result.matchNode.levelAdd;
2223 /* Replace leafDatum and recompute leafSize */
2224 if (!isnull)
2225 {
2229 leafSize += sizeof(ItemIdData);
2230 }
2231
2232 /*
2233 * Check new tuple size; fail if it can't fit, unless the
2234 * opclass says it can handle the situation by suffixing.
2235 *
2236 * However, the opclass can only shorten the leaf datum,
2237 * which may not be enough to ever make the tuple fit,
2238 * since INCLUDE columns might alone use more than a page.
2239 * Depending on the opclass' behavior, that could lead to
2240 * an infinite loop --- spgtextproc.c, for example, will
2241 * just repeatedly generate an empty-string leaf datum
2242 * once it runs out of data. Actual bugs in opclasses
2243 * might cause infinite looping, too. To detect such a
2244 * loop, check to see if we are making progress by
2245 * reducing the leafSize in each pass. This is a bit
2246 * tricky though. Because of alignment considerations,
2247 * the total tuple size might not decrease on every pass.
2248 * Also, there are edge cases where the choose method
2249 * might seem to not make progress for a cycle or two.
2250 * Somewhat arbitrarily, we allow up to 10 no-progress
2251 * iterations before failing. (This limit should be more
2252 * than MAXALIGN, to accommodate opclasses that trim one
2253 * byte from the leaf datum per pass.)
2254 */
2256 {
2257 bool ok = false;
2258
2259 if (state->config.longValuesOK && !isnull)
2260 {
2261 if (leafSize < bestLeafSize)
2262 {
2263 ok = true;
2266 }
2267 else if (++numNoProgressCycles < 10)
2268 ok = true;
2269 }
2270 if (!ok)
2271 ereport(ERROR,
2273 errmsg("index row size %zu exceeds maximum %zu for index \"%s\"",
2274 leafSize - sizeof(ItemIdData),
2277 errhint("Values larger than a buffer page cannot be indexed.")));
2278 }
2279
2280 /*
2281 * Loop around and attempt to insert the new leafDatum at
2282 * "current" (which might reference an existing child
2283 * tuple, or might be invalid to force us to find a new
2284 * page for the tuple).
2285 */
2286 break;
2287 case spgAddNode:
2288 /* AddNode is not sensible if nodes don't have labels */
2289 if (in.nodeLabels == NULL)
2290 elog(ERROR, "cannot add a node to an inner tuple without node labels");
2291 /* Add node to inner tuple, per request */
2293 &current, &parent,
2294 out.result.addNode.nodeN,
2296
2297 /*
2298 * Retry insertion into the enlarged node. We assume that
2299 * we'll get a MatchNode result this time.
2300 */
2302 break;
2303 case spgSplitTuple:
2304 /* Split inner tuple, per request */
2306 &current, &out);
2307
2308 /* Retry insertion into the split node */
2310 break;
2311 default:
2312 elog(ERROR, "unrecognized SPGiST choose result: %d",
2313 (int) out.resultType);
2314 break;
2315 }
2316 }
2317 } /* end loop */
2318
2319 /*
2320 * Release any buffers we're still holding. Beware of possibility that
2321 * current and parent reference same buffer.
2322 */
2323 if (current.buffer != InvalidBuffer)
2324 {
2326 UnlockReleaseBuffer(current.buffer);
2327 }
2328 if (parent.buffer != InvalidBuffer &&
2329 parent.buffer != current.buffer)
2330 {
2333 }
2334
2335 /*
2336 * We do not support being called while some outer function is holding a
2337 * buffer lock (or any other reason to postpone query cancels). If that
2338 * were the case, telling the caller to retry would create an infinite
2339 * loop.
2340 */
2342
2343 /*
2344 * Finally, check for interrupts again. If there was a query cancel,
2345 * ProcessInterrupts() will be able to throw the error here. If it was
2346 * some other kind of interrupt that can just be cleared, return false to
2347 * tell our caller to retry.
2348 */
2350
2351 return result;
2352}
#define InvalidBlockNumber
Definition block.h:33
#define InvalidBuffer
Definition buf.h:25
BlockNumber BufferGetBlockNumber(Buffer buffer)
Definition bufmgr.c:4357
bool ConditionalLockBuffer(Buffer buffer)
Definition bufmgr.c:6484
void ReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5505
void UnlockReleaseBuffer(Buffer buffer)
Definition bufmgr.c:5522
Buffer ReadBuffer(Relation reln, BlockNumber blockNum)
Definition bufmgr.c:874
static Page BufferGetPage(Buffer buffer)
Definition bufmgr.h:470
@ BUFFER_LOCK_EXCLUSIVE
Definition bufmgr.h:220
static void LockBuffer(Buffer buffer, BufferLockMode mode)
Definition bufmgr.h:332
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition bufpage.h:269
static void * PageGetItem(PageData *page, const ItemIdData *itemId)
Definition bufpage.h:379
#define Min(x, y)
Definition c.h:1093
#define OidIsValid(objectId)
Definition c.h:860
int errcode(int sqlerrcode)
Definition elog.c:874
int errhint(const char *fmt,...) pg_attribute_printf(1
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define ereport(elevel,...)
Definition elog.h:150
Datum FunctionCall2Coll(FmgrInfo *flinfo, Oid collation, Datum arg1, Datum arg2)
Definition fmgr.c:1151
Datum FunctionCall1Coll(FmgrInfo *flinfo, Oid collation, Datum arg1)
Definition fmgr.c:1131
#define PG_DETOAST_DATUM(datum)
Definition fmgr.h:240
FmgrInfo * index_getprocinfo(Relation irel, AttrNumber attnum, uint16 procnum)
Definition indexam.c:917
RegProcedure index_getprocid(Relation irel, AttrNumber attnum, uint16 procnum)
Definition indexam.c:883
void pfree(void *pointer)
Definition mcxt.c:1616
#define INTERRUPTS_CAN_BE_PROCESSED()
Definition miscadmin.h:130
#define CHECK_FOR_INTERRUPTS()
Definition miscadmin.h:123
#define INTERRUPTS_PENDING_CONDITION()
Definition miscadmin.h:113
static char * errmsg
#define InvalidOffsetNumber
Definition off.h:26
#define FirstOffsetNumber
Definition off.h:27
#define INDEX_MAX_KEYS
uint64 pg_prng_uint64_range(pg_prng_state *state, uint64 rmin, uint64 rmax)
Definition pg_prng.c:144
pg_prng_state pg_global_prng_state
Definition pg_prng.c:34
#define RelationGetRelationName(relation)
Definition rel.h:548
static void spgAddNodeAction(Relation index, SpGistState *state, SpGistInnerTuple innerTuple, SPPageDesc *current, SPPageDesc *parent, int nodeN, Datum nodeLabel)
static void spgSplitNodeAction(Relation index, SpGistState *state, SpGistInnerTuple innerTuple, SPPageDesc *current, spgChooseOut *out)
static void spgMatchNodeAction(Relation index, SpGistState *state, SpGistInnerTuple innerTuple, SPPageDesc *current, SPPageDesc *parent, int nodeN)
static bool doPickSplit(Relation index, SpGistState *state, SPPageDesc *current, SPPageDesc *parent, SpGistLeafTuple newLeafTuple, int level, bool isNulls, bool isNew)
static void moveLeafs(Relation index, SpGistState *state, SPPageDesc *current, SPPageDesc *parent, SpGistLeafTuple newLeafTuple, bool isNulls)
static int checkSplitConditions(Relation index, SpGistState *state, SPPageDesc *current, int *nToSplit)
static void addLeafTuple(Relation index, SpGistState *state, SpGistLeafTuple leafTuple, SPPageDesc *current, SPPageDesc *parent, bool isNulls, bool isNew)
#define SPGIST_COMPRESS_PROC
Definition spgist.h:28
#define SPGIST_CHOOSE_PROC
Definition spgist.h:24
@ spgMatchNode
Definition spgist.h:69
@ spgAddNode
Definition spgist.h:70
@ spgSplitTuple
Definition spgist.h:71
#define SPGIST_NULL_BLKNO
#define GBUF_NULLS
SpGistInnerTupleData * SpGistInnerTuple
#define SpGistPageStoresNulls(page)
#define GBUF_LEAF
#define spgFirstIncludeColumn
#define SpGistPageIsLeaf(page)
#define SPGIST_PAGE_CAPACITY
#define SPGIST_ROOT_BLKNO
#define SpGistPageGetFreeSpace(p, n)
Datum * spgExtractNodeLabels(SpGistState *state, SpGistInnerTuple innerTuple)
Definition spgutils.c:1159
SpGistLeafTuple spgFormLeafTuple(SpGistState *state, const ItemPointerData *heapPtr, const Datum *datums, const bool *isnulls)
Definition spgutils.c:870
Buffer SpGistGetBuffer(Relation index, int flags, int needSpace, bool *isNew)
Definition spgutils.c:568
void SpGistSetLastUsedPage(Relation index, Buffer buffer)
Definition spgutils.c:672
Size SpGistGetLeafTupleSize(TupleDesc tupleDescriptor, const Datum *datums, const bool *isnulls)
Definition spgutils.c:817
Buffer buffer
Definition spgdoinsert.c:39
OffsetNumber offnum
Definition spgdoinsert.c:41
BlockNumber blkno
Definition spgdoinsert.c:38
Datum * nodeLabels
Definition spgist.h:64
bool hasPrefix
Definition spgist.h:61
Datum prefixDatum
Definition spgist.h:62
int nNodes
Definition spgist.h:63
Datum datum
Definition spgist.h:55
int level
Definition spgist.h:57
Datum leafDatum
Definition spgist.h:56
bool allTheSame
Definition spgist.h:60
union spgChooseOut::@56 result
spgChooseResultType resultType
Definition spgist.h:76
int levelAdd
Definition spgist.h:82
struct spgChooseOut::@56::@58 addNode
Datum nodeLabel
Definition spgist.h:87
struct spgChooseOut::@56::@57 matchNode
Datum restDatum
Definition spgist.h:83
int nodeN
Definition spgist.h:81
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:193

References addLeafTuple(), spgChooseOut::addNode, spgChooseIn::allTheSame, Assert, CompactAttribute::attlen, SPPageDesc::blkno, SPPageDesc::buffer, BUFFER_LOCK_EXCLUSIVE, BufferGetBlockNumber(), BufferGetPage(), CHECK_FOR_INTERRUPTS, checkSplitConditions(), ConditionalLockBuffer(), spgChooseIn::datum, doPickSplit(), elog, ereport, errcode(), errhint(), errmsg, ERROR, fb(), FirstOffsetNumber, FunctionCall1Coll(), FunctionCall2Coll(), GBUF_LEAF, GBUF_NULLS, spgChooseIn::hasPrefix, i, index_getprocid(), index_getprocinfo(), INDEX_MAX_KEYS, INTERRUPTS_CAN_BE_PROCESSED, INTERRUPTS_PENDING_CONDITION, InvalidBlockNumber, InvalidBuffer, InvalidOffsetNumber, spgChooseIn::leafDatum, spgChooseIn::level, spgChooseOut::levelAdd, LockBuffer(), spgChooseOut::matchNode, Min, moveLeafs(), spgChooseIn::nNodes, SPPageDesc::node, spgChooseOut::nodeLabel, spgChooseIn::nodeLabels, spgChooseOut::nodeN, SPPageDesc::offnum, OidIsValid, SPPageDesc::page, PageGetItem(), PageGetItemId(), pfree(), PG_DETOAST_DATUM, pg_global_prng_state, pg_prng_uint64_range(), PointerGetDatum(), spgChooseIn::prefixDatum, ReadBuffer(), RelationGetRelationName, ReleaseBuffer(), spgChooseOut::restDatum, spgChooseOut::result, spgChooseOut::resultType, SGITDATUM, SpGistLeafTupleData::size, spgAddNode, spgAddNodeAction(), spgExtractNodeLabels(), spgFirstIncludeColumn, spgFormLeafTuple(), SPGIST_CHOOSE_PROC, SPGIST_COMPRESS_PROC, SPGIST_NULL_BLKNO, SPGIST_PAGE_CAPACITY, SPGIST_ROOT_BLKNO, SpGistGetBuffer(), SpGistGetLeafTupleSize(), SpGistPageGetFreeSpace, SpGistPageIsLeaf, SpGistPageStoresNulls, SpGistSetLastUsedPage(), spgKeyColumn, spgMatchNode, spgMatchNodeAction(), spgSplitNodeAction(), spgSplitTuple, TupleDescCompactAttr(), and UnlockReleaseBuffer().

Referenced by spginsert(), and spgistBuildCallback().

◆ spgExtractNodeLabels()

Datum * spgExtractNodeLabels ( SpGistState state,
SpGistInnerTuple  innerTuple 
)
extern

Definition at line 1159 of file spgutils.c.

1160{
1161 Datum *nodeLabels;
1162 int i;
1163 SpGistNodeTuple node;
1164
1165 /* Either all the labels must be NULL, or none. */
1166 node = SGITNODEPTR(innerTuple);
1167 if (IndexTupleHasNulls(node))
1168 {
1169 SGITITERATE(innerTuple, i, node)
1170 {
1171 if (!IndexTupleHasNulls(node))
1172 elog(ERROR, "some but not all node labels are null in SPGiST inner tuple");
1173 }
1174 /* They're all null, so just return NULL */
1175 return NULL;
1176 }
1177 else
1178 {
1179 nodeLabels = palloc_array(Datum, innerTuple->nNodes);
1180 SGITITERATE(innerTuple, i, node)
1181 {
1182 if (IndexTupleHasNulls(node))
1183 elog(ERROR, "some but not all node labels are null in SPGiST inner tuple");
1184 nodeLabels[i] = SGNTDATUM(node, state);
1185 }
1186 return nodeLabels;
1187 }
1188}
static bool IndexTupleHasNulls(const IndexTupleData *itup)
Definition itup.h:77
#define SGITITERATE(x, i, nt)
#define SGNTDATUM(x, s)

References elog, ERROR, fb(), i, IndexTupleHasNulls(), palloc_array, SGITITERATE, SGITNODEPTR, and SGNTDATUM.

Referenced by spgdoinsert(), and spgInitInnerConsistentIn().

◆ spgFormDeadTuple()

SpGistDeadTuple spgFormDeadTuple ( SpGistState state,
int  tupstate,
BlockNumber  blkno,
OffsetNumber  offnum 
)
extern

Definition at line 1084 of file spgutils.c.

1086{
1087 SpGistDeadTuple tuple = (SpGistDeadTuple) state->deadTupleStorage;
1088
1089 tuple->tupstate = tupstate;
1090 tuple->size = SGDTSIZE;
1092
1093 if (tupstate == SPGIST_REDIRECT)
1094 {
1095 ItemPointerSet(&tuple->pointer, blkno, offnum);
1096 tuple->xid = state->redirectXid;
1097 }
1098 else
1099 {
1100 ItemPointerSetInvalid(&tuple->pointer);
1101 tuple->xid = InvalidTransactionId;
1102 }
1103
1104 return tuple;
1105}
static void ItemPointerSet(ItemPointerData *pointer, BlockNumber blockNumber, OffsetNumber offNum)
Definition itemptr.h:135
static void ItemPointerSetInvalid(ItemPointerData *pointer)
Definition itemptr.h:184
SpGistDeadTupleData * SpGistDeadTuple
#define SPGIST_REDIRECT
#define InvalidTransactionId
Definition transam.h:31

References InvalidOffsetNumber, InvalidTransactionId, ItemPointerSet(), ItemPointerSetInvalid(), SpGistDeadTupleData::pointer, SGDTSIZE, SGLT_SET_NEXTOFFSET, SpGistDeadTupleData::size, SPGIST_REDIRECT, SpGistDeadTupleData::tupstate, and SpGistDeadTupleData::xid.

Referenced by spgAddNodeAction(), spgPageIndexMultiDelete(), and spgRedoAddNode().

◆ spgFormInnerTuple()

SpGistInnerTuple spgFormInnerTuple ( SpGistState state,
bool  hasPrefix,
Datum  prefix,
int  nNodes,
SpGistNodeTuple nodes 
)
extern

Definition at line 1001 of file spgutils.c.

1003{
1005 unsigned int size;
1006 unsigned int prefixSize;
1007 int i;
1008 char *ptr;
1009
1010 /* Compute size needed */
1011 if (hasPrefix)
1012 prefixSize = SpGistGetInnerTypeSize(&state->attPrefixType, prefix);
1013 else
1014 prefixSize = 0;
1015
1016 size = SGITHDRSZ + prefixSize;
1017
1018 /* Note: we rely on node tuple sizes to be maxaligned already */
1019 for (i = 0; i < nNodes; i++)
1020 size += IndexTupleSize(nodes[i]);
1021
1022 /*
1023 * Ensure that we can replace the tuple with a dead tuple later. This
1024 * test is unnecessary given current tuple layouts, but let's be safe.
1025 */
1026 if (size < SGDTSIZE)
1027 size = SGDTSIZE;
1028
1029 /*
1030 * Inner tuple should be small enough to fit on a page
1031 */
1032 if (size > SPGIST_PAGE_CAPACITY - sizeof(ItemIdData))
1033 ereport(ERROR,
1035 errmsg("SP-GiST inner tuple size %zu exceeds maximum %zu",
1036 (Size) size,
1038 errhint("Values larger than a buffer page cannot be indexed.")));
1039
1040 /*
1041 * Check for overflow of header fields --- probably can't fail if the
1042 * above succeeded, but let's be paranoid
1043 */
1044 if (size > SGITMAXSIZE ||
1045 prefixSize > SGITMAXPREFIXSIZE ||
1046 nNodes > SGITMAXNNODES)
1047 elog(ERROR, "SPGiST inner tuple header field is too small");
1048
1049 /* OK, form the tuple */
1050 tup = (SpGistInnerTuple) palloc0(size);
1051
1052 tup->nNodes = nNodes;
1053 tup->prefixSize = prefixSize;
1054 tup->size = size;
1055
1056 if (hasPrefix)
1057 memcpyInnerDatum(SGITDATAPTR(tup), &state->attPrefixType, prefix);
1058
1059 ptr = (char *) SGITNODEPTR(tup);
1060
1061 for (i = 0; i < nNodes; i++)
1062 {
1063 SpGistNodeTuple node = nodes[i];
1064
1065 memcpy(ptr, node, IndexTupleSize(node));
1066 ptr += IndexTupleSize(node);
1067 }
1068
1069 return tup;
1070}
size_t Size
Definition c.h:691
#define SGITDATAPTR(x)
#define SGITMAXSIZE
#define SGITMAXPREFIXSIZE
#define SGITHDRSZ
#define SGITMAXNNODES
static void memcpyInnerDatum(void *target, SpGistTypeDesc *att, Datum datum)
Definition spgutils.c:796
unsigned int SpGistGetInnerTypeSize(SpGistTypeDesc *att, Datum datum)
Definition spgutils.c:778

References elog, ereport, errcode(), errhint(), errmsg, ERROR, fb(), i, IndexTupleSize(), memcpyInnerDatum(), palloc0(), SGDTSIZE, SGITDATAPTR, SGITHDRSZ, SGITMAXNNODES, SGITMAXPREFIXSIZE, SGITMAXSIZE, SGITNODEPTR, SPGIST_PAGE_CAPACITY, and SpGistGetInnerTypeSize().

Referenced by addNode(), doPickSplit(), and spgSplitNodeAction().

◆ spgFormLeafTuple()

SpGistLeafTuple spgFormLeafTuple ( SpGistState state,
const ItemPointerData heapPtr,
const Datum datums,
const bool isnulls 
)
extern

Definition at line 870 of file spgutils.c.

872{
874 TupleDesc tupleDescriptor = state->leafTupDesc;
875 Size size;
876 Size hoff;
878 bool needs_null_mask = false;
879 int natts = tupleDescriptor->natts;
880 char *tp; /* ptr to tuple data */
881 uint16 tupmask = 0; /* unused heap_fill_tuple output */
882
883 /*
884 * Decide whether we need a nulls bitmask.
885 *
886 * If there is only a key attribute (natts == 1), never use a bitmask, for
887 * compatibility with the pre-v14 layout of leaf tuples. Otherwise, we
888 * need one if any attribute is null.
889 */
890 if (natts > 1)
891 {
892 for (int i = 0; i < natts; i++)
893 {
894 if (isnulls[i])
895 {
896 needs_null_mask = true;
897 break;
898 }
899 }
900 }
901
902 /*
903 * Calculate size of the data part; same as for heap tuples.
904 */
906
907 /*
908 * Compute total size.
909 */
911 size = hoff + data_size;
912 size = MAXALIGN(size);
913
914 /*
915 * Ensure that we can replace the tuple with a dead tuple later. This test
916 * is unnecessary when there are any non-null attributes, but be safe.
917 */
918 if (size < SGDTSIZE)
919 size = SGDTSIZE;
920
921 /* OK, form the tuple */
922 tup = (SpGistLeafTuple) palloc0(size);
923
924 tup->size = size;
926 tup->heapPtr = *heapPtr;
927
928 tp = (char *) tup + hoff;
929
930 if (needs_null_mask)
931 {
932 bits8 *bp; /* ptr to null bitmap in tuple */
933
934 /* Set nullmask presence bit in SpGistLeafTuple header */
936 /* Fill the data area and null mask */
937 bp = (bits8 *) ((char *) tup + sizeof(SpGistLeafTupleData));
939 &tupmask, bp);
940 }
941 else if (natts > 1 || !isnulls[spgKeyColumn])
942 {
943 /* Fill data area only */
945 &tupmask, (bits8 *) NULL);
946 }
947 /* otherwise we have no data, nor a bitmap, to fill */
948
949 return tup;
950}
uint16_t uint16
Definition c.h:617
Size heap_compute_data_size(TupleDesc tupleDesc, const Datum *values, const bool *isnull)
Definition heaptuple.c:219
void heap_fill_tuple(TupleDesc tupleDesc, const Datum *values, const bool *isnull, char *data, Size data_size, uint16 *infomask, bits8 *bit)
Definition heaptuple.c:401
#define SGLT_SET_HASNULLMASK(spgLeafTuple, hasnulls)
struct SpGistLeafTupleData * SpGistLeafTuple

References fb(), heap_compute_data_size(), heap_fill_tuple(), i, InvalidOffsetNumber, MAXALIGN, palloc0(), SGDTSIZE, SGLT_SET_HASNULLMASK, SGLT_SET_NEXTOFFSET, SGLTHDRSZ, and spgKeyColumn.

Referenced by doPickSplit(), and spgdoinsert().

◆ spgFormNodeTuple()

SpGistNodeTuple spgFormNodeTuple ( SpGistState state,
Datum  label,
bool  isnull 
)
extern

Definition at line 959 of file spgutils.c.

960{
962 unsigned int size;
963 unsigned short infomask = 0;
964
965 /* compute space needed (note result is already maxaligned) */
966 size = SGNTHDRSZ;
967 if (!isnull)
968 size += SpGistGetInnerTypeSize(&state->attLabelType, label);
969
970 /*
971 * Here we make sure that the size will fit in the field reserved for it
972 * in t_info.
973 */
974 if ((size & INDEX_SIZE_MASK) != size)
977 errmsg("index row requires %zu bytes, maximum size is %zu",
978 (Size) size, (Size) INDEX_SIZE_MASK)));
979
980 tup = (SpGistNodeTuple) palloc0(size);
981
982 if (isnull)
984 /* we don't bother setting the INDEX_VAR_MASK bit */
985 infomask |= size;
986 tup->t_info = infomask;
987
988 /* The TID field will be filled in later */
989 ItemPointerSetInvalid(&tup->t_tid);
990
991 if (!isnull)
992 memcpyInnerDatum(SGNTDATAPTR(tup), &state->attLabelType, label);
993
994 return tup;
995}
#define INDEX_NULL_MASK
Definition itup.h:68
#define INDEX_SIZE_MASK
Definition itup.h:65
static char * label
#define SGNTHDRSZ

References ereport, errcode(), errmsg, ERROR, fb(), INDEX_NULL_MASK, INDEX_SIZE_MASK, ItemPointerSetInvalid(), label, memcpyInnerDatum(), palloc0(), SGNTDATAPTR, SGNTHDRSZ, and SpGistGetInnerTypeSize().

Referenced by addNode(), doPickSplit(), and spgSplitNodeAction().

◆ spgGetCache()

SpGistCache * spgGetCache ( Relation  index)
extern

Definition at line 189 of file spgutils.c.

190{
191 SpGistCache *cache;
192
193 if (index->rd_amcache == NULL)
194 {
195 Oid atttype;
196 spgConfigIn in;
198
199 cache = MemoryContextAllocZero(index->rd_indexcxt,
200 sizeof(SpGistCache));
201
202 /* SPGiST must have one key column and can also have INCLUDE columns */
205
206 /*
207 * Get the actual (well, nominal) data type of the key column. We
208 * pass this to the opclass config function so that polymorphic
209 * opclasses are possible.
210 */
211 atttype = GetIndexInputType(index, spgKeyColumn + 1);
212
213 /* Call the config function to get config info for the opclass */
214 in.attType = atttype;
215
218 index->rd_indcollation[spgKeyColumn],
219 PointerGetDatum(&in),
220 PointerGetDatum(&cache->config));
221
222 /*
223 * If leafType isn't specified, use the declared index column type,
224 * which index.c will have derived from the opclass's opcintype.
225 * (Although we now make spgvalidate.c warn if these aren't the same,
226 * old user-defined opclasses may not set the STORAGE parameter
227 * correctly, so believe leafType if it's given.)
228 */
229 if (!OidIsValid(cache->config.leafType))
230 {
231 cache->config.leafType =
233
234 /*
235 * If index column type is binary-coercible to atttype (for
236 * example, it's a domain over atttype), treat it as plain atttype
237 * to avoid thinking we need to compress.
238 */
239 if (cache->config.leafType != atttype &&
240 IsBinaryCoercible(cache->config.leafType, atttype))
241 cache->config.leafType = atttype;
242 }
243
244 /* Get the information we need about each relevant datatype */
245 fillTypeDesc(&cache->attType, atttype);
246
247 if (cache->config.leafType != atttype)
248 {
252 errmsg("compress method must be defined when leaf type is different from input type")));
253
254 fillTypeDesc(&cache->attLeafType, cache->config.leafType);
255 }
256 else
257 {
258 /* Save lookups in this common case */
259 cache->attLeafType = cache->attType;
260 }
261
263 fillTypeDesc(&cache->attLabelType, cache->config.labelType);
264
265 /*
266 * Finally, if it's a real index (not a partitioned one), get the
267 * lastUsedPages data from the metapage
268 */
269 if (index->rd_rel->relkind != RELKIND_PARTITIONED_INDEX)
270 {
272 SpGistMetaPageData *metadata;
273
276
278
279 if (metadata->magicNumber != SPGIST_MAGIC_NUMBER)
280 elog(ERROR, "index \"%s\" is not an SP-GiST index",
282
283 cache->lastUsedPages = metadata->lastUsedPages;
284
286 }
287
288 index->rd_amcache = cache;
289 }
290 else
291 {
292 /* assume it's up to date */
293 cache = (SpGistCache *) index->rd_amcache;
294 }
295
296 return cache;
297}
int Buffer
Definition buf.h:23
@ BUFFER_LOCK_SHARE
Definition bufmgr.h:210
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1266
bool IsBinaryCoercible(Oid srctype, Oid targettype)
unsigned int Oid
#define IndexRelationGetNumberOfAttributes(relation)
Definition rel.h:526
#define IndexRelationGetNumberOfKeyAttributes(relation)
Definition rel.h:533
#define SPGIST_CONFIG_PROC
Definition spgist.h:23
#define SpGistPageGetMeta(p)
#define SPGIST_METAPAGE_BLKNO
#define SPGIST_MAGIC_NUMBER
static Oid GetIndexInputType(Relation index, AttrNumber indexcol)
Definition spgutils.c:122
static void fillTypeDesc(SpGistTypeDesc *desc, Oid type)
Definition spgutils.c:167
SpGistLUPCache lastUsedPages
SpGistLUPCache lastUsedPages
Oid attType
Definition spgist.h:38
Oid leafType
Definition spgist.h:45
Oid labelType
Definition spgist.h:44
Oid prefixType
Definition spgist.h:43

References Assert, SpGistCache::attLabelType, SpGistCache::attLeafType, SpGistCache::attPrefixType, spgConfigIn::attType, SpGistCache::attType, BUFFER_LOCK_SHARE, BufferGetPage(), SpGistCache::config, elog, ereport, errcode(), errmsg, ERROR, fb(), fillTypeDesc(), FunctionCall2Coll(), GetIndexInputType(), index_getprocid(), index_getprocinfo(), INDEX_MAX_KEYS, IndexRelationGetNumberOfAttributes, IndexRelationGetNumberOfKeyAttributes, IsBinaryCoercible(), spgConfigOut::labelType, SpGistMetaPageData::lastUsedPages, SpGistCache::lastUsedPages, spgConfigOut::leafType, LockBuffer(), SpGistMetaPageData::magicNumber, MemoryContextAllocZero(), OidIsValid, PointerGetDatum(), spgConfigOut::prefixType, ReadBuffer(), RelationGetDescr, RelationGetRelationName, SPGIST_COMPRESS_PROC, SPGIST_CONFIG_PROC, SPGIST_MAGIC_NUMBER, SPGIST_METAPAGE_BLKNO, SpGistPageGetMeta, spgKeyColumn, TupleDescAttr(), and UnlockReleaseBuffer().

Referenced by allocNewBuffer(), initSpGistState(), spgcanreturn(), SpGistGetBuffer(), and SpGistSetLastUsedPage().

◆ SpGistGetBuffer()

Buffer SpGistGetBuffer ( Relation  index,
int  flags,
int  needSpace,
bool isNew 
)
extern

Definition at line 568 of file spgutils.c.

569{
570 SpGistCache *cache = spgGetCache(index);
572
573 /* Bail out if even an empty page wouldn't meet the demand */
575 elog(ERROR, "desired SPGiST tuple size is too big");
576
577 /*
578 * If possible, increase the space request to include relation's
579 * fillfactor. This ensures that when we add unrelated tuples to a page,
580 * we try to keep 100-fillfactor% available for adding tuples that are
581 * related to the ones already on it. But fillfactor mustn't cause an
582 * error for requests that would otherwise be legal.
583 */
586
587 /* Get the cache entry for this flags setting */
588 lup = GET_LUP(cache, flags);
589
590 /* If we have nothing cached, just turn it over to allocNewBuffer */
591 if (lup->blkno == InvalidBlockNumber)
592 {
593 *isNew = true;
594 return allocNewBuffer(index, flags);
595 }
596
597 /* fixed pages should never be in cache */
598 Assert(!SpGistBlockIsFixed(lup->blkno));
599
600 /* If cached freeSpace isn't enough, don't bother looking at the page */
601 if (lup->freeSpace >= needSpace)
602 {
603 Buffer buffer;
604 Page page;
605
606 buffer = ReadBuffer(index, lup->blkno);
607
608 if (!ConditionalLockBuffer(buffer))
609 {
610 /*
611 * buffer is locked by another process, so return a new buffer
612 */
613 ReleaseBuffer(buffer);
614 *isNew = true;
615 return allocNewBuffer(index, flags);
616 }
617
618 page = BufferGetPage(buffer);
619
620 if (PageIsNew(page) || SpGistPageIsDeleted(page) || PageIsEmpty(page))
621 {
622 /* OK to initialize the page */
623 uint16 pageflags = 0;
624
625 if (GBUF_REQ_LEAF(flags))
627 if (GBUF_REQ_NULLS(flags))
630 lup->freeSpace = PageGetExactFreeSpace(page) - needSpace;
631 *isNew = true;
632 return buffer;
633 }
634
635 /*
636 * Check that page is of right type and has enough space. We must
637 * recheck this since our cache isn't necessarily up to date.
638 */
639 if ((GBUF_REQ_LEAF(flags) ? SpGistPageIsLeaf(page) : !SpGistPageIsLeaf(page)) &&
641 {
642 int freeSpace = PageGetExactFreeSpace(page);
643
644 if (freeSpace >= needSpace)
645 {
646 /* Success, update freespace info and return the buffer */
647 lup->freeSpace = freeSpace - needSpace;
648 *isNew = false;
649 return buffer;
650 }
651 }
652
653 /*
654 * fallback to allocation of new buffer
655 */
656 UnlockReleaseBuffer(buffer);
657 }
658
659 /* No success with cache, so return a new buffer */
660 *isNew = true;
661 return allocNewBuffer(index, flags);
662}
static bool PageIsEmpty(const PageData *page)
Definition bufpage.h:249
static bool PageIsNew(const PageData *page)
Definition bufpage.h:259
PageData * Page
Definition bufpage.h:81
#define SpGistGetTargetPageFreeSpace(relation)
#define SpGistPageIsDeleted(page)
#define SPGIST_NULLS
#define GBUF_REQ_NULLS(flags)
#define SpGistBlockIsFixed(blkno)
#define GBUF_REQ_LEAF(flags)
#define SPGIST_LEAF
void SpGistInitBuffer(Buffer b, uint16 f)
Definition spgutils.c:721
static Buffer allocNewBuffer(Relation index, int flags)
Definition spgutils.c:512
#define GET_LUP(c, f)
Definition spgutils.c:489

References allocNewBuffer(), Assert, BufferGetPage(), ConditionalLockBuffer(), elog, ERROR, fb(), GBUF_REQ_LEAF, GBUF_REQ_NULLS, GET_LUP, InvalidBlockNumber, Min, PageGetExactFreeSpace(), PageIsEmpty(), PageIsNew(), ReadBuffer(), ReleaseBuffer(), spgGetCache(), SPGIST_LEAF, SPGIST_NULLS, SPGIST_PAGE_CAPACITY, SpGistBlockIsFixed, SpGistGetTargetPageFreeSpace, SpGistInitBuffer(), SpGistPageIsDeleted, SpGistPageIsLeaf, SpGistPageStoresNulls, and UnlockReleaseBuffer().

Referenced by doPickSplit(), moveLeafs(), spgAddNodeAction(), spgdoinsert(), and spgSplitNodeAction().

◆ SpGistGetInnerTypeSize()

unsigned int SpGistGetInnerTypeSize ( SpGistTypeDesc att,
Datum  datum 
)
extern

Definition at line 778 of file spgutils.c.

779{
780 unsigned int size;
781
782 if (att->attbyval)
783 size = sizeof(Datum);
784 else if (att->attlen > 0)
785 size = att->attlen;
786 else
787 size = VARSIZE_ANY(DatumGetPointer(datum));
788
789 return MAXALIGN(size);
790}
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
static Size VARSIZE_ANY(const void *PTR)
Definition varatt.h:460

References DatumGetPointer(), fb(), MAXALIGN, and VARSIZE_ANY().

Referenced by spgFormInnerTuple(), and spgFormNodeTuple().

◆ SpGistGetLeafTupleSize()

Size SpGistGetLeafTupleSize ( TupleDesc  tupleDescriptor,
const Datum datums,
const bool isnulls 
)
extern

Definition at line 817 of file spgutils.c.

819{
820 Size size;
822 bool needs_null_mask = false;
823 int natts = tupleDescriptor->natts;
824
825 /*
826 * Decide whether we need a nulls bitmask.
827 *
828 * If there is only a key attribute (natts == 1), never use a bitmask, for
829 * compatibility with the pre-v14 layout of leaf tuples. Otherwise, we
830 * need one if any attribute is null.
831 */
832 if (natts > 1)
833 {
834 for (int i = 0; i < natts; i++)
835 {
836 if (isnulls[i])
837 {
838 needs_null_mask = true;
839 break;
840 }
841 }
842 }
843
844 /*
845 * Calculate size of the data part; same as for heap tuples.
846 */
848
849 /*
850 * Compute total size.
851 */
853 size += data_size;
854 size = MAXALIGN(size);
855
856 /*
857 * Ensure that we can replace the tuple with a dead tuple later. This test
858 * is unnecessary when there are any non-null attributes, but be safe.
859 */
860 if (size < SGDTSIZE)
861 size = SGDTSIZE;
862
863 return size;
864}

References fb(), heap_compute_data_size(), i, MAXALIGN, SGDTSIZE, and SGLTHDRSZ.

Referenced by spgdoinsert().

◆ SpGistInitBuffer()

void SpGistInitBuffer ( Buffer  b,
uint16  f 
)
extern

Definition at line 721 of file spgutils.c.

722{
725}
static Size BufferGetPageSize(Buffer buffer)
Definition bufmgr.h:459
int b
Definition isn.c:74
void SpGistInitPage(Page page, uint16 f)
Definition spgutils.c:707

References Assert, b, BufferGetPage(), BufferGetPageSize(), fb(), and SpGistInitPage().

Referenced by allocNewBuffer(), doPickSplit(), spgbuild(), SpGistGetBuffer(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), and spgRedoSplitTuple().

◆ SpGistInitMetapage()

void SpGistInitMetapage ( Page  page)
extern

Definition at line 731 of file spgutils.c.

732{
733 SpGistMetaPageData *metadata;
734 int i;
735
737 metadata = SpGistPageGetMeta(page);
738 memset(metadata, 0, sizeof(SpGistMetaPageData));
740
741 /* initialize last-used-page cache to empty */
742 for (i = 0; i < SPGIST_CACHED_PAGES; i++)
744
745 /*
746 * Set pd_lower just past the end of the metadata. This is essential,
747 * because without doing so, metadata will be lost if xlog.c compresses
748 * the page.
749 */
750 ((PageHeader) page)->pd_lower =
751 ((char *) metadata + sizeof(SpGistMetaPageData)) - (char *) page;
752}
PageHeaderData * PageHeader
Definition bufpage.h:199
#define SPGIST_META
#define SPGIST_CACHED_PAGES
SpGistLastUsedPage cachedPage[SPGIST_CACHED_PAGES]

References SpGistLastUsedPage::blkno, SpGistLUPCache::cachedPage, fb(), i, InvalidBlockNumber, SpGistMetaPageData::lastUsedPages, SpGistMetaPageData::magicNumber, SPGIST_CACHED_PAGES, SPGIST_MAGIC_NUMBER, SPGIST_META, SpGistInitPage(), and SpGistPageGetMeta.

Referenced by spgbuild(), and spgbuildempty().

◆ SpGistInitPage()

void SpGistInitPage ( Page  page,
uint16  f 
)
extern

Definition at line 707 of file spgutils.c.

708{
709 SpGistPageOpaque opaque;
710
711 PageInit(page, BLCKSZ, sizeof(SpGistPageOpaqueData));
712 opaque = SpGistPageGetOpaque(page);
713 opaque->flags = f;
715}
void PageInit(Page page, Size pageSize, Size specialSize)
Definition bufpage.c:42
#define SPGIST_PAGE_ID

References fb(), SpGistPageOpaqueData::flags, PageInit(), SpGistPageOpaqueData::spgist_page_id, SPGIST_PAGE_ID, and SpGistPageGetOpaque.

Referenced by spgbuildempty(), SpGistInitBuffer(), and SpGistInitMetapage().

◆ SpGistNewBuffer()

Buffer SpGistNewBuffer ( Relation  index)
extern

Definition at line 393 of file spgutils.c.

394{
395 Buffer buffer;
396
397 /* First, try to get a page from FSM */
398 for (;;)
399 {
401
402 if (blkno == InvalidBlockNumber)
403 break; /* nothing known to FSM */
404
405 /*
406 * The fixed pages shouldn't ever be listed in FSM, but just in case
407 * one is, ignore it.
408 */
409 if (SpGistBlockIsFixed(blkno))
410 continue;
411
412 buffer = ReadBuffer(index, blkno);
413
414 /*
415 * We have to guard against the possibility that someone else already
416 * recycled this page; the buffer may be locked if so.
417 */
418 if (ConditionalLockBuffer(buffer))
419 {
420 Page page = BufferGetPage(buffer);
421
422 if (PageIsNew(page))
423 return buffer; /* OK to use, if never initialized */
424
425 if (SpGistPageIsDeleted(page) || PageIsEmpty(page))
426 return buffer; /* OK to use */
427
429 }
430
431 /* Can't use it, so release buffer and try again */
432 ReleaseBuffer(buffer);
433 }
434
437
438 return buffer;
439}
uint32 BlockNumber
Definition block.h:31
Buffer ExtendBufferedRel(BufferManagerRelation bmr, ForkNumber forkNum, BufferAccessStrategy strategy, uint32 flags)
Definition bufmgr.c:974
@ BUFFER_LOCK_UNLOCK
Definition bufmgr.h:205
@ EB_LOCK_FIRST
Definition bufmgr.h:87
#define BMR_REL(p_rel)
Definition bufmgr.h:114
BlockNumber GetFreeIndexPage(Relation rel)
Definition indexfsm.c:38
@ MAIN_FORKNUM
Definition relpath.h:58

References BMR_REL, BUFFER_LOCK_UNLOCK, BufferGetPage(), ConditionalLockBuffer(), EB_LOCK_FIRST, ExtendBufferedRel(), fb(), GetFreeIndexPage(), InvalidBlockNumber, LockBuffer(), MAIN_FORKNUM, PageIsEmpty(), PageIsNew(), ReadBuffer(), ReleaseBuffer(), SpGistBlockIsFixed, and SpGistPageIsDeleted.

Referenced by allocNewBuffer(), and spgbuild().

◆ SpGistPageAddNewItem()

OffsetNumber SpGistPageAddNewItem ( SpGistState state,
Page  page,
const void item,
Size  size,
OffsetNumber startOffset,
bool  errorOK 
)
extern

Definition at line 1202 of file spgutils.c.

1204{
1207 maxoff,
1208 offnum;
1209
1210 if (opaque->nPlaceholder > 0 &&
1211 PageGetExactFreeSpace(page) + SGDTSIZE >= MAXALIGN(size))
1212 {
1213 /* Try to replace a placeholder */
1214 maxoff = PageGetMaxOffsetNumber(page);
1215 offnum = InvalidOffsetNumber;
1216
1217 for (;;)
1218 {
1219 if (startOffset && *startOffset != InvalidOffsetNumber)
1220 i = *startOffset;
1221 else
1223 for (; i <= maxoff; i++)
1224 {
1226 PageGetItemId(page, i));
1227
1228 if (it->tupstate == SPGIST_PLACEHOLDER)
1229 {
1230 offnum = i;
1231 break;
1232 }
1233 }
1234
1235 /* Done if we found a placeholder */
1236 if (offnum != InvalidOffsetNumber)
1237 break;
1238
1239 if (startOffset && *startOffset != InvalidOffsetNumber)
1240 {
1241 /* Hint was no good, re-search from beginning */
1242 *startOffset = InvalidOffsetNumber;
1243 continue;
1244 }
1245
1246 /* Hmm, no placeholder found? */
1247 opaque->nPlaceholder = 0;
1248 break;
1249 }
1250
1251 if (offnum != InvalidOffsetNumber)
1252 {
1253 /* Replace the placeholder tuple */
1254 PageIndexTupleDelete(page, offnum);
1255
1256 offnum = PageAddItem(page, item, size, offnum, false, false);
1257
1258 /*
1259 * We should not have failed given the size check at the top of
1260 * the function, but test anyway. If we did fail, we must PANIC
1261 * because we've already deleted the placeholder tuple, and
1262 * there's no other way to keep the damage from getting to disk.
1263 */
1264 if (offnum != InvalidOffsetNumber)
1265 {
1266 Assert(opaque->nPlaceholder > 0);
1267 opaque->nPlaceholder--;
1268 if (startOffset)
1269 *startOffset = offnum + 1;
1270 }
1271 else
1272 elog(PANIC, "failed to add item of size %zu to SPGiST index page",
1273 size);
1274
1275 return offnum;
1276 }
1277 }
1278
1279 /* No luck in replacing a placeholder, so just add it to the page */
1280 offnum = PageAddItem(page, item, size,
1281 InvalidOffsetNumber, false, false);
1282
1283 if (offnum == InvalidOffsetNumber && !errorOK)
1284 elog(ERROR, "failed to add item of size %zu to SPGiST index page",
1285 size);
1286
1287 return offnum;
1288}
void PageIndexTupleDelete(Page page, OffsetNumber offnum)
Definition bufpage.c:1051
#define PageAddItem(page, item, size, offsetNumber, overwrite, is_heap)
Definition bufpage.h:504
static OffsetNumber PageGetMaxOffsetNumber(const PageData *page)
Definition bufpage.h:397
#define PANIC
Definition elog.h:42
uint16 OffsetNumber
Definition off.h:24
#define SPGIST_PLACEHOLDER

References Assert, elog, ERROR, fb(), FirstOffsetNumber, i, InvalidOffsetNumber, MAXALIGN, SpGistPageOpaqueData::nPlaceholder, PageAddItem, PageGetExactFreeSpace(), PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), PageIndexTupleDelete(), PANIC, SGDTSIZE, SPGIST_PLACEHOLDER, and SpGistPageGetOpaque.

Referenced by addLeafTuple(), doPickSplit(), moveLeafs(), spgAddNodeAction(), and spgSplitNodeAction().

◆ SpGistSetLastUsedPage()

void SpGistSetLastUsedPage ( Relation  index,
Buffer  buffer 
)
extern

Definition at line 672 of file spgutils.c.

673{
674 SpGistCache *cache = spgGetCache(index);
676 int freeSpace;
677 Page page = BufferGetPage(buffer);
678 BlockNumber blkno = BufferGetBlockNumber(buffer);
679 int flags;
680
681 /* Never enter fixed pages (root pages) in cache, though */
682 if (SpGistBlockIsFixed(blkno))
683 return;
684
685 if (SpGistPageIsLeaf(page))
686 flags = GBUF_LEAF;
687 else
688 flags = GBUF_INNER_PARITY(blkno);
689 if (SpGistPageStoresNulls(page))
690 flags |= GBUF_NULLS;
691
692 lup = GET_LUP(cache, flags);
693
694 freeSpace = PageGetExactFreeSpace(page);
695 if (lup->blkno == InvalidBlockNumber || lup->blkno == blkno ||
696 lup->freeSpace < freeSpace)
697 {
698 lup->blkno = blkno;
699 lup->freeSpace = freeSpace;
700 }
701}
#define GBUF_INNER_PARITY(x)

References BufferGetBlockNumber(), BufferGetPage(), fb(), GBUF_INNER_PARITY, GBUF_LEAF, GBUF_NULLS, GET_LUP, InvalidBlockNumber, PageGetExactFreeSpace(), spgGetCache(), SpGistBlockIsFixed, SpGistPageIsLeaf, and SpGistPageStoresNulls.

Referenced by doPickSplit(), moveLeafs(), spgAddNodeAction(), spgdoinsert(), spgMatchNodeAction(), spgprocesspending(), spgSplitNodeAction(), and spgvacuumpage().

◆ SpGistUpdateMetaPage()

void SpGistUpdateMetaPage ( Relation  index)
extern

Definition at line 449 of file spgutils.c.

450{
451 SpGistCache *cache = (SpGistCache *) index->rd_amcache;
452
453 if (cache != NULL)
454 {
456
458
460 {
463
464 metadata->lastUsedPages = cache->lastUsedPages;
465
466 /*
467 * Set pd_lower just past the end of the metadata. This is
468 * essential, because without doing so, metadata will be lost if
469 * xlog.c compresses the page. (We must do this here because
470 * pre-v11 versions of PG did not set the metapage's pd_lower
471 * correctly, so a pg_upgraded index might contain the wrong
472 * value.)
473 */
474 ((PageHeader) metapage)->pd_lower =
475 ((char *) metadata + sizeof(SpGistMetaPageData)) - (char *) metapage;
476
479 }
480 else
481 {
483 }
484 }
485}
void MarkBufferDirty(Buffer buffer)
Definition bufmgr.c:3063

References BufferGetPage(), ConditionalLockBuffer(), fb(), SpGistMetaPageData::lastUsedPages, SpGistCache::lastUsedPages, MarkBufferDirty(), ReadBuffer(), ReleaseBuffer(), SPGIST_METAPAGE_BLKNO, SpGistPageGetMeta, and UnlockReleaseBuffer().

Referenced by spgbuild(), spginsert(), and spgvacuumscan().

◆ spgPageIndexMultiDelete()

void spgPageIndexMultiDelete ( SpGistState state,
Page  page,
OffsetNumber itemnos,
int  nitems,
int  firststate,
int  reststate,
BlockNumber  blkno,
OffsetNumber  offnum 
)
extern

Definition at line 131 of file spgdoinsert.c.

135{
136 OffsetNumber firstItem;
138 SpGistDeadTuple tuple = NULL;
139 int i;
140
141 if (nitems == 0)
142 return; /* nothing to do */
143
144 /*
145 * For efficiency we want to use PageIndexMultiDelete, which requires the
146 * targets to be listed in sorted order, so we have to sort the itemnos
147 * array. (This also greatly simplifies the math for reinserting the
148 * replacement tuples.) However, we must not scribble on the caller's
149 * array, so we have to make a copy.
150 */
152 if (nitems > 1)
154
156
157 firstItem = itemnos[0];
158
159 for (i = 0; i < nitems; i++)
160 {
161 OffsetNumber itemno = sortednos[i];
162 int tupstate;
163
164 tupstate = (itemno == firstItem) ? firststate : reststate;
165 if (tuple == NULL || tuple->tupstate != tupstate)
166 tuple = spgFormDeadTuple(state, tupstate, blkno, offnum);
167
168 if (PageAddItem(page, tuple, tuple->size, itemno, false, false) != itemno)
169 elog(ERROR, "failed to add item of size %u to SPGiST index page",
170 tuple->size);
171
172 if (tupstate == SPGIST_REDIRECT)
173 SpGistPageGetOpaque(page)->nRedirection++;
174 else if (tupstate == SPGIST_PLACEHOLDER)
175 SpGistPageGetOpaque(page)->nPlaceholder++;
176 }
177}
void PageIndexMultiDelete(Page page, OffsetNumber *itemnos, int nitems)
Definition bufpage.c:1160
#define nitems(x)
Definition indent.h:31
#define MaxIndexTuplesPerPage
Definition itup.h:181
#define qsort(a, b, c, d)
Definition port.h:495
static int cmpOffsetNumbers(const void *a, const void *b)
SpGistDeadTuple spgFormDeadTuple(SpGistState *state, int tupstate, BlockNumber blkno, OffsetNumber offnum)
Definition spgutils.c:1084

References cmpOffsetNumbers(), elog, ERROR, fb(), i, MaxIndexTuplesPerPage, nitems, PageAddItem, PageIndexMultiDelete(), qsort, SpGistDeadTupleData::size, spgFormDeadTuple(), SPGIST_PLACEHOLDER, SPGIST_REDIRECT, SpGistPageGetOpaque, and SpGistDeadTupleData::tupstate.

Referenced by doPickSplit(), moveLeafs(), spgRedoMoveLeafs(), spgRedoPickSplit(), spgRedoVacuumLeaf(), and vacuumLeafPage().

◆ spgproperty()

bool spgproperty ( Oid  index_oid,
int  attno,
IndexAMProperty  prop,
const char propname,
bool res,
bool isnull 
)
extern

Definition at line 1297 of file spgutils.c.

1300{
1301 Oid opclass,
1302 opfamily,
1303 opcintype;
1305 int i;
1306
1307 /* Only answer column-level inquiries */
1308 if (attno == 0)
1309 return false;
1310
1311 switch (prop)
1312 {
1314 break;
1315 default:
1316 return false;
1317 }
1318
1319 /*
1320 * Currently, SP-GiST distance-ordered scans require that there be a
1321 * distance operator in the opclass with the default types. So we assume
1322 * that if such an operator exists, then there's a reason for it.
1323 */
1324
1325 /* First we need to know the column's opclass. */
1326 opclass = get_index_column_opclass(index_oid, attno);
1327 if (!OidIsValid(opclass))
1328 {
1329 *isnull = true;
1330 return true;
1331 }
1332
1333 /* Now look up the opclass family and input datatype. */
1334 if (!get_opclass_opfamily_and_input_type(opclass, &opfamily, &opcintype))
1335 {
1336 *isnull = true;
1337 return true;
1338 }
1339
1340 /* And now we can check whether the operator is provided. */
1342 ObjectIdGetDatum(opfamily));
1343
1344 *res = false;
1345
1346 for (i = 0; i < catlist->n_members; i++)
1347 {
1348 HeapTuple amoptup = &catlist->members[i]->tuple;
1350
1351 if (amopform->amoppurpose == AMOP_ORDER &&
1352 (amopform->amoplefttype == opcintype ||
1353 amopform->amoprighttype == opcintype) &&
1354 opfamily_can_sort_type(amopform->amopsortfamily,
1355 get_op_rettype(amopform->amopopr)))
1356 {
1357 *res = true;
1358 break;
1359 }
1360 }
1361
1363
1364 *isnull = false;
1365
1366 return true;
1367}
@ AMPROP_DISTANCE_ORDERABLE
Definition amapi.h:46
bool opfamily_can_sort_type(Oid opfamilyoid, Oid datatypeoid)
Definition amvalidate.c:271
static void * GETSTRUCT(const HeapTupleData *tuple)
bool get_opclass_opfamily_and_input_type(Oid opclass, Oid *opfamily, Oid *opcintype)
Definition lsyscache.c:1407
Oid get_op_rettype(Oid opno)
Definition lsyscache.c:1553
Oid get_index_column_opclass(Oid index_oid, int attno)
Definition lsyscache.c:3734
END_CATALOG_STRUCT typedef FormData_pg_amop * Form_pg_amop
Definition pg_amop.h:92
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
#define ReleaseSysCacheList(x)
Definition syscache.h:134
#define SearchSysCacheList1(cacheId, key1)
Definition syscache.h:127

References AMPROP_DISTANCE_ORDERABLE, fb(), Form_pg_amop, get_index_column_opclass(), get_op_rettype(), get_opclass_opfamily_and_input_type(), GETSTRUCT(), i, ObjectIdGetDatum(), OidIsValid, opfamily_can_sort_type(), ReleaseSysCacheList, and SearchSysCacheList1.

Referenced by spghandler().

◆ spgUpdateNodeLink()

void spgUpdateNodeLink ( SpGistInnerTuple  tup,
int  nodeN,
BlockNumber  blkno,
OffsetNumber  offset 
)
extern

Definition at line 52 of file spgdoinsert.c.

54{
55 int i;
56 SpGistNodeTuple node;
57
58 SGITITERATE(tup, i, node)
59 {
60 if (i == nodeN)
61 {
62 ItemPointerSet(&node->t_tid, blkno, offset);
63 return;
64 }
65 }
66
67 elog(ERROR, "failed to find requested node %d in SPGiST inner tuple",
68 nodeN);
69}
ItemPointerData t_tid
Definition itup.h:37

References elog, ERROR, fb(), i, ItemPointerSet(), SGITITERATE, and IndexTupleData::t_tid.

Referenced by saveNodeLink(), spgRedoAddLeaf(), spgRedoAddNode(), spgRedoMoveLeafs(), spgRedoPickSplit(), and spgSplitNodeAction().