From 5f853c655663bf829c02895de2b445a5f3caa24b Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Mon, 6 Oct 2008 14:13:17 +0000
Subject: [PATCH] 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.

---
 contrib/pageinspect/pageinspect.sql.in |  6 +--
 contrib/pageinspect/rawpage.c          | 11 +++---
 doc/src/sgml/pageinspect.sgml          |  8 ++--
 doc/src/sgml/storage.sgml              |  9 ++---
 src/backend/catalog/catalog.c          | 53 ++++++++++++++++++++------
 src/backend/utils/adt/dbsize.c         | 31 +--------------
 src/include/catalog/catalog.h          |  5 ++-
 src/include/catalog/catversion.h       |  4 +-
 src/include/storage/relfilenode.h      |  4 +-
 9 files changed, 67 insertions(+), 64 deletions(-)

diff --git a/contrib/pageinspect/pageinspect.sql.in b/contrib/pageinspect/pageinspect.sql.in
index 49fea9eb51f..6ed49fa5440 100644
--- a/contrib/pageinspect/pageinspect.sql.in
+++ b/contrib/pageinspect/pageinspect.sql.in
@@ -1,4 +1,4 @@
-/* $PostgreSQL: pgsql/contrib/pageinspect/pageinspect.sql.in,v 1.5 2008/09/30 10:52:09 heikki Exp $ */
+/* $PostgreSQL: pgsql/contrib/pageinspect/pageinspect.sql.in,v 1.6 2008/10/06 14:13:17 heikki Exp $ */
 
 -- Adjust this setting to control where the objects get created.
 SET search_path = public;
@@ -6,14 +6,14 @@ SET search_path = public;
 --
 -- get_raw_page()
 --
-CREATE OR REPLACE FUNCTION get_raw_page(text, int4, int4)
+CREATE OR REPLACE FUNCTION get_raw_page(text, text, int4)
 RETURNS bytea
 AS 'MODULE_PATHNAME', 'get_raw_page'
 LANGUAGE C STRICT;
 
 CREATE OR REPLACE FUNCTION get_raw_page(text, int4) 
 RETURNS bytea
-AS $$ SELECT get_raw_page($1, 0, $2); $$
+AS $$ SELECT get_raw_page($1, 'main', $2); $$
 LANGUAGE SQL STRICT;
 
 --
diff --git a/contrib/pageinspect/rawpage.c b/contrib/pageinspect/rawpage.c
index 51c6ee179f4..13216d12e04 100644
--- a/contrib/pageinspect/rawpage.c
+++ b/contrib/pageinspect/rawpage.c
@@ -8,7 +8,7 @@
  * Copyright (c) 2007-2008, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/contrib/pageinspect/rawpage.c,v 1.7 2008/09/30 10:52:09 heikki Exp $
+ *	  $PostgreSQL: pgsql/contrib/pageinspect/rawpage.c,v 1.8 2008/10/06 14:13:17 heikki Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -17,6 +17,7 @@
 
 #include "access/heapam.h"
 #include "access/transam.h"
+#include "catalog/catalog.h"
 #include "catalog/namespace.h"
 #include "catalog/pg_type.h"
 #include "fmgr.h"
@@ -41,8 +42,9 @@ Datum
 get_raw_page(PG_FUNCTION_ARGS)
 {
 	text	   *relname = PG_GETARG_TEXT_P(0);
-	uint32		forknum = PG_GETARG_UINT32(1);
+	text	   *forkname = PG_GETARG_TEXT_P(1);
 	uint32		blkno = PG_GETARG_UINT32(2);
+	ForkNumber	forknum;
 
 	Relation	rel;
 	RangeVar   *relrv;
@@ -55,10 +57,7 @@ get_raw_page(PG_FUNCTION_ARGS)
 				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
 				 (errmsg("must be superuser to use raw functions"))));
 
