From c9a6490991f6ab4e01f50eb9983777fca8a6e75a Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Mon, 4 Sep 2006 02:03:04 +0000
Subject: [PATCH] Clean up some leftover problems in pgstattuple: remove
 unwanted and unportable elog(NOTICE) report, fix install/uninstall sequence.
 Itagaki Takahiro

---
 contrib/pgstattuple/pgstatindex.c             |  2 +-
 contrib/pgstattuple/pgstattuple.c             | 99 ++++---------------
 contrib/pgstattuple/pgstattuple.sql.in        |  8 --
 contrib/pgstattuple/uninstall_pgstattuple.sql | 16 ++-
 4 files changed, 32 insertions(+), 93 deletions(-)

diff --git a/contrib/pgstattuple/pgstatindex.c b/contrib/pgstattuple/pgstatindex.c
index bda69c252d8..d684a3e055e 100644
--- a/contrib/pgstattuple/pgstatindex.c
+++ b/contrib/pgstattuple/pgstatindex.c
@@ -561,7 +561,7 @@ bt_page_items(PG_FUNCTION_ARGS)
 			values[j] = palloc(32);
 			snprintf(values[j++], 32, "(%u,%u)", blkno, itup->t_tid.ip_posid);
 			values[j] = palloc(32);
-			snprintf(values[j++], 32, "%d", IndexTupleSize(itup));
+			snprintf(values[j++], 32, "%d", (int) IndexTupleSize(itup));
 			values[j] = palloc(32);
 			snprintf(values[j++], 32, "%c", IndexTupleHasNulls(itup) ? 't' : 'f');
 			values[j] = palloc(32);
diff --git a/contrib/pgstattuple/pgstattuple.c b/contrib/pgstattuple/pgstattuple.c
index ed3f1031d6d..6ff6012073a 100644
--- a/contrib/pgstattuple/pgstattuple.c
+++ b/contrib/pgstattuple/pgstattuple.c
@@ -1,5 +1,5 @@
 /*
- * $PostgreSQL: pgsql/contrib/pgstattuple/pgstattuple.c,v 1.23 2006/07/11 17:26:58 momjian Exp $
+ * $PostgreSQL: pgsql/contrib/pgstattuple/pgstattuple.c,v 1.24 2006/09/04 02:03:04 tgl Exp $
  *
  * Copyright (c) 2001,2002	Tatsuo Ishii
  *
@@ -59,35 +59,19 @@ typedef struct pgstattuple_type
 	uint64	free_space;			/* free/reusable space in bytes */
 } pgstattuple_type;
 
-/*
- * struct pgstat_btree_type
- */
-typedef struct pgstat_btree_type
-{
-	pgstattuple_type	base;	/* inherits pgstattuple_type */
-
-	uint64	continuous;
-	uint64	forward;
-	uint64	backward;
-} pgstat_btree_type;
-
 typedef void (*pgstat_page)(pgstattuple_type *, Relation, BlockNumber);
 
 static Datum build_pgstattuple_type(pgstattuple_type *stat,
 	FunctionCallInfo fcinfo);
 static Datum pgstat_relation(Relation rel, FunctionCallInfo fcinfo);
 static Datum pgstat_heap(Relation rel, FunctionCallInfo fcinfo);
-static Datum pgstat_btree(Relation rel, FunctionCallInfo fcinfo);
 static void pgstat_btree_page(pgstattuple_type *stat,
 	Relation rel, BlockNumber blkno);
-static Datum pgstat_hash(Relation rel, FunctionCallInfo fcinfo);
 static void pgstat_hash_page(pgstattuple_type *stat,
 	Relation rel, BlockNumber blkno);
-static Datum pgstat_gist(Relation rel, FunctionCallInfo fcinfo);
 static void pgstat_gist_page(pgstattuple_type *stat,
 	Relation rel, BlockNumber blkno);
