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
 

Macros

#define DatumGetItemPointer(X)   ((ItemPointer) DatumGetPointer(X))
 
#define ItemPointerGetDatum(X)   PointerGetDatum(X)
 

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)
 

Macro Definition Documentation

◆ DatumGetItemPointer

#define DatumGetItemPointer (   X)    ((ItemPointer) DatumGetPointer(X))

Definition at line 24 of file ginfuncs.c.

◆ ItemPointerGetDatum

#define ItemPointerGetDatum (   X)    PointerGetDatum(X)

Definition at line 25 of file ginfuncs.c.

Referenced by gin_leafpage_items().

Typedef Documentation

◆ gin_leafpage_items_state

Function Documentation

◆ gin_leafpage_items()

Datum gin_leafpage_items ( PG_FUNCTION_ARGS  )

Definition at line 164 of file ginfuncs.c.

References construct_array(), cur, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, GinPostingList::first, GinPageOpaqueData::flags, get_call_result_type(), get_page_from_raw(), GIN_COMPRESSED, GIN_DATA, GIN_LEAF, GinDataLeafPageGetPostingList, GinDataLeafPageGetPostingListSize, GinNextPostingListSegment, ginPostingListDecode(), heap_form_tuple(), HeapTupleGetDatum, i, ItemPointerGetDatum, gin_leafpage_items_state::lastseg, MAXALIGN, MemoryContextSwitchTo(), FuncCallContext::multi_call_memory_ctx, GinPostingList::nbytes, PageGetSpecialPointer, PageGetSpecialSize, palloc(), pfree(), PG_GETARG_BYTEA_P, 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.

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, TYPALIGN_SHORT));
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 
264  SRF_RETURN_DONE(fctx);
265 }
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:205
ItemPointer ginPostingListDecode(GinPostingList *plist, int *ndecoded)
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:293
#define PointerGetDatum(X)
Definition: postgres.h:556
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:3292
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:610
bool superuser(void)
Definition: superuser.c:46
GinPostingList * seg
Definition: ginfuncs.c:159
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define GIN_COMPRESSED
Definition: ginblock.h:48
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:297
#define GIN_DATA
Definition: ginblock.h:40
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:299
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:334
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 GinDataLeafPageGetPostingListSize(page)
Definition: ginblock.h:281
GinPostingList * lastseg
Definition: ginfuncs.c:160
int errdetail(const char *fmt,...)
Definition: elog.c:957
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:248
#define ereport(elevel,...)
Definition: elog.h:144
uint16 nbytes
Definition: ginblock.h:339
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:691
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
#define PageGetSpecialSize(page)
Definition: bufpage.h:300
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void * user_fctx
Definition: funcapi.h:82
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
int i
#define ItemPointerGetDatum(X)
Definition: ginfuncs.c:25
Definition: c.h:555
#define UInt16GetDatum(X)
Definition: postgres.h:465
Pointer Page
Definition: bufpage.h:78
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:317
#define GIN_LEAF
Definition: ginblock.h:41
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:295

◆ gin_metapage_info()

Datum gin_metapage_info ( PG_FUNCTION_ARGS  )

Definition at line 34 of file ginfuncs.c.

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

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 }
BlockNumber nEntryPages
Definition: ginblock.h:80
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:205
int errcode(int sqlerrcode)
Definition: elog.c:610
bool superuser(void)
Definition: superuser.c:46
BlockNumber nTotalPages
Definition: ginblock.h:79
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
int64 nEntries
Definition: ginblock.h:82
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:334
int64 nPendingHeapTuples
Definition: ginblock.h:74
#define ERROR
Definition: elog.h:43
#define GIN_META
Definition: ginblock.h:43
BlockNumber head
Definition: ginblock.h:61
BlockNumber tail
Definition: ginblock.h:62
int errdetail(const char *fmt,...)
Definition: elog.c:957
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1701
GinPageOpaqueData * GinPageOpaque
Definition: ginblock.h:38
uintptr_t Datum
Definition: postgres.h:367
Page page
Definition: btreefuncs.c:248
#define ereport(elevel,...)
Definition: elog.h:144
uint32 tailFreeSize
Definition: ginblock.h:67
Page get_page_from_raw(bytea *raw_page)
Definition: rawpage.c:187
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
int32 ginVersion
Definition: ginblock.h:99
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define Int32GetDatum(X)
Definition: postgres.h:479
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
BlockNumber nDataPages
Definition: ginblock.h:81
Definition: c.h:555
#define GinPageGetMeta(p)
Definition: ginblock.h:104
Pointer Page
Definition: bufpage.h:78
BlockNumber nPendingPages
Definition: ginblock.h:73

