gin_stat works on 10.0
[gevel.git] / gevel.c
diff --git a/gevel.c b/gevel.c
index 7a8c891..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"
@@ -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)
@@ -733,7 +740,11 @@ processTuple( FuncCallContext  *funcctx,  GinStatState *st, IndexTuple itup ) {
 
                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);
@@ -835,7 +846,11 @@ gin_count_estimate(PG_FUNCTION_ARGS) {
        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];
@@ -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,