Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5f853c6

Browse files
committed
Use fork names instead of numbers in the file names for additional
relation forks. While the file names are not visible to users, for those that do peek into the data directory, it's nice to have more descriptive names. Per Greg Stark's suggestion.
1 parent e59ad5f commit 5f853c6

File tree

9 files changed

+67
-64
lines changed

9 files changed

+67
-64
lines changed

contrib/pageinspect/pageinspect.sql.in

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
/* $PostgreSQL: pgsql/contrib/pageinspect/pageinspect.sql.in,v 1.5 2008/09/30 10:52:09 heikki Exp $ */
1+
/* $PostgreSQL: pgsql/contrib/pageinspect/pageinspect.sql.in,v 1.6 2008/10/06 14:13:17 heikki Exp $ */
22

33
-- Adjust this setting to control where the objects get created.
44
SET search_path = public;
55

66
--
77
-- get_raw_page()
88
--
9-
CREATE OR REPLACE FUNCTION get_raw_page(text, int4, int4)
9+
CREATE OR REPLACE FUNCTION get_raw_page(text, text, int4)
1010
RETURNS bytea
1111
AS 'MODULE_PATHNAME', 'get_raw_page'
1212
LANGUAGE C STRICT;
1313

1414
CREATE OR REPLACE FUNCTION get_raw_page(text, int4)
1515
RETURNS bytea
16-
AS $$ SELECT get_raw_page($1, 0, $2); $$
16+
AS $$ SELECT get_raw_page($1, 'main', $2); $$
1717
LANGUAGE SQL STRICT;
1818

1919
--

contrib/pageinspect/rawpage.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Copyright (c) 2007-2008, PostgreSQL Global Development Group
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/contrib/pageinspect/rawpage.c,v 1.7 2008/09/30 10:52:09 heikki Exp $
11+
* $PostgreSQL: pgsql/contrib/pageinspect/rawpage.c,v 1.8 2008/10/06 14:13:17 heikki Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -17,6 +17,7 @@
1717

1818
#include "access/heapam.h"
1919
#include "access/transam.h"
20+
#include "catalog/catalog.h"
2021
#include "catalog/namespace.h"
2122
#include "catalog/pg_type.h"
2223
#include "fmgr.h"
@@ -41,8 +42,9 @@ Datum
4142
get_raw_page(PG_FUNCTION_ARGS)
4243
{
4344
text *relname = PG_GETARG_TEXT_P(0);
44-
uint32 forknum = PG_GETARG_UINT32(1);
45+
text *forkname = PG_GETARG_TEXT_P(1);
4546
uint32 blkno = PG_GETARG_UINT32(2);
47+
ForkNumber forknum;
4648

4749
Relation rel;
4850
RangeVar *relrv;
@@ -55,10 +57,7 @@ get_raw_page(PG_FUNCTION_ARGS)
5557
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
5658
(errmsg("must be superuser to use raw functions"))));
5759

58-
if (forknum > MAX_FORKNUM)
59-
ereport(ERROR,
60-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
61-
errmsg("invalid fork number")));
60+
forknum = forkname_to_number(text_to_cstring(forkname));
6261

6362
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
6463
rel = relation_openrv(relrv, AccessShareLock);

doc/src/sgml/pageinspect.sgml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/pageinspect.sgml,v 1.4 2008/09/30 10:52:09 heikki Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/pageinspect.sgml,v 1.5 2008/10/06 14:13:17 heikki Exp $ -->
22

33
<sect1 id="pageinspect">
44
<title>pageinspect</title>
@@ -19,16 +19,16 @@
1919
<variablelist>
2020
<varlistentry>
2121
<term>
22-
<function>get_raw_page(relname text, forknum int, blkno int) returns bytea</function>
22+
<function>get_raw_page(relname text, fork text, blkno int) returns bytea</function>
2323
</term>
2424

2525
<listitem>
2626
<para>
2727
<function>get_raw_page</function> reads the specified block of the named
2828
table and returns a copy as a <type>bytea</> value. This allows a
2929
single time-consistent copy of the block to be obtained.
30-
<literal>forknum</literal> should be 0 for the main data fork, or 1 for
31-
the FSM.
30+
<literal>fork</literal> should be <literal>'main'</literal> for the main
31+
data fork, or <literal>'fsm'</literal> for the FSM.
3232
</para>
3333
</listitem>
3434
</varlistentry>