-static Datum pgstat_index(pgstattuple_type *stat,
-	Relation rel, BlockNumber start,
+static Datum pgstat_index(Relation rel, BlockNumber start,
 	pgstat_page pagefn, FunctionCallInfo fcinfo);
 static void pgstat_index_page(pgstattuple_type *stat, Page page,
 	OffsetNumber minoff, OffsetNumber maxoff);
@@ -217,11 +201,14 @@ pgstat_relation(Relation rel, FunctionCallInfo fcinfo)
 		switch(rel->rd_rel->relam)
 		{
 		case BTREE_AM_OID:
-			return pgstat_btree(rel, fcinfo);
+			return pgstat_index(rel, BTREE_METAPAGE + 1,
+				pgstat_btree_page, fcinfo);
 		case HASH_AM_OID:
-			return pgstat_hash(rel, fcinfo);
+			return pgstat_index(rel, HASH_METAPAGE + 1,
+				pgstat_hash_page, fcinfo);
 		case GIST_AM_OID:
-			return pgstat_gist(rel, fcinfo);
+			return pgstat_index(rel, GIST_ROOT_BLKNO + 1,
+				pgstat_gist_page, fcinfo);
 		case GIN_AM_OID:
 			err = "gin index";
 			break;
@@ -321,36 +308,13 @@ pgstat_heap(Relation rel, FunctionCallInfo fcinfo)
 }
 
 /*
- * pgstat_btree -- returns live/dead tuples info in a btree index
- */
-static Datum
-pgstat_btree(Relation rel, FunctionCallInfo fcinfo)
-{
-	pgstat_btree_type	stat = { { 0 } };
-	Datum				datum;
-
-	datum = pgstat_index((pgstattuple_type *) &stat, rel,
-		BTREE_METAPAGE + 1, pgstat_btree_page, fcinfo);
-
-	ereport(NOTICE,
-		(errmsg("%.2f%% fragmented",
-			100.0 * (stat.forward + stat.backward) /
-			(stat.continuous + stat.forward + stat.backward)),
-		errhint("continuous=%llu, forward=%llu, backward=%llu",
-			stat.continuous, stat.forward, stat.backward)));
-
-	return datum;
-}
-
-/*
- * pgstat_btree_page
+ * pgstat_btree_page -- check tuples in a btree page
  */
 static void
 pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
 {
 	Buffer				buf;
 	Page				page;
-	pgstat_btree_type  *btstat = (pgstat_btree_type *)stat;
 
 	buf = ReadBuffer(rel, blkno);
 	LockBuffer(buf, BT_READ);
@@ -373,16 +337,6 @@ pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
 		}
 		else if (P_ISLEAF(opaque))
 		{
-			/* check fragmentation */
-			if (P_RIGHTMOST(opaque))
-				btstat->continuous++;
-			else if (opaque->btpo_next < blkno)
-				btstat->backward++;
-			else if (opaque->btpo_next > blkno + 1)
-				btstat->forward++;
-			else
-				btstat->continuous++;
-
 			pgstat_index_page(stat, page, P_FIRSTDATAKEY(opaque),
 				PageGetMaxOffsetNumber(page));
 		}
@@ -396,17 +350,7 @@ pgstat_btree_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
 }
 
 /*
- * pgstat_hash -- returns live/dead tuples info in a hash index
- */
-static Datum
-pgstat_hash(Relation rel, FunctionCallInfo fcinfo)
-{
-	pgstattuple_type	stat = { 0 };
-	return pgstat_index(&stat, rel, HASH_METAPAGE + 1, pgstat_hash_page, fcinfo);
-}
-
-/*
- * pgstat_hash_page
+ * pgstat_hash_page -- check tuples in a hash page
  */
 static void
 pgstat_hash_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
@@ -448,17 +392,7 @@ pgstat_hash_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
 }
 
 /*
- * pgstat_gist -- returns live/dead tuples info in a gist index
- */
-static Datum
-pgstat_gist(Relation rel, FunctionCallInfo fcinfo)
-{
-	pgstattuple_type	stat = { 0 };
-	return pgstat_index(&stat, rel, GIST_ROOT_BLKNO + 1, pgstat_gist_page, fcinfo);
-}
-
-/*
- * pgstat_gist_page
+ * pgstat_gist_page -- check tuples in a gist page
  */
 static void
 pgstat_gist_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
@@ -488,11 +422,12 @@ pgstat_gist_page(pgstattuple_type *stat, Relation rel, BlockNumber blkno)
  * pgstat_index -- returns live/dead tuples info in a generic index
  */
 static Datum
