9.6 snapshot too old support
[gevel.git] / gevel.c
diff --git a/gevel.c b/gevel.c
index f0e3738..bfadb97 100644 (file)
--- a/gevel.c
+++ b/gevel.c
@@ -18,6 +18,9 @@
 #endif
 #include "access/heapam.h"
 #include "catalog/index.h"
+#if PG_VERSION_NUM >= 90600
+#include <catalog/pg_am.h>
+#endif
 #include "miscadmin.h"
 #include "storage/lmgr.h"
 #include "catalog/namespace.h"
 #include <access/relscan.h>
 
 
-#define PAGESIZE       (BLCKSZ - MAXALIGN(sizeof(PageHeaderData) + sizeof(ItemIdData)))
+#define PAGESIZE       (BLCKSZ - MAXALIGN(sizeof(PageHeaderData) + sizeof(ItemIdData)))
 
 #ifndef PG_NARGS
 #define PG_NARGS() (fcinfo->nargs)
 #endif
 
 #if PG_VERSION_NUM >= 90600
-#define        ISNULL          true
+#define        ISNULL          true
 #define ISNOTNULL      false
 #define heap_formtuple heap_form_tuple
 #else
@@ -64,7 +67,7 @@ typedef struct {
        int maxlevel;
        text    *txt;
        char    *ptr;
-       int     len;
+       int             len;
 } IdxInfo;
 
 static Relation checkOpenedRelation(Relation r, Oid PgAmOid);
