F.30. pgstattuple
pgstattuple 模組提供各種函數來取得 tuple 層級的統計資訊。
由於這些函數會回傳詳細的 page-level 資訊,因此預設是限制存取的。 預設情況下,只有角色 pg_stat_scan_tables 具有 EXECUTE 權限。超級使用者當然可以繞過此限制。安裝此延伸功能後,使用者可以發出 GRANT 指令來授予函數的權限,以允許其他人執行它們。但是,最好還是將這些使用者加到 pg_stat_scan_tables 角色群組之中。
F.30.1. Functions
pgstattuple(regclass) returns record
pgstattuple
returns a relation's physical length, percentage of “dead” tuples, and other info. This may help users to determine whether vacuum is necessary or not. The argument is the target relation's name (optionally schema-qualified) or OID. For example:
The output columns are described in Table F.22.
Table F.22. pgstattuple
Output Columns
pgstattuple
Output ColumnsColumn | Type | Description |
---|---|---|
|
| Physical relation length in bytes |
|
| Number of live tuples |
|
| Total length of live tuples in bytes |
|
| Percentage of live tuples |
|
| Number of dead tuples |
|
| Total length of dead tuples in bytes |
|
| Percentage of dead tuples |
|
| Total free space in bytes |
|
| Percentage of free space |
Note
The table_len
will always be greater than the sum of the tuple_len
, dead_tuple_len
and free_space
. The difference is accounted for by fixed page overhead, the per-page table of pointers to tuples, and padding to ensure that tuples are correctly aligned.
pgstattuple
acquires only a read lock on the relation. So the results do not reflect an instantaneous snapshot; concurrent updates will affect them.
pgstattuple
judges a tuple is “dead” if HeapTupleSatisfiesDirty
returns false.pgstattuple(text) returns record
This is the same as pgstattuple(regclass)
, except that the target relation is specified as TEXT. This function is kept because of backward-compatibility so far, and will be deprecated in some future release.pgstatindex(regclass) returns record
pgstatindex
returns a record showing information about a B-tree index. For example:
The output columns are:
Column | Type | Description |
---|---|---|
|
| B-tree version number |
|
| Tree level of the root page |
|
| Total index size in bytes |
|
| Location of root page (zero if none) |
|
| Number of “internal” (upper-level) pages |
|
| Number of leaf pages |
|
| Number of empty pages |
|
| Number of deleted pages |
|
| Average density of leaf pages |
|
| Leaf page fragmentation |
The reported index_size
will normally correspond to one more page than is accounted for by internal_pages + leaf_pages + empty_pages + deleted_pages
, because it also includes the index's metapage.
As with pgstattuple
, the results are accumulated page-by-page, and should not be expected to represent an instantaneous snapshot of the whole index.pgstatindex(text) returns record
This is the same as pgstatindex(regclass)
, except that the target index is specified as TEXT. This function is kept because of backward-compatibility so far, and will be deprecated in some future release.pgstatginindex(regclass) returns record
pgstatginindex
returns a record showing information about a GIN index. For example:
The output columns are:
Column | Type | Description |
---|---|---|
|
| GIN version number |
|
| Number of pages in the pending list |
|
| Number of tuples in the pending list |
pgstathashindex(regclass) returns record
pgstathashindex
returns a record showing information about a HASH index. For example:
The output columns are:
Column | Type | Description |
---|---|---|
|
| HASH version number |
|
| Number of bucket pages |
|
| Number of overflow pages |
|
| Number of bitmap pages |
|
| Number of unused pages |
|
| Number of live tuples |
|
| Number of dead tuples |
|
| Percentage of free space |
pg_relpages(regclass) returns bigint
pg_relpages
returns the number of pages in the relation.pg_relpages(text) returns bigint
This is the same as pg_relpages(regclass)
, except that the target relation is specified as TEXT. This function is kept because of backward-compatibility so far, and will be deprecated in some future release.pgstattuple_approx(regclass) returns record
pgstattuple_approx
is a faster alternative to pgstattuple
that returns approximate results. The argument is the target relation's name or OID. For example:
The output columns are described in Table F.23.
Whereas pgstattuple
always performs a full-table scan and returns an exact count of live and dead tuples (and their sizes) and free space, pgstattuple_approx
tries to avoid the full-table scan and returns exact dead tuple statistics along with an approximation of the number and size of live tuples and free space.
It does this by skipping pages that have only visible tuples according to the visibility map (if a page has the corresponding VM bit set, then it is assumed to contain no dead tuples). For such pages, it derives the free space value from the free space map, and assumes that the rest of the space on the page is taken up by live tuples.
For pages that cannot be skipped, it scans each tuple, recording its presence and size in the appropriate counters, and adding up the free space on the page. At the end, it estimates the total number of live tuples based on the number of pages and tuples scanned (in the same way that VACUUM estimates pg_class.reltuples).
Table F.23. pgstattuple_approx
Output Columns
pgstattuple_approx
Output ColumnsColumn | Type | Description |
---|---|---|
|
| Physical relation length in bytes (exact) |
|
| Percentage of table scanned |
|
| Number of live tuples (estimated) |
|
| Total length of live tuples in bytes (estimated) |
|
| Percentage of live tuples |
|
| Number of dead tuples (exact) |
|
| Total length of dead tuples in bytes (exact) |
|
| Percentage of dead tuples |
|
| Total free space in bytes (estimated) |
|
| Percentage of free space |
In the above output, the free space figures may not match the pgstattuple
output exactly, because the free space map gives us an exact figure, but is not guaranteed to be accurate to the byte.
F.30.2. Authors
Tatsuo Ishii, Satoshi Nagayasu and Abhijit Menon-Sen