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-2024, PostgreSQL Global Development Group
6  *
7  * IDENTIFICATION
8  * contrib/pageinspect/ginfuncs.c
9  */
10 #include "postgres.h"
11 
12 #include "access/gin_private.h"
13 #include "access/htup_details.h"
14 #include "catalog/pg_type.h"
15 #include "funcapi.h"
16 #include "miscadmin.h"
17 #include "pageinspect.h"
18 #include "utils/array.h"
19 #include "utils/builtins.h"
20 
21 
25 
26 
27 Datum
29 {
30  bytea *raw_page = PG_GETARG_BYTEA_P(0);
31  TupleDesc tupdesc;
32  Page page;
33  GinPageOpaque opaq;
34  GinMetaPageData *metadata;
35  HeapTuple resultTuple;
36  Datum values[10];
37  bool nulls[10];
38 
39  if (!superuser())
40  ereport(ERROR,
41  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
42  errmsg("must be superuser to use raw page functions")));
43 
44  page = get_page_from_raw(raw_page);
45 
46  if (PageIsNew(page))
48 
49  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
50  ereport(ERROR,
51  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
52  errmsg("input page is not a valid GIN metapage"),
53  errdetail("Expected special size %d, got %d.",
54  (int) MAXALIGN(sizeof(GinPageOpaqueData)),
55  (int) PageGetSpecialSize(page))));
56 
57  opaq = GinPageGetOpaque(page);
58 
59  if (opaq->flags != GIN_META)
60  ereport(ERROR,
61  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
62  errmsg("input page is not a GIN metapage"),
63  errdetail("Flags %04X, expected %04X",
64  opaq->flags, GIN_META)));
65 
66  /* Build a tuple descriptor for our result type */
67  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
68  elog(ERROR, "return type must be a row type");
69 
70  metadata = GinPageGetMeta(page);
71 
72  memset(nulls, 0, sizeof(nulls));
73 
74  values[0] = Int64GetDatum(metadata->head);
75  values[1] = Int64GetDatum(metadata->tail);
76  values[2] = Int32GetDatum(metadata->tailFreeSize);
77  values[3] = Int64GetDatum(metadata->nPendingPages);
78  values[4] = Int64GetDatum(metadata->nPendingHeapTuples);
79 
80  /* statistics, updated by VACUUM */
81  values[5] = Int64GetDatum(metadata->nTotalPages);
82  values[6] = Int64GetDatum(metadata->nEntryPages);
83  values[7] = Int64GetDatum(metadata->nDataPages);
84  values[8] = Int64GetDatum(metadata->nEntries);
85 
86  values[9] = Int32GetDatum(metadata->ginVersion);
87 
88  /* Build and return the result tuple. */
89  resultTuple = heap_form_tuple(tupdesc, values, nulls);
90 
91  return HeapTupleGetDatum(resultTuple);
92 }
93 
94 
95 Datum
97 {
98  bytea *raw_page = PG_GETARG_BYTEA_P(0);
99  TupleDesc tupdesc;
100  Page page;
101  GinPageOpaque opaq;
102  HeapTuple resultTuple;
103  Datum values[3];
104  bool nulls[3];
105  Datum flags[16];
106  int nflags = 0;
107  uint16 flagbits;
108 
109  if (!superuser())
110  ereport(ERROR,
111  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
112  errmsg("must be superuser to use raw page functions")));
113 
114  page = get_page_from_raw(raw_page);
115 
116  if (PageIsNew(page))
117  PG_RETURN_NULL();
118 
119  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
120  ereport(ERROR,
121  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
122  errmsg("input page is not a valid GIN data leaf page"),
123  errdetail("Expected special size %d, got %d.",
124  (int) MAXALIGN(sizeof(GinPageOpaqueData)),
125  (int) PageGetSpecialSize(page))));
126 
127  opaq = GinPageGetOpaque(page);
128 
129  /* Build a tuple descriptor for our result type */
130  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
131  elog(ERROR, "return type must be a row type");
132 
133  /* Convert the flags bitmask to an array of human-readable names */
134  flagbits = opaq->flags;
135  if (flagbits & GIN_DATA)
136  flags[nflags++] = CStringGetTextDatum("data");
137  if (flagbits & GIN_LEAF)
138  flags[nflags++] = CStringGetTextDatum("leaf");
139  if (flagbits & GIN_DELETED)
140  flags[nflags++] = CStringGetTextDatum("deleted");
141  if (flagbits & GIN_META)
142  flags[nflags++] = CStringGetTextDatum("meta");
143  if (flagbits & GIN_LIST)
144  flags[nflags++] = CStringGetTextDatum("list");
145  if (flagbits & GIN_LIST_FULLROW)
146  flags[nflags++] = CStringGetTextDatum("list_fullrow");
147  if (flagbits & GIN_INCOMPLETE_SPLIT)
148  flags[nflags++] = CStringGetTextDatum("incomplete_split");
149  if (flagbits & GIN_COMPRESSED)
150  flags[nflags++] = CStringGetTextDatum("compressed");
151  flagbits &= ~(GIN_DATA | GIN_LEAF | GIN_DELETED | GIN_META | GIN_LIST |
153  if (flagbits)
154  {
155  /* any flags we don't recognize are printed in hex */
156  flags[nflags++] = DirectFunctionCall1(to_hex32, Int32GetDatum(flagbits));
157  }
158 
159  memset(nulls, 0, sizeof(nulls));
160 
161  values[0] = Int64GetDatum(opaq->rightlink);
162  values[1] = Int32GetDatum(opaq->maxoff);
163  values[2] = PointerGetDatum(construct_array_builtin(flags, nflags, TEXTOID));
164 
165  /* Build and return the result tuple. */
166  resultTuple = heap_form_tuple(tupdesc, values, nulls);
167 
168  return HeapTupleGetDatum(resultTuple);
169 }
170 
172 {
177 
178 Datum
180 {
181  bytea *raw_page = PG_GETARG_BYTEA_P(0);
182  FuncCallContext *fctx;
183  gin_leafpage_items_state *inter_call_data;
184 
185  if (!superuser())
186  ereport(ERROR,
187  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
188  errmsg("must be superuser to use raw page functions")));
189 
190  if (SRF_IS_FIRSTCALL())
191  {
192  TupleDesc tupdesc;
193  MemoryContext mctx;
194  Page page;
195  GinPageOpaque opaq;
196 
197  fctx = SRF_FIRSTCALL_INIT();
199 
200  page = get_page_from_raw(raw_page);
201 
202  if (PageIsNew(page))
203  {
204  MemoryContextSwitchTo(mctx);
205  PG_RETURN_NULL();
206  }
207 
208  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
209  ereport(ERROR,
210  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
211  errmsg("input page is not a valid GIN data leaf page"),
212  errdetail("Expected special size %d, got %d.",
213  (int) MAXALIGN(sizeof(GinPageOpaqueData)),
214  (int) PageGetSpecialSize(page))));
215 
216  opaq = GinPageGetOpaque(page);
217  if (opaq->flags != (GIN_DATA | GIN_LEAF | GIN_COMPRESSED))
218  ereport(ERROR,
219  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
220  errmsg("input page is not a compressed GIN data leaf page"),
221  errdetail("Flags %04X, expected %04X",
222  opaq->flags,
224 
225  inter_call_data = palloc(sizeof(gin_leafpage_items_state));
226 
227  /* Build a tuple descriptor for our result type */
228  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
229  elog(ERROR, "return type must be a row type");
230 
231  inter_call_data->tupd = tupdesc;
232 
233  inter_call_data->seg = GinDataLeafPageGetPostingList(page);
234  inter_call_data->lastseg = (GinPostingList *)
235  (((char *) inter_call_data->seg) +
237 
238  fctx->user_fctx = inter_call_data;
239 
240  MemoryContextSwitchTo(mctx);
241  }
242 
243  fctx = SRF_PERCALL_SETUP();
244  inter_call_data = fctx->user_fctx;
245 
246  if (inter_call_data->seg != inter_call_data->lastseg)
247  {
248  GinPostingList *cur = inter_call_data->seg;
249  HeapTuple resultTuple;
250  Datum result;
251  Datum values[3];
252  bool nulls[3];
253  int ndecoded,
254  i;
255  ItemPointer tids;
256  Datum *tids_datum;
257 
258  memset(nulls, 0, sizeof(nulls));
259 
260  values[0] = ItemPointerGetDatum(&cur->first);
261  values[1] = UInt16GetDatum(cur->nbytes);
262 
263  /* build an array of decoded item pointers */
264  tids = ginPostingListDecode(cur, &ndecoded);
265  tids_datum = (Datum *) palloc(ndecoded * sizeof(Datum));
266  for (i = 0; i < ndecoded; i++)
267  tids_datum[i] = ItemPointerGetDatum(&tids[i]);
268  values[2] = PointerGetDatum(construct_array_builtin(tids_datum, ndecoded, TIDOID));
269  pfree(tids_datum);
270  pfree(tids);
271 
272  /* Build and return the result tuple. */
273  resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls);
274  result = HeapTupleGetDatum(resultTuple);
275 
276  inter_call_data->seg = GinNextPostingListSegment(cur);
277 
278  SRF_RETURN_NEXT(fctx, result);
279  }
280 
281  SRF_RETURN_DONE(fctx);
282 }
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3381
static Datum values[MAXATTR]
Definition: bootstrap.c:151
Pointer Page
Definition: bufpage.h:81
static bool PageIsNew(Page page)
Definition: bufpage.h:233
static uint16 PageGetSpecialSize(Page page)
Definition: bufpage.h:316
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define MAXALIGN(LEN)
Definition: c.h:765
uint16_t uint16
Definition: c.h:484
struct cursor * cur
Definition: ecpg.c:29
int errdetail(const char *fmt,...)
Definition: elog.c:1203
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:641
#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:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
#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
Datum gin_leafpage_items(PG_FUNCTION_ARGS)
Definition: ginfuncs.c:179
Datum gin_page_opaque_info(PG_FUNCTION_ARGS)
Definition: ginfuncs.c:96
Datum gin_metapage_info(PG_FUNCTION_ARGS)
Definition: ginfuncs.c:28
ItemPointer ginPostingListDecode(GinPostingList *plist, int *ndecoded_out)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1116
int i
Definition: isn.c:72
static Datum ItemPointerGetDatum(const ItemPointerData *X)
Definition: itemptr.h:237
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc(Size size)
Definition: mcxt.c:1317
Page get_page_from_raw(bytea *raw_page)
Definition: rawpage.c:215
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static Datum UInt16GetDatum(uint16 X)
Definition: postgres.h:192
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
MemoryContextSwitchTo(old_ctx)
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:174
GinPostingList * lastseg
Definition: ginfuncs.c:175
Definition: c.h:641
bool superuser(void)
Definition: superuser.c:46
Datum to_hex32(PG_FUNCTION_ARGS)
Definition: varlena.c:5007