patternsqlMajor
What is the meaning of n_live_tup and n_dead_tup in pg_stat_user_tables
Viewed 0 times
meaningthewhatn_dead_tupandn_live_tuppg_stat_user_tables
Problem
What is the meaning of
n_live_tup and n_dead_tup in pg_stat_user_tables or pgstattuple?Solution
Those two columns are the result of
Representing the number of live and dead rows (tuples) in the table.
Find those functions in the manual.
Dead rows are deleted rows that will later be reused for new rows from
Dead rows can be removed by
Whenever a row is deleted or updated, the old row version becomes invisible to all other transactions starting after the transaction has been committed - with default
Those are just statistics. You need to enable statistics collection in
More about the statistics collector in the manual here.
SELECT pg_stat_get_live_tuples(c.oid) AS n_live_tup
, pg_stat_get_dead_tuples(c.oid) AS n_dead_tup
FROM pg_class c;Representing the number of live and dead rows (tuples) in the table.
Find those functions in the manual.
Dead rows are deleted rows that will later be reused for new rows from
INSERTs or UPDATEs (the space, not the data). Some dead rows (or reserved free space) can be particularly useful for HOT updates (Heap-Only Tuples) that can reuse space in the same data page efficiently. More on H.O.T.:- PostgreSQL Initial Database Size
- Release notes for PostgreSQL 8.3 where this feature was introduced.
Dead rows can be removed by
VACUUM FULL (or plain VACUUM if it gets lucky) or similar operations on the table, thereby shrinking the physical size accordingly.Whenever a row is deleted or updated, the old row version becomes invisible to all other transactions starting after the transaction has been committed - with default
READ COMMITTED transaction isolation. The row is completely dead as soon as there are no more uncommitted older transactions. That is necessary for PostgreSQL's MVCC model to handle concurrency.Those are just statistics. You need to enable statistics collection in
postgresql.conf if you want them to be updated automatically. track_counts should be on by default, though. Bear in mind that statistics are not updated instantaneously.More about the statistics collector in the manual here.
Code Snippets
SELECT pg_stat_get_live_tuples(c.oid) AS n_live_tup
, pg_stat_get_dead_tuples(c.oid) AS n_dead_tup
FROM pg_class c;Context
StackExchange Database Administrators Q#14801, answer score: 42
Revisions (0)
No revisions yet.