|
| 1 | +/* |
| 2 | + * gistfuncs.c |
| 3 | + *Functions to investigate the content of GiST indexes |
| 4 | + * |
| 5 | + * Copyright (c) 2014-2020, PostgreSQL Global Development Group |
| 6 | + * |
| 7 | + * IDENTIFICATION |
| 8 | + *contrib/pageinspect/gistfuncs.c |
| 9 | + */ |
| 10 | +#include"postgres.h" |
| 11 | + |
| 12 | +#include"access/gist.h" |
| 13 | +#include"access/gist_private.h" |
| 14 | +#include"access/htup.h" |
| 15 | +#include"access/relation.h" |
| 16 | +#include"catalog/namespace.h" |
| 17 | +#include"funcapi.h" |
| 18 | +#include"miscadmin.h" |
| 19 | +#include"pageinspect.h" |
| 20 | +#include"storage/itemptr.h" |
| 21 | +#include"utils/array.h" |
| 22 | +#include"utils/builtins.h" |
| 23 | +#include"utils/rel.h" |
| 24 | +#include"utils/pg_lsn.h" |
| 25 | +#include"utils/varlena.h" |
| 26 | + |
| 27 | +PG_FUNCTION_INFO_V1(gist_page_opaque_info); |
| 28 | +PG_FUNCTION_INFO_V1(gist_page_items); |
| 29 | +PG_FUNCTION_INFO_V1(gist_page_items_bytea); |
| 30 | + |
| 31 | +#defineItemPointerGetDatum(X) PointerGetDatum(X) |
| 32 | + |
| 33 | + |
| 34 | +Datum |
| 35 | +gist_page_opaque_info(PG_FUNCTION_ARGS) |
| 36 | +{ |
| 37 | +bytea*raw_page=PG_GETARG_BYTEA_P(0); |
| 38 | +TupleDesctupdesc; |
| 39 | +Pagepage; |
| 40 | +GISTPageOpaqueopaq; |
| 41 | +HeapTupleresultTuple; |
| 42 | +Datumvalues[4]; |
| 43 | +boolnulls[4]; |
| 44 | +Datumflags[16]; |
| 45 | +intnflags=0; |
| 46 | +uint16flagbits; |
| 47 | + |
| 48 | +if (!superuser()) |
| 49 | +ereport(ERROR, |
| 50 | +(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 51 | +errmsg("must be superuser to use raw page functions"))); |
| 52 | + |
| 53 | +page=get_page_from_raw(raw_page); |
| 54 | + |
| 55 | +opaq= (GISTPageOpaque)PageGetSpecialPointer(page); |
| 56 | + |
| 57 | +/* Build a tuple descriptor for our result type */ |
| 58 | +if (get_call_result_type(fcinfo,NULL,&tupdesc)!=TYPEFUNC_COMPOSITE) |
| 59 | +elog(ERROR,"return type must be a row type"); |
| 60 | + |
| 61 | +/* Convert the flags bitmask to an array of human-readable names */ |
| 62 | +flagbits=opaq->flags; |
| 63 | +if (flagbits&F_LEAF) |
| 64 | +flags[nflags++]=CStringGetTextDatum("leaf"); |
| 65 | +if (flagbits&F_DELETED) |
| 66 | +flags[nflags++]=CStringGetTextDatum("deleted"); |
| 67 | +if (flagbits&F_TUPLES_DELETED) |
| 68 | +flags[nflags++]=CStringGetTextDatum("tuples_deleted"); |
| 69 | +if (flagbits&F_FOLLOW_RIGHT) |
| 70 | +flags[nflags++]=CStringGetTextDatum("follow_right"); |
| 71 | +if (flagbits&F_HAS_GARBAGE) |
| 72 | +flags[nflags++]=CStringGetTextDatum("has_garbage"); |
| 73 | +flagbits &= ~(F_LEAF |F_DELETED |F_TUPLES_DELETED |F_FOLLOW_RIGHT |F_HAS_GARBAGE); |
| 74 | +if (flagbits) |
| 75 | +{ |
| 76 | +/* any flags we don't recognize are printed in hex */ |
| 77 | +flags[nflags++]=DirectFunctionCall1(to_hex32,Int32GetDatum(flagbits)); |
| 78 | +} |
| 79 | + |
| 80 | +memset(nulls,0,sizeof(nulls)); |
| 81 | + |
| 82 | +values[0]=LSNGetDatum(PageGetLSN(page)); |
| 83 | +values[1]=LSNGetDatum(GistPageGetNSN(page)); |
| 84 | +values[2]=Int64GetDatum(opaq->rightlink); |
| 85 | +values[3]=PointerGetDatum(construct_array(flags,nflags, |
| 86 | +TEXTOID, |
| 87 | +-1, false,TYPALIGN_INT)); |
| 88 | + |
| 89 | +/* Build and return the result tuple. */ |
| 90 | +resultTuple=heap_form_tuple(tupdesc,values,nulls); |
| 91 | + |
| 92 | +returnHeapTupleGetDatum(resultTuple); |
| 93 | +} |
| 94 | + |
| 95 | +typedefstructgist_page_items_state |
| 96 | +{ |
| 97 | +Pagepage; |
| 98 | +TupleDesctupd; |
| 99 | +OffsetNumberoffset; |
| 100 | +Relationrel; |
| 101 | +}gist_page_items_state; |
| 102 | + |
| 103 | +Datum |
| 104 | +gist_page_items_bytea(PG_FUNCTION_ARGS) |
| 105 | +{ |
| 106 | +bytea*raw_page=PG_GETARG_BYTEA_P(0); |
| 107 | +FuncCallContext*fctx; |
| 108 | +gist_page_items_state*inter_call_data; |
| 109 | + |
| 110 | +if (!superuser()) |
| 111 | +ereport(ERROR, |
| 112 | +(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 113 | +errmsg("must be superuser to use raw page functions"))); |
| 114 | + |
| 115 | +if (SRF_IS_FIRSTCALL()) |
| 116 | +{ |
| 117 | +TupleDesctupdesc; |
| 118 | +MemoryContextmctx; |
| 119 | +Pagepage; |
| 120 | + |
| 121 | +fctx=SRF_FIRSTCALL_INIT(); |
| 122 | +mctx=MemoryContextSwitchTo(fctx->multi_call_memory_ctx); |
| 123 | + |
| 124 | +page=get_page_from_raw(raw_page); |
| 125 | + |
| 126 | +inter_call_data=palloc(sizeof(gist_page_items_state)); |
| 127 | + |
| 128 | +/* Build a tuple descriptor for our result type */ |
| 129 | +if (get_call_result_type(fcinfo,NULL,&tupdesc)!=TYPEFUNC_COMPOSITE) |
| 130 | +elog(ERROR,"return type must be a row type"); |
| 131 | + |
| 132 | +if (GistPageIsDeleted(page)) |
| 133 | +elog(NOTICE,"page is deleted"); |
| 134 | + |
| 135 | +inter_call_data->page=page; |
| 136 | +inter_call_data->tupd=tupdesc; |
| 137 | +inter_call_data->offset=FirstOffsetNumber; |
| 138 | + |
| 139 | +fctx->max_calls=PageGetMaxOffsetNumber(page); |
| 140 | +fctx->user_fctx=inter_call_data; |
| 141 | + |
| 142 | +MemoryContextSwitchTo(mctx); |
| 143 | +} |
| 144 | + |
| 145 | +fctx=SRF_PERCALL_SETUP(); |
| 146 | +inter_call_data=fctx->user_fctx; |
| 147 | + |
| 148 | +if (fctx->call_cntr<fctx->max_calls) |
| 149 | +{ |
| 150 | +Pagepage=inter_call_data->page; |
| 151 | +OffsetNumberoffset=inter_call_data->offset; |
| 152 | +HeapTupleresultTuple; |
| 153 | +Datumresult; |
| 154 | +Datumvalues[4]; |
| 155 | +boolnulls[4]; |
| 156 | +ItemIdid; |
| 157 | +IndexTupleitup; |
| 158 | +bytea*tuple_bytea; |
| 159 | +inttuple_len; |
| 160 | + |
| 161 | +id=PageGetItemId(page,offset); |
| 162 | + |
| 163 | +if (!ItemIdIsValid(id)) |
| 164 | +elog(ERROR,"invalid ItemId"); |
| 165 | + |
| 166 | +itup= (IndexTuple)PageGetItem(page,id); |
| 167 | +tuple_len=IndexTupleSize(itup); |
| 168 | + |
| 169 | +memset(nulls,0,sizeof(nulls)); |
| 170 | + |
| 171 | +values[0]=DatumGetInt16(offset); |
| 172 | +values[1]=ItemPointerGetDatum(&itup->t_tid); |
| 173 | +values[2]=Int32GetDatum((int)IndexTupleSize(itup)); |
| 174 | + |
| 175 | +tuple_bytea= (bytea*)palloc(tuple_len+VARHDRSZ); |
| 176 | +SET_VARSIZE(tuple_bytea,tuple_len+VARHDRSZ); |
| 177 | +memcpy(VARDATA(tuple_bytea),itup,tuple_len); |
| 178 | +values[3]=PointerGetDatum(tuple_bytea); |
| 179 | + |
| 180 | +/* Build and return the result tuple. */ |
| 181 | +resultTuple=heap_form_tuple(inter_call_data->tupd,values,nulls); |
| 182 | +result=HeapTupleGetDatum(resultTuple); |
| 183 | + |
| 184 | +inter_call_data->offset++; |
| 185 | +SRF_RETURN_NEXT(fctx,result); |
| 186 | +} |
| 187 | + |
| 188 | +SRF_RETURN_DONE(fctx); |
| 189 | +} |
| 190 | + |
| 191 | +Datum |
| 192 | +gist_page_items(PG_FUNCTION_ARGS) |
| 193 | +{ |
| 194 | +bytea*raw_page=PG_GETARG_BYTEA_P(0); |
| 195 | +OidindexRelid=PG_GETARG_OID(1); |
| 196 | +FuncCallContext*fctx; |
| 197 | +gist_page_items_state*inter_call_data; |
| 198 | + |
| 199 | +if (!superuser()) |
| 200 | +ereport(ERROR, |
| 201 | +(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE), |
| 202 | +errmsg("must be superuser to use raw page functions"))); |
| 203 | + |
| 204 | +if (SRF_IS_FIRSTCALL()) |
| 205 | +{ |
| 206 | +RelationindexRel; |
| 207 | +TupleDesctupdesc; |
| 208 | +MemoryContextmctx; |
| 209 | +Pagepage; |
| 210 | + |
| 211 | +fctx=SRF_FIRSTCALL_INIT(); |
| 212 | +mctx=MemoryContextSwitchTo(fctx->multi_call_memory_ctx); |
| 213 | + |
| 214 | +page=get_page_from_raw(raw_page); |
| 215 | + |
| 216 | +inter_call_data=palloc(sizeof(gist_page_items_state)); |
| 217 | + |
| 218 | +/* Open the relation */ |
| 219 | +indexRel=index_open(indexRelid,AccessShareLock); |
| 220 | + |
| 221 | +/* Build a tuple descriptor for our result type */ |
| 222 | +if (get_call_result_type(fcinfo,NULL,&tupdesc)!=TYPEFUNC_COMPOSITE) |
| 223 | +elog(ERROR,"return type must be a row type"); |
| 224 | + |
| 225 | +if (GistPageIsDeleted(page)) |
| 226 | +elog(NOTICE,"page is deleted"); |
| 227 | + |
| 228 | +inter_call_data->page=page; |
| 229 | +inter_call_data->tupd=tupdesc; |
| 230 | +inter_call_data->offset=FirstOffsetNumber; |
| 231 | +inter_call_data->rel=indexRel; |
| 232 | + |
| 233 | +fctx->max_calls=PageGetMaxOffsetNumber(page); |
| 234 | +fctx->user_fctx=inter_call_data; |
| 235 | + |
| 236 | +MemoryContextSwitchTo(mctx); |
| 237 | +} |
| 238 | + |
| 239 | +fctx=SRF_PERCALL_SETUP(); |
| 240 | +inter_call_data=fctx->user_fctx; |
| 241 | + |
| 242 | +if (fctx->call_cntr<fctx->max_calls) |
| 243 | +{ |
| 244 | +Pagepage=inter_call_data->page; |
| 245 | +OffsetNumberoffset=inter_call_data->offset; |
| 246 | +HeapTupleresultTuple; |
| 247 | +Datumresult; |
| 248 | +Datumvalues[4]; |
| 249 | +boolnulls[4]; |
| 250 | +ItemIdid; |
| 251 | +IndexTupleitup; |
| 252 | +Datumitup_values[INDEX_MAX_KEYS]; |
| 253 | +boolitup_isnull[INDEX_MAX_KEYS]; |
| 254 | +char*key_desc; |
| 255 | + |
| 256 | +id=PageGetItemId(page,offset); |
| 257 | + |
| 258 | +if (!ItemIdIsValid(id)) |
| 259 | +elog(ERROR,"invalid ItemId"); |
| 260 | + |
| 261 | +itup= (IndexTuple)PageGetItem(page,id); |
| 262 | + |
| 263 | +index_deform_tuple(itup,RelationGetDescr(inter_call_data->rel), |
| 264 | +itup_values,itup_isnull); |
| 265 | + |
| 266 | +key_desc=BuildIndexValueDescription(inter_call_data->rel,itup_values, |
| 267 | +itup_isnull); |
| 268 | + |
| 269 | +memset(nulls,0,sizeof(nulls)); |
| 270 | + |
| 271 | +values[0]=DatumGetInt16(offset); |
| 272 | +values[1]=ItemPointerGetDatum(&itup->t_tid); |
| 273 | +values[2]=Int32GetDatum((int)IndexTupleSize(itup)); |
| 274 | +values[3]=CStringGetTextDatum(key_desc); |
| 275 | + |
| 276 | +/* Build and return the result tuple. */ |
| 277 | +resultTuple=heap_form_tuple(inter_call_data->tupd,values,nulls); |
| 278 | +result=HeapTupleGetDatum(resultTuple); |
| 279 | + |
| 280 | +inter_call_data->offset++; |
| 281 | +SRF_RETURN_NEXT(fctx,result); |
| 282 | +} |
| 283 | + |
| 284 | +relation_close(inter_call_data->rel,AccessShareLock); |
| 285 | + |
| 286 | +SRF_RETURN_DONE(fctx); |
| 287 | +} |