Skip to content

Commit 7d06674

Browse files
authored
Merge pull request #177 from natefoo/query-live-tuples
Add a query for `n_live_tup` from pg_stat_user_tables
2 parents 33e6adb + 9039a00 commit 7d06674

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

parts/22-query.sh

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5770,3 +5770,59 @@ query_user-info() { ## <-|user [user [...]]> : Retrieve information about users
57705770
(galaxy_user.email in ('$users_string') or galaxy_user.username in ('$users_string') or galaxy_user.id::text in ('$users_string'))
57715771
EOF
57725772
}
5773+
5774+
query_live-tuples() { ##? [--no-analyzed-only] [table]: Estimate table row counts using the n_live_tup stat
5775+
meta <<-EOF
5776+
AUTHORS: natefoo
5777+
ADDED: 23
5778+
EOF
5779+
handle_help "$@" <<-EOF
5780+
Query the 'n_live_tup' value from pg_stat_user_tables as an estimate of total row count in the table.
5781+
5782+
NOTE: This will not be accurate if the table has not been analyzed since the last time PostgreSQL was
5783+
started. The 'gxadmin mutate analyze-unanalyzed-tables' function can be run to ensure all tables are
5784+
analyzed.
5785+
5786+
$gxadmin query live-tuples | head
5787+
relname | n_live_tup
5788+
----------------------------------------------------------------+------------
5789+
alembic_version | 1
5790+
api_keys | 4242
5791+
celery_user_rate_limit | 0
5792+
chat_exchange | 128
5793+
chat_exchange_message | 132
5794+
cleanup_event | 100
5795+
cleanup_event_dataset_association | 1251
5796+
cleanup_event_hda_association | 2898
5797+
5798+
By default, only analyzed tables will be included in the output. All tables can be included using
5799+
'--no-analyzed-only'. The value for a single table can be obtained with the optional 'table' argument.
5800+
EOF
5801+
5802+
fields="count=1"
5803+
tags="table_name=0"
5804+
5805+
analyze_null_string="AND (last_analyze IS NOT NULL OR last_autoanalyze IS NOT NULL)"
5806+
if [[ -n $arg_no_analyzed_only ]]; then
5807+
analyze_null_string=
5808+
fi
5809+
5810+
table_clause=
5811+
if [[ -n $arg_table ]]; then
5812+
table_clause="AND relname = '$arg_table'"
5813+
fi
5814+
5815+
read -r -d '' QUERY <<-EOF
5816+
SELECT
5817+
relname,
5818+
n_live_tup
5819+
FROM
5820+
pg_stat_user_tables
5821+
WHERE
5822+
schemaname = 'public'
5823+
$analyze_null_string
5824+
$table_clause
5825+
ORDER BY
5826+
relname
5827+
EOF
5828+
}

0 commit comments

Comments
 (0)