tips
24 tips Postgresql
One command, setting or habit at a time about Postgresql: what it does, the line to copy and a caveat or two.
Query tuning 8
-
A contrib module that logs the execution plan of any statement slower than a limit you set.
LOAD 'auto_explain'; -
Lists indexes with zero scans since the statistics were last reset, so you can review them for removal.
FROM pg_stat_user_indexes -
Prints the run time of every statement so you can compare a query before and after a change.
appdb=# \timing on -
Builds an index on a live table while inserts and updates keep running.
CREATE INDEX CONCURRENTLY orders_customer_idx ON orders (customer_id); -
Gives one login a maximum query time so its runaway queries stop on their own.
ALTER ROLE report SET statement_timeout = '30s'; -
Ranks normalized queries by their total run time so you tune the ones that matter.
shared_preload_libraries = 'pg_stat_statements' -
Lists waiting sessions together with the process IDs that hold the lock they need.
state, wait_event_type, left(query, 60) AS query -
Returns one row per group, such as the latest reading of each sensor, without a subquery.
SELECT DISTINCT ON (sensor_id) sensor_id, recorded_at, temperature
SQL shells 7
-
Repeats the last server error with its SQLSTATE code, details and source location.
appdb=# \errverbose -
Prints a marker for NULL values so you can tell them apart from empty text.
\pset null '(null)' -
Stores PostgreSQL passwords per host and user in a private file that all client tools read.
echo 'db1.example.com:5432:*:backup:change-me' >> ~/.pgpass -
Creates thousands of realistic test rows with one statement to try an index or a query.
CREATE TABLE readings (id int, sensor int, taken_at timestamptz, value numeric); -
Picks the gentle or the hard way to stop a runaway query from another session.
SELECT pid, now() - query_start AS running, left(query, 60) -
Edits a long query in your own editor and runs it when you save and quit.
appdb=# \e -
Shows the source of a PostgreSQL function with numbered lines, to match an error message.
\sf+ my_function
Backups and dumps 6
-
Leaves ownership and GRANT statements out of a plain SQL dump, so another user can load it.
pg_dump -O -x -f mydb.sql mydb -
Converts a binary pg_dump archive into a plain SQL script you can read, grep or edit.
pg_restore -f - -t users appdb.dump | less -
Writes table, index and function definitions without any rows, ready to compare or keep in git.
pg_dump -s -f prod-schema.sql -h db1.example.com appdb -
Restores a custom-format dump minus the items you commented out of its table of contents.
pg_restore -L appdb.list -d appdb_new appdb.dump -
Dumps only the roles and tablespaces of a PostgreSQL server, which per-database dumps do not contain.
pg_dumpall --globals-only -f globals.sql -
Restores a PostgreSQL archive dump using several concurrent jobs so large restores finish sooner.
pg_restore -j 4 -d appdb appdb.dump
Admin and checks 3
-
Shows which edited settings in the config file are not active yet because the server has to restart.
FROM pg_settings -
Lists the rules in pg_hba.conf as rows, with an error message for lines that cannot be parsed.
FROM pg_hba_file_rules -
Reports dead tuples and free space of a table, skipping pages that the visibility map marks clean.
CREATE EXTENSION pgstattuple;
Nothing in this filter yet.