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