-pgstat_index(pgstattuple_type *stat, Relation rel, BlockNumber start,
-	pgstat_page pagefn, FunctionCallInfo fcinfo)
+pgstat_index(Relation rel, BlockNumber start, pgstat_page pagefn,
+			 FunctionCallInfo fcinfo)
 {
 	BlockNumber nblocks;
 	BlockNumber blkno;
+	pgstattuple_type	stat = { 0 };
 
 	blkno = start;
 	for (;;)
@@ -505,17 +440,17 @@ pgstat_index(pgstattuple_type *stat, Relation rel, BlockNumber start,
 		/* Quit if we've scanned the whole relation */
 		if (blkno >= nblocks)
 		{
-			stat->table_len = (uint64) nblocks * BLCKSZ;
+			stat.table_len = (uint64) nblocks * BLCKSZ;
 			break;
 		}
 
 		for (; blkno < nblocks; blkno++)
-			pagefn(stat, rel, blkno);
+			pagefn(&stat, rel, blkno);
 	}
 
 	relation_close(rel, AccessShareLock);
 
-	return build_pgstattuple_type(stat, fcinfo);
+	return build_pgstattuple_type(&stat, fcinfo);
 }
 
 /*
diff --git a/contrib/pgstattuple/pgstattuple.sql.in b/contrib/pgstattuple/pgstattuple.sql.in
index cb401d94c7c..39220f35366 100644
--- a/contrib/pgstattuple/pgstattuple.sql.in
+++ b/contrib/pgstattuple/pgstattuple.sql.in
@@ -26,7 +26,6 @@ LANGUAGE C STRICT;
 --
 -- pgstatindex
 --
-DROP TYPE pgstatindex_type CASCADE;
 CREATE TYPE pgstatindex_type AS (
   version int4,
   tree_level int4,
@@ -48,7 +47,6 @@ LANGUAGE 'C' STRICT;
 --
 -- bt_metap()
 --
-DROP TYPE bt_metap_type CASCADE;
 CREATE TYPE bt_metap_type AS (
   magic int4,
   version int4,
@@ -66,7 +64,6 @@ LANGUAGE 'C' STRICT;
 --
 -- bt_page_stats()
 --
-DROP TYPE bt_page_stats_type CASCADE;
 CREATE TYPE bt_page_stats_type AS (
   blkno int4,
   type char,
@@ -81,8 +78,6 @@ CREATE TYPE bt_page_stats_type AS (
   btpo_flags int4
 );
 
-DROP FUNCTION bt_page_stats(text, int4);
-
 CREATE OR REPLACE FUNCTION bt_page_stats(text, int4)
 RETURNS bt_page_stats_type
 AS 'MODULE_PATHNAME', 'bt_page_stats'
@@ -91,7 +86,6 @@ LANGUAGE 'C' STRICT;
 --
 -- bt_page_items()
 --
-DROP TYPE bt_page_items_type CASCADE;
 CREATE TYPE bt_page_items_type AS (
   itemoffset int4,
   ctid tid,
@@ -101,8 +95,6 @@ CREATE TYPE bt_page_items_type AS (
   data text
 );
 
-DROP FUNCTION bt_page_items(text, int4);
-
 CREATE OR REPLACE FUNCTION bt_page_items(text, int4)
 RETURNS SETOF bt_page_items_type
 AS 'MODULE_PATHNAME', 'bt_page_items'
diff --git a/contrib/pgstattuple/uninstall_pgstattuple.sql b/contrib/pgstattuple/uninstall_pgstattuple.sql
index efecc3b2e42..5b857bb868f 100644
--- a/contrib/pgstattuple/uninstall_pgstattuple.sql
+++ b/contrib/pgstattuple/uninstall_pgstattuple.sql
@@ -2,7 +2,19 @@
 SET search_path = public;
 
 DROP FUNCTION pgstattuple(oid);
-
 DROP FUNCTION pgstattuple(text);
-
 DROP TYPE pgstattuple_type;
+
+DROP FUNCTION pgstatindex(text);
+DROP TYPE pgstatindex_type;
+
+DROP FUNCTION bt_metap(text);
+DROP TYPE bt_metap_type;
+
+DROP FUNCTION bt_page_stats(text, int4);
+DROP TYPE bt_page_stats_type;
+
+DROP FUNCTION bt_page_items(text, int4);
+DROP TYPE bt_page_items_type;
+
+DROP FUNCTION pg_relpages(text);
-- 
GitLab