Line data Source code
1 : /*
2 : * gistfuncs.c
3 : * Functions to investigate the content of GiST indexes
4 : *
5 : * Copyright (c) 2014-2026, PostgreSQL Global Development Group
6 : *
7 : * IDENTIFICATION
8 : * contrib/pageinspect/gistfuncs.c
9 : */
10 : #include "postgres.h"
11 :
12 : #include "access/genam.h"
13 : #include "access/gist.h"
14 : #include "access/htup.h"
15 : #include "access/htup_details.h"
16 : #include "access/relation.h"
17 : #include "catalog/pg_am_d.h"
18 : #include "funcapi.h"
19 : #include "miscadmin.h"
20 : #include "pageinspect.h"
21 : #include "storage/itemptr.h"
22 : #include "utils/array.h"
23 : #include "utils/builtins.h"
24 : #include "utils/lsyscache.h"
25 : #include "utils/pg_lsn.h"
26 : #include "utils/rel.h"
27 : #include "utils/ruleutils.h"
28 :
29 0 : PG_FUNCTION_INFO_V1(gist_page_opaque_info);
30 0 : PG_FUNCTION_INFO_V1(gist_page_items);
31 0 : PG_FUNCTION_INFO_V1(gist_page_items_bytea);
32 :
33 : #define IS_GIST(r) ((r)->rd_rel->relam == GIST_AM_OID)
34 :
35 :
36 : static Page verify_gist_page(bytea *raw_page);
37 :
38 : /*
39 : * Verify that the given bytea contains a GIST page or die in the attempt.
40 : * A pointer to the page is returned.
41 : */
42 : static Page
43 0 : verify_gist_page(bytea *raw_page)
44 : {
45 0 : Page page = get_page_from_raw(raw_page);
46 0 : GISTPageOpaque opaq;
47 :
48 0 : if (PageIsNew(page))
49 0 : return page;
50 :
51 : /* verify the special space has the expected size */
52 0 : if (PageGetSpecialSize(page) != MAXALIGN(sizeof(GISTPageOpaqueData)))
53 0 : ereport(ERROR,
54 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
55 : errmsg("input page is not a valid %s page", "GiST"),
56 : errdetail("Expected special size %d, got %d.",
57 : (int) MAXALIGN(sizeof(GISTPageOpaqueData)),
58 : (int) PageGetSpecialSize(page))));
59 :
60 0 : opaq = GistPageGetOpaque(page);
61 0 : if (opaq->gist_page_id != GIST_PAGE_ID)
62 0 : ereport(ERROR,
63 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
64 : errmsg("input page is not a valid %s page", "GiST"),
65 : errdetail("Expected %08x, got %08x.",
66 : GIST_PAGE_ID,
67 : opaq->gist_page_id)));
68 :
69 0 : return page;
70 0 : }
71 :
72 : Datum
73 0 : gist_page_opaque_info(PG_FUNCTION_ARGS)
74 : {
75 0 : bytea *raw_page = PG_GETARG_BYTEA_P(0);
76 0 : TupleDesc tupdesc;
77 0 : Page page;
78 0 : HeapTuple resultTuple;
79 0 : Datum values[4];
80 0 : bool nulls[4];
81 0 : Datum flags[16];
82 0 : int nflags = 0;
83 0 : uint16 flagbits;
84 :
85 0 : if (!superuser())
86 0 : ereport(ERROR,
87 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
88 : errmsg("must be superuser to use raw page functions")));
89 :
90 0 : page = verify_gist_page(raw_page);
91 :
92 0 : if (PageIsNew(page))
93 0 : PG_RETURN_NULL();
94 :
95 : /* Build a tuple descriptor for our result type */
96 0 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
97 0 : elog(ERROR, "return type must be a row type");
98 :
99 : /* Convert the flags bitmask to an array of human-readable names */
100 0 : flagbits = GistPageGetOpaque(page)->flags;
101 0 : if (flagbits & F_LEAF)
102 0 : flags[nflags++] = CStringGetTextDatum("leaf");
103 0 : if (flagbits & F_DELETED)
104 0 : flags[nflags++] = CStringGetTextDatum("deleted");
105 0 : if (flagbits & F_TUPLES_DELETED)
106 0 : flags[nflags++] = CStringGetTextDatum("tuples_deleted");
107 0 : if (flagbits & F_FOLLOW_RIGHT)
108 0 : flags[nflags++] = CStringGetTextDatum("follow_right");
109 0 : if (flagbits & F_HAS_GARBAGE)
110 0 : flags[nflags++] = CStringGetTextDatum("has_garbage");
111 0 : flagbits &= ~(F_LEAF | F_DELETED | F_TUPLES_DELETED | F_FOLLOW_RIGHT | F_HAS_GARBAGE);
112 0 : if (flagbits)
113 : {
114 : /* any flags we don't recognize are printed in hex */
115 0 : flags[nflags++] = DirectFunctionCall1(to_hex32, Int32GetDatum(flagbits));
116 0 : }
117 :
118 0 : memset(nulls, 0, sizeof(nulls));
119 :
120 0 : values[0] = LSNGetDatum(PageGetLSN(page));
121 0 : values[1] = LSNGetDatum(GistPageGetNSN(page));
122 0 : values[2] = Int64GetDatum(GistPageGetOpaque(page)->rightlink);
123 0 : values[3] = PointerGetDatum(construct_array_builtin(flags, nflags, TEXTOID));
124 :
125 : /* Build and return the result tuple. */
126 0 : resultTuple = heap_form_tuple(tupdesc, values, nulls);
127 :
128 0 : return HeapTupleGetDatum(resultTuple);
129 0 : }
130 :
131 : Datum
132 0 : gist_page_items_bytea(PG_FUNCTION_ARGS)
133 : {
134 0 : bytea *raw_page = PG_GETARG_BYTEA_P(0);
135 0 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
136 0 : Page page;
137 0 : OffsetNumber offset;
138 0 : OffsetNumber maxoff = InvalidOffsetNumber;
139 :
140 0 : if (!superuser())
141 0 : ereport(ERROR,
142 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
143 : errmsg("must be superuser to use raw page functions")));
144 :
145 0 : InitMaterializedSRF(fcinfo, 0);
146 :
147 0 : page = verify_gist_page(raw_page);
148 :
149 0 : if (PageIsNew(page))
150 0 : PG_RETURN_NULL();
151 :
152 : /* Avoid bogus PageGetMaxOffsetNumber() call with deleted pages */
153 0 : if (GistPageIsDeleted(page))
154 0 : elog(NOTICE, "page is deleted");
155 : else
156 0 : maxoff = PageGetMaxOffsetNumber(page);
157 :
158 0 : for (offset = FirstOffsetNumber;
159 0 : offset <= maxoff;
160 0 : offset++)
161 : {
162 0 : Datum values[5];
163 0 : bool nulls[5];
164 0 : ItemId id;
165 0 : IndexTuple itup;
166 0 : bytea *tuple_bytea;
167 0 : int tuple_len;
168 :
169 0 : id = PageGetItemId(page, offset);
170 :
171 0 : if (!ItemIdIsValid(id))
172 0 : elog(ERROR, "invalid ItemId");
173 :
174 0 : itup = (IndexTuple) PageGetItem(page, id);
175 0 : tuple_len = IndexTupleSize(itup);
176 :
177 0 : memset(nulls, 0, sizeof(nulls));
178 :
179 0 : values[0] = UInt16GetDatum(offset);
180 0 : values[1] = ItemPointerGetDatum(&itup->t_tid);
181 0 : values[2] = Int32GetDatum((int) IndexTupleSize(itup));
182 :
183 0 : tuple_bytea = (bytea *) palloc(tuple_len + VARHDRSZ);
184 0 : SET_VARSIZE(tuple_bytea, tuple_len + VARHDRSZ);
185 0 : memcpy(VARDATA(tuple_bytea), itup, tuple_len);
186 0 : values[3] = BoolGetDatum(ItemIdIsDead(id));
187 0 : values[4] = PointerGetDatum(tuple_bytea);
188 :
189 0 : tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
190 0 : }
191 :
192 0 : return (Datum) 0;
193 0 : }
194 :
195 : Datum
196 0 : gist_page_items(PG_FUNCTION_ARGS)
197 : {
198 0 : bytea *raw_page = PG_GETARG_BYTEA_P(0);
199 0 : Oid indexRelid = PG_GETARG_OID(1);
200 0 : ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
201 0 : Relation indexRel;
202 0 : TupleDesc tupdesc;
203 0 : Page page;
204 0 : uint16 flagbits;
205 0 : bits16 printflags = 0;
206 0 : OffsetNumber offset;
207 0 : OffsetNumber maxoff = InvalidOffsetNumber;
208 0 : char *index_columns;
209 :
210 0 : if (!superuser())
211 0 : ereport(ERROR,
212 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
213 : errmsg("must be superuser to use raw page functions")));
214 :
215 0 : InitMaterializedSRF(fcinfo, 0);
216 :
217 : /* Open the relation */
218 0 : indexRel = index_open(indexRelid, AccessShareLock);
219 :
220 0 : if (!IS_GIST(indexRel))
221 0 : ereport(ERROR,
222 : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
223 : errmsg("\"%s\" is not a %s index",
224 : RelationGetRelationName(indexRel), "GiST")));
225 :
226 0 : page = verify_gist_page(raw_page);
227 :
228 0 : if (PageIsNew(page))
229 : {
230 0 : index_close(indexRel, AccessShareLock);
231 0 : PG_RETURN_NULL();
232 0 : }
233 :
234 0 : flagbits = GistPageGetOpaque(page)->flags;
235 :
236 : /*
237 : * Included attributes are added when dealing with leaf pages, discarded
238 : * for non-leaf pages as these include only data for key attributes.
239 : */
240 0 : printflags |= RULE_INDEXDEF_PRETTY;
241 0 : if (flagbits & F_LEAF)
242 : {
243 0 : tupdesc = RelationGetDescr(indexRel);
244 0 : }
245 : else
246 : {
247 0 : tupdesc = CreateTupleDescTruncatedCopy(RelationGetDescr(indexRel),
248 0 : IndexRelationGetNumberOfKeyAttributes(indexRel));
249 0 : printflags |= RULE_INDEXDEF_KEYS_ONLY;
250 : }
251 :
252 0 : index_columns = pg_get_indexdef_columns_extended(indexRelid,
253 0 : printflags);
254 :
255 : /* Avoid bogus PageGetMaxOffsetNumber() call with deleted pages */
256 0 : if (GistPageIsDeleted(page))
257 0 : elog(NOTICE, "page is deleted");
258 : else
259 0 : maxoff = PageGetMaxOffsetNumber(page);
260 :
261 0 : for (offset = FirstOffsetNumber;
262 0 : offset <= maxoff;
263 0 : offset++)
264 : {
265 0 : Datum values[5];
266 0 : bool nulls[5];
267 0 : ItemId id;
268 0 : IndexTuple itup;
269 0 : Datum itup_values[INDEX_MAX_KEYS];
270 0 : bool itup_isnull[INDEX_MAX_KEYS];
271 0 : StringInfoData buf;
272 0 : int i;
273 :
274 0 : id = PageGetItemId(page, offset);
275 :
276 0 : if (!ItemIdIsValid(id))
277 0 : elog(ERROR, "invalid ItemId");
278 :
279 0 : itup = (IndexTuple) PageGetItem(page, id);
280 :
281 0 : index_deform_tuple(itup, tupdesc,
282 0 : itup_values, itup_isnull);
283 :
284 0 : memset(nulls, 0, sizeof(nulls));
285 :
286 0 : values[0] = UInt16GetDatum(offset);
287 0 : values[1] = ItemPointerGetDatum(&itup->t_tid);
288 0 : values[2] = Int32GetDatum((int) IndexTupleSize(itup));
289 0 : values[3] = BoolGetDatum(ItemIdIsDead(id));
290 :
291 0 : if (index_columns)
292 : {
293 0 : initStringInfo(&buf);
294 0 : appendStringInfo(&buf, "(%s)=(", index_columns);
295 :
296 : /* Most of this is copied from record_out(). */
297 0 : for (i = 0; i < tupdesc->natts; i++)
298 : {
299 0 : char *value;
300 0 : char *tmp;
301 0 : bool nq = false;
302 :
303 0 : if (itup_isnull[i])
304 0 : value = "null";
305 : else
306 : {
307 0 : Oid foutoid;
308 0 : bool typisvarlena;
309 0 : Oid typoid;
310 :
311 0 : typoid = TupleDescAttr(tupdesc, i)->atttypid;
312 0 : getTypeOutputInfo(typoid, &foutoid, &typisvarlena);
313 0 : value = OidOutputFunctionCall(foutoid, itup_values[i]);
314 0 : }
315 :
316 0 : if (i == IndexRelationGetNumberOfKeyAttributes(indexRel))
317 0 : appendStringInfoString(&buf, ") INCLUDE (");
318 0 : else if (i > 0)
319 0 : appendStringInfoString(&buf, ", ");
320 :
321 : /* Check whether we need double quotes for this value */
322 0 : nq = (value[0] == '\0'); /* force quotes for empty string */
323 0 : for (tmp = value; *tmp; tmp++)
324 : {
325 0 : char ch = *tmp;
326 :
327 0 : if (ch == '"' || ch == '\\' ||
328 0 : ch == '(' || ch == ')' || ch == ',' ||
329 0 : isspace((unsigned char) ch))
330 : {
331 0 : nq = true;
332 0 : break;
333 : }
334 0 : }
335 :
336 : /* And emit the string */
337 0 : if (nq)
338 0 : appendStringInfoCharMacro(&buf, '"');
339 0 : for (tmp = value; *tmp; tmp++)
340 : {
341 0 : char ch = *tmp;
342 :
343 0 : if (ch == '"' || ch == '\\')
344 0 : appendStringInfoCharMacro(&buf, ch);
345 0 : appendStringInfoCharMacro(&buf, ch);
346 0 : }
347 0 : if (nq)
348 0 : appendStringInfoCharMacro(&buf, '"');
349 0 : }
350 :
351 0 : appendStringInfoChar(&buf, ')');
352 :
353 0 : values[4] = CStringGetTextDatum(buf.data);
354 0 : nulls[4] = false;
355 0 : }
356 : else
357 : {
358 0 : values[4] = (Datum) 0;
359 0 : nulls[4] = true;
360 : }
361 :
362 0 : tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
363 0 : }
364 :
365 0 : index_close(indexRel, AccessShareLock);
366 :
367 0 : return (Datum) 0;
368 0 : }
|