Skip to content

Commit a43974c

Browse files
committed
Code review for contrib/pg_freespacemap. Add a storedpages column to
pg_freespacemap_relations --- while one could theoretically get that number by counting rows in pg_freespacemap_pages, it's surely the hard way to do it. Avoid expensive and inconvenient conversion to and from text format. Minor code and docs cleanup.
1 parent 46287bd commit a43974c

File tree

3 files changed

+181
-211
lines changed

3 files changed

+181
-211
lines changed

contrib/pg_freespacemap/README.pg_freespacemap

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ Notes
3939
Column | references | Description
4040
----------------+----------------------+------------------------------------
4141
reltablespace | pg_tablespace.oid | Tablespace oid of the relation.
42-
reldatabase | pg_database.oid | Database for the relation.
43-
relfilenode | pg_class.relfilenode | Refilenode of the relation.
42+
reldatabase | pg_database.oid | Database oid of the relation.
43+
relfilenode | pg_class.relfilenode | Relfilenode of the relation.
4444
avgrequest | | Moving average of free space
45-
| | requests.
46-
lastpagecount | | Count of pages examined for useful
47-
| | free space.
48-
nextpage | | page index (from 0) to start next
45+
| | requests (NULL for indexes)
46+
lastpagecount | | Count of pages last reported as
47+
| | containing useful free space.
48+
storedpages | | Count of pages actually stored
49+
| | in free space map.
50+
nextpage | | Page index (from 0) to start next
4951
| | search at.
5052

5153

@@ -54,24 +56,33 @@ Notes
5456
Column | references | Description
5557
----------------+----------------------+------------------------------------
5658
reltablespace | pg_tablespace.oid | Tablespace oid of the relation.
57-
reldatabase | pg_database.oid | Database for the relation.
58-
relfilenode | pg_class.relfilenode | Refilenode of the relation.
59-
relblocknumber | | Page offset in the relation.
59+
reldatabase | pg_database.oid | Database oid of the relation.
60+
relfilenode | pg_class.relfilenode | Relfilenode of the relation.
61+
relblocknumber | | Page number in the relation.
6062
bytes | | Free bytes in the page, or NULL
6163
| | for an index page (see below).
6264

6365

6466
For pg_freespacemap_relations, there is one row for each relation in the free
65-
space map.
67+
space map. storedpages is the number of pages actually stored in the map,
68+
while lastpagecount is the number of pages VACUUM last tried to store
69+
(ie, the number that VACUUM thought had useful amounts of free space).
70+
71+
If storedpages is consistently less than lastpagecount then it'd be a good
72+
idea to increase max_fsm_pages. Also, if the number of rows in
73+
pg_freespacemap_relations is close to max_fsm_relations, then you should
74+
consider increasing max_fsm_relations.
6675

6776
For pg_freespacemap_pages, there is one row for each page in the free space
68-
map.
77+
map. The number of rows for a relation will match the storedpages column
78+
in pg_freespacemap_relations.
6979

70-
Because the map is shared by all the databases, there are relations and pages
71-
from relations not belonging to the current database.
80+
For indexes, what is tracked is entirely-unused pages, rather than free
81+
space within pages. Therefore, the average request size and free bytes
82+
within a page are not meaningful, and are shown as NULL.
7283

73-
The view 'freespacemap_pages' can contain pages for btree indexes if they
74-
were emptied by a vacuum process. The bytes field is set to NULL in this case.
84+
Because the map is shared by all the databases, it will include relations
85+
not belonging to the current database.
7586

7687
When either of the views are accessed, internal free space map locks are
7788
taken, and a copy of the map data is made for them to display.
@@ -85,44 +96,43 @@ Sample output - pg_freespacemap_relations
8596

8697
regression=# \d pg_freespacemap_relations
8798
View "public.pg_freespacemap_relations"
88-
Column | Type | Modifiers
99+
Column | Type | Modifiers
89100
---------------+---------+-----------
90-
reltablespace | oid |
91-
reldatabase | oid |
92-
relfilenode | oid |
93-
avgrequest | bigint |
94-
lastpagecount | integer |
95-
nextpage | integer |
101+
reltablespace | oid |
102+
reldatabase | oid |
103+
relfilenode | oid |
104+
avgrequest | integer |
105+
lastpagecount | integer |
106+
storedpages | integer |
107+
nextpage | integer |
96108
View definition:
97-
SELECT p.reltablespace, p.reldatabase, p.relfilenode, p.avgrequest, p.lastpagecount, p.nextpage
98-
FROM pg_freespacemap_relations() p(reltablespace oid, reldatabase oid, relfilenode oid, avgrequest bigint, lastpagecount integer, nextpage integer);
109+
SELECT p.reltablespace, p.reldatabase, p.relfilenode, p.avgrequest, p.lastpagecount, p.storedpages, p.nextpage
110+
FROM pg_freespacemap_relations() p(reltablespace oid, reldatabase oid, relfilenode oid, avgrequest integer, lastpagecount integer, storedpages integer, nextpage integer);
99111

100-
regression=# SELECT c.relname, r.avgrequest, r.lastpagecount, r.nextpage
112+
regression=# SELECT c.relname, r.avgrequest, r.lastpagecount, r.storedpages
101113
FROM pg_freespacemap_relations r INNER JOIN pg_class c
102114
ON c.relfilenode = r.relfilenode INNER JOIN pg_database d
103115
ON r.reldatabase = d.oid AND (d.datname = current_database())
104-
ORDER BY c.relname LIMIT 10;
105-
relname | avgrequest | lastpagecount | nextpage
106-
--------------+------------+---------------+----------
107-
a_star | 250 | 1 | 0
108-
abstime_tbl | 249 | 1 | 0
109-
aggtest | 250 | 1 | 0
110-
altinhoid | 250 | 1 | 0
111-
altstartwith | 250 | 1 | 0
112-
arrtest | 254 | 1 | 0
113-
b_star | 250 | 1 | 0
114-
box_tbl | 250 | 1 | 0
115-
bt_f8_heap | 92 | 1 | 0
116-
bt_i4_heap | 94 | 1 | 0
116+
ORDER BY r.storedpages DESC LIMIT 10;
117+
relname | avgrequest | lastpagecount | storedpages
118+
---------------------------------+------------+---------------+-------------
119+
onek | 256 | 109 | 109
120+
pg_attribute | 167 | 93 | 93
121+
pg_class | 191 | 49 | 49
122+
pg_attribute_relid_attnam_index | | 48 | 48
123+
onek2 | 256 | 37 | 37
124+
pg_depend | 95 | 26 | 26
125+
pg_type | 199 | 16 | 16
126+
pg_rewrite | 1011 | 13 | 13
127+
pg_class_relname_nsp_index | | 10 | 10
128+
pg_proc | 302 | 8 | 8
117129
(10 rows)
118130

119-
regression=#
120-
121131

122132
Sample output - pg_freespacemap_pages
123133
-------------
124134

125-
regression=# \d pg_freespacemap_pages;
135+
regression=# \d pg_freespacemap_pages
126136
View "public.pg_freespacemap_pages"
127137
Column | Type | Modifiers
128138
----------------+---------+-----------
@@ -154,8 +164,6 @@ regression=# SELECT c.relname, p.relblocknumber, p.bytes
154164
bt_i4_heap | 49 | 8008
155165
(10 rows)
156166

157-
regression=#
158-
159167

160168

161169
Author

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy