PostgreSQL Source Code  git master
ginfuncs.c File Reference
#include "postgres.h"
#include "access/gin.h"
#include "access/gin_private.h"
#include "access/htup_details.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "pageinspect.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/rel.h"
Include dependency graph for ginfuncs.c:

Go to the source code of this file.

Data Structures

struct  gin_leafpage_items_state
 

Typedefs

typedef struct gin_leafpage_items_state gin_leafpage_items_state
 

Functions

 PG_FUNCTION_INFO_V1 (gin_metapage_info)
 
 PG_FUNCTION_INFO_V1 (gin_page_opaque_info)
 
 PG_FUNCTION_INFO_V1 (gin_leafpage_items)
 
Datum gin_metapage_info (PG_FUNCTION_ARGS)
 
Datum gin_page_opaque_info (PG_FUNCTION_ARGS)
 
Datum gin_leafpage_items (PG_FUNCTION_ARGS)
 

Typedef Documentation

◆ gin_leafpage_items_state

Function Documentation

◆ gin_leafpage_items()

Datum gin_leafpage_items ( PG_FUNCTION_ARGS  )

Definition at line 182 of file ginfuncs.c.

183 {
184  bytea *raw_page = PG_GETARG_BYTEA_P(0);
185  FuncCallContext *fctx;
186  gin_leafpage_items_state *inter_call_data;
187 
188  if (!superuser())
189  ereport(ERROR,
190  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
191  errmsg("must be superuser to use raw page functions")));
192 
193  if (SRF_IS_FIRSTCALL())
194  {
195  TupleDesc tupdesc;
196  MemoryContext mctx;
197  Page page;
198  GinPageOpaque opaq;
199 
200  fctx = SRF_FIRSTCALL_INIT();
202 
203  page = get_page_from_raw(raw_page);
204 
205  if (PageIsNew(page))
206  {
207  MemoryContextSwitchTo(mctx);
208  PG_RETURN_NULL();
209  }
210 
211  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
212  ereport(ERROR,
213  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
214  errmsg("input page is not a valid GIN data leaf page"),
215  errdetail("Expected special size %d, got %d.",
216  (int) MAXALIGN(sizeof(GinPageOpaqueData)),
217  (int) PageGetSpecialSize(page))));
218 
219  opaq = GinPageGetOpaque(page);
220  if (opaq->flags != (GIN_DATA | GIN_LEAF | GIN_COMPRESSED))
221  ereport(ERROR,
222  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
223  errmsg("input page is not a compressed GIN data leaf page"),
224  errdetail("Flags %04X, expected %04X",
225  opaq->flags,
227 
228  inter_call_data = palloc(sizeof(gin_leafpage_items_state));
229 
230  /* Build a tuple descriptor for our result type */
231  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
232  elog(ERROR, "return type must be a row type");
233 
234  inter_call_data->tupd = tupdesc;
235 
236  inter_call_data->seg = GinDataLeafPageGetPostingList(page);
237  inter_call_data->lastseg = (GinPostingList *)
238  (((char *) inter_call_data->seg) +
240 
241  fctx->user_fctx = inter_call_data;
242 
243  MemoryContextSwitchTo(mctx);
244  }
245 
246  fctx = SRF_PERCALL_SETUP();
247  inter_call_data = fctx->user_fctx;
248 
249  if (inter_call_data->seg != inter_call_data->lastseg)
250  {
251  GinPostingList *cur = inter_call_data->seg;
252  HeapTuple resultTuple;
253  Datum result;
254  Datum values[3];
255  bool nulls[3];
256  int ndecoded,
257  i;
258  ItemPointer tids;
259  Datum *tids_datum;
260 
261  memset(nulls, 0, sizeof(nulls));
262 
263  values[0] = ItemPointerGetDatum(&cur->first);
264  values[1] = UInt16GetDatum(cur->nbytes);
265 
266  /* build an array of decoded item pointers */
267  tids = ginPostingListDecode(cur, &ndecoded);
268  tids_datum = (Datum *) palloc(ndecoded * sizeof(Datum));
269  for (i = 0; i < ndecoded; i++)
270  tids_datum[i] = ItemPointerGetDatum(&tids[i]);
271  values[2] = PointerGetDatum(construct_array_builtin(tids_datum, ndecoded, TIDOID));
272  pfree(tids_datum);
273  pfree(tids);
274 
275  /* Build and return the result tuple. */
276  resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls);
277  result = HeapTupleGetDatum(resultTuple);
278 
279  inter_call_data->seg = GinNextPostingListSegment(cur);
280 
281  SRF_RETURN_NEXT(fctx, result);
282  }
283 
284  SRF_RETURN_DONE(fctx);
285 }
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3375
static Datum values[MAXATTR]
Definition: bootstrap.c:156
Pointer Page
Definition: bufpage.h:78
static bool PageIsNew(Page page)
Definition: bufpage.h:230
static uint16 PageGetSpecialSize(Page page)
Definition: bufpage.h:313
#define MAXALIGN(LEN)
Definition: c.h:800
struct cursor * cur
Definition: ecpg.c:28
int errdetail(const char *fmt,...)
Definition: elog.c:1208
int errcode(int sqlerrcode)
Definition: elog.c:860
int errmsg(const char *fmt,...)
Definition: elog.c:1075
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:335
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_COMPRESSED
Definition: ginblock.h:48
#define GinPageGetOpaque(page)
Definition: ginblock.h:110
#define GinDataLeafPageGetPostingListSize(page)
Definition: ginblock.h:280
#define GIN_LEAF
Definition: ginblock.h:42
#define GinNextPostingListSegment(cur)
Definition: ginblock.h:343
#define GinDataLeafPageGetPostingList(page)
Definition: ginblock.h:278
ItemPointer ginPostingListDecode(GinPostingList *plist, int *ndecoded_out)
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
int i
Definition: isn.c:73
static Datum ItemPointerGetDatum(const ItemPointerData *X)
Definition: itemptr.h:237
void pfree(void *pointer)
Definition: mcxt.c:1431
void * palloc(Size size)
Definition: mcxt.c:1201
Page get_page_from_raw(bytea *raw_page)
Definition: rawpage.c:215
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
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
void * user_fctx
Definition: funcapi.h:82
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
GinPostingList * seg
Definition: ginfuncs.c:177
GinPostingList * lastseg
Definition: ginfuncs.c:178
Definition: c.h:676
bool superuser(void)
Definition: superuser.c:46

References construct_array_builtin(), cur, elog(), ereport, errcode(), errdetail(), errmsg(), ERROR, GinPageOpaqueData::flags, get_call_result_type(), get_page_from_raw(), GIN_COMPRESSED, GIN_DATA, GIN_LEAF, GinDataLeafPageGetPostingList, GinDataLeafPageGetPostingListSize, GinNextPostingListSegment, GinPageGetOpaque, ginPostingListDecode(), heap_form_tuple(), HeapTupleGetDatum(), i, ItemPointerGetDatum(), gin_leafpage_items_state::lastseg, MAXALIGN, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, PageGetSpecialSize(), PageIsNew(), palloc(), pfree(), PG_GETARG_BYTEA_P, PG_RETURN_NULL, PointerGetDatum(), gin_leafpage_items_state::seg, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, superuser(), gin_leafpage_items_state::tupd, TYPEFUNC_COMPOSITE, UInt16GetDatum(), FuncCallContext::user_fctx, and values.

◆ gin_metapage_info()

Datum gin_metapage_info ( PG_FUNCTION_ARGS  )

Definition at line 31 of file ginfuncs.c.

32 {
33  bytea *raw_page = PG_GETARG_BYTEA_P(0);
34  TupleDesc tupdesc;
35  Page page;
36  GinPageOpaque opaq;
37  GinMetaPageData *metadata;
38  HeapTuple resultTuple;
39  Datum values[10];
40  bool nulls[10];
41 
42  if (!superuser())
43  ereport(ERROR,
44  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
45  errmsg("must be superuser to use raw page functions")));
46 
47  page = get_page_from_raw(raw_page);
48 
49  if (PageIsNew(page))
51 
52  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
53  ereport(ERROR,
54  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
55  errmsg("input page is not a valid GIN metapage"),
56  errdetail("Expected special size %d, got %d.",
57  (int) MAXALIGN(sizeof(GinPageOpaqueData)),
58  (int) PageGetSpecialSize(page))));
59 
60  opaq = GinPageGetOpaque(page);
61 
62  if (opaq->flags != GIN_META)
63  ereport(ERROR,
64  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
65  errmsg("input page is not a GIN metapage"),
66  errdetail("Flags %04X, expected %04X",
67  opaq->flags, GIN_META)));
68 
69  /* Build a tuple descriptor for our result type */
70  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
71  elog(ERROR, "return type must be a row type");
72 
73  metadata = GinPageGetMeta(page);
74 
75  memset(nulls, 0, sizeof(nulls));
76 
77  values[0] = Int64GetDatum(metadata->head);
78  values[1] = Int64GetDatum(metadata->tail);
79  values[2] = Int32GetDatum(metadata->tailFreeSize);
80  values[3] = Int64GetDatum(metadata->nPendingPages);
81  values[4] = Int64GetDatum(metadata->nPendingHeapTuples);
82 
83  /* statistics, updated by VACUUM */
84  values[5] = Int64GetDatum(metadata->nTotalPages);
85  values[6] = Int64GetDatum(metadata->nEntryPages);
86  values[7] = Int64GetDatum(metadata->nDataPages);
87  values[8] = Int64GetDatum(metadata->nEntries);
88 
89  values[9] = Int32GetDatum(metadata->ginVersion);
90 
91  /* Build and return the result tuple. */
92  resultTuple = heap_form_tuple(tupdesc, values, nulls);
93 
94  return HeapTupleGetDatum(resultTuple);
95 }
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1807
#define GIN_META
Definition: ginblock.h:44
#define GinPageGetMeta(p)
Definition: ginblock.h:104
static Datum Int32GetDatum(int32 X)
Definition: postgres.h:212
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

References elog(), ereport, errcode(), errdetail(), errmsg(), ERROR, GinPageOpaqueData::flags, get_call_result_type(), get_page_from_raw(), GIN_META, GinPageGetMeta, GinPageGetOpaque, GinMetaPageData::ginVersion, GinMetaPageData::head, heap_form_tuple(), HeapTupleGetDatum(), Int32GetDatum(), Int64GetDatum(), MAXALIGN, GinMetaPageData::nDataPages, GinMetaPageData::nEntries, GinMetaPageData::nEntryPages, GinMetaPageData::nPendingHeapTuples, GinMetaPageData::nPendingPages, GinMetaPageData::nTotalPages, PageGetSpecialSize(), PageIsNew(), PG_GETARG_BYTEA_P, PG_RETURN_NULL, superuser(), GinMetaPageData::tail, GinMetaPageData::tailFreeSize, TYPEFUNC_COMPOSITE, and values.

◆ gin_page_opaque_info()

Datum gin_page_opaque_info ( PG_FUNCTION_ARGS  )

Definition at line 99 of file ginfuncs.c.

100 {
101  bytea *raw_page = PG_GETARG_BYTEA_P(0);
102  TupleDesc tupdesc;
103  Page page;
104  GinPageOpaque opaq;
105  HeapTuple resultTuple;
106  Datum values[3];
107  bool nulls[3];
108  Datum flags[16];
109  int nflags = 0;
110  uint16 flagbits;
111 
112  if (!superuser())
113  ereport(ERROR,
114  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
115  errmsg("must be superuser to use raw page functions")));
116 
117  page = get_page_from_raw(raw_page);
118 
119  if (PageIsNew(page))
120  PG_RETURN_NULL();
121 
122  if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GinPageOpaqueData)))
123  ereport(ERROR,
124  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
125  errmsg("input page is not a valid GIN data leaf page"),
126  errdetail("Expected special size %d, got %d.",
127  (int) MAXALIGN(sizeof(GinPageOpaqueData)),
128  (int) PageGetSpecialSize(page))));
129 
130  opaq = GinPageGetOpaque(page);
131 
132  /* Build a tuple descriptor for our result type */
133  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
134  elog(ERROR, "return type must be a row type");
135 
136  /* Convert the flags bitmask to an array of human-readable names */
137  flagbits = opaq->flags;
138  if (flagbits & GIN_DATA)
139  flags[nflags++] = CStringGetTextDatum("data");
140  if (flagbits & GIN_LEAF)
141  flags[nflags++] = CStringGetTextDatum("leaf");
142  if (flagbits & GIN_DELETED)
143  flags[nflags++] = CStringGetTextDatum("deleted");
144  if (flagbits & GIN_META)
145  flags[nflags++] = CStringGetTextDatum("meta");
146  if (flagbits & GIN_LIST)
147  flags[nflags++] = CStringGetTextDatum("list");
148  if (flagbits & GIN_LIST_FULLROW)
149  flags[nflags++] = CStringGetTextDatum("list_fullrow");
150  if (flagbits & GIN_INCOMPLETE_SPLIT)
151  flags[nflags++] = CStringGetTextDatum("incomplete_split");
152  if (flagbits & GIN_COMPRESSED)
153  flags[nflags++] = CStringGetTextDatum("compressed");
154  flagbits &= ~(GIN_DATA | GIN_LEAF | GIN_DELETED | GIN_META | GIN_LIST |
156  if (flagbits)
157  {
158  /* any flags we don't recognize are printed in hex */
159  flags[nflags++] = DirectFunctionCall1(to_hex32, Int32GetDatum(flagbits));
160  }
161 
162  memset(nulls, 0, sizeof(nulls));
163 
164  values[0] = Int64GetDatum(opaq->rightlink);
165  values[1] = Int32GetDatum(opaq->maxoff);
166  values[2] = PointerGetDatum(construct_array_builtin(flags, nflags, TEXTOID));
167 
168  /* Build and return the result tuple. */
169  resultTuple = heap_form_tuple(tupdesc, values, nulls);
170 
171  return HeapTupleGetDatum(resultTuple);
172 }
#define CStringGetTextDatum(s)
Definition: builtins.h:97
unsigned short uint16
Definition: c.h:494
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
#define GIN_LIST_FULLROW
Definition: ginblock.h:46
#define GIN_DELETED
Definition: ginblock.h:43
#define GIN_LIST
Definition: ginblock.h:45
#define GIN_INCOMPLETE_SPLIT
Definition: ginblock.h:47
OffsetNumber maxoff
Definition: ginblock.h:33
BlockNumber rightlink
Definition: ginblock.h:32
Datum to_hex32(PG_FUNCTION_ARGS)
Definition: varlena.c:4993

References construct_array_builtin(), CStringGetTextDatum, DirectFunctionCall1, elog(), ereport, errcode(), errdetail(), errmsg(), ERROR, GinPageOpaqueData::flags, get_call_result_type(), get_page_from_raw(), GIN_COMPRESSED, GIN_DATA, GIN_DELETED, GIN_INCOMPLETE_SPLIT, GIN_LEAF, GIN_LIST, GIN_LIST_FULLROW, GIN_META, GinPageGetOpaque, heap_form_tuple(), HeapTupleGetDatum(), Int32GetDatum(), Int64GetDatum(), MAXALIGN, GinPageOpaqueData::maxoff, PageGetSpecialSize(), PageIsNew(), PG_GETARG_BYTEA_P, PG_RETURN_NULL, PointerGetDatum(), GinPageOpaqueData::rightlink, superuser(), to_hex32(), TYPEFUNC_COMPOSITE, and values.

◆ PG_FUNCTION_INFO_V1() [1/3]

PG_FUNCTION_INFO_V1 ( gin_leafpage_items  )

◆ PG_FUNCTION_INFO_V1() [2/3]

PG_FUNCTION_INFO_V1 ( gin_metapage_info  )

◆ PG_FUNCTION_INFO_V1() [3/3]

PG_FUNCTION_INFO_V1 ( gin_page_opaque_info  )