2005-05-17 05:34:18 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* gist_private.h
|
|
|
|
* private declarations for GiST -- declarations related to the
|
|
|
|
* internal implementation of GiST, not the public API
|
|
|
|
*
|
2006-03-05 16:59:11 +01:00
|
|
|
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
|
2005-05-17 05:34:18 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
2006-05-10 11:19:54 +02:00
|
|
|
* $PostgreSQL: pgsql/src/include/access/gist_private.h,v 1.13 2006/05/10 09:19:54 teodor Exp $
|
2005-05-17 05:34:18 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#ifndef GIST_PRIVATE_H
|
|
|
|
#define GIST_PRIVATE_H
|
|
|
|
|
2005-06-14 13:45:14 +02:00
|
|
|
#include "access/itup.h"
|
2005-05-17 05:34:18 +02:00
|
|
|
#include "access/gist.h"
|
|
|
|
#include "access/xlog.h"
|
2005-06-14 13:45:14 +02:00
|
|
|
#include "access/xlogdefs.h"
|
2005-05-17 05:34:18 +02:00
|
|
|
#include "fmgr.h"
|
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
#define GIST_UNLOCK BUFFER_LOCK_UNLOCK
|
2005-06-27 14:45:23 +02:00
|
|
|
#define GIST_SHARE BUFFER_LOCK_SHARE
|
|
|
|
#define GIST_EXCLUSIVE BUFFER_LOCK_EXCLUSIVE
|
|
|
|
|
|
|
|
|
2005-05-17 05:34:18 +02:00
|
|
|
/*
|
2005-06-27 14:45:23 +02:00
|
|
|
* XXX old comment!!!
|
2005-05-17 05:34:18 +02:00
|
|
|
* When we descend a tree, we keep a stack of parent pointers. This
|
|
|
|
* allows us to follow a chain of internal node points until we reach
|
|
|
|
* a leaf node, and then back up the stack to re-examine the internal
|
|
|
|
* nodes.
|
|
|
|
*
|
|
|
|
* 'parent' is the previous stack entry -- i.e. the node we arrived
|
|
|
|
* from. 'block' is the node's block number. 'offset' is the offset in
|
|
|
|
* the node's page that we stopped at (i.e. we followed the child
|
|
|
|
* pointer located at the specified offset).
|
|
|
|
*/
|
2005-06-27 14:45:23 +02:00
|
|
|
typedef struct GISTSearchStack
|
2005-05-17 05:34:18 +02:00
|
|
|
{
|
2005-06-27 14:45:23 +02:00
|
|
|
struct GISTSearchStack *next;
|
2005-05-17 05:34:18 +02:00
|
|
|
BlockNumber block;
|
2005-06-27 14:45:23 +02:00
|
|
|
/* to identify page changed */
|
|
|
|
GistNSN lsn;
|
|
|
|
/* to recognize split occured */
|
|
|
|
GistNSN parentlsn;
|
|
|
|
} GISTSearchStack;
|
2005-05-17 05:34:18 +02:00
|
|
|
|
|
|
|
typedef struct GISTSTATE
|
|
|
|
{
|
|
|
|
FmgrInfo consistentFn[INDEX_MAX_KEYS];
|
|
|
|
FmgrInfo unionFn[INDEX_MAX_KEYS];
|
|
|
|
FmgrInfo compressFn[INDEX_MAX_KEYS];
|
|
|
|
FmgrInfo decompressFn[INDEX_MAX_KEYS];
|
|
|
|
FmgrInfo penaltyFn[INDEX_MAX_KEYS];
|
|
|
|
FmgrInfo picksplitFn[INDEX_MAX_KEYS];
|
|
|
|
FmgrInfo equalFn[INDEX_MAX_KEYS];
|
|
|
|
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
} GISTSTATE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When we're doing a scan, we need to keep track of the parent stack
|
|
|
|
* for the marked and current items.
|
|
|
|
*/
|
|
|
|
typedef struct GISTScanOpaqueData
|
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
GISTSearchStack *stack;
|
|
|
|
GISTSearchStack *markstk;
|
|
|
|
uint16 flags;
|
|
|
|
GISTSTATE *giststate;
|
|
|
|
MemoryContext tempCxt;
|
|
|
|
Buffer curbuf;
|
|
|
|
Buffer markbuf;
|
2005-05-17 05:34:18 +02:00
|
|
|
} GISTScanOpaqueData;
|
|
|
|
|
|
|
|
typedef GISTScanOpaqueData *GISTScanOpaque;
|
|
|
|
|
2005-06-27 14:45:23 +02:00
|
|
|
/* XLog stuff */
|
2005-10-15 04:49:52 +02:00
|
|
|
extern const XLogRecPtr XLogRecPtrForTemp;
|
2005-06-27 14:45:23 +02:00
|
|
|
|
2006-03-31 01:03:10 +02:00
|
|
|
#define XLOG_GIST_PAGE_UPDATE 0x00
|
|
|
|
#define XLOG_GIST_NEW_ROOT 0x20
|
|
|
|
#define XLOG_GIST_PAGE_SPLIT 0x30
|
|
|
|
#define XLOG_GIST_INSERT_COMPLETE 0x40
|
|
|
|
#define XLOG_GIST_CREATE_INDEX 0x50
|
2005-06-27 14:45:23 +02:00
|
|
|
|
2006-03-31 01:03:10 +02:00
|
|
|
typedef struct gistxlogPageUpdate
|
2005-10-15 04:49:52 +02:00
|
|
|
{
|
|
|
|
RelFileNode node;
|
|
|
|
BlockNumber blkno;
|
2005-06-27 14:45:23 +02:00
|
|
|
|
|
|
|
uint16 ntodelete;
|
2005-10-15 04:49:52 +02:00
|
|
|
bool isemptypage;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It used to identify completeness of insert. Sets to leaf itup
|
|
|
|
*/
|
|
|
|
ItemPointerData key;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* follow: 1. todelete OffsetNumbers 2. tuples to insert
|
|
|
|
*/
|
2006-03-31 01:03:10 +02:00
|
|
|
} gistxlogPageUpdate;
|
2005-06-27 14:45:23 +02:00
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
typedef struct gistxlogPageSplit
|
|
|
|
{
|
|
|
|
RelFileNode node;
|
|
|
|
BlockNumber origblkno; /* splitted page */
|
2006-03-31 01:03:10 +02:00
|
|
|
bool origleaf; /* was splitted page a leaf page? */
|
2005-06-27 14:45:23 +02:00
|
|
|
uint16 npage;
|
|
|
|
|
2006-03-31 01:03:10 +02:00
|
|
|
/* see comments on gistxlogPageUpdate */
|
2005-10-15 04:49:52 +02:00
|
|
|
ItemPointerData key;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* follow: 1. gistxlogPage and array of IndexTupleData per page
|
|
|
|
*/
|
2005-06-27 14:45:23 +02:00
|
|
|
} gistxlogPageSplit;
|
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
typedef struct gistxlogPage
|
|
|
|
{
|
|
|
|
BlockNumber blkno;
|
2006-03-31 01:03:10 +02:00
|
|
|
int num; /* number of index tuples following */
|
2005-10-15 04:49:52 +02:00
|
|
|
} gistxlogPage;
|
2005-06-27 14:45:23 +02:00
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
typedef struct gistxlogInsertComplete
|
|
|
|
{
|
|
|
|
RelFileNode node;
|
2005-06-27 14:45:23 +02:00
|
|
|
/* follows ItemPointerData key to clean */
|
|
|
|
} gistxlogInsertComplete;
|
|
|
|
|
2006-03-31 01:03:10 +02:00
|
|
|
|
2005-06-27 14:45:23 +02:00
|
|
|
/* SplitedPageLayout - gistSplit function result */
|
2005-10-15 04:49:52 +02:00
|
|
|
typedef struct SplitedPageLayout
|
|
|
|
{
|
|
|
|
gistxlogPage block;
|
|
|
|
IndexTupleData *list;
|
|
|
|
int lenlist;
|
2006-05-10 11:19:54 +02:00
|
|
|
IndexTuple itup; /* union key for page */
|
|
|
|
Page page; /* to operate */
|
2005-10-15 04:49:52 +02:00
|
|
|
Buffer buffer; /* to write after all proceed */
|
2005-06-27 14:45:23 +02:00
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
struct SplitedPageLayout *next;
|
2005-06-27 14:45:23 +02:00
|
|
|
} SplitedPageLayout;
|
|
|
|
|
2005-06-14 13:45:14 +02:00
|
|
|
/*
|
|
|
|
* GISTInsertStack used for locking buffers and transfer arguments during
|
|
|
|
* insertion
|
|
|
|
*/
|
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
typedef struct GISTInsertStack
|
|
|
|
{
|
2005-06-14 13:45:14 +02:00
|
|
|
/* current page */
|
2005-10-15 04:49:52 +02:00
|
|
|
BlockNumber blkno;
|
2005-06-14 13:45:14 +02:00
|
|
|
Buffer buffer;
|
|
|
|
Page page;
|
2005-06-27 14:45:23 +02:00
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
/*
|
|
|
|
* log sequence number from page->lsn to recognize page update and
|
|
|
|
* compare it with page's nsn to recognize page split
|
|
|
|
*/
|
2005-06-27 14:45:23 +02:00
|
|
|
GistNSN lsn;
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-06-14 13:45:14 +02:00
|
|
|
/* child's offset */
|
2005-10-15 04:49:52 +02:00
|
|
|
OffsetNumber childoffnum;
|
2005-06-14 13:45:14 +02:00
|
|
|
|
2005-06-27 14:45:23 +02:00
|
|
|
/* pointer to parent and child */
|
2005-10-15 04:49:52 +02:00
|
|
|
struct GISTInsertStack *parent;
|
|
|
|
struct GISTInsertStack *child;
|
2005-06-14 13:45:14 +02:00
|
|
|
|
2005-06-27 14:45:23 +02:00
|
|
|
/* for gistFindPath */
|
2005-10-15 04:49:52 +02:00
|
|
|
struct GISTInsertStack *next;
|
2005-06-14 13:45:14 +02:00
|
|
|
} GISTInsertStack;
|
|
|
|
|
2005-06-27 14:45:23 +02:00
|
|
|
#define XLogRecPtrIsInvalid( r ) ( (r).xlogid == 0 && (r).xrecoff == 0 )
|
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2005-06-14 13:45:14 +02:00
|
|
|
Relation r;
|
2005-10-15 04:49:52 +02:00
|
|
|
IndexTuple *itup; /* in/out, points to compressed entry */
|
|
|
|
int ituplen; /* length of itup */
|
|
|
|
GISTInsertStack *stack;
|
|
|
|
bool needInsertComplete;
|
2005-06-14 13:45:14 +02:00
|
|
|
|
|
|
|
/* pointer to heap tuple */
|
2005-10-15 04:49:52 +02:00
|
|
|
ItemPointerData key;
|
2005-06-14 13:45:14 +02:00
|
|
|
} GISTInsertState;
|
|
|
|
|
2005-05-17 05:34:18 +02:00
|
|
|
/*
|
|
|
|
* When we're doing a scan and updating a tree at the same time, the
|
|
|
|
* updates may affect the scan. We use the flags entry of the scan's
|
|
|
|
* opaque space to record our actual position in response to updates
|
|
|
|
* that we can't handle simply by adjusting pointers.
|
|
|
|
*/
|
|
|
|
#define GS_CURBEFORE ((uint16) (1 << 0))
|
|
|
|
#define GS_MRKBEFORE ((uint16) (1 << 1))
|
|
|
|
|
|
|
|
/* root page of a gist index */
|
|
|
|
#define GIST_ROOT_BLKNO 0
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When we update a relation on which we're doing a scan, we need to
|
|
|
|
* check the scan and fix it if the update affected any of the pages
|
2005-10-15 04:49:52 +02:00
|
|
|
* it touches. Otherwise, we can miss records that we should see.
|
2005-05-17 05:34:18 +02:00
|
|
|
* The only times we need to do this are for deletions and splits. See
|
|
|
|
* the code in gistscan.c for how the scan is fixed. These two
|
|
|
|
* constants tell us what sort of operation changed the index.
|
|
|
|
*/
|
|
|
|
#define GISTOP_DEL 0
|
2005-10-15 04:49:52 +02:00
|
|
|
/* #define GISTOP_SPLIT 1 */
|
2005-05-17 05:34:18 +02:00
|
|
|
|
2005-06-14 13:45:14 +02:00
|
|
|
#define ATTSIZE(datum, tupdesc, i, isnull) \
|
2005-10-15 04:49:52 +02:00
|
|
|
( \
|
|
|
|
(isnull) ? 0 : \
|
|
|
|
att_addlength(0, (tupdesc)->attrs[(i)-1]->attlen, (datum)) \
|
|
|
|
)
|
2005-06-14 13:45:14 +02:00
|
|
|
|
2005-06-20 12:29:37 +02:00
|
|
|
/*
|
|
|
|
* mark tuples on inner pages during recovery
|
|
|
|
*/
|
|
|
|
#define TUPLE_IS_VALID 0xffff
|
|
|
|
#define TUPLE_IS_INVALID 0xfffe
|
|
|
|
|
|
|
|
#define GistTupleIsInvalid(itup) ( ItemPointerGetOffsetNumber( &((itup)->t_tid) ) == TUPLE_IS_INVALID )
|
|
|
|
#define GistTupleSetValid(itup) ItemPointerSetOffsetNumber( &((itup)->t_tid), TUPLE_IS_VALID )
|
|
|
|
#define GistTupleSetInvalid(itup) ItemPointerSetOffsetNumber( &((itup)->t_tid), TUPLE_IS_INVALID )
|
2005-06-14 13:45:14 +02:00
|
|
|
|
2005-05-17 05:34:18 +02:00
|
|
|
/* gist.c */
|
|
|
|
extern Datum gistbuild(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum gistinsert(PG_FUNCTION_ARGS);
|
|
|
|
extern MemoryContext createTempGistContext(void);
|
|
|
|
extern void initGISTstate(GISTSTATE *giststate, Relation index);
|
|
|
|
extern void freeGISTstate(GISTSTATE *giststate);
|
2005-06-14 13:45:14 +02:00
|
|
|
extern void gistmakedeal(GISTInsertState *state, GISTSTATE *giststate);
|
2005-06-27 14:45:23 +02:00
|
|
|
extern void gistnewroot(Relation r, Buffer buffer, IndexTuple *itup, int len, ItemPointer key);
|
2005-06-06 19:01:25 +02:00
|
|
|
|
2006-05-10 11:19:54 +02:00
|
|
|
extern SplitedPageLayout *gistSplit(Relation r, Page page, IndexTuple *itup,
|
|
|
|
int len, GISTSTATE *giststate);
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2006-03-31 01:03:10 +02:00
|
|
|
extern GISTInsertStack *gistFindPath(Relation r, BlockNumber child);
|
2005-06-27 14:45:23 +02:00
|
|
|
|
2005-06-14 13:45:14 +02:00
|
|
|
/* gistxlog.c */
|
2005-05-17 05:34:18 +02:00
|
|
|
extern void gist_redo(XLogRecPtr lsn, XLogRecord *record);
|
2006-03-24 05:32:13 +01:00
|
|
|
extern void gist_desc(StringInfo buf, uint8 xl_info, char *rec);
|
2005-06-14 13:45:14 +02:00
|
|
|
extern void gist_xlog_startup(void);
|
|
|
|
extern void gist_xlog_cleanup(void);
|
2005-06-20 12:29:37 +02:00
|
|
|
extern IndexTuple gist_form_invalid_tuple(BlockNumber blkno);
|
|
|
|
|
2006-03-31 01:03:10 +02:00
|
|
|
extern XLogRecData *formUpdateRdata(RelFileNode node, Buffer buffer,
|
2005-10-15 04:49:52 +02:00
|
|
|
OffsetNumber *todelete, int ntodelete, bool emptypage,
|
|
|
|
IndexTuple *itup, int ituplen, ItemPointer key);
|
2005-06-20 12:29:37 +02:00
|
|
|
|
2006-03-31 01:03:10 +02:00
|
|
|
extern XLogRecData *formSplitRdata(RelFileNode node,
|
|
|
|
BlockNumber blkno, bool page_is_leaf,
|
2005-10-15 04:49:52 +02:00
|
|
|
ItemPointer key, SplitedPageLayout *dist);
|
2005-06-20 12:29:37 +02:00
|
|
|
|
|
|
|
extern XLogRecPtr gistxlogInsertCompletion(RelFileNode node, ItemPointerData *keys, int len);
|
2005-05-17 05:34:18 +02:00
|
|
|
|
|
|
|
/* gistget.c */
|
|
|
|
extern Datum gistgettuple(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum gistgetmulti(PG_FUNCTION_ARGS);
|
|
|
|
|
2005-06-14 13:45:14 +02:00
|
|
|
/* gistutil.c */
|
2006-05-10 11:19:54 +02:00
|
|
|
|
|
|
|
#define GiSTPageSize \
|
|
|
|
( BLCKSZ - SizeOfPageHeaderData - MAXALIGN(sizeof(GISTPageOpaqueData)) )
|
|
|
|
|
|
|
|
extern bool gistfitpage(IndexTuple *itvec, int len);
|
|
|
|
extern bool gistnospace(Page page, IndexTuple *itvec, int len, OffsetNumber todelete);
|
2005-11-06 23:39:21 +01:00
|
|
|
extern void gistcheckpage(Relation rel, Buffer buf);
|
2005-10-15 04:49:52 +02:00
|
|
|
extern Buffer gistNewBuffer(Relation r);
|
2005-06-14 13:45:14 +02:00
|
|
|
extern OffsetNumber gistfillbuffer(Relation r, Page page, IndexTuple *itup,
|
2005-10-15 04:49:52 +02:00
|
|
|
int len, OffsetNumber off);
|
|
|
|
extern IndexTuple *gistextractbuffer(Buffer buffer, int *len /* out */ );
|
|
|
|
extern IndexTuple *gistjoinvector(
|
|
|
|
IndexTuple *itvec, int *len,
|
|
|
|
IndexTuple *additvec, int addlen);
|
2005-06-14 13:45:14 +02:00
|
|
|
extern IndexTuple gistunion(Relation r, IndexTuple *itvec,
|
2005-10-15 04:49:52 +02:00
|
|
|
int len, GISTSTATE *giststate);
|
2005-06-14 13:45:14 +02:00
|
|
|
extern IndexTuple gistgetadjusted(Relation r,
|
2005-10-15 04:49:52 +02:00
|
|
|
IndexTuple oldtup,
|
|
|
|
IndexTuple addtup,
|
|
|
|
GISTSTATE *giststate);
|
2005-06-14 13:45:14 +02:00
|
|
|
extern int gistfindgroup(GISTSTATE *giststate,
|
2005-10-15 04:49:52 +02:00
|
|
|
GISTENTRY *valvec, GIST_SPLITVEC *spl);
|
2005-06-14 13:45:14 +02:00
|
|
|
extern void gistadjsubkey(Relation r,
|
2005-10-15 04:49:52 +02:00
|
|
|
IndexTuple *itup, int len,
|
|
|
|
GIST_SPLITVEC *v,
|
|
|
|
GISTSTATE *giststate);
|
2005-06-14 13:45:14 +02:00
|
|
|
extern IndexTuple gistFormTuple(GISTSTATE *giststate,
|
2005-10-15 04:49:52 +02:00
|
|
|
Relation r, Datum *attdata, int *datumsize, bool *isnull);
|
2005-06-14 13:45:14 +02:00
|
|
|
|
|
|
|
extern OffsetNumber gistchoose(Relation r, Page p,
|
2005-10-15 04:49:52 +02:00
|
|
|
IndexTuple it,
|
|
|
|
GISTSTATE *giststate);
|
2005-06-14 13:45:14 +02:00
|
|
|
extern void gistcentryinit(GISTSTATE *giststate, int nkey,
|
2005-10-15 04:49:52 +02:00
|
|
|
GISTENTRY *e, Datum k,
|
|
|
|
Relation r, Page pg,
|
|
|
|
OffsetNumber o, int b, bool l, bool isNull);
|
2005-06-14 13:45:14 +02:00
|
|
|
extern void gistDeCompressAtt(GISTSTATE *giststate, Relation r,
|
2005-10-15 04:49:52 +02:00
|
|
|
IndexTuple tuple, Page p, OffsetNumber o,
|
|
|
|
GISTENTRY *attdata, bool *isnull);
|
|
|
|
extern void gistunionsubkey(Relation r, GISTSTATE *giststate,
|
|
|
|
IndexTuple *itvec, GIST_SPLITVEC *spl, bool isall);
|
2005-06-14 13:45:14 +02:00
|
|
|
extern void GISTInitBuffer(Buffer b, uint32 f);
|
|
|
|
extern void gistdentryinit(GISTSTATE *giststate, int nkey, GISTENTRY *e,
|
|
|
|
Datum k, Relation r, Page pg, OffsetNumber o,
|
|
|
|
int b, bool l, bool isNull);
|
2005-06-20 12:29:37 +02:00
|
|
|
void gistUserPicksplit(Relation r, GistEntryVector *entryvec, GIST_SPLITVEC *v,
|
2005-10-15 04:49:52 +02:00
|
|
|
IndexTuple *itup, int len, GISTSTATE *giststate);
|
2005-06-20 12:29:37 +02:00
|
|
|
|
|
|
|
/* gistvacuum.c */
|
|
|
|
extern Datum gistbulkdelete(PG_FUNCTION_ARGS);
|
|
|
|
extern Datum gistvacuumcleanup(PG_FUNCTION_ARGS);
|
2005-06-14 13:45:14 +02:00
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
#endif /* GIST_PRIVATE_H */
|