gin_stat works on 10.0
[gevel.git] / gevel.c
diff --git a/gevel.c b/gevel.c
index 59bec84..a00c0b8 100644 (file)
--- a/gevel.c
+++ b/gevel.c
 #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"
 #if PG_VERSION_NUM >= 80300
 #include <tsearch/ts_utils.h>
 #endif
+#if PG_VERSION_NUM >= 100000
+#include <utils/regproc.h>
+#include <utils/varlena.h>
+#endif
 #include <utils/tqual.h>
 #include "utils/builtins.h"
 #include "utils/lsyscache.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 +71,7 @@ typedef struct {
        int maxlevel;
        text    *txt;
        char    *ptr;
-       int     len;
+       int             len;
 } IdxInfo;
 
 static Relation checkOpenedRelation(Relation r, Oid PgAmOid);
@@ -142,7 +149,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 +246,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 +596,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 +689,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,14 +733,18 @@ 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);
@@ -828,14 +839,18 @@ 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);
+       TIDBitmap               *bitmap = tbm_create(work_mem * 1024L
+#if PG_VERSION_NUM >= 100000
+                                                                                , NULL
+#endif
+                                                                                );
 #else
 #define        MAXTIDS         1024
        ItemPointerData tids[MAXTIDS];
@@ -851,8 +866,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);
@@ -1032,8 +1047,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);
@@ -1045,14 +1060,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;
@@ -1098,8 +1113,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;
@@ -1209,7 +1224,7 @@ next:
                                prst->nulls[7] = ISNOTNULL;
                } else {
                        SpGistInnerTuple        innerTuple = (SpGistInnerTuple)dtuple;
-                       int                             i;
+                       int                                     i;
                        SpGistNodeTuple         node;
 
                        SGITITERATE(innerTuple, i, node) {
@@ -1289,6 +1304,10 @@ gin_statpage(PG_FUNCTION_ARGS)
        BlockNumber blkno;
        char            res[1024];
        uint32          totalPages,
+#if PG_VERSION_NUM >= 100000
+                               deletedPages = 0,
+                               emptyDataPages = 0,
+#endif
                                entryPages = 0,
                                dataPages = 0,
                                dataInnerPages = 0,
@@ -1331,19 +1350,35 @@ gin_statpage(PG_FUNCTION_ARGS)
                page = BufferGetPage(buffer);
                header = (PageHeader)page;
 
+#if PG_VERSION_NUM >= 100000
+               if (GinPageIsDeleted(page))
+               {
+                       deletedPages++;
+               }
+               else
+#endif
                if (GinPageIsData(page))
                {
                        dataPages++;
                        if (GinPageIsLeaf(page))
                        {
-                               ItemPointerData minItem;
+                               ItemPointerData minItem, *ptr;
                                int nlist;
 
+
                                dataLeafPages++;
                                dataLeafFreeSpace += header->pd_upper - header->pd_lower;
                                ItemPointerSetMin(&minItem);
-                               pfree(GinDataLeafPageGetItems(page, &nlist, minItem));
-                               dataLeafIptrsCount += nlist;
+
+                               ptr = GinDataLeafPageGetItems(page, &nlist, minItem);
+
+                               if (ptr)
+                               {
+                                       pfree(ptr);
+                                       dataLeafIptrsCount += nlist;
+                               }
+                               else
+                                       emptyDataPages++;
                        }
                        else
                        {
@@ -1399,6 +1434,10 @@ gin_statpage(PG_FUNCTION_ARGS)
 
        snprintf(res, sizeof(res),
                         "totalPages:            %u\n"
+#if PG_VERSION_NUM >= 100000
+                        "deletedPages:          %u\n"
+                        "emptyDataPages:        %u\n"
+#endif
                         "dataPages:             %u\n"
                         "dataInnerPages:        %u\n"
                         "dataLeafPages:         %u\n"
@@ -1418,6 +1457,10 @@ gin_statpage(PG_FUNCTION_ARGS)
                         "entryAttrSize:         " INT64_FORMAT "\n"
                         ,
                         totalPages,
+#if PG_VERSION_NUM >= 100000
+                        deletedPages,
+                        emptyDataPages,
+#endif
                         dataPages,
                         dataInnerPages,
                         dataLeafPages,