doc/src/sgml/storage.sgml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/storage.sgml,v 1.25 2008/09/30 10:52:10 heikki Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/storage.sgml,v 1.26 2008/10/06 14:13:17 heikki Exp $ -->
22

33
<chapter id="storage">
44

@@ -134,8 +134,7 @@ or index's <firstterm>filenode</> number, which can be found in
134134
main file (aka. main fork), a <firstterm>free space map</> (see
135135
<xref linkend="storage-fsm">) that stores information about free space
136136
available in the relation, is stored in a file named after the filenode
137-
number, with the the _1 suffix. For example, if the table's filenode number
138-
is 12345, the FSM file is named <filename>12345_1</>.
137+
number, with the the <literal>_fsm</> suffix.
139138
</para>
140139

141140
<caution>
@@ -385,9 +384,9 @@ comparison table, in which all the HTML pages were cut down to 7 kB to fit.
385384
A Free Space Map is stored with every heap and index relation, except for
386385
hash indexes, to keep track of available space in the relation. It's stored
387386
along the main relation data, in a separate FSM relation fork, named after
388-
relfilenode of the relation, but with a <literal>_1</> suffix. For example,
387+
relfilenode of the relation, but with a <literal>_fsm</> suffix. For example,
389388
if the relfilenode of a relation is 12345, the FSM is stored in a file called
390-
<filename>12345_1</>, in the same directory as the main relation file.
389+
<filename>12345_fsm</>, in the same directory as the main relation file.
391390
</para>
392391

393392
<para>

src/backend/catalog/catalog.c

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/catalog/catalog.c,v 1.78 2008/08/11 11:05:10 heikki Exp $
13+
* $PostgreSQL: pgsql/src/backend/catalog/catalog.c,v 1.79 2008/10/06 14:13:17 heikki Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -43,8 +43,38 @@
4343

4444

4545
#define OIDCHARS 10 /* max chars printed by %u */
46-
#define FORKNUMCHARS 1 /* max chars for a fork number */
46+
#define FORKNAMECHARS 4 /* max chars for a fork name */
4747

48+
/*
49+
* Lookup table of fork name by fork number.
50+
*
51+
* If you add a new entry, remember to update the errhint below, and the
52+
* documentation for pg_relation_size(). Also keep FORKNAMECHARS above
53+
* up-to-date.
54+
*/
55+
const char *forkNames[] = {
56+
"main", /* MAIN_FORKNUM */
57+
"fsm" /* FSM_FORKNUM */
58+
};
59+
60+
/*
61+
* forkname_to_number - look up fork number by name
62+
*/
63+
ForkNumber
64+
forkname_to_number(char *forkName)
65+
{
66+
ForkNumber forkNum;
67+
68+
for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
69+
if (strcmp(forkNames[forkNum], forkName) == 0)
70+
return forkNum;
71+
72+
ereport(ERROR,
73+
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
74+
errmsg("invalid fork name"),
75+
errhint("Valid fork names are 'main' and 'fsm'")));
76+
return InvalidForkNumber; /* keep compiler quiet */
77+
}
4878

