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