Viewing and estimating with statistics
The statistics information collected for each table is easiest to see using the pg_stats
view. The amount of information it returns is a bit overwhelming though, and not well suited to the standard display format. The following script is named table-stats.sh
in the book's file set, and it tries to display the statistics information for a particular table (with an optional database too) in a way that makes the information as readable as possible:
#!/bin/bash if [ -z "$1" ]; then echo "Usage: table-stats.sh table [db]" exit 1 fi TABLENAME="$1" if [ -n "$2" ] ; then DB="-d $2" fi PSQL="psql $DB -x -c " $PSQL " SELECT tablename,attname,null_frac,avg_width,n_distinct,correlation, most_common_vals,most_common_freqs,histogram_bounds FROM pg_stats WHERE tablename='$TABLENAME'; " | grep -v "\-\[ RECORD "
A typical usage would be:
./table-stats.sh...