use mktemp instead of tempfile, as mktemp not available on default centos.

Also move pg_stat test to top so can fail fast.