4979
/*
5080
* relpath - construct path to a relation's file
@@ -61,22 +91,22 @@ relpath(RelFileNode rnode, ForkNumber forknum)
6191
{
6292
/* Shared system relations live in {datadir}/global */
6393
Assert(rnode.dbNode == 0);
64-
pathlen = 7 + OIDCHARS + 1 + FORKNUMCHARS + 1;
94+
pathlen = 7 + OIDCHARS + 1 + FORKNAMECHARS + 1;
6595
path = (char *) palloc(pathlen);
6696
if (forknum != MAIN_FORKNUM)
67-
snprintf(path, pathlen, "global/%u_%u",
68-
rnode.relNode, forknum);
97+
snprintf(path, pathlen, "global/%u_%s",
98+
rnode.relNode, forkNames[forknum]);
6999
else
70100
snprintf(path, pathlen, "global/%u", rnode.relNode);
71101
}
72102
else if (rnode.spcNode == DEFAULTTABLESPACE_OID)
73103
{
74104
/* The default tablespace is {datadir}/base */
75-
pathlen = 5 + OIDCHARS + 1 + OIDCHARS + 1 + FORKNUMCHARS + 1;
105+
pathlen = 5 + OIDCHARS + 1 + OIDCHARS + 1 + FORKNAMECHARS + 1;
76106
path = (char *) palloc(pathlen);
77107
if (forknum != MAIN_FORKNUM)
78-
snprintf(path, pathlen, "base/%u/%u_%u",
79-
rnode.dbNode, rnode.relNode, forknum);
108+
snprintf(path, pathlen, "base/%u/%u_%s",
109+
rnode.dbNode, rnode.relNode, forkNames[forknum]);
80110
else
81111
snprintf(path, pathlen, "base/%u/%u",
82112
rnode.dbNode, rnode.relNode);
@@ -85,11 +115,12 @@ relpath(RelFileNode rnode, ForkNumber forknum)
85115
{
86116
/* All other tablespaces are accessed via symlinks */
87117
pathlen = 10 + OIDCHARS + 1 + OIDCHARS + 1 + OIDCHARS + 1
88-
+ FORKNUMCHARS + 1;
118+
+ FORKNAMECHARS + 1;
89119
path = (char *) palloc(pathlen);
90120
if (forknum != MAIN_FORKNUM)
91-
snprintf(path, pathlen, "pg_tblspc/%u/%u/%u_%u",
92-
rnode.spcNode, rnode.dbNode, rnode.relNode, forknum);
121+
snprintf(path, pathlen, "pg_tblspc/%u/%u/%u_%s",
122+
rnode.spcNode, rnode.dbNode, rnode.relNode,
123+
forkNames[forknum]);
93124
else
94125
snprintf(path, pathlen, "pg_tblspc/%u/%u/%u",
95126
rnode.spcNode, rnode.dbNode, rnode.relNode);

src/backend/utils/adt/dbsize.c

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Copyright (c) 2002-2008, PostgreSQL Global Development Group
66
*
77
* IDENTIFICATION
8-
* $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.21 2008/10/03 07:33:09 heikki Exp $
8+
* $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.22 2008/10/06 14:13:17 heikki Exp $
99
*
1010
*/
1111

@@ -283,35 +283,6 @@ calculate_relation_size(RelFileNode *rfn, ForkNumber forknum)
283283
return totalsize;
284284
}
285285

286-
287-
/*
288-
* XXX: Consider making this global and moving elsewhere. But currently
289-
* there's no other users for this.
290-
*
291-
* Remember to also update the errhint below if you add entries, and the
292-
* documentation for pg_relation_size().
293-
*/
294-
static char *forkNames[] = {
295-
"main", /* MAIN_FORKNUM */
296-
"fsm" /* FSM_FORKNUM */
297-
};
298-
299-
static ForkNumber
300-
forkname_to_number(char *forkName)
301-
{
302-
ForkNumber forkNum;
303-
304-
for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
305-
if (strcmp(forkNames[forkNum], forkName) == 0)
306-
return forkNum;
307-
308-
ereport(ERROR,
309-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
310-
errmsg("invalid fork name"),
311-
errhint("Valid fork names are 'main' and 'fsm'")));
312-
return InvalidForkNumber; /* keep compiler quiet */
313-
}
314-
315286
Datum
316287
pg_relation_size(PG_FUNCTION_ARGS)
317288
{

src/include/catalog/catalog.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/catalog.h,v 1.41 2008/08/11 11:05:11 heikki Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/catalog.h,v 1.42 2008/10/06 14:13:17 heikki Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -19,6 +19,9 @@
1919
#include "utils/relcache.h"
2020

2121

22+
extern const char *forkNames[];
23+
extern ForkNumber forkname_to_number(char *forkName);
24+
2225
extern char *relpath(RelFileNode rnode, ForkNumber forknum);
2326
extern char *GetDatabasePath(Oid dbNode, Oid spcNode);
2427

src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.494 2008/10/06 13:05:37 mha Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.495 2008/10/06 14:13:17 heikki Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 200810061
56+
#define CATALOG_VERSION_NO 200810062
5757

5858
#endif

src/include/storage/relfilenode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/storage/relfilenode.h,v 1.18 2008/10/03 07:33:10 heikki Exp $
10+
* $PostgreSQL: pgsql/src/include/storage/relfilenode.h,v 1.19 2008/10/06 14:13:17 heikki Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -27,7 +27,7 @@ typedef enum ForkNumber
2727
FSM_FORKNUM
2828
/*
2929
* NOTE: if you add a new fork, change MAX_FORKNUM below and update the
30-
* name to number mapping in utils/adt/dbsize.c
30+
* forkNames array in catalog.c
3131
*/
3232
} ForkNumber;
3333

0 commit comments

Comments
 (0)