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