F.60. pgstattuple — получение статистики на уровне кортежей #
Модуль pgstattuple предоставляет различные функции для получения статистики на уровне кортежей.
Так как эти функции возвращают подробную информацию, относящуюся к уровню страницы, доступ к ним по умолчанию ограничен. Право EXECUTE для них имеет только роль pg_stat_scan_tables. Разумеется, суперпользователи могут обойти это ограничение. После того как это расширение установлено, можно поменять права доступа к этим функциям командами GRANT и разрешить их выполнение другим пользователям. Однако предпочтительнее будет добавить этих пользователей в роль pg_stat_scan_tables.
F.60.1. Функции #
-
pgstattuple(regclass) returns record Функция
pgstattupleвозвращает физическую длину отношения, процент «мёртвых» кортежей и другую информацию. Она может быть полезна для принятия решения о необходимости очистки. В аргументе передаётся имя (возможно, дополненное схемой) или OID целевого отношения. Например:test=> SELECT * FROM pgstattuple('pg_catalog.pg_proc'); -[ RECORD 1 ]------+------- table_len | 458752 tuple_count | 1470 tuple_len | 438896 tuple_percent | 95.67 dead_tuple_count | 11 dead_tuple_len | 3157 dead_tuple_percent | 0.69 free_space | 8932 free_percent | 1.95Столбцы результата описаны в Таблице F.50.
Таблица F.50. Столбцы результата
pgstattupleСтолбец Тип Описание table_lenbigintФизическая длина отношения в байтах tuple_countbigintКоличество «живых» кортежей tuple_lenbigintОбщая длина «живых» кортежей в байтах tuple_percentfloat8Процент «живых» кортежей dead_tuple_countbigintКоличество «мёртвых» кортежей dead_tuple_lenbigintОбщая длина «мёртвых» кортежей в байтах dead_tuple_percentfloat8Процент «мёртвых» кортежей free_spacebigintОбщий объём свободного пространства в байтах free_percentfloat8Процент свободного пространства Примечание
Значение
table_lenвсегда будет больше суммыtuple_len,dead_tuple_lenиfree_space. Разница объясняется фиксированными издержками, внутристраничной таблицей указателей на кортежи и пропусками, добавляемыми для выравнивания кортежей.Функция
pgstattupleполучает блокировку отношения только для чтения. Таким образом, её результаты отражают не мгновенный снимок; на них будут влиять параллельные изменения.pgstattupleсчитает кортеж «мёртвым», еслиHeapTupleSatisfiesDirtyвозвращает false.-
pgstattuple(text) returns record Эта функция равнозначна функции
pgstattuple(regclass)за исключением того, что для неё целевое отношение задаётся в текстовом виде. Данная функция оставлена для обратной совместимости, в будущем она может перейти в разряд устаревших.-
pgstatindex(regclass) returns record Функция
pgstatindexвозвращает запись с информацией об индексе типа B-дерево. Например:test=> SELECT * FROM pgstatindex('pg_cast_oid_index'); -[ RECORD 1 ]------+------ version | 2 tree_level | 0 index_size | 16384 root_block_no | 1 internal_pages | 0 leaf_pages | 1 empty_pages | 0 deleted_pages | 0 avg_leaf_density | 54.27 leaf_fragmentation | 0Столбцы результата:
Столбец Тип Описание versionintegerНомер версии B-дерева tree_levelintegerУровень корневой страницы в дереве index_sizebigintОбщий объём индекса в байтах root_block_nobigintРасположение страницы корня (0, если её нет) internal_pagesbigintКоличество «внутренних» страниц (верхнего уровня) leaf_pagesbigintКоличество страниц на уровне листьев empty_pagesbigintКоличество пустых страниц deleted_pagesbigintКоличество удалённых страниц avg_leaf_densityfloat8Средняя плотность страниц на уровне листьев leaf_fragmentationfloat8Фрагментация на уровне листьев Выдаваемый размер индекса (
index_size) обычно вычисляется по формулеinternal_pages + leaf_pages + empty_pages + deleted_pagesплюс одна страница, так как в нём учитывается и метастраница индекса.Как и
pgstattuple, эта функция собирает данные страница за страницей и не следует ожидать, что её результат представляет мгновенный снимок всего индекса.-
pgstatindex(text) returns record Эта функция равнозначна функции
pgstatindex(regclass)за исключением того, что для неё целевое отношение задаётся в текстовом виде. Данная функция оставлена для обратной совместимости, в будущем она может перейти в разряд устаревших.-
pgstatginindex(regclass) returns record Функция
pgstatginindexвозвращает запись с информацией об индексе типа GIN. Например:test=> SELECT * FROM pgstatginindex('test_gin_index'); -[ RECORD 1 ]--+-- version | 1 pending_pages | 0 pending_tuples | 0Столбцы результата:
Столбец Тип Описание versionintegerНомер версии GIN pending_pagesintegerКоличество страниц в списке ожидающих обработки pending_tuplesbigintКоличество кортежей в списке ожидающих обработки -
pgstathashindex(regclass) returns record Функция
pgstathashindexвозвращает запись с информацией о хеш-индексе. Например:test=> select * from pgstathashindex('con_hash_index'); -[ RECORD 1 ]--+----------------- version | 4 bucket_pages | 33081 overflow_pages | 0 bitmap_pages | 1 unused_pages | 32455 live_items | 10204006 dead_items | 0 free_percent | 61.8005949100872Столбцы результата:
Столбец Тип Описание versionintegerНомер версии HASH bucket_pagesbigintКоличество страниц групп overflow_pagesbigintКоличество страниц переполнения bitmap_pagesbigintКоличество страниц битовой карты unused_pagesbigintКоличество неиспользованных страниц live_itemsbigintКоличество «живых» кортежей dead_tuplesbigintКоличество «мёртвых» кортежей free_percentfloatПроцент свободного пространства -
pg_relpages(regclass) returns bigint Функция
pg_relpagesвозвращает число страниц в отношении.-
pg_relpages(text) returns bigint Эта функция равнозначна функции
pg_relpages(regclass)за исключением того, что для неё целевое отношение задаётся в текстовом виде. Данная функция оставлена для обратной совместимости, в будущем она может перейти в разряд устаревших.-
pgstattuple_approx(regclass) returns record Функция
pgstattuple_approxявляется более быстрой альтернативойpgstattuple, возвращающей приблизительные результаты. В качестве аргумента ей передаётся имя или OID целевого отношения. Например:test=> SELECT * FROM pgstattuple_approx('pg_catalog.pg_proc'::regclass); -[ RECORD 1 ]--------+------- table_len | 573440 scanned_percent | 2 approx_tuple_count | 2740 approx_tuple_len | 561210 approx_tuple_percent | 97.87 dead_tuple_count | 0 dead_tuple_len | 0 dead_tuple_percent | 0 approx_free_space | 11996 approx_free_percent | 2.09Выходные столбцы описаны в Таблице F.51.
Тогда как
pgstattupleвсегда производит полное сканирование таблицы и возвращает точное число живых и мёртвых кортежей (и их размер), а также точный объём свободного пространства, функцияpgstattuple_approxпытается избежать полного сканирования и возвращает точную статистику только по мёртвым кортежам, а количество и объём живых кортежей, как и объём свободного пространства определяет приблизительно.Она делает это, пропуская страницы, в которых, согласно карте видимости, есть только видимые кортежи (если для страницы установлен соответствующий бит, предполагается, что она не содержит мёртвых кортежей). Для таких страниц эта функция узнаёт объём свободного пространства из карты свободного пространства и предполагает, что остальное пространство на странице занято живыми кортежами.
На страницах, которые нельзя пропустить, она сканирует каждый кортеж, отражает его наличие и размер в соответствующих счётчиках и суммирует свободное пространство на странице. В конце она оценивает приблизительно общее число живых кортежей, исходя из числа просканированных страниц и кортежей (так же, как VACUUM рассчитывает значение pg_class.reltuples).
Таблица F.51. Столбцы результата
pgstattuple_approxСтолбец Тип Описание table_lenbigintФизическая длина отношения в байтах (точная) scanned_percentfloat8Просканированный процент таблицы approx_tuple_countbigintКоличество «живых» кортежей (приблизительное) approx_tuple_lenbigintОбщая длина «живых» кортежей в байтах (приблизительная) approx_tuple_percentfloat8Процент «живых» кортежей dead_tuple_countbigintКоличество «мёртвых» кортежей (точное) dead_tuple_lenbigintОбщая длина «мёртвых» кортежей в байтах (точная) dead_tuple_percentfloat8Процент «мёртвых» кортежей approx_free_spacebigintОбщий объём свободного пространства в байтах (приблизительный) approx_free_percentfloat8Процент свободного пространства В показанном выше выводе показатели свободного пространства могут не соответствовать выводу
pgstattupleв точности, потому что карта свободного пространства показывает верное значение, но не гарантируется, что оно будет точным до байта.
F.60.2. Авторы #
Тацуо Исии, Сатоши Нагаясу и Абхиджит Менон-Сен
F.60. pgstattuple — obtain tuple-level statistics #
The pgstattuple module provides various functions to obtain tuple-level statistics.
Because these functions return detailed page-level information, access is restricted by default. By default, only the role pg_stat_scan_tables has EXECUTE privilege. Superusers of course bypass this restriction. After the extension has been installed, users may issue GRANT commands to change the privileges on the functions to allow others to execute them. However, it might be preferable to add those users to the pg_stat_scan_tables role instead.
F.60.1. Functions #
-
pgstattuple(regclass) returns record pgstattuplereturns 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:test=> SELECT * FROM pgstattuple('pg_catalog.pg_proc'); -[ RECORD 1 ]------+------- table_len | 458752 tuple_count | 1470 tuple_len | 438896 tuple_percent | 95.67 dead_tuple_count | 11 dead_tuple_len | 3157 dead_tuple_percent | 0.69 free_space | 8932 free_percent | 1.95The output columns are described in Table F.50.
Table F.50.
pgstattupleOutput ColumnsColumn Type Description table_lenbigintPhysical relation length in bytes tuple_countbigintNumber of live tuples tuple_lenbigintTotal length of live tuples in bytes tuple_percentfloat8Percentage of live tuples dead_tuple_countbigintNumber of dead tuples dead_tuple_lenbigintTotal length of dead tuples in bytes dead_tuple_percentfloat8Percentage of dead tuples free_spacebigintTotal free space in bytes free_percentfloat8Percentage of free space Note
The
table_lenwill always be greater than the sum of thetuple_len,dead_tuple_lenandfree_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.pgstattupleacquires only a read lock on the relation. So the results do not reflect an instantaneous snapshot; concurrent updates will affect them.pgstattuplejudges a tuple is “dead” ifHeapTupleSatisfiesDirtyreturns 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 pgstatindexreturns a record showing information about a B-tree index. For example:test=> SELECT * FROM pgstatindex('pg_cast_oid_index'); -[ RECORD 1 ]------+------ version | 2 tree_level | 0 index_size | 16384 root_block_no | 1 internal_pages | 0 leaf_pages | 1 empty_pages | 0 deleted_pages | 0 avg_leaf_density | 54.27 leaf_fragmentation | 0The output columns are:
Column Type Description versionintegerB-tree version number tree_levelintegerTree level of the root page index_sizebigintTotal index size in bytes root_block_nobigintLocation of root page (zero if none) internal_pagesbigintNumber of “internal” (upper-level) pages leaf_pagesbigintNumber of leaf pages empty_pagesbigintNumber of empty pages deleted_pagesbigintNumber of deleted pages avg_leaf_densityfloat8Average density of leaf pages leaf_fragmentationfloat8Leaf page fragmentation The reported
index_sizewill normally correspond to one more page than is accounted for byinternal_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 pgstatginindexreturns a record showing information about a GIN index. For example:test=> SELECT * FROM pgstatginindex('test_gin_index'); -[ RECORD 1 ]--+-- version | 1 pending_pages | 0 pending_tuples | 0The output columns are:
Column Type Description versionintegerGIN version number pending_pagesintegerNumber of pages in the pending list pending_tuplesbigintNumber of tuples in the pending list -
pgstathashindex(regclass) returns record pgstathashindexreturns a record showing information about a HASH index. For example:test=> select * from pgstathashindex('con_hash_index'); -[ RECORD 1 ]--+----------------- version | 4 bucket_pages | 33081 overflow_pages | 0 bitmap_pages | 1 unused_pages | 32455 live_items | 10204006 dead_items | 0 free_percent | 61.8005949100872The output columns are:
Column Type Description versionintegerHASH version number bucket_pagesbigintNumber of bucket pages overflow_pagesbigintNumber of overflow pages bitmap_pagesbigintNumber of bitmap pages unused_pagesbigintNumber of unused pages live_itemsbigintNumber of live tuples dead_tuplesbigintNumber of dead tuples free_percentfloatPercentage of free space -
pg_relpages(regclass) returns bigint pg_relpagesreturns 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_approxis a faster alternative topgstattuplethat returns approximate results. The argument is the target relation's name or OID. For example:test=> SELECT * FROM pgstattuple_approx('pg_catalog.pg_proc'::regclass); -[ RECORD 1 ]--------+------- table_len | 573440 scanned_percent | 2 approx_tuple_count | 2740 approx_tuple_len | 561210 approx_tuple_percent | 97.87 dead_tuple_count | 0 dead_tuple_len | 0 dead_tuple_percent | 0 approx_free_space | 11996 approx_free_percent | 2.09The output columns are described in Table F.51.
Whereas
pgstattuplealways performs a full-table scan and returns an exact count of live and dead tuples (and their sizes) and free space,pgstattuple_approxtries 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.51.
pgstattuple_approxOutput ColumnsColumn Type Description table_lenbigintPhysical relation length in bytes (exact) scanned_percentfloat8Percentage of table scanned approx_tuple_countbigintNumber of live tuples (estimated) approx_tuple_lenbigintTotal length of live tuples in bytes (estimated) approx_tuple_percentfloat8Percentage of live tuples dead_tuple_countbigintNumber of dead tuples (exact) dead_tuple_lenbigintTotal length of dead tuples in bytes (exact) dead_tuple_percentfloat8Percentage of dead tuples approx_free_spacebigintTotal free space in bytes (estimated) approx_free_percentfloat8Percentage of free space In the above output, the free space figures may not match the
pgstattupleoutput exactly, because the free space map gives us an exact figure, but is not guaranteed to be accurate to the byte.
F.60.2. Authors #
Tatsuo Ishii, Satoshi Nagayasu and Abhijit Menon-Sen