1 <!-- doc/src/sgml/pgstattuple.sgml -->
3 <sect1 id=
"pgstattuple" xreflabel=
"pgstattuple">
4 <title>pgstattuple
— obtain tuple-level statistics
</title>
6 <indexterm zone=
"pgstattuple">
7 <primary>pgstattuple
</primary>
11 The
<filename>pgstattuple
</filename> module provides various functions to
12 obtain tuple-level statistics.
16 Because these functions return detailed page-level information, access is
17 restricted by default. By default, only the
18 role
<literal>pg_stat_scan_tables
</literal> has
<literal>EXECUTE
</literal>
19 privilege. Superusers of course bypass this restriction. After the
20 extension has been installed, users may issue
<command>GRANT
</command>
21 commands to change the privileges on the functions to allow others to
22 execute them. However, it might be preferable to add those users to
23 the
<literal>pg_stat_scan_tables
</literal> role instead.
26 <sect2 id=
"pgstattuple-funcs">
27 <title>Functions
</title>
33 <primary>pgstattuple
</primary>
35 <function>pgstattuple(regclass) returns record
</function>
40 <function>pgstattuple
</function> returns a relation's physical length,
41 percentage of
<quote>dead
</quote> tuples, and other info. This may help users
42 to determine whether vacuum is necessary or not. The argument is the
43 target relation's name (optionally schema-qualified) or OID.
46 test=
> SELECT * FROM pgstattuple('pg_catalog.pg_proc');
47 -[ RECORD
1 ]------+-------
54 dead_tuple_percent |
0.69
58 The output columns are described in
<xref linkend=
"pgstattuple-columns"/>.
61 <table id=
"pgstattuple-columns">
62 <title><function>pgstattuple
</function> Output Columns
</title>
68 <entry>Description
</entry>
74 <entry><structfield>table_len
</structfield></entry>
75 <entry><type>bigint
</type></entry>
76 <entry>Physical relation length in bytes
</entry>
79 <entry><structfield>tuple_count
</structfield></entry>
80 <entry><type>bigint
</type></entry>
81 <entry>Number of live tuples
</entry>
84 <entry><structfield>tuple_len
</structfield></entry>
85 <entry><type>bigint
</type></entry>
86 <entry>Total length of live tuples in bytes
</entry>
89 <entry><structfield>tuple_percent
</structfield></entry>
90 <entry><type>float8
</type></entry>
91 <entry>Percentage of live tuples
</entry>
94 <entry><structfield>dead_tuple_count
</structfield></entry>
95 <entry><type>bigint
</type></entry>
96 <entry>Number of dead tuples
</entry>
99 <entry><structfield>dead_tuple_len
</structfield></entry>
100 <entry><type>bigint
</type></entry>
101 <entry>Total length of dead tuples in bytes
</entry>
104 <entry><structfield>dead_tuple_percent
</structfield></entry>
105 <entry><type>float8
</type></entry>
106 <entry>Percentage of dead tuples
</entry>
109 <entry><structfield>free_space
</structfield></entry>
110 <entry><type>bigint
</type></entry>
111 <entry>Total free space in bytes
</entry>
114 <entry><structfield>free_percent
</structfield></entry>
115 <entry><type>float8
</type></entry>
116 <entry>Percentage of free space
</entry>
125 The
<literal>table_len
</literal> will always be greater than the sum
126 of the
<literal>tuple_len
</literal>,
<literal>dead_tuple_len
</literal>
127 and
<literal>free_space
</literal>. The difference is accounted for by
128 fixed page overhead, the per-page table of pointers to tuples, and
129 padding to ensure that tuples are correctly aligned.
134 <function>pgstattuple
</function> acquires only a read lock on the
135 relation. So the results do not reflect an instantaneous snapshot;
136 concurrent updates will affect them.
140 <function>pgstattuple
</function> judges a tuple is
<quote>dead
</quote> if
141 <function>HeapTupleSatisfiesDirty
</function> returns false.
148 <function>pgstattuple(text) returns record
</function>
153 This is the same as
<function>pgstattuple(regclass)
</function>, except
154 that the target relation is specified as TEXT. This function is kept
155 because of backward-compatibility so far, and will be deprecated in
164 <primary>pgstatindex
</primary>
166 <function>pgstatindex(regclass) returns record
</function>
171 <function>pgstatindex
</function> returns a record showing information
172 about a B-tree index. For example:
174 test=
> SELECT * FROM pgstatindex('pg_cast_oid_index');
175 -[ RECORD
1 ]------+------
184 avg_leaf_density |
54.27
185 leaf_fragmentation |
0
190 The output columns are:
196 <entry>Column
</entry>
198 <entry>Description
</entry>
204 <entry><structfield>version
</structfield></entry>
205 <entry><type>integer
</type></entry>
206 <entry>B-tree version number
</entry>
210 <entry><structfield>tree_level
</structfield></entry>
211 <entry><type>integer
</type></entry>
212 <entry>Tree level of the root page
</entry>
216 <entry><structfield>index_size
</structfield></entry>
217 <entry><type>bigint
</type></entry>
218 <entry>Total index size in bytes
</entry>
222 <entry><structfield>root_block_no
</structfield></entry>
223 <entry><type>bigint
</type></entry>
224 <entry>Location of root page (zero if none)
</entry>
228 <entry><structfield>internal_pages
</structfield></entry>
229 <entry><type>bigint
</type></entry>
230 <entry>Number of
<quote>internal
</quote> (upper-level) pages
</entry>
234 <entry><structfield>leaf_pages
</structfield></entry>
235 <entry><type>bigint
</type></entry>
236 <entry>Number of leaf pages
</entry>
240 <entry><structfield>empty_pages
</structfield></entry>
241 <entry><type>bigint
</type></entry>
242 <entry>Number of empty pages
</entry>
246 <entry><structfield>deleted_pages
</structfield></entry>
247 <entry><type>bigint
</type></entry>
248 <entry>Number of deleted pages
</entry>
252 <entry><structfield>avg_leaf_density
</structfield></entry>
253 <entry><type>float8
</type></entry>
254 <entry>Average density of leaf pages
</entry>
258 <entry><structfield>leaf_fragmentation
</structfield></entry>
259 <entry><type>float8
</type></entry>
260 <entry>Leaf page fragmentation
</entry>
269 The reported
<literal>index_size
</literal> will normally correspond to one more
270 page than is accounted for by
<literal>internal_pages + leaf_pages +
271 empty_pages + deleted_pages
</literal>, because it also includes the
276 As with
<function>pgstattuple
</function>, the results are accumulated
277 page-by-page, and should not be expected to represent an
278 instantaneous snapshot of the whole index.
285 <function>pgstatindex(text) returns record
</function>
290 This is the same as
<function>pgstatindex(regclass)
</function>, except
291 that the target index is specified as TEXT. This function is kept
292 because of backward-compatibility so far, and will be deprecated in
301 <primary>pgstatginindex
</primary>
303 <function>pgstatginindex(regclass) returns record
</function>
308 <function>pgstatginindex
</function> returns a record showing information
309 about a GIN index. For example:
311 test=
> SELECT * FROM pgstatginindex('test_gin_index');
320 The output columns are:
326 <entry>Column
</entry>
328 <entry>Description
</entry>
334 <entry><structfield>version
</structfield></entry>
335 <entry><type>integer
</type></entry>
336 <entry>GIN version number
</entry>
340 <entry><structfield>pending_pages
</structfield></entry>
341 <entry><type>integer
</type></entry>
342 <entry>Number of pages in the pending list
</entry>
346 <entry><structfield>pending_tuples
</structfield></entry>
347 <entry><type>bigint
</type></entry>
348 <entry>Number of tuples in the pending list
</entry>
361 <primary>pgstathashindex
</primary>
363 <function>pgstathashindex(regclass) returns record
</function>
368 <function>pgstathashindex
</function> returns a record showing information
369 about a HASH index. For example:
371 test=
> select * from pgstathashindex('con_hash_index');
372 -[ RECORD
1 ]--+-----------------
378 live_items |
10204006
380 free_percent |
61.8005949100872
385 The output columns are:
391 <entry>Column
</entry>
393 <entry>Description
</entry>
399 <entry><structfield>version
</structfield></entry>
400 <entry><type>integer
</type></entry>
401 <entry>HASH version number
</entry>
405 <entry><structfield>bucket_pages
</structfield></entry>
406 <entry><type>bigint
</type></entry>
407 <entry>Number of bucket pages
</entry>
411 <entry><structfield>overflow_pages
</structfield></entry>
412 <entry><type>bigint
</type></entry>
413 <entry>Number of overflow pages
</entry>
417 <entry><structfield>bitmap_pages
</structfield></entry>
418 <entry><type>bigint
</type></entry>
419 <entry>Number of bitmap pages
</entry>
423 <entry><structfield>unused_pages
</structfield></entry>
424 <entry><type>bigint
</type></entry>
425 <entry>Number of unused pages
</entry>
429 <entry><structfield>live_items
</structfield></entry>
430 <entry><type>bigint
</type></entry>
431 <entry>Number of live tuples
</entry>
435 <entry><structfield>dead_tuples
</structfield></entry>
436 <entry><type>bigint
</type></entry>
437 <entry>Number of dead tuples
</entry>
441 <entry><structfield>free_percent
</structfield></entry>
442 <entry><type>float
</type></entry>
443 <entry>Percentage of free space
</entry>
456 <primary>pg_relpages
</primary>
458 <function>pg_relpages(regclass) returns bigint
</function>
463 <function>pg_relpages
</function> returns the number of pages in the
471 <function>pg_relpages(text) returns bigint
</function>
476 This is the same as
<function>pg_relpages(regclass)
</function>, except
477 that the target relation is specified as TEXT. This function is kept
478 because of backward-compatibility so far, and will be deprecated in
487 <primary>pgstattuple_approx
</primary>
489 <function>pgstattuple_approx(regclass) returns record
</function>
494 <function>pgstattuple_approx
</function> is a faster alternative to
495 <function>pgstattuple
</function> that returns approximate results.
496 The argument is the target relation's name or OID.
499 test=
> SELECT * FROM pgstattuple_approx('pg_catalog.pg_proc'::regclass);
500 -[ RECORD
1 ]--------+-------
503 approx_tuple_count |
2740
504 approx_tuple_len |
561210
505 approx_tuple_percent |
97.87
508 dead_tuple_percent |
0
509 approx_free_space |
11996
510 approx_free_percent |
2.09
512 The output columns are described in
<xref linkend=
"pgstatapprox-columns"/>.
516 Whereas
<function>pgstattuple
</function> always performs a
517 full-table scan and returns an exact count of live and dead tuples
518 (and their sizes) and free space,
<function>pgstattuple_approx
</function>
519 tries to avoid the full-table scan and returns exact dead tuple
520 statistics along with an approximation of the number and
521 size of live tuples and free space.
525 It does this by skipping pages that have only visible tuples
526 according to the visibility map (if a page has the corresponding VM
527 bit set, then it is assumed to contain no dead tuples). For such
528 pages, it derives the free space value from the free space map, and
529 assumes that the rest of the space on the page is taken up by live
534 For pages that cannot be skipped, it scans each tuple, recording its
535 presence and size in the appropriate counters, and adding up the
536 free space on the page. At the end, it estimates the total number of
537 live tuples based on the number of pages and tuples scanned (in the
538 same way that VACUUM estimates pg_class.reltuples).
541 <table id=
"pgstatapprox-columns">
542 <title><function>pgstattuple_approx
</function> Output Columns
</title>
546 <entry>Column
</entry>
548 <entry>Description
</entry>
554 <entry><structfield>table_len
</structfield></entry>
555 <entry><type>bigint
</type></entry>
556 <entry>Physical relation length in bytes (exact)
</entry>
559 <entry><structfield>scanned_percent
</structfield></entry>
560 <entry><type>float8
</type></entry>
561 <entry>Percentage of table scanned
</entry>
564 <entry><structfield>approx_tuple_count
</structfield></entry>
565 <entry><type>bigint
</type></entry>
566 <entry>Number of live tuples (estimated)
</entry>
569 <entry><structfield>approx_tuple_len
</structfield></entry>
570 <entry><type>bigint
</type></entry>
571 <entry>Total length of live tuples in bytes (estimated)
</entry>
574 <entry><structfield>approx_tuple_percent
</structfield></entry>
575 <entry><type>float8
</type></entry>
576 <entry>Percentage of live tuples
</entry>
579 <entry><structfield>dead_tuple_count
</structfield></entry>
580 <entry><type>bigint
</type></entry>
581 <entry>Number of dead tuples (exact)
</entry>
584 <entry><structfield>dead_tuple_len
</structfield></entry>
585 <entry><type>bigint
</type></entry>
586 <entry>Total length of dead tuples in bytes (exact)
</entry>
589 <entry><structfield>dead_tuple_percent
</structfield></entry>
590 <entry><type>float8
</type></entry>
591 <entry>Percentage of dead tuples
</entry>
594 <entry><structfield>approx_free_space
</structfield></entry>
595 <entry><type>bigint
</type></entry>
596 <entry>Total free space in bytes (estimated)
</entry>
599 <entry><structfield>approx_free_percent
</structfield></entry>
600 <entry><type>float8
</type></entry>
601 <entry>Percentage of free space
</entry>
609 In the above output, the free space figures may not match the
610 <function>pgstattuple
</function> output exactly, because the free
611 space map gives us an exact figure, but is not guaranteed to be
612 accurate to the byte.
621 <sect2 id=
"pgstattuple-authors">
622 <title>Authors
</title>
625 Tatsuo Ishii, Satoshi Nagayasu and Abhijit Menon-Sen