v11 support
[gevel.git] / gevel.c
diff --git a/gevel.c b/gevel.c
index facfa40..ff5e5c4 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 ISNOTNULL      false
+#define heap_formtuple heap_form_tuple
+#else
+#define ISNULL         'n'
+#define ISNOTNULL      ' '
+#endif
+
 static char
 *t2c(text* in) {
-        char *out=palloc( VARSIZE(in) );
-        memcpy(out, VARDATA(in), VARSIZE(in)-VARHDRSZ);
-        out[ VARSIZE(in)-VARHDRSZ ] ='\0';
-        return out;
+       char *out=palloc(VARSIZE(in));
+
+       memcpy(out, VARDATA(in), VARSIZE(in)-VARHDRSZ);
+       out[ VARSIZE(in)-VARHDRSZ ] ='\0';
+       return out;
 }
 
 typedef struct {
        int maxlevel;
        text    *txt;
        char    *ptr;
-       int     len;    
+       int             len;
 } IdxInfo;
 
 static Relation checkOpenedRelation(Relation r, Oid PgAmOid);
 
 #ifdef PG_MODULE_MAGIC
-/* >= 8.2 */ 
+/* >= 8.2 */
 
 PG_MODULE_MAGIC;
 
 static Relation
 gist_index_open(RangeVar *relvar) {
-#if PG_VERSION_NUM < 90200 
+#if PG_VERSION_NUM < 90200
        Oid relOid = RangeVarGetRelid(relvar, false);
 #else
        Oid relOid = RangeVarGetRelid(relvar, NoLock, false);
@@ -79,7 +96,7 @@ gist_index_open(RangeVar *relvar) {
 
 static Relation
 gin_index_open(RangeVar *relvar) {
-#if PG_VERSION_NUM < 90200 
+#if PG_VERSION_NUM < 90200
        Oid relOid = RangeVarGetRelid(relvar, false);
 #else
        Oid relOid = RangeVarGetRelid(relvar, NoLock, false);
@@ -130,9 +147,9 @@ gin_index_close(Relation rel) {
 #define SET_VARSIZE(p,l)       VARATT_SIZEP(p)=(l)
 #endif
 
-static Relation 
+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)
@@ -143,7 +160,7 @@ checkOpenedRelation(Relation r, Oid PgAmOid) {
                                        get_namespace_name(RelationGetNamespace(r)),
                                        RelationGetRelationName(r)
                        );
-       
+
        return r;
 }
 
@@ -175,13 +192,13 @@ gist_dumptree(Relation r, int level, BlockNumber blk, OffsetNumber coff, IdxInfo
                info->ptr = ((char*)info->txt)+dist;
        }
 
-       sprintf(info->ptr, "%s%d(l:%d) blk: %u numTuple: %d free: %db(%.2f%%) rightlink:%u (%s)\n", 
+       sprintf(info->ptr, "%s%d(l:%d) blk: %u numTuple: %d free: %db(%.2f%%) rightlink:%u (%s)\n",
                pred,
-               coff, 
-               level, 
+               coff,
+               level,
                blk,
-               (int) maxoff, 
-               (int) PageGetFreeSpace(page),  
+               (int) maxoff,
+               (int) PageGetFreeSpace(page),
                100.0*(((float)PAGESIZE)-(float)PageGetFreeSpace(page))/((float)PAGESIZE),
                GistPageGetOpaque(page)->rightlink,
                ( GistPageGetOpaque(page)->rightlink == InvalidBlockNumber ) ? "InvalidBlockNumber" : "OK" );
@@ -214,7 +231,7 @@ gist_tree(PG_FUNCTION_ARGS) {
        index = gist_index_open(relvar);
        PG_FREE_IF_COPY(name,0);
 
-       info.maxlevel = ( PG_NARGS() > 1 ) ? PG_GETARG_INT32(1) : -1; 
+       info.maxlevel = ( PG_NARGS() > 1 ) ? PG_GETARG_INT32(1) : -1;
        info.len=1024;
        info.txt=(text*)palloc( info.len );
        info.ptr=((char*)info.txt)+VARHDRSZ;
@@ -229,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;
@@ -312,7 +329,7 @@ gist_stat(PG_FUNCTION_ARGS) {
        gist_index_close(index);
        pfree(relname);
 
-       sprintf(ptr, 
+       sprintf(ptr,
                "Number of levels:          %d\n"
                "Number of pages:           %d\n"
                "Number of leaf pages:      %d\n"
@@ -333,7 +350,7 @@ gist_stat(PG_FUNCTION_ARGS) {
                info.totalsize);
 
        ptr=strchr(ptr,'\0');
-                
+
        SET_VARSIZE(out, ptr-((char*)out));
        PG_RETURN_POINTER(out);
 }
@@ -351,7 +368,11 @@ typedef struct {
        RangeVar   *relvar;
        Relation        index;
        Datum   *dvalues;
+#if PG_VERSION_NUM >= 90600
+       bool    *nulls;
+#else
        char    *nulls;
+#endif
        GPItem  *item;
 } TypeStorage;
 
@@ -360,7 +381,7 @@ openGPPage( FuncCallContext *funcctx, BlockNumber blk ) {
        GPItem  *nitem;
        MemoryContext     oldcontext;
        Relation index = ( (TypeStorage*)(funcctx->user_fctx) )->index;
-       
+
        oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
        nitem = (GPItem*)palloc( sizeof(GPItem) );
        memset(nitem,0,sizeof(GPItem));
@@ -369,24 +390,30 @@ openGPPage( FuncCallContext *funcctx, BlockNumber blk ) {
        nitem->page = (Page) BufferGetPage(nitem->buffer);
        nitem->offset=FirstOffsetNumber;
        nitem->next = ( (TypeStorage*)(funcctx->user_fctx) )->item;
-       nitem->level = ( nitem->next ) ? nitem->next->level+1 : 1; 
+       nitem->level = ( nitem->next ) ? nitem->next->level+1 : 1;
        ( (TypeStorage*)(funcctx->user_fctx) )->item = nitem;
 
        MemoryContextSwitchTo(oldcontext);
        return nitem;
-} 
+}
 
 static GPItem*
 closeGPPage( FuncCallContext *funcctx ) {
        GPItem  *oitem = ( (TypeStorage*)(funcctx->user_fctx) )->item;
 
        ( (TypeStorage*)(funcctx->user_fctx) )->item = oitem->next;
-       
+
        ReleaseBuffer(oitem->buffer);
        pfree( oitem );
-       return ( (TypeStorage*)(funcctx->user_fctx) )->item; 
+       return ( (TypeStorage*)(funcctx->user_fctx) )->item;
 }
 
+#if PG_VERSION_NUM >= 110000
+#define TS_GET_TYPEVAL(s, i, v)        (s)->index->rd_att->attrs[(i)].v
+#else
+#define TS_GET_TYPEVAL(s, i, v)        (s)->index->rd_att->attrs[(i)]->v
+#endif
+
 static void
 setup_firstcall(FuncCallContext  *funcctx, text *name) {
        MemoryContext     oldcontext;
@@ -414,14 +441,14 @@ setup_firstcall(FuncCallContext  *funcctx, text *name) {
                        tupdesc,
                        i+3,
                        attname,
-                       st->index->rd_att->attrs[i]->atttypid,
-                       st->index->rd_att->attrs[i]->atttypmod,
-                       st->index->rd_att->attrs[i]->attndims
+                       TS_GET_TYPEVAL(st, i, atttypid),
+                       TS_GET_TYPEVAL(st, i, atttypmod),
+                       TS_GET_TYPEVAL(st, i, attndims)
                );
        }
 
        st->dvalues = (Datum *) palloc((tupdesc->natts+2) * sizeof(Datum));
-       st->nulls = (char *) palloc((tupdesc->natts+2) * sizeof(char));
+       st->nulls = palloc((tupdesc->natts+2) * sizeof(*st->nulls));
 
        funcctx->slot = TupleDescGetSlot(tupdesc);
        funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
@@ -430,16 +457,16 @@ setup_firstcall(FuncCallContext  *funcctx, text *name) {
        pfree(relname);
 
        st->item=openGPPage(funcctx, GIST_ROOT_BLKNO);
-} 
+}
 
-static void 
+static void
 close_call( FuncCallContext  *funcctx ) {
        TypeStorage *st = (TypeStorage*)(funcctx->user_fctx);
-       
-       while( st->item && closeGPPage(funcctx) );
-       
-       pfree( st->dvalues );
-       pfree( st->nulls );
+
+       while(st->item && closeGPPage(funcctx));
+
+       pfree(st->dvalues);
+       pfree(st->nulls);
 
        gist_index_close(st->index);
 }
@@ -464,7 +491,7 @@ gist_print(PG_FUNCTION_ARGS) {
                PG_FREE_IF_COPY(name,0);
        }
 
-       funcctx = SRF_PERCALL_SETUP();  
+       funcctx = SRF_PERCALL_SETUP();
        st = (TypeStorage*)(funcctx->user_fctx);
 
        if ( !st->item ) {
@@ -476,23 +503,23 @@ gist_print(PG_FUNCTION_ARGS) {
                if ( ! closeGPPage(funcctx) ) {
                        close_call(funcctx);
                        SRF_RETURN_DONE(funcctx);
-               } 
+               }
        }
 
        iid = PageGetItemId( st->item->page, st->item->offset );
        ituple = (IndexTuple) PageGetItem(st->item->page, iid);
 
        st->dvalues[0] = Int32GetDatum( st->item->level );
-       st->nulls[0] = ' ';
+       st->nulls[0] = ISNOTNULL;
        st->dvalues[1] = BoolGetDatum( (!GistPageIsLeaf(st->item->page) && GistTupleIsInvalid(ituple)) ? false : true );
-       st->nulls[1] = ' ';
+       st->nulls[1] = ISNOTNULL;
        for(i=2; i<funcctx->attinmeta->tupdesc->natts; i++) {
                if ( !GistPageIsLeaf(st->item->page) && GistTupleIsInvalid(ituple) ) {
                        st->dvalues[i] = (Datum)0;
-                       st->nulls[i] = 'n';
+                       st->nulls[i] = ISNULL;
                } else {
-                       st->dvalues[i] = index_getattr(ituple, i-1, st->index->rd_att, &isnull); 
-                       st->nulls[i] = ( isnull ) ? 'n' : ' ';
+                       st->dvalues[i] = index_getattr(ituple, i-1, st->index->rd_att, &isnull);
+                       st->nulls[i] = ( isnull ) ? ISNULL : ISNOTNULL;
                }
        }
 
@@ -517,7 +544,11 @@ typedef struct GinStatState {
        GinNullCategory category;
 #endif
        Datum                   dvalues[2];
+#if PG_VERSION_NUM >= 110000
+       bool                    nulls[2];
+#else
        char                    nulls[2];
+#endif
 } GinStatState;
 
 static bool
@@ -529,7 +560,7 @@ moveRightIfItNeeded( GinStatState *st )
                /*
                * We scaned the whole page, so we should take right page
                */
-               BlockNumber blkno = GinPageGetOpaque(page)->rightlink;               
+               BlockNumber blkno = GinPageGetOpaque(page)->rightlink;
 
                if ( GinPageRightMost(page) )
                        return false;  /* no more page */
@@ -544,13 +575,13 @@ moveRightIfItNeeded( GinStatState *st )
 }
 
 /*
- * Refinds a previois position, at returns it has correctly 
+ * Refinds a previois position, at returns it has correctly
  * set offset and buffer is locked
  */
 static bool
 refindPosition(GinStatState *st)
 {
-       Page    page;        
+       Page    page;
 
        /* find left if needed (it causes only for first search) */
        for (;;) {
@@ -575,14 +606,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;
@@ -611,7 +642,7 @@ refindPosition(GinStatState *st)
 #endif
                if ( cmp == 0 )
                {
-                       if ( !st->index->rd_att->attrs[st->attnum]->attbyval )
+                       if ( st->curval && !TS_GET_TYPEVAL(st, st->attnum, attbyval) )
                                pfree( (void*) st->curval );
                        return true;
                }
@@ -648,13 +679,13 @@ gin_setup_firstcall(FuncCallContext  *funcctx, text *name, int attnum) {
        funcctx->user_fctx = (void*)st;
 
        tupdesc = CreateTemplateTupleDesc(2, false);
-       TupleDescInitEntry(tupdesc, 1, "value", 
-                       st->index->rd_att->attrs[st->attnum]->atttypid, 
-                       st->index->rd_att->attrs[st->attnum]->atttypmod,
-                       st->index->rd_att->attrs[st->attnum]->attndims);
+       TupleDescInitEntry(tupdesc, 1, "value",
+                       TS_GET_TYPEVAL(st, st->attnum, atttypid),
+                       TS_GET_TYPEVAL(st, st->attnum, atttypmod),
+                       TS_GET_TYPEVAL(st, st->attnum, attndims));
        TupleDescInitEntry(tupdesc, 2, "nrow", INT4OID, -1, 0);
 
-       memset( st->nulls, ' ', 2*sizeof(char) );
+       memset( st->nulls, ISNOTNULL, 2*sizeof(*st->nulls) );
 
        funcctx->slot = TupleDescGetSlot(tupdesc);
        funcctx->attinmeta = TupleDescGetAttInMetadata(tupdesc);
@@ -668,30 +699,40 @@ gin_setup_firstcall(FuncCallContext  *funcctx, text *name, int attnum) {
 
 static void
 processTuple( FuncCallContext  *funcctx,  GinStatState *st, IndexTuple itup ) {
-       MemoryContext           oldcontext;
-#if PG_VERSION_NUM < 80400
+       MemoryContext           oldcontext;
+#if PG_VERSION_NUM >= 90100
+       Datum                           key;
+#elif PG_VERSION_NUM < 80400
        bool                            isnull;
 #endif
 
        oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
+
+#if PG_VERSION_NUM >= 90100
+       key = gintuple_get_key(&st->ginstate, itup, &st->category);
+
+       if (st->category != GIN_CAT_NORM_KEY)
+               st->curval = (Datum)0;
+       else
+#endif
        st->curval = datumCopy(
 #if PG_VERSION_NUM >= 90100
-                                       gintuple_get_key(&st->ginstate, itup, &st->category),
+                                       key,
 #elif PG_VERSION_NUM >= 80400
                                        gin_index_getattr(&st->ginstate, itup),
 #else
                                        index_getattr(itup, FirstOffsetNumber, st->ginstate.tupdesc, &isnull),
 #endif
-                                       st->index->rd_att->attrs[st->attnum]->attbyval,
-                                       st->index->rd_att->attrs[st->attnum]->attlen );
+                                       TS_GET_TYPEVAL(st, st->attnum, attbyval),
+                                       TS_GET_TYPEVAL(st, st->attnum, attlen));
        MemoryContextSwitchTo(oldcontext);
 
        st->dvalues[0] = st->curval;
 #if PG_VERSION_NUM >= 90100
        /* do no distiguish various null category */
-       st->nulls[0] = (st->category == GIN_CAT_NORM_KEY) ? ' ' : 'n';
+       st->nulls[0] = (st->category == GIN_CAT_NORM_KEY) ? ISNOTNULL : ISNULL;
 #endif
-               
+
        if ( GinIsPostingTree(itup) ) {
                BlockNumber     rootblkno = GinGetPostingTree(itup);
 #if PG_VERSION_NUM >= 90400
@@ -702,32 +743,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);
@@ -761,7 +807,7 @@ gin_stat(PG_FUNCTION_ARGS) {
                PG_FREE_IF_COPY(name,0);
        }
 
-       funcctx = SRF_PERCALL_SETUP();  
+       funcctx = SRF_PERCALL_SETUP();
        st = (GinStatState*)(funcctx->user_fctx);
 
        if ( refindPosition(st) == false ) {
@@ -773,8 +819,8 @@ gin_stat(PG_FUNCTION_ARGS) {
 
        for(;;) {
                st->offset++;
-       
-               if (moveRightIfItNeeded(st)==false) { 
+
+               if (moveRightIfItNeeded(st)==false) {
                        UnlockReleaseBuffer( st->buffer );
                        gin_index_close(st->index);
 
@@ -782,7 +828,7 @@ gin_stat(PG_FUNCTION_ARGS) {
                }
 
                page = BufferGetPage(st->buffer);
-               ituple = (IndexTuple) PageGetItem(page, PageGetItemId(page, st->offset)); 
+               ituple = (IndexTuple) PageGetItem(page, PageGetItemId(page, st->offset));
 
 #if PG_VERSION_NUM >= 80400
                if (st->attnum + 1 == gintuple_get_attrnum(&st->ginstate, ituple))
@@ -791,7 +837,7 @@ gin_stat(PG_FUNCTION_ARGS) {
        }
 
        processTuple( funcctx,  st, ituple );
-       
+
        htuple = heap_formtuple(funcctx->attinmeta->tupdesc, st->dvalues, st->nulls);
        result = TupleGetDatum(funcctx->slot, htuple);
 
@@ -803,14 +849,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];
@@ -826,8 +876,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);
@@ -1007,8 +1057,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);
@@ -1020,16 +1070,20 @@ 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;
-       Datum           dvalues[7 /* see CreateTemplateTupleDesc call */];
-       char            nulls[7 /* see CreateTemplateTupleDesc call */];
+       Relation        index;
+       Datum           dvalues[8 /* see CreateTemplateTupleDesc call */];
+#if PG_VERSION_NUM >= 110000
+       bool                    nulls[8];
+#else
+       char                    nulls[8];
+#endif
        List            *stack;
 } SPGistPrint;
 
@@ -1044,7 +1098,7 @@ pushSPGistPrint(FuncCallContext *funcctx, SPGistPrint *prst, ItemPointer ip, int
        e->iptr = *ip;
        e->nlabel = 0;
        e->level = level;
-       prst->stack = lcons(e, prst->stack); 
+       prst->stack = lcons(e, prst->stack);
 
        MemoryContextSwitchTo(oldcontext);
 }
@@ -1073,8 +1127,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;
@@ -1094,16 +1148,17 @@ spgist_print(PG_FUNCTION_ARGS)
                prst->index = index;
                initSpGistState(&prst->state, index);
 
-               tupdesc = CreateTemplateTupleDesc(3 /* types */ + 1 /* level */ + 1 /* nlabel */ +  2 /* tids */, false);
+               tupdesc = CreateTemplateTupleDesc(3 /* types */ + 1 /* level */ + 1 /* nlabel */ +  2 /* tids */ + 1, false);
                TupleDescInitEntry(tupdesc, 1, "tid", TIDOID, -1, 0);
-               TupleDescInitEntry(tupdesc, 2, "node", INT4OID, -1, 0);
-               TupleDescInitEntry(tupdesc, 3, "level", INT4OID, -1, 0);
-               TupleDescInitEntry(tupdesc, 4, "tid_pointer", TIDOID, -1, 0);
-               TupleDescInitEntry(tupdesc, 5, "prefix", 
+               TupleDescInitEntry(tupdesc, 2, "allthesame", BOOLOID, -1, 0);
+               TupleDescInitEntry(tupdesc, 3, "node", INT4OID, -1, 0);
+               TupleDescInitEntry(tupdesc, 4, "level", INT4OID, -1, 0);
+               TupleDescInitEntry(tupdesc, 5, "tid_pointer", TIDOID, -1, 0);
+               TupleDescInitEntry(tupdesc, 6, "prefix",
                                (prst->state.attPrefixType.type == VOIDOID) ? INT4OID : prst->state.attPrefixType.type, -1, 0);
-               TupleDescInitEntry(tupdesc, 6, "label", 
+               TupleDescInitEntry(tupdesc, 7, "label",
                                (prst->state.attLabelType.type == VOIDOID) ? INT4OID : prst->state.attLabelType.type, -1, 0);
-               TupleDescInitEntry(tupdesc, 7, "leaf", 
+               TupleDescInitEntry(tupdesc, 8, "leaf",
                                (prst->state.attType.type == VOIDOID) ? INT4OID : prst->state.attType.type, -1, 0);
 
                funcctx->slot = TupleDescGetSlot(tupdesc);
@@ -1120,7 +1175,7 @@ spgist_print(PG_FUNCTION_ARGS)
                PG_FREE_IF_COPY(name,0);
        }
 
-       funcctx = SRF_PERCALL_SETUP();  
+       funcctx = SRF_PERCALL_SETUP();
        prst = (SPGistPrint*)(funcctx->user_fctx);
 
 next:
@@ -1170,19 +1225,20 @@ next:
                                tid = palloc(sizeof(ItemPointerData));
                                *tid = s->iptr;
                                prst->dvalues[0] = PointerGetDatum(tid);
-                               prst->nulls[0] = ' ';
-                               prst->nulls[1] = 'n';
-                               prst->dvalues[2]  = s->level; 
-                               prst->nulls[2] = ' ';
-                               prst->nulls[3] = 'n';
-                               prst->nulls[4] = 'n';
-                               prst->nulls[5] = 'n';
-                               prst->dvalues[6]  = datumCopy(SGLTDATUM(leafTuple, &prst->state), 
-                                                                                       prst->state.attType.attbyval, prst->state.attType.attlen); 
-                               prst->nulls[6] = ' ';
+                               prst->nulls[0] = ISNOTNULL;
+                               prst->nulls[1] = ISNULL;
+                               prst->nulls[2] = ISNULL;
+                               prst->dvalues[3]  = s->level;
+                               prst->nulls[3] = ISNOTNULL;
+                               prst->nulls[4] = ISNULL;
+                               prst->nulls[5] = ISNULL;
+                               prst->nulls[6] = ISNULL;
+                               prst->dvalues[7]  = datumCopy(SGLTDATUM(leafTuple, &prst->state),
+                                                                                       prst->state.attType.attbyval, prst->state.attType.attlen);
+                               prst->nulls[7] = ISNOTNULL;
                } else {
                        SpGistInnerTuple        innerTuple = (SpGistInnerTuple)dtuple;
-                       int                             i;
+                       int                                     i;
                        SpGistNodeTuple         node;
 
                        SGITITERATE(innerTuple, i, node) {
@@ -1201,28 +1257,30 @@ next:
                        tid = palloc(sizeof(ItemPointerData));
                        *tid = s->iptr;
                        prst->dvalues[0] = PointerGetDatum(tid);
-                       prst->nulls[0] = ' ';
-                       prst->dvalues[1] = Int32GetDatum(s->nlabel);
-                       prst->nulls[1] = ' ';
-                       prst->dvalues[2]  = s->level; 
-                       prst->nulls[2] = ' ';
+                       prst->nulls[0] = ISNOTNULL;
+                       prst->dvalues[1] = innerTuple->allTheSame;
+                       prst->nulls[1] = ISNOTNULL;
+                       prst->dvalues[2] = Int32GetDatum(s->nlabel);
+                       prst->nulls[2] = ISNOTNULL;
+                       prst->dvalues[3]  = s->level;
+                       prst->nulls[3] = ISNOTNULL;
                        tid = palloc(sizeof(ItemPointerData));
                        *tid = node->t_tid;
-                       prst->dvalues[3] = PointerGetDatum(tid);
-                       prst->nulls[3] = ' ';
+                       prst->dvalues[4] = PointerGetDatum(tid);
+                       prst->nulls[5] = ISNOTNULL;
                        if (innerTuple->prefixSize > 0) {
-                               prst->dvalues[4]  = datumCopy(SGITDATUM(innerTuple, &prst->state), 
-                                                                                       prst->state.attPrefixType.attbyval, prst->state.attPrefixType.attlen); 
-                               prst->nulls[4] = ' ';
+                               prst->dvalues[5]  = datumCopy(SGITDATUM(innerTuple, &prst->state),
+                                                                                       prst->state.attPrefixType.attbyval, prst->state.attPrefixType.attlen);
+                               prst->nulls[5] = ISNOTNULL;
                        } else
-                               prst->nulls[4] = 'n';
+                               prst->nulls[5] = ISNULL;
                        if (!IndexTupleHasNulls(node)) {
-                               prst->dvalues[5]  = datumCopy(SGNTDATUM(node, &prst->state), 
-                                                                                       prst->state.attLabelType.attbyval, prst->state.attLabelType.attlen); 
-                               prst->nulls[5] = ' ';
+                               prst->dvalues[6]  = datumCopy(SGNTDATUM(node, &prst->state),
+                                                                                       prst->state.attLabelType.attbyval, prst->state.attLabelType.attlen);
+                               prst->nulls[6] = ISNOTNULL;
                        } else
-                               prst->nulls[5] = 'n';
-                       prst->nulls[6] = 'n';
+                               prst->nulls[6] = ISNULL;
+                       prst->nulls[7] = ISNULL;
 
                        pushSPGistPrint(funcctx, prst, &node->t_tid, s->level + 1);
                        s->nlabel = i + 1;
@@ -1260,6 +1318,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,
@@ -1302,19 +1364,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
                        {
@@ -1370,6 +1448,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"
@@ -1389,6 +1471,10 @@ gin_statpage(PG_FUNCTION_ARGS)
                         "entryAttrSize:         " INT64_FORMAT "\n"
                         ,
                         totalPages,
+#if PG_VERSION_NUM >= 100000
+                        deletedPages,
+                        emptyDataPages,
+#endif
                         dataPages,
                         dataInnerPages,
                         dataLeafPages,