◆ gin_page_opaque_info()

Datum gin_page_opaque_info ( PG_FUNCTION_ARGS  )

Definition at line 90 of file ginfuncs.c.

References construct_array(), CStringGetTextDatum, DirectFunctionCall1, elog, ereport, errcode(), 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, heap_form_tuple(), HeapTupleGetDatum, Int32GetDatum, Int64GetDatum(), GinPageOpaqueData::maxoff, user_args::page, PageGetSpecialPointer, PG_GETARG_BYTEA_P, PointerGetDatum, GinPageOpaqueData::rightlink, superuser(), to_hex32(), TYPEFUNC_COMPOSITE, and values.

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,
148  -1, false, TYPALIGN_INT));
149 
150  /* Build and return the result tuple. */
151  resultTuple = heap_form_tuple(tupdesc, values, nulls);
152 
153  return HeapTupleGetDatum(resultTuple);
154 }
#define GIN_DELETED
Definition: ginblock.h:42
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:205
#define PointerGetDatum(X)
Definition: postgres.h:556
BlockNumber rightlink
Definition: ginblock.h:31
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3292
int errcode(int sqlerrcode)
Definition: elog.c:610
bool superuser(void)
Definition: superuser.c:46
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:624
#define GIN_COMPRESSED
Definition: ginblock.h:48
#define GIN_DATA
Definition: ginblock.h:40
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:334
#define GIN_LIST_FULLROW
Definition: ginblock.h:45
unsigned short uint16
Definition: c.h:366
#define ERROR
Definition: elog.h:43
#define GIN_META
Definition: ginblock.h:43
#define GIN_LIST
Definition: ginblock.h:44
Datum Int64GetDatum(int64 X)
Definition: fmgr.c:1701
OffsetNumber maxoff
Definition: ginblock.h:32
GinPageOpaqueData * GinPageOpaque
Definition: ginblock.h:38
uintptr_t Datum
Definition: postgres.h:367
Page page
Definition: btreefuncs.c:248
#define ereport(elevel,...)
Definition: elog.h:144
Datum to_hex32(PG_FUNCTION_ARGS)
Definition: varlena.c:5035
Page get_page_from_raw(bytea *raw_page)
Definition: rawpage.c:187
#define PageGetSpecialPointer(page)
Definition: bufpage.h:326
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:220
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define GIN_INCOMPLETE_SPLIT
Definition: ginblock.h:46
#define Int32GetDatum(X)
Definition: postgres.h:479
int errmsg(const char *fmt,...)
Definition: elog.c:824
#define elog(elevel,...)
Definition: elog.h:214
#define CStringGetTextDatum(s)
Definition: builtins.h:87
Definition: c.h:555
Pointer Page
Definition: bufpage.h:78
#define GIN_LEAF
Definition: ginblock.h:41

◆ PG_FUNCTION_INFO_V1() [1/3]

PG_FUNCTION_INFO_V1 ( gin_metapage_info  )

◆ PG_FUNCTION_INFO_V1() [2/3]

PG_FUNCTION_INFO_V1 ( gin_page_opaque_info  )

◆ PG_FUNCTION_INFO_V1() [3/3]

PG_FUNCTION_INFO_V1 ( gin_leafpage_items  )