-	if (forknum > MAX_FORKNUM)
-		ereport(ERROR,
-				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-				 errmsg("invalid fork number")));
+	forknum = forkname_to_number(text_to_cstring(forkname));
 
 	relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
 	rel = relation_openrv(relrv, AccessShareLock);
diff --git a/doc/src/sgml/pageinspect.sgml b/doc/src/sgml/pageinspect.sgml
index 94249399e10..e510d202aa4 100644
--- a/doc/src/sgml/pageinspect.sgml
+++ b/doc/src/sgml/pageinspect.sgml
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/pageinspect.sgml,v 1.4 2008/09/30 10:52:09 heikki Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/pageinspect.sgml,v 1.5 2008/10/06 14:13:17 heikki Exp $ -->
 
 <sect1 id="pageinspect">
  <title>pageinspect</title>
@@ -19,7 +19,7 @@
   <variablelist>
    <varlistentry>
     <term>
-     <function>get_raw_page(relname text, forknum int, blkno int) returns bytea</function>
+     <function>get_raw_page(relname text, fork text, blkno int) returns bytea</function>
     </term>
 
     <listitem>
@@ -27,8 +27,8 @@
       <function>get_raw_page</function> reads the specified block of the named
       table and returns a copy as a <type>bytea</> value.  This allows a
       single time-consistent copy of the block to be obtained.
-      <literal>forknum</literal> should be 0 for the main data fork, or 1 for
-      the FSM.
+      <literal>fork</literal> should be <literal>'main'</literal> for the main
+      data fork, or <literal>'fsm'</literal> for the FSM.
      </para>
     </listitem>
    </varlistentry>
diff --git a/doc/src/sgml/storage.sgml b/doc/src/sgml/storage.sgml
index 51f8a2fe165..9b93ea5599b 100644
--- a/doc/src/sgml/storage.sgml
+++ b/doc/src/sgml/storage.sgml
@@ -1,4 +1,4 @@
-<!-- $PostgreSQL: pgsql/doc/src/sgml/storage.sgml,v 1.25 2008/09/30 10:52:10 heikki Exp $ -->
+<!-- $PostgreSQL: pgsql/doc/src/sgml/storage.sgml,v 1.26 2008/10/06 14:13:17 heikki Exp $ -->
 
 <chapter id="storage">
 
@@ -134,8 +134,7 @@ or index's <firstterm>filenode</> number, which can be found in
 main file (aka. main fork), a <firstterm>free space map</> (see
 <xref linkend="storage-fsm">) that stores information about free space
 available in the relation, is stored in a file named after the filenode
-number, with the the _1 suffix. For example, if the table's filenode number
-is 12345, the FSM file is named <filename>12345_1</>.
+number, with the the <literal>_fsm</> suffix.
 </para>
 
 <caution>
@@ -385,9 +384,9 @@ comparison table, in which all the HTML pages were cut down to 7 kB to fit.
 A Free Space Map is stored with every heap and index relation, except for
 hash indexes, to keep track of available space in the relation. It's stored
 along the main relation data, in a separate FSM relation fork, named after
-relfilenode of the relation, but with a <literal>_1</> suffix. For example,
+relfilenode of the relation, but with a <literal>_fsm</> suffix. For example,
 if the relfilenode of a relation is 12345, the FSM is stored in a file called
-<filename>12345_1</>, in the same directory as the main relation file.
+<filename>12345_fsm</>, in the same directory as the main relation file.
 </para>
 
 <para>
diff --git a/src/backend/catalog/catalog.c b/src/backend/catalog/catalog.c
index eb7b39c86bf..733b9d9622d 100644
--- a/src/backend/catalog/catalog.c
+++ b/src/backend/catalog/catalog.c
@@ -10,7 +10,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/catalog/catalog.c,v 1.78 2008/08/11 11:05:10 heikki Exp $
+ *	  $PostgreSQL: pgsql/src/backend/catalog/catalog.c,v 1.79 2008/10/06 14:13:17 heikki Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -43,8 +43,38 @@
 
 
 #define OIDCHARS		10			/* max chars printed by %u */