@@ -142,7 +145,7 @@ gin_index_close(Relation rel) {
 
 static Relation
 checkOpenedRelation(Relation r, Oid PgAmOid) {
-       if ( r->rd_am == NULL )
+       if ( r->rd_index == NULL )
                elog(ERROR, "Relation %s.%s is not an index",
                                        get_namespace_name(RelationGetNamespace(r)),
                                        RelationGetRelationName(r)
@@ -239,12 +242,12 @@ gist_tree(PG_FUNCTION_ARGS) {
 }
 
 typedef struct {
-       int     level;
-       int     numpages;
-       int     numleafpages;
-       int     numtuple;
-       int     numinvalidtuple;
-       int     numleaftuple;
+       int             level;
+       int             numpages;
+       int             numleafpages;
+       int             numtuple;
+       int             numinvalidtuple;
+       int             numleaftuple;
        uint64  tuplesize;
        uint64  leaftuplesize;
        uint64  totalsize;
@@ -589,14 +592,14 @@ refindPosition(GinStatState *st)
        }
 
        for(;;) {
-               int                     cmp;
+               int                             cmp;
 #if PG_VERSION_NUM >= 90100
                GinNullCategory category;
 #elif PG_VERSION_NUM < 80400
                bool                    isnull = false;
 #endif
                Datum                   datum;
-               IndexTuple              itup;
+               IndexTuple              itup;
 
                if (moveRightIfItNeeded(st)==false)
                        return false;
@@ -682,7 +685,7 @@ gin_setup_firstcall(FuncCallContext  *funcctx, text *name, int attnum) {
 
 static void
 processTuple( FuncCallContext  *funcctx,  GinStatState *st, IndexTuple itup ) {
-       MemoryContext           oldcontext;
+       MemoryContext           oldcontext;
 #if PG_VERSION_NUM >= 90100
        Datum                           key;
 #elif PG_VERSION_NUM < 80400
@@ -726,32 +729,37 @@ processTuple( FuncCallContext  *funcctx,  GinStatState *st, IndexTuple itup ) {
                ItemPointer             list;
 #else
                GinPostingTreeScan *gdi;
-               Buffer          entrybuffer;
+               Buffer                  entrybuffer;
 #endif
                Page        page;
                uint32          predictNumber;
 
                LockBuffer(st->buffer, GIN_UNLOCK);
 #if PG_VERSION_NUM >= 90400
-               stack = ginScanBeginPostingTree(&btree, st->index, rootblkno);
+               stack = ginScanBeginPostingTree(&btree, st->index, rootblkno
+#if PG_VERSION_NUM >= 90600
+                                                                               , NULL
+#endif
+                                                                               );
                page = BufferGetPage(stack->buffer);
                ItemPointerSetMin(&minItem);
                list = GinDataLeafPageGetItems(page, &nlist, minItem);
                pfree(list);
                predictNumber = stack->predictNumber;
+               st->dvalues[1] = Int32GetDatum( predictNumber * nlist );
 #elif PG_VERSION_NUM >= 90100
                gdi = ginPrepareScanPostingTree(st->index, rootblkno, TRUE);
                entrybuffer = ginScanBeginPostingTree(gdi);
                page = BufferGetPage(entrybuffer);
                predictNumber = gdi->stack->predictNumber;
+               st->dvalues[1] = Int32GetDatum( predictNumber * GinPageGetOpaque(page)->maxoff );
 #else
                gdi = prepareScanPostingTree(st->index, rootblkno, TRUE);
                entrybuffer = scanBeginPostingTree(gdi);
                page = BufferGetPage(entrybuffer);
                predictNumber = gdi->stack->predictNumber;
-#endif
-
                st->dvalues[1] = Int32GetDatum( predictNumber * GinPageGetOpaque(page)->maxoff );
+#endif
 
 #if PG_VERSION_NUM < 90400
                LockBuffer(entrybuffer, GIN_UNLOCK);
@@ -827,11 +835,11 @@ Datum gin_count_estimate(PG_FUNCTION_ARGS);
 #if PG_VERSION_NUM >= 80300
 Datum
 gin_count_estimate(PG_FUNCTION_ARGS) {
-       text                    *name=PG_GETARG_TEXT_P(0);
-       Relation                index;
+       text                    *name=PG_GETARG_TEXT_P(0);
+       Relation                index;
        IndexScanDesc   scan;
        int64                   count = 0;
-       char                    *relname=t2c(name);
+       char                    *relname=t2c(name);
        ScanKeyData             key;
 #if PG_VERSION_NUM >= 80400
        TIDBitmap               *bitmap = tbm_create(work_mem * 1024L);
@@ -850,8 +858,8 @@ gin_count_estimate(PG_FUNCTION_ARGS) {
                elog(ERROR, "Column type is not a tsvector");
        }
 
-       key.sk_flags    = 0;
-       key.sk_attno    = 1;
+       key.sk_flags = 0;
+       key.sk_attno = 1;
        key.sk_strategy = TSearchStrategyNumber;
        key.sk_subtype  = 0;
        key.sk_argument = PG_GETARG_DATUM(1);
@@ -1031,8 +1039,8 @@ spgist_stat(PG_FUNCTION_ARGS)
                         usedSpace / 1024.0,
                         usedInnerSpace / 1024.0,
                         usedLeafSpace / 1024.0,
-                        (((double) bufferSize) * ((double) totalPages) - usedSpace) / 1024,
-                        100.0 * (usedSpace / (((double) bufferSize) * ((double) totalPages))),
+                        (((double) bufferSize) * ((double) totalPages) - usedSpace) / 1024,
+                        100.0 * (usedSpace / (((double) bufferSize) * ((double) totalPages))),
                         leafTuples, innerTuples, nAllTheSame,
                         nLeafPlaceholder, nInnerPlaceholder,
                         nLeafRedirect, nInnerRedirect);
@@ -1044,14 +1052,14 @@ spgist_stat(PG_FUNCTION_ARGS)
 #if PG_VERSION_NUM >= 90200
 
 typedef struct SPGistPrintStackElem {
-       ItemPointerData         iptr;
+       ItemPointerData         iptr;
        int16                           nlabel;
        int                                     level;
 } SPGistPrintStackElem;
 
 typedef struct SPGistPrint {
        SpGistState     state;
-       Relation        index;
+       Relation        index;
        Datum           dvalues[8 /* see CreateTemplateTupleDesc call */];
        char            nulls[8 /* see CreateTemplateTupleDesc call */];
        List            *stack;
@@ -1097,8 +1105,8 @@ spgist_print(PG_FUNCTION_ARGS)
        MemoryContext                   oldcontext;
 
        if (SRF_IS_FIRSTCALL()) {
-               text                    *name=PG_GETARG_TEXT_P(0);
-               RangeVar                *relvar;
+               text                    *name=PG_GETARG_TEXT_P(0);
+               RangeVar                *relvar;
                Relation                index;
                ItemPointerData ipd;
                TupleDesc               tupdesc;
@@ -1208,7 +1216,7 @@ next:
                                prst->nulls[7] = ISNOTNULL;
                } else {
                        SpGistInnerTuple        innerTuple = (SpGistInnerTuple)dtuple;
-                       int                             i;
+                       int                                     i;
                        SpGistNodeTuple         node;
 
                        SGITITERATE(innerTuple, i, node) {