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