-#define FORKNUMCHARS	1			/* max chars for a fork number */
+#define FORKNAMECHARS	4			/* max chars for a fork name */
 
+/*
+ * Lookup table of fork name by fork number.
+ *
+ * If you add a new entry, remember to update the errhint below, and the
+ * documentation for pg_relation_size(). Also keep FORKNAMECHARS above
+ * up-to-date.
+ */
+const char *forkNames[] = {
+	"main", /* MAIN_FORKNUM */
+	"fsm"   /* FSM_FORKNUM */
+};
+
+/*
+ * forkname_to_number - look up fork number by name
+ */
+ForkNumber
+forkname_to_number(char *forkName)
+{
+	ForkNumber forkNum;
+
+	for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
+		if (strcmp(forkNames[forkNum], forkName) == 0)
+			return forkNum;
+
+	ereport(ERROR,
+			(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+			 errmsg("invalid fork name"),
+			 errhint("Valid fork names are 'main' and 'fsm'")));
+	return InvalidForkNumber; /* keep compiler quiet */
+}
 
 /*
  * relpath			- construct path to a relation's file
@@ -61,22 +91,22 @@ relpath(RelFileNode rnode, ForkNumber forknum)
 	{
 		/* Shared system relations live in {datadir}/global */
 		Assert(rnode.dbNode == 0);
-		pathlen = 7 + OIDCHARS + 1 + FORKNUMCHARS + 1;
+		pathlen = 7 + OIDCHARS + 1 + FORKNAMECHARS + 1;
 		path = (char *) palloc(pathlen);
 		if (forknum != MAIN_FORKNUM)
