| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #ifndef GIST_PRIVATE_H |
|---|
| 15 | #define GIST_PRIVATE_H |
|---|
| 16 | |
|---|
| 17 | #include "access/gist.h" |
|---|
| 18 | #include "access/itup.h" |
|---|
| 19 | #include "access/xlog.h" |
|---|
| 20 | #include "access/xlogdefs.h" |
|---|
| 21 | #include "fmgr.h" |
|---|
| 22 | |
|---|
| 23 | #define GIST_UNLOCK BUFFER_LOCK_UNLOCK |
|---|
| 24 | #define GIST_SHARE BUFFER_LOCK_SHARE |
|---|
| 25 | #define GIST_EXCLUSIVE BUFFER_LOCK_EXCLUSIVE |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | typedef struct GISTSearchStack |
|---|
| 41 | { |
|---|
| 42 | struct GISTSearchStack *next; |
|---|
| 43 | BlockNumber block; |
|---|
| 44 | |
|---|
| 45 | GistNSN lsn; |
|---|
| 46 | |
|---|
| 47 | GistNSN parentlsn; |
|---|
| 48 | } GISTSearchStack; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | typedef struct GISTPQ |
|---|
| 57 | { |
|---|
| 58 | |
|---|
| 59 | } GISTPQ; |
|---|
| 60 | |
|---|
| 61 | typedef struct GISTSTATE |
|---|
| 62 | { |
|---|
| 63 | FmgrInfo consistentFn[INDEX_MAX_KEYS]; |
|---|
| 64 | FmgrInfo unionFn[INDEX_MAX_KEYS]; |
|---|
| 65 | FmgrInfo compressFn[INDEX_MAX_KEYS]; |
|---|
| 66 | FmgrInfo decompressFn[INDEX_MAX_KEYS]; |
|---|
| 67 | FmgrInfo penaltyFn[INDEX_MAX_KEYS]; |
|---|
| 68 | FmgrInfo picksplitFn[INDEX_MAX_KEYS]; |
|---|
| 69 | FmgrInfo equalFn[INDEX_MAX_KEYS]; |
|---|
| 70 | FmgrInfo distanceFn[INDEX_MAX_KEYS]; |
|---|
| 71 | |
|---|
| 72 | TupleDesc tupdesc; |
|---|
| 73 | } GISTSTATE; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | typedef struct GISTScanOpaqueData |
|---|
| 80 | { |
|---|
| 81 | GISTSearchStack *stack; |
|---|
| 82 | GISTSearchStack *markstk; |
|---|
| 83 | uint16 flags; |
|---|
| 84 | GISTSTATE *giststate; |
|---|
| 85 | MemoryContext tempCxt; |
|---|
| 86 | Buffer curbuf; |
|---|
| 87 | Buffer markbuf; |
|---|
| 88 | GISTPQ *pq; |
|---|
| 89 | } GISTScanOpaqueData; |
|---|
| 90 | |
|---|
| 91 | typedef GISTScanOpaqueData *GISTScanOpaque; |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | extern const XLogRecPtr XLogRecPtrForTemp; |
|---|
| 95 | |
|---|
| 96 | #define XLOG_GIST_PAGE_UPDATE 0x00 |
|---|
| 97 | #define XLOG_GIST_NEW_ROOT 0x20 |
|---|
| 98 | #define XLOG_GIST_PAGE_SPLIT 0x30 |
|---|
| 99 | #define XLOG_GIST_INSERT_COMPLETE 0x40 |
|---|
| 100 | #define XLOG_GIST_CREATE_INDEX 0x50 |
|---|
| 101 | #define XLOG_GIST_PAGE_DELETE 0x60 |
|---|
| 102 | |
|---|
| 103 | typedef struct gistxlogPageUpdate |
|---|
| 104 | { |
|---|
| 105 | RelFileNode node; |
|---|
| 106 | BlockNumber blkno; |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | ItemPointerData key; |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | uint16 ntodelete; |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | } gistxlogPageUpdate; |
|---|
| 120 | |
|---|
| 121 | typedef struct gistxlogPageSplit |
|---|
| 122 | { |
|---|
| 123 | RelFileNode node; |
|---|
| 124 | BlockNumber origblkno; |
|---|
| 125 | bool origleaf; |
|---|
| 126 | uint16 npage; |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | ItemPointerData key; |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | } gistxlogPageSplit; |
|---|
| 135 | |
|---|
| 136 | typedef struct gistxlogPage |
|---|
| 137 | { |
|---|
| 138 | BlockNumber blkno; |
|---|
| 139 | int num; |
|---|
| 140 | } gistxlogPage; |
|---|
| 141 | |
|---|
| 142 | typedef struct gistxlogInsertComplete |
|---|
| 143 | { |
|---|
| 144 | RelFileNode node; |
|---|
| 145 | |
|---|
| 146 | } gistxlogInsertComplete; |
|---|
| 147 | |
|---|
| 148 | typedef struct gistxlogPageDelete |
|---|
| 149 | { |
|---|
| 150 | RelFileNode node; |
|---|
| 151 | BlockNumber blkno; |
|---|
| 152 | } gistxlogPageDelete; |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | typedef struct SplitedPageLayout |
|---|
| 156 | { |
|---|
| 157 | gistxlogPage block; |
|---|
| 158 | IndexTupleData *list; |
|---|
| 159 | int lenlist; |
|---|
| 160 | IndexTuple itup; |
|---|
| 161 | Page page; |
|---|
| 162 | Buffer buffer; |
|---|
| 163 | |
|---|
| 164 | struct SplitedPageLayout *next; |
|---|
| 165 | } SplitedPageLayout; |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | typedef struct GISTInsertStack |
|---|
| 173 | { |
|---|
| 174 | |
|---|
| 175 | BlockNumber blkno; |
|---|
| 176 | Buffer buffer; |
|---|
| 177 | Page page; |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | GistNSN lsn; |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | OffsetNumber childoffnum; |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | struct GISTInsertStack *parent; |
|---|
| 190 | struct GISTInsertStack *child; |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | struct GISTInsertStack *next; |
|---|
| 194 | } GISTInsertStack; |
|---|
| 195 | |
|---|
| 196 | typedef struct GistSplitVector |
|---|
| 197 | { |
|---|
| 198 | GIST_SPLITVEC splitVector; |
|---|
| 199 | |
|---|
| 200 | Datum spl_lattr[INDEX_MAX_KEYS]; |
|---|
| 201 | |
|---|
| 202 | bool spl_lisnull[INDEX_MAX_KEYS]; |
|---|
| 203 | bool spl_leftvalid; |
|---|
| 204 | |
|---|
| 205 | Datum spl_rattr[INDEX_MAX_KEYS]; |
|---|
| 206 | |
|---|
| 207 | bool spl_risnull[INDEX_MAX_KEYS]; |
|---|
| 208 | bool spl_rightvalid; |
|---|
| 209 | |
|---|
| 210 | bool *spl_equiv; |
|---|
| 211 | |
|---|
| 212 | } GistSplitVector; |
|---|
| 213 | |
|---|
| 214 | #define XLogRecPtrIsInvalid( r ) ( (r).xlogid == 0 && (r).xrecoff == 0 ) |
|---|
| 215 | |
|---|
| 216 | typedef struct |
|---|
| 217 | { |
|---|
| 218 | Relation r; |
|---|
| 219 | IndexTuple *itup; |
|---|
| 220 | int ituplen; |
|---|
| 221 | Size freespace; |
|---|
| 222 | GISTInsertStack *stack; |
|---|
| 223 | bool needInsertComplete; |
|---|
| 224 | |
|---|
| 225 | |
|---|
| 226 | ItemPointerData key; |
|---|
| 227 | } GISTInsertState; |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | |
|---|
| 235 | #define GS_CURBEFORE ((uint16) (1 << 0)) |
|---|
| 236 | #define GS_MRKBEFORE ((uint16) (1 << 1)) |
|---|
| 237 | |
|---|
| 238 | |
|---|
| 239 | #define GIST_ROOT_BLKNO 0 |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | #define TUPLE_IS_VALID 0xffff |
|---|
| 245 | #define TUPLE_IS_INVALID 0xfffe |
|---|
| 246 | |
|---|
| 247 | #define GistTupleIsInvalid(itup) ( ItemPointerGetOffsetNumber( &((itup)->t_tid) ) == TUPLE_IS_INVALID ) |
|---|
| 248 | #define GistTupleSetValid(itup) ItemPointerSetOffsetNumber( &((itup)->t_tid), TUPLE_IS_VALID ) |
|---|
| 249 | #define GistTupleSetInvalid(itup) ItemPointerSetOffsetNumber( &((itup)->t_tid), TUPLE_IS_INVALID ) |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | extern Datum gistbuild(PG_FUNCTION_ARGS); |
|---|
| 253 | extern Datum gistinsert(PG_FUNCTION_ARGS); |
|---|
| 254 | extern MemoryContext createTempGistContext(void); |
|---|
| 255 | extern void initGISTstate(GISTSTATE *giststate, Relation index); |
|---|
| 256 | extern void freeGISTstate(GISTSTATE *giststate); |
|---|
| 257 | extern void gistmakedeal(GISTInsertState *state, GISTSTATE *giststate); |
|---|
| 258 | extern void gistnewroot(Relation r, Buffer buffer, IndexTuple *itup, int len, ItemPointer key); |
|---|
| 259 | |
|---|
| 260 | extern SplitedPageLayout *gistSplit(Relation r, Page page, IndexTuple *itup, |
|---|
| 261 | int len, GISTSTATE *giststate); |
|---|
| 262 | |
|---|
| 263 | extern GISTInsertStack *gistFindPath(Relation r, BlockNumber child); |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | extern void gist_redo(XLogRecPtr lsn, XLogRecord *record); |
|---|
| 267 | extern void gist_desc(StringInfo buf, uint8 xl_info, char *rec); |
|---|
| 268 | extern void gist_xlog_startup(void); |
|---|
| 269 | extern void gist_xlog_cleanup(void); |
|---|
| 270 | extern bool gist_safe_restartpoint(void); |
|---|
| 271 | extern IndexTuple gist_form_invalid_tuple(BlockNumber blkno); |
|---|
| 272 | |
|---|
| 273 | extern XLogRecData *formUpdateRdata(RelFileNode node, Buffer buffer, |
|---|
| 274 | OffsetNumber *todelete, int ntodelete, |
|---|
| 275 | IndexTuple *itup, int ituplen, ItemPointer key); |
|---|
| 276 | |
|---|
| 277 | extern XLogRecData *formSplitRdata(RelFileNode node, |
|---|
| 278 | BlockNumber blkno, bool page_is_leaf, |
|---|
| 279 | ItemPointer key, SplitedPageLayout *dist); |
|---|
| 280 | |
|---|
| 281 | extern XLogRecPtr gistxlogInsertCompletion(RelFileNode node, ItemPointerData *keys, int len); |
|---|
| 282 | |
|---|
| 283 | |
|---|
| 284 | extern Datum gistgettuple(PG_FUNCTION_ARGS); |
|---|
| 285 | extern Datum gistgetmulti(PG_FUNCTION_ARGS); |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | extern void gistfreestack(GISTSearchStack *s); |
|---|
| 289 | extern GISTSearchStack *gistcopystack(GISTSearchStack *s); |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | #define GiSTPageSize \ |
|---|
| 294 | ( BLCKSZ - SizeOfPageHeaderData - MAXALIGN(sizeof(GISTPageOpaqueData)) ) |
|---|
| 295 | |
|---|
| 296 | #define GIST_MIN_FILLFACTOR 10 |
|---|
| 297 | #define GIST_DEFAULT_FILLFACTOR 90 |
|---|
| 298 | |
|---|
| 299 | extern Datum gistoptions(PG_FUNCTION_ARGS); |
|---|
| 300 | extern bool gistfitpage(IndexTuple *itvec, int len); |
|---|
| 301 | extern bool gistnospace(Page page, IndexTuple *itvec, int len, OffsetNumber todelete, Size freespace); |
|---|
| 302 | extern void gistcheckpage(Relation rel, Buffer buf); |
|---|
| 303 | extern Buffer gistNewBuffer(Relation r); |
|---|
| 304 | extern OffsetNumber gistfillbuffer(Relation r, Page page, IndexTuple *itup, |
|---|
| 305 | int len, OffsetNumber off); |
|---|
| 306 | extern IndexTuple *gistextractpage(Page page, int *len ); |
|---|
| 307 | extern IndexTuple *gistjoinvector( |
|---|
| 308 | IndexTuple *itvec, int *len, |
|---|
| 309 | IndexTuple *additvec, int addlen); |
|---|
| 310 | extern IndexTupleData *gistfillitupvec(IndexTuple *vec, int veclen, int *memlen); |
|---|
| 311 | |
|---|
| 312 | extern IndexTuple gistunion(Relation r, IndexTuple *itvec, |
|---|
| 313 | int len, GISTSTATE *giststate); |
|---|
| 314 | extern IndexTuple gistgetadjusted(Relation r, |
|---|
| 315 | IndexTuple oldtup, |
|---|
| 316 | IndexTuple addtup, |
|---|
| 317 | GISTSTATE *giststate); |
|---|
| 318 | extern IndexTuple gistFormTuple(GISTSTATE *giststate, |
|---|
| 319 | Relation r, Datum *attdata, bool *isnull, bool newValues); |
|---|
| 320 | |
|---|
| 321 | extern OffsetNumber gistchoose(Relation r, Page p, |
|---|
| 322 | IndexTuple it, |
|---|
| 323 | GISTSTATE *giststate); |
|---|
| 324 | extern void gistcentryinit(GISTSTATE *giststate, int nkey, |
|---|
| 325 | GISTENTRY *e, Datum k, |
|---|
| 326 | Relation r, Page pg, |
|---|
| 327 | OffsetNumber o, bool l, bool isNull); |
|---|
| 328 | |
|---|
| 329 | extern void GISTInitBuffer(Buffer b, uint32 f); |
|---|
| 330 | extern void gistdentryinit(GISTSTATE *giststate, int nkey, GISTENTRY *e, |
|---|
| 331 | Datum k, Relation r, Page pg, OffsetNumber o, |
|---|
| 332 | bool l, bool isNull); |
|---|
| 333 | |
|---|
| 334 | extern float gistpenalty(GISTSTATE *giststate, int attno, |
|---|
| 335 | GISTENTRY *key1, bool isNull1, |
|---|
| 336 | GISTENTRY *key2, bool isNull2); |
|---|
| 337 | extern bool gistMakeUnionItVec(GISTSTATE *giststate, IndexTuple *itvec, int len, int startkey, |
|---|
| 338 | Datum *attr, bool *isnull); |
|---|
| 339 | extern bool gistKeyIsEQ(GISTSTATE *giststate, int attno, Datum a, Datum b); |
|---|
| 340 | extern void gistDeCompressAtt(GISTSTATE *giststate, Relation r, IndexTuple tuple, Page p, |
|---|
| 341 | OffsetNumber o, GISTENTRY *attdata, bool *isnull); |
|---|
| 342 | |
|---|
| 343 | extern void gistMakeUnionKey(GISTSTATE *giststate, int attno, |
|---|
| 344 | GISTENTRY *entry1, bool isnull1, |
|---|
| 345 | GISTENTRY *entry2, bool isnull2, |
|---|
| 346 | Datum *dst, bool *dstisnull); |
|---|
| 347 | |
|---|
| 348 | |
|---|
| 349 | extern Datum gistbulkdelete(PG_FUNCTION_ARGS); |
|---|
| 350 | extern Datum gistvacuumcleanup(PG_FUNCTION_ARGS); |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | extern void gistSplitByKey(Relation r, Page page, IndexTuple *itup, |
|---|
| 354 | int len, GISTSTATE *giststate, |
|---|
| 355 | GistSplitVector *v, GistEntryVector *entryvec, |
|---|
| 356 | int attno); |
|---|
| 357 | |
|---|
| 358 | #endif |
|---|