1 /*
2 * ginfuncs.c
3 * Functions to investigate the content of GIN indexes
4 *
5 * Copyright (c) 2014-2021, 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
28 PG_FUNCTION_INFO_V1(gin_metapage_info);
29 PG_FUNCTION_INFO_V1(gin_page_opaque_info);
30 PG_FUNCTION_INFO_V1(gin_leafpage_items);
31
32
33 Datum
gin_metapage_info(PG_FUNCTION_ARGS)34 gin_metapage_info(PG_FUNCTION_ARGS)
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
gin_page_opaque_info(PG_FUNCTION_ARGS)90 gin_page_opaque_info(PG_FUNCTION_ARGS)
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 }
155
156 typedef struct gin_leafpage_items_state
157 {
158 TupleDesc tupd;
159 GinPostingList *seg;
160 GinPostingList *lastseg;
161 } gin_leafpage_items_state;
162
163 Datum
gin_leafpage_items(PG_FUNCTION_ARGS)164 gin_leafpage_items(PG_FUNCTION_ARGS)
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();
183 mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx);
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,
202 (GIN_DATA | GIN_LEAF | GIN_COMPRESSED))));
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) +
215 GinDataLeafPageGetPostingListSize(page));
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 }
266