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-2022, 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  if (PageIsNew(page))
54 
55  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
56  ereport(ERROR,
57  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
58  errmsg("input page is not a valid GIN metapage"),
59  errdetail("Expected special size %d, got %d.",
60  (int) MAXALIGN(sizeof(GinPageOpaqueData)),
61  (int) PageGetSpecialSize(page))));
62 
63  opaq = GinPageGetOpaque(page);
64 
65  if (opaq->flags != GIN_META)
66  ereport(ERROR,
67  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
68  errmsg("input page is not a GIN metapage"),
69  errdetail("Flags %04X, expected %04X",
70  opaq->flags, GIN_META)));
71 
72  /* Build a tuple descriptor for our result type */
73  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
74  elog(ERROR, "return type must be a row type");
75 
76  metadata = GinPageGetMeta(page);
77 
78  memset(nulls, 0, sizeof(nulls));
79 
80  values[0] = Int64GetDatum(metadata->head);
81  values[1] = Int64GetDatum(metadata->tail);
82  values[2] = Int32GetDatum(metadata->tailFreeSize);
83  values[3] = Int64GetDatum(metadata->nPendingPages);
84  values[4] = Int64GetDatum(metadata->nPendingHeapTuples);
85 
86  /* statistics, updated by VACUUM */
87  values[5] = Int64GetDatum(metadata->nTotalPages);
88  values[6] = Int64GetDatum(metadata->nEntryPages);
89  values[7] = Int64GetDatum(metadata->nDataPages);
90  values[8] = Int64GetDatum(metadata->nEntries);
91 
92  values[9] = Int32GetDatum(metadata->ginVersion);
93 
94  /* Build and return the result tuple. */
95  resultTuple = heap_form_tuple(tupdesc, values, nulls);
96 
97  return HeapTupleGetDatum(resultTuple);
98 }
99 
100 
101 Datum
103 {
104  bytea *raw_page = PG_GETARG_BYTEA_P(0);
105  TupleDesc tupdesc;
106  Page page;
107  GinPageOpaque opaq;
108  HeapTuple resultTuple;
109  Datum values[3];
110  bool nulls[3];
111  Datum flags[16];
112  int nflags = 0;
113  uint16 flagbits;
114 
115  if (!superuser())
116  ereport(ERROR,
117  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
118  errmsg("must be superuser to use raw page functions")));
119 
120  page = get_page_from_raw(raw_page);
121 
122  if (PageIsNew(page))
123  PG_RETURN_NULL();
124 
125  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
126  ereport(ERROR,
127  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
128  errmsg("input page is not a valid GIN data leaf page"),
129  errdetail("Expected special size %d, got %d.",
130  (int) MAXALIGN(sizeof(GinPageOpaqueData)),
131  (int) PageGetSpecialSize(page))));
132 
133  opaq = GinPageGetOpaque(page);
134 
135  /* Build a tuple descriptor for our result type */
136  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
137  elog(ERROR, "return type must be a row type");
138 
139  /* Convert the flags bitmask to an array of human-readable names */
140  flagbits = opaq->flags;
141  if (flagbits & GIN_DATA)
142  flags[nflags++] = CStringGetTextDatum("data");
143  if (flagbits & GIN_LEAF)
144  flags[nflags++] = CStringGetTextDatum("leaf");
145  if (flagbits & GIN_DELETED)
146  flags[nflags++] = CStringGetTextDatum("deleted");
147  if (flagbits & GIN_META)
148  flags[nflags++] = CStringGetTextDatum("meta");
149  if (flagbits & GIN_LIST)
150  flags[nflags++] = CStringGetTextDatum("list");
151  if (flagbits & GIN_LIST_FULLROW)
152  flags[nflags++] = CStringGetTextDatum("list_fullrow");
153  if (flagbits & GIN_INCOMPLETE_SPLIT)
154  flags[nflags++] = CStringGetTextDatum("incomplete_split");
155  if (flagbits & GIN_COMPRESSED)
156  flags[nflags++] = CStringGetTextDatum("compressed");
157  flagbits &= ~(GIN_DATA | GIN_LEAF | GIN_DELETED | GIN_META | GIN_LIST |
159  if (flagbits)
160  {
161  /* any flags we don't recognize are printed in hex */
162  flags[nflags++] = DirectFunctionCall1(to_hex32, Int32GetDatum(flagbits));
163  }
164 
165  memset(nulls, 0, sizeof(nulls));
166 
167  values[0] = Int64GetDatum(opaq->rightlink);
168  values[1] = Int32GetDatum(opaq->maxoff);
169  values[2] = PointerGetDatum(construct_array(flags, nflags,
170  TEXTOID,
171  -1, false, TYPALIGN_INT));
172 
173  /* Build and return the result tuple. */
174  resultTuple = heap_form_tuple(tupdesc, values, nulls);
175 
176  return HeapTupleGetDatum(resultTuple);
177 }
178 
180 {
185 
186 Datum
188 {
189  bytea *raw_page = PG_GETARG_BYTEA_P(0);
190  FuncCallContext *fctx;
191  gin_leafpage_items_state *inter_call_data;
192 
193  if (!superuser())
194  ereport(ERROR,
195  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
196  errmsg("must be superuser to use raw page functions")));
197 
198  if (SRF_IS_FIRSTCALL())
199  {
200  TupleDesc tupdesc;
201  MemoryContext mctx;
202  Page page;
203  GinPageOpaque opaq;
204 
205  fctx = SRF_FIRSTCALL_INIT();
207 
208  page = get_page_from_raw(raw_page);
209 
210  if (PageIsNew(page))
211  {
212  MemoryContextSwitchTo(mctx);
213  PG_RETURN_NULL();
214  }
215 
216  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
217  ereport(ERROR,
218  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
219  errmsg("input page is not a valid GIN data leaf page"),
220  errdetail("Expected special size %d, got %d.",
221  (int) MAXALIGN(sizeof(GinPageOpaqueData)),
222  (int) PageGetSpecialSize(page))));
223 
224  opaq = GinPageGetOpaque(page);
225  if (opaq->flags != (GIN_DATA | GIN_LEAF | GIN_COMPRESSED))
226  ereport(ERROR,
227  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
228  errmsg("input page is not a compressed GIN data leaf page"),
229  errdetail("Flags %04X, expected %04X",
230  opaq->flags,
232 
233  inter_call_data = palloc(sizeof(gin_leafpage_items_state));
234 
235  /* Build a tuple descriptor for our result type */
236  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
237  elog(ERROR, "return type must be a row type");
238 
239  inter_call_data->tupd = tupdesc;
240 
241  inter_call_data->seg = GinDataLeafPageGetPostingList(page);
242  inter_call_data->lastseg = (GinPostingList *)
243  (((char *) inter_call_data->seg) +
245 
246  fctx->user_fctx = inter_call_data;
247 
248  MemoryContextSwitchTo(mctx);
249  }
250 
251  fctx = SRF_PERCALL_SETUP();
252  inter_call_data = fctx->user_fctx;
253 
254  if (inter_call_data->seg != inter_call_data->lastseg)
255  {
256  GinPostingList *cur = inter_call_data->seg;
257  HeapTuple resultTuple;
258  Datum result;
259  Datum values[3];
260  bool nulls[3];
261  int ndecoded,
262  i;
263  ItemPointer tids;
264  Datum *tids_datum;
265 
266  memset(nulls, 0, sizeof(nulls));
267 
268  values[0] = ItemPointerGetDatum(&cur->first);
269  values[1] = UInt16GetDatum(cur->nbytes);
270 
271  /* build an array of decoded item pointers */
272  tids = ginPostingListDecode(cur, &ndecoded);
273  tids_datum = (Datum *) palloc(ndecoded * sizeof(Datum));
274  for (i = 0; i < ndecoded; i++)
275  tids_datum[i] = ItemPointerGetDatum(&tids[i]);
276  values[2] = PointerGetDatum(construct_array(tids_datum,
277  ndecoded,
278  TIDOID,
279  sizeof(ItemPointerData),
280  false, TYPALIGN_SHORT));
281  pfree(tids_datum);
282  pfree(tids);
283 
284  /* Build and return the result tuple. */
285  resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls);
286  result = HeapTupleGetDatum(resultTuple);
287 
288  inter_call_data->seg = GinNextPostingListSegment(cur);
289 
290  SRF_RETURN_NEXT(fctx, result);
291  }
292 
293  SRF_RETURN_DONE(fctx);
294 }
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3319
static Datum values[MAXATTR]
Definition: bootstrap.c:156
Pointer Page
Definition: bufpage.h:78
#define PageGetSpecialSize(page)
Definition: bufpage.h:299
#define PageIsNew(page)
Definition: bufpage.h:228
#define CStringGetTextDatum(s)
Definition: builtins.h:85
unsigned short uint16
Definition: c.h:440
#define MAXALIGN(LEN)
Definition: c.h:757
struct cursor * cur
Definition: ecpg.c:28
int errdetail(const char *fmt,...)
Definition: elog.c:1037
int errcode(int sqlerrcode)
Definition: elog.c:693
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define ERROR
Definition: elog.h:33
#define elog(elevel,...)
Definition: elog.h:218
#define ereport(elevel,...)
Definition: elog.h:143
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1683
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:631
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:335
#define PG_FUNCTION_ARGS
Definition: fmgr.h:193
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:299
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:303
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:305
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:301
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:323
#define GIN_DATA
Definition: ginblock.h:41
#define GIN_LIST_FULLROW
Definition: ginblock.h:46
#define GIN_COMPRESSED
Definition: ginblock.h:48
#define GinPageGetOpaque(page)
Definition: ginblock.h:110
#define GinDataLeafPageGetPostingListSize(page)
Definition: ginblock.h:280
#define GIN_DELETED
Definition: ginblock.h:43
#define GIN_LEAF
Definition: ginblock.h:42
#define GIN_LIST
Definition: ginblock.h:45
#define GinNextPostingListSegment(cur)
Definition: ginblock.h:343
#define GIN_META
Definition: ginblock.h:44
#define GIN_INCOMPLETE_SPLIT
Definition: ginblock.h:47
#define GinPageGetMeta(p)
Definition: ginblock.h:104
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:278
PG_FUNCTION_INFO_V1(gin_metapage_info)
struct gin_leafpage_items_state gin_leafpage_items_state
#define ItemPointerGetDatum(X)
Definition: ginfuncs.c:25
Datum gin_leafpage_items(PG_FUNCTION_ARGS)
Definition: ginfuncs.c:187
Datum gin_page_opaque_info(PG_FUNCTION_ARGS)
Definition: ginfuncs.c:102
Datum gin_metapage_info(PG_FUNCTION_ARGS)
Definition: ginfuncs.c:34
ItemPointer ginPostingListDecode(GinPostingList *plist, int *ndecoded)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
int i
Definition: isn.c:73
void pfree(void *pointer)
Definition: mcxt.c:1175
void * palloc(Size size)
Definition: mcxt.c:1068
Page get_page_from_raw(bytea *raw_page)
Definition: rawpage.c:215
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
uintptr_t Datum
Definition: postgres.h:411
#define Int32GetDatum(X)
Definition: postgres.h:523
#define UInt16GetDatum(X)
Definition: postgres.h:509
#define PointerGetDatum(X)
Definition: postgres.h:600
void * user_fctx
Definition: funcapi.h:82
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
int64 nEntries
Definition: ginblock.h:82
int32 ginVersion
Definition: ginblock.h:99
BlockNumber tail
Definition: ginblock.h:62
BlockNumber nEntryPages
Definition: ginblock.h:80
uint32 tailFreeSize
Definition: ginblock.h:67
BlockNumber nPendingPages
Definition: ginblock.h:73
int64 nPendingHeapTuples
Definition: ginblock.h:74
BlockNumber nTotalPages
Definition: ginblock.h:79
BlockNumber nDataPages
Definition: ginblock.h:81
BlockNumber head
Definition: ginblock.h:61
OffsetNumber maxoff
Definition: ginblock.h:33
BlockNumber rightlink
Definition: ginblock.h:32
GinPostingList * seg
Definition: ginfuncs.c:182
GinPostingList * lastseg
Definition: ginfuncs.c:183
Definition: c.h:622
bool superuser(void)
Definition: superuser.c:46
Datum to_hex32(PG_FUNCTION_ARGS)
Definition: varlena.c:5190