-			snprintf(path, pathlen, "global/%u_%u",
-					 rnode.relNode, forknum);
+			snprintf(path, pathlen, "global/%u_%s",
+					 rnode.relNode, forkNames[forknum]);
 		else
 			snprintf(path, pathlen, "global/%u", rnode.relNode);
 	}
 	else if (rnode.spcNode == DEFAULTTABLESPACE_OID)
 	{
 		/* The default tablespace is {datadir}/base */
-		pathlen = 5 + OIDCHARS + 1 + OIDCHARS + 1 + FORKNUMCHARS + 1;
+		pathlen = 5 + OIDCHARS + 1 + OIDCHARS + 1 + FORKNAMECHARS + 1;
 		path = (char *) palloc(pathlen);
 		if (forknum != MAIN_FORKNUM)
-			snprintf(path, pathlen, "base/%u/%u_%u",
-					 rnode.dbNode, rnode.relNode, forknum);
+			snprintf(path, pathlen, "base/%u/%u_%s",
+					 rnode.dbNode, rnode.relNode, forkNames[forknum]);
 		else
 			snprintf(path, pathlen, "base/%u/%u",
 					 rnode.dbNode, rnode.relNode);
@@ -85,11 +115,12 @@ relpath(RelFileNode rnode, ForkNumber forknum)
 	{
 		/* All other tablespaces are accessed via symlinks */
 		pathlen = 10 + OIDCHARS + 1 + OIDCHARS + 1 + OIDCHARS + 1
-			+ FORKNUMCHARS + 1;
+			+ FORKNAMECHARS + 1;
 		path = (char *) palloc(pathlen);
 		if (forknum != MAIN_FORKNUM)
-			snprintf(path, pathlen, "pg_tblspc/%u/%u/%u_%u",
-					 rnode.spcNode, rnode.dbNode, rnode.relNode, forknum);
+			snprintf(path, pathlen, "pg_tblspc/%u/%u/%u_%s",
+					 rnode.spcNode, rnode.dbNode, rnode.relNode,
+					 forkNames[forknum]);
 		else
 			snprintf(path, pathlen, "pg_tblspc/%u/%u/%u",
 					 rnode.spcNode, rnode.dbNode, rnode.relNode);
diff --git a/src/backend/utils/adt/dbsize.c b/src/backend/utils/adt/dbsize.c
index d225b3e7c75..4d3f8c0b5ce 100644
--- a/src/backend/utils/adt/dbsize.c
+++ b/src/backend/utils/adt/dbsize.c
@@ -5,7 +5,7 @@
  * Copyright (c) 2002-2008, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.21 2008/10/03 07:33:09 heikki Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/adt/dbsize.c,v 1.22 2008/10/06 14:13:17 heikki Exp $
  *
  */
 
@@ -283,35 +283,6 @@ calculate_relation_size(RelFileNode *rfn, ForkNumber forknum)
 	return totalsize;
 }
 
-
-/*
- * XXX: Consider making this global and moving elsewhere. But currently
- * there's no other users for this.
- *
- * Remember to also update the errhint below if you add entries, and the
- * documentation for pg_relation_size().
- */
-static char *forkNames[] = {
-	"main", /* MAIN_FORKNUM */
-	"fsm"   /* FSM_FORKNUM */
-};
-
-static ForkNumber
-forkname_to_number(char *forkName)
-{
-	ForkNumber forkNum;
-
-	for (forkNum = 0; forkNum <= MAX_FORKNUM; forkNum++)
-		if (strcmp(forkNames[forkNum], forkName) == 0)
-			return forkNum;
-
-	ereport(ERROR,
-			(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
-			 errmsg("invalid fork name"),
-			 errhint("Valid fork names are 'main' and 'fsm'")));
-	return InvalidForkNumber; /* keep compiler quiet */
-}
-
 Datum
 pg_relation_size(PG_FUNCTION_ARGS)
 {
diff --git a/src/include/catalog/catalog.h b/src/include/catalog/catalog.h
index 52a69e13419..fb555cbc6b7 100644
--- a/src/include/catalog/catalog.h
+++ b/src/include/catalog/catalog.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/catalog.h,v 1.41 2008/08/11 11:05:11 heikki Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/catalog.h,v 1.42 2008/10/06 14:13:17 heikki Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -19,6 +19,9 @@
 #include "utils/relcache.h"
 
 
+extern const char *forkNames[];
+extern ForkNumber forkname_to_number(char *forkName);
+
 extern char *relpath(RelFileNode rnode, ForkNumber forknum);
 extern char *GetDatabasePath(Oid dbNode, Oid spcNode);
 
diff --git a/src/include/catalog/catversion.h b/src/include/catalog/catversion.h
index fd6895263bb..cdd9fa272ea 100644
--- a/src/include/catalog/catversion.h
+++ b/src/include/catalog/catversion.h
@@ -37,7 +37,7 @@
  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.494 2008/10/06 13:05:37 mha Exp $
+ * $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.495 2008/10/06 14:13:17 heikki Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -53,6 +53,6 @@
  */
 
 /*							yyyymmddN */
-#define CATALOG_VERSION_NO	200810061
+#define CATALOG_VERSION_NO	200810062
 
 #endif
diff --git a/src/include/storage/relfilenode.h b/src/include/storage/relfilenode.h
index 128cff99a9d..383cc18a578 100644
--- a/src/include/storage/relfilenode.h
+++ b/src/include/storage/relfilenode.h
@@ -7,7 +7,7 @@
  * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/storage/relfilenode.h,v 1.18 2008/10/03 07:33:10 heikki Exp $
+ * $PostgreSQL: pgsql/src/include/storage/relfilenode.h,v 1.19 2008/10/06 14:13:17 heikki Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -27,7 +27,7 @@ typedef enum ForkNumber
 	FSM_FORKNUM
 	/*
 	 * NOTE: if you add a new fork, change MAX_FORKNUM below and update the
-	 * name to number mapping in utils/adt/dbsize.c
+	 * forkNames array in catalog.c
 	 */
 } ForkNumber;
 
-- 
GitLab