1 /*-------------------------------------------------------------------------
2  *
3  * fsmfuncs.c
4  *	  Functions to investigate FSM pages
5  *
6  * These functions are restricted to superusers for the fear of introducing
7  * security holes if the input checking isn't as water-tight as it should.
8  * You'd need to be superuser to obtain a raw page image anyway, so
9  * there's hardly any use case for using these without superuser-rights
10  * anyway.
11  *
12  * Copyright (c) 2007-2016, PostgreSQL Global Development Group
13  *
14  * IDENTIFICATION
15  *	  contrib/pageinspect/fsmfuncs.c
16  *
17  *-------------------------------------------------------------------------
18  */
19 
20 #include "postgres.h"
21 
22 #include "funcapi.h"
23 #include "lib/stringinfo.h"
24 #include "miscadmin.h"
25 #include "storage/fsm_internals.h"
26 #include "utils/builtins.h"
27 
28 /*
29  * Dumps the contents of a FSM page.
30  */
31 PG_FUNCTION_INFO_V1(fsm_page_contents);
32 
33 Datum
fsm_page_contents(PG_FUNCTION_ARGS)34 fsm_page_contents(PG_FUNCTION_ARGS)
35 {
36 	bytea	   *raw_page = PG_GETARG_BYTEA_P(0);
37 	StringInfoData sinfo;
38 	FSMPage		fsmpage;
39 	int			i;
40 
41 	if (!superuser())
42 		ereport(ERROR,
43 				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
44 				 (errmsg("must be superuser to use raw page functions"))));
45 
46 	fsmpage = (FSMPage) PageGetContents(VARDATA(raw_page));
47 
48 	initStringInfo(&sinfo);
49 
50 	for (i = 0; i < NodesPerPage; i++)
51 	{
52 		if (fsmpage->fp_nodes[i] != 0)
53 			appendStringInfo(&sinfo, "%d: %d\n", i, fsmpage->fp_nodes[i]);
54 	}
55 	appendStringInfo(&sinfo, "fp_next_slot: %d\n", fsmpage->fp_next_slot);
56 
57 	PG_RETURN_TEXT_P(cstring_to_text_with_len(sinfo.data, sinfo.len));
58 }
59