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