PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
ginfuncs.c
Go to the documentation of this file.
1 /*
2  * ginfuncs.c
3  * Functions to investigate the content of GIN indexes
4  *
5  * Copyright (c) 2014-2017, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * contrib/pageinspect/ginfuncs.c
9  */
10 #include "postgres.h"
11 
12 #include "pageinspect.h"
13 
14 #include "access/gin.h"
15 #include "access/gin_private.h"
16 #include "access/htup_details.h"
17 #include "catalog/namespace.h"
18 #include "catalog/pg_type.h"
19 #include "funcapi.h"
20 #include "miscadmin.h"
21 #include "utils/array.h"
22 #include "utils/builtins.h"
23 #include "utils/rel.h"
24 
25 #define DatumGetItemPointer(X) ((ItemPointer) DatumGetPointer(X))
26 #define ItemPointerGetDatum(X) PointerGetDatum(X)
27 
28 
32 
33 
34 Datum
36 {
37  bytea *raw_page = PG_GETARG_BYTEA_P(0);
38  TupleDesc tupdesc;
39  Page page;
40  GinPageOpaque opaq;
41  GinMetaPageData *metadata;
42  HeapTuple resultTuple;
43  Datum values[10];
44  bool nulls[10];
45 
46  if (!superuser())
47  ereport(ERROR,
48  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
49  (errmsg("must be superuser to use raw page functions"))));
50 
51  page = get_page_from_raw(raw_page);
52 
53  opaq = (GinPageOpaque) PageGetSpecialPointer(page);
54  if (opaq->flags != GIN_META)
55  ereport(ERROR,
56  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
57  errmsg("input page is not a GIN metapage"),
58  errdetail("Flags %04X, expected %04X",
59  opaq->flags, GIN_META)));
60 
61  /* Build a tuple descriptor for our result type */
62  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
63  elog(ERROR, "return type must be a row type");
64 
65  metadata = GinPageGetMeta(page);
66 
67  memset(nulls, 0, sizeof(nulls));
68 
69  values[0] = Int64GetDatum(metadata->head);
70  values[1] = Int64GetDatum(metadata->tail);
71  values[2] = Int32GetDatum(metadata->tailFreeSize);
72  values[3] = Int64GetDatum(metadata->nPendingPages);
73  values[4] = Int64GetDatum(metadata->nPendingHeapTuples);
74 
75  /* statistics, updated by VACUUM */
76  values[5] = Int64GetDatum(metadata->nTotalPages);
77  values[6] = Int64GetDatum(metadata->nEntryPages);
78  values[7] = Int64GetDatum(metadata->nDataPages);
79  values[8] = Int64GetDatum(metadata->nEntries);
80 
81  values[9] = Int32GetDatum(metadata->ginVersion);
82 
83  /* Build and return the result tuple. */
84  resultTuple = heap_form_tuple(tupdesc, values, nulls);
85 
86  return HeapTupleGetDatum(resultTuple);
87 }
88 
89 
90 Datum
92 {
93  bytea *raw_page = PG_GETARG_BYTEA_P(0);
94  TupleDesc tupdesc;
95  Page page;
96  GinPageOpaque opaq;
97  HeapTuple resultTuple;
98  Datum values[3];
99  bool nulls[3];
100  Datum flags[16];
101  int nflags = 0;
102  uint16 flagbits;
103 
104  if (!superuser())
105  ereport(ERROR,
106  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
107  (errmsg("must be superuser to use raw page functions"))));
108 
109  page = get_page_from_raw(raw_page);
110 
111  opaq = (GinPageOpaque) PageGetSpecialPointer(page);
112 
113  /* Build a tuple descriptor for our result type */
114  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
115  elog(ERROR, "return type must be a row type");
116 
117  /* Convert the flags bitmask to an array of human-readable names */
118  flagbits = opaq->flags;
119  if (flagbits & GIN_DATA)
120  flags[nflags++] = CStringGetTextDatum("data");
121  if (flagbits & GIN_LEAF)
122  flags[nflags++] = CStringGetTextDatum("leaf");
123  if (flagbits & GIN_DELETED)
124  flags[nflags++] = CStringGetTextDatum("deleted");
125  if (flagbits & GIN_META)
126  flags[nflags++] = CStringGetTextDatum("meta");
127  if (flagbits & GIN_LIST)
128  flags[nflags++] = CStringGetTextDatum("list");
129  if (flagbits & GIN_LIST_FULLROW)
130  flags[nflags++] = CStringGetTextDatum("list_fullrow");
131  if (flagbits & GIN_INCOMPLETE_SPLIT)
132  flags[nflags++] = CStringGetTextDatum("incomplete_split");
133  if (flagbits & GIN_COMPRESSED)
134  flags[nflags++] = CStringGetTextDatum("compressed");
135  flagbits &= ~(GIN_DATA | GIN_LEAF | GIN_DELETED | GIN_META | GIN_LIST |
136  GIN_LIST_FULLROW | GIN_INCOMPLETE_SPLIT | GIN_COMPRESSED);
137  if (flagbits)
138  {
139  /* any flags we don't recognize are printed in hex */
140  flags[nflags++] = DirectFunctionCall1(to_hex32, Int32GetDatum(flagbits));
141  }
142 
143  memset(nulls, 0, sizeof(nulls));
144 
145  values[0] = Int64GetDatum(opaq->rightlink);
146  values[1] = Int32GetDatum(opaq->maxoff);
147  values[2] = PointerGetDatum(construct_array(flags, nflags,
148  TEXTOID, -1, false, 'i'));
149 
150  /* Build and return the result tuple. */
151  resultTuple = heap_form_tuple(tupdesc, values, nulls);
152 
153  return HeapTupleGetDatum(resultTuple);
154 }
155 
157 {
162 
163 Datum
165 {
166  bytea *raw_page = PG_GETARG_BYTEA_P(0);
167  FuncCallContext *fctx;
168  gin_leafpage_items_state *inter_call_data;
169 
170  if (!superuser())
171  ereport(ERROR,
172  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
173  (errmsg("must be superuser to use raw page functions"))));
174 
175  if (SRF_IS_FIRSTCALL())
176  {
177  TupleDesc tupdesc;
178  MemoryContext mctx;
179  Page page;
180  GinPageOpaque opaq;
181 
182  fctx = SRF_FIRSTCALL_INIT();
184 
185  page = get_page_from_raw(raw_page);
186 
187  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
188  ereport(ERROR,
189  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
190  errmsg("input page is not a valid GIN data leaf page"),
191  errdetail("Special size %d, expected %d",
192  (int) PageGetSpecialSize(page),
193  (int) MAXALIGN(sizeof(GinPageOpaqueData)))));
194 
195  opaq = (GinPageOpaque) PageGetSpecialPointer(page);
196  if (opaq->flags != (GIN_DATA | GIN_LEAF | GIN_COMPRESSED))
197  ereport(ERROR,
198  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
199  errmsg("input page is not a compressed GIN data leaf page"),
200  errdetail("Flags %04X, expected %04X",
201  opaq->flags,
203 
204  inter_call_data = palloc(sizeof(gin_leafpage_items_state));
205 
206  /* Build a tuple descriptor for our result type */
207  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
208  elog(ERROR, "return type must be a row type");
209 
210  inter_call_data->tupd = tupdesc;
211 
212  inter_call_data->seg = GinDataLeafPageGetPostingList(page);
213  inter_call_data->lastseg = (GinPostingList *)
214  (((char *) inter_call_data->seg) +
216 
217  fctx->user_fctx = inter_call_data;
218 
219  MemoryContextSwitchTo(mctx);
220  }
221 
222  fctx = SRF_PERCALL_SETUP();
223  inter_call_data = fctx->user_fctx;
224 
225  if (inter_call_data->seg != inter_call_data->lastseg)
226  {
227  GinPostingList *cur = inter_call_data->seg;
228  HeapTuple resultTuple;
229  Datum result;
230  Datum values[3];
231  bool nulls[3];
232  int ndecoded,
233  i;
234  ItemPointer tids;
235  Datum *tids_datum;
236 
237  memset(nulls, 0, sizeof(nulls));
238 
239  values[0] = ItemPointerGetDatum(&cur->first);
240  values[1] = UInt16GetDatum(cur->nbytes);
241 
242  /* build an array of decoded item pointers */
243  tids = ginPostingListDecode(cur, &ndecoded);
244  tids_datum = (Datum *) palloc(ndecoded * sizeof(Datum));
245  for (i = 0; i < ndecoded; i++)
246  tids_datum[i] = ItemPointerGetDatum(&tids[i]);
247  values[2] = PointerGetDatum(construct_array(tids_datum,
248  ndecoded,
249  TIDOID,
250  sizeof(ItemPointerData),
251  false, 's'));
252  pfree(tids_datum);
253  pfree(tids);
254 
255  /* Build and return the result tuple. */
256  resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls);
257  result = HeapTupleGetDatum(resultTuple);
258 
259  inter_call_data->seg = GinNextPostingListSegment(cur);
260 
261  SRF_RETURN_NEXT(fctx, result);
262  }
263  else
264  SRF_RETURN_DONE(fctx);
265 }
BlockNumber nEntryPages
Definition: ginblock.h:79
#define GIN_DELETED
Definition: ginblock.h:41
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
ItemPointer ginPostingListDecode(GinPostingList *plist, int *ndecoded)
#define TEXTOID
Definition: pg_type.h:324
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:562
Datum gin_metapage_info(PG_FUNCTION_ARGS)
Definition: ginfuncs.c:35
BlockNumber rightlink
Definition: ginblock.h:30
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3279
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
GinPostingList * seg
Definition: ginfuncs.c:159
BlockNumber nTotalPages
Definition: ginblock.h:78
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:695
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
int64 nEntries
Definition: ginblock.h:81
#define GIN_COMPRESSED
Definition: ginblock.h:47
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define GIN_DATA
Definition: ginblock.h:39
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:299
int64 nPendingHeapTuples
Definition: ginblock.h:73
#define GIN_LIST_FULLROW
Definition: ginblock.h:44
#define TIDOID
Definition: pg_type.h:332
unsigned short uint16
Definition: c.h:257
ItemPointerData first
Definition: ginblock.h:328
void pfree(void *pointer)
Definition: mcxt.c:949
#define GinNextPostingListSegment(cur)
Definition: ginblock.h:334
#define ERROR
Definition: elog.h:43
#define GIN_META
Definition: ginblock.h:42
#define GinDataLeafPageGetPostingListSize(page)
Definition: ginblock.h:271
GinPostingList * lastseg
Definition: ginfuncs.c:160
PG_FUNCTION_INFO_V1(gin_metapage_info)
BlockNumber head
Definition: ginblock.h:60
#define GIN_LIST
Definition: ginblock.h:43
BlockNumber tail
Definition: ginblock.h:61
int errdetail(const char *fmt,...)
Definition: elog.c:873
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1786
OffsetNumber maxoff
Definition: ginblock.h:31
#define ereport(elevel, rest)
Definition: elog.h:122
GinPageOpaqueData * GinPageOpaque
Definition: ginblock.h:37
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:269
uintptr_t Datum
Definition: postgres.h:372
Page page
Definition: btreefuncs.c:245
Datum to_hex32(PG_FUNCTION_ARGS)
Definition: varlena.c:4525
uint16 nbytes
Definition: ginblock.h:329
uint32 tailFreeSize
Definition: ginblock.h:66
Page get_page_from_raw(bytea *raw_page)
Definition: rawpage.c:183
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
#define PageGetSpecialPointer(page)
Definition: bufpage.h:322
#define MAXALIGN(LEN)
Definition: c.h:576
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
#define PageGetSpecialSize(page)
Definition: bufpage.h:296
int32 ginVersion
Definition: ginblock.h:98
static Datum values[MAXATTR]
Definition: bootstrap.c:164
#define GIN_INCOMPLETE_SPLIT
Definition: ginblock.h:45
#define Int32GetDatum(X)
Definition: postgres.h:485
void * user_fctx
Definition: funcapi.h:90
struct gin_leafpage_items_state gin_leafpage_items_state
void * palloc(Size size)
Definition: mcxt.c:848
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum gin_page_opaque_info(PG_FUNCTION_ARGS)
Definition: ginfuncs.c:91
int i
#define ItemPointerGetDatum(X)
Definition: ginfuncs.c:26
#define CStringGetTextDatum(s)
Definition: builtins.h:91
BlockNumber nDataPages
Definition: ginblock.h:80
Datum gin_leafpage_items(PG_FUNCTION_ARGS)
Definition: ginfuncs.c:164
Definition: c.h:433
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
#define GinPageGetMeta(p)
Definition: ginblock.h:103
#define elog
Definition: elog.h:219
#define UInt16GetDatum(X)
Definition: postgres.h:471
Pointer Page
Definition: bufpage.h:74
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define GIN_LEAF
Definition: ginblock.h:40
BlockNumber nPendingPages
Definition: ginblock.h:72
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287