diff --git a/README.md b/README.md index 5093e28..0a7d0bf 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Questions? Ideas? Contact me: nik@postgres.ai, Nikolay Samokhvalov. ## Credits -**postgres_dba** is based on useful queries created and improved by many developers. Here is incomplete list of them: +**postgres_dba** is based on useful queries created and improved by many developers. Here is an incomplete list of them: * Jehan-Guillaume (ioguix) de Rorthais https://github.com/ioguix/pgsql-bloat-estimation * Alexey Lesovsky, Alexey Ermakov, Maxim Boguk, Ilya Kosmodemiansky et al. https://github.com/dataegret/pg-utils * Josh Berkus, Quinn Weaver et al. from PostgreSQL Experts, Inc. https://github.com/pgexperts/pgx_scripts @@ -104,9 +104,7 @@ If you are running psql and Postgres server on the same machine, just launch psq psql -U ``` -And type `:dba ` in psql. (Or `\i /path/to/postgres_dba/start.psql` if you haven't added shortcut to your `~/.psqlrc` file). - -– it will open interactive menu. +And type `:dba ` in psql. (Or `\i /path/to/postgres_dba/start.psql` if you haven't added shortcut to your `~/.psqlrc` file). This will open an interactive menu. ### Connect to Remote Postgres Server What to do if you need to connect to a remote Postgres server? Usually, Postgres is behind a firewall and/or doesn't listen to a public network interface. So you need to be able to connect to the server using SSH. If you can do it, then just create SSH tunnel (assuming that Postgres listens to default port 5432 on that server: diff --git a/init/generate.sh b/init/generate.sh index 91d7526..219be2e 100755 --- a/init/generate.sh +++ b/init/generate.sh @@ -84,5 +84,5 @@ echo " \\ir ./$OUT" >> "$OUT" echo "\\endif" >> "$OUT" echo "Done." -cd ->/dev/null +cd - >/dev/null exit 0 diff --git a/matviews/refresh_all.sql b/matviews/refresh_all.sql index 3579e13..d56536c 100644 --- a/matviews/refresh_all.sql +++ b/matviews/refresh_all.sql @@ -1,7 +1,7 @@ -- Use this to do the very 1st REFRESH for your matviews -- In case when there are complex relations between matviews, -- it might perform multiple iterations and eventually refreshes --- all matviews (either all w/o data or absolutely all -- it's up to you). +-- all matviews (either all without data or absolutely all -- it's up to you). -- set this to TRUE here if you need ALL matviews to be refreshed, not only those that already have been refreshed set postgres_dba.refresh_matviews_with_data = FALSE; @@ -22,7 +22,7 @@ begin set postgres_dba.refresh_matviews_with_data = true; end if; if current_setting('postgres_dba.refresh_matviews_with_data')::boolean then - raise notice 'Refreshing ALL matviews (run ''set postgres_dba.refresh_matviews_with_data_forced = TRUE;'' to refresh only matviews w/o data).'; + raise notice 'Refreshing ALL matviews (run ''set postgres_dba.refresh_matviews_with_data_forced = FALSE;'' to refresh only matviews without data).'; for matview in select format('"%s"."%s"', schemaname::text, matviewname::text) from pg_matviews @@ -32,7 +32,7 @@ begin execute sql; end loop; else - raise notice 'Refreshing only matviews w/o data (run ''set postgres_dba.refresh_matviews_with_data_forced = TRUE;'' to refresh all matviews).'; + raise notice 'Refreshing only matviews without data (run ''set postgres_dba.refresh_matviews_with_data_forced = TRUE;'' to refresh all matviews).'; end if; iter := 1; diff --git a/roles/alter_user_with_random_password.psql b/roles/alter_user_with_random_password.psql index 2f7bf59..a4bfd0f 100644 --- a/roles/alter_user_with_random_password.psql +++ b/roles/alter_user_with_random_password.psql @@ -56,7 +56,7 @@ begin (case when lower(current_setting('postgres_dba.login')::text) not in ('0', '', 'no', 'false', 'n', 'f') then ' login' else '' end)); raise debug 'SQL: %', sql; execute sql; - raise debug 'User % altered, password: %', current_setting('postgres_dba.username')::text, pwd; + raise info 'User % altered, password: %', current_setting('postgres_dba.username')::text, pwd; end; $$ language plpgsql; diff --git a/sql/0_node.sql b/sql/0_node.sql index 62d5f1d..19d4490 100644 --- a/sql/0_node.sql +++ b/sql/0_node.sql @@ -30,7 +30,7 @@ select end)::int)::text || ' second')::interval)::text || '; paused: ' || :postgres_dba_is_wal_replay_paused()::text || ')' else - 'Master' + 'Primary' end as value union all ( diff --git a/sql/b1_table_estimation.sql b/sql/b1_table_estimation.sql index acee405..4d46cb6 100644 --- a/sql/b1_table_estimation.sql +++ b/sql/b1_table_estimation.sql @@ -3,7 +3,7 @@ --This SQL is derived from https://github.com/ioguix/pgsql-bloat-estimation/blob/master/table/table_bloat.sql /* -* WARNING: executed with a non-superuser role, the query inspect only tables you are granted to read. +* WARNING: executed with a non-superuser role, the query inspects only tables you are granted to read. * This query is compatible with PostgreSQL 9.0 and more */ @@ -85,10 +85,10 @@ select then '~' || pg_size_pretty((real_size - bloat_size)::numeric) else null end as "Live", - greatest(last_autovacuum, last_vacuum)::timestamp(0)::text + greatest(last_autovacuum, last_vacuum)::timestamp(0)::text || case greatest(last_autovacuum, last_vacuum) when last_autovacuum then ' (auto)' - else '' end as "Last Vaccuum", + else '' end as "Last Vacuum", ( select coalesce(substring(array_to_string(reloptions, ' ') from 'fillfactor=([0-9]+)')::smallint, 100) diff --git a/sql/b2_btree_estimation.sql b/sql/b2_btree_estimation.sql index 70e9fe2..a89210f 100644 --- a/sql/b2_btree_estimation.sql +++ b/sql/b2_btree_estimation.sql @@ -2,7 +2,7 @@ -- enhanced version of https://github.com/ioguix/pgsql-bloat-estimation/blob/master/btree/btree_bloat.sql --- WARNING: executed with a non-superuser role, the query inspect only index on tables you are granted to read. +-- WARNING: executed with a non-superuser role, the query inspects only indexes on tables you are granted to read. -- WARNING: rows with is_na = 't' are known to have bad statistics ("name" type is not supported). -- This query is compatible with PostgreSQL 8.2+ @@ -26,9 +26,9 @@ with step1 as ( /* per tuple header: add IndexAttributeBitMapData if some cols are null-able */ case when max(coalesce(s.null_frac,0)) = 0 then 2 -- IndexTupleData size - else 2 + (( 32 + 8 - 1 ) / 8) -- IndexTupleData size + IndexAttributeBitMapData size ( max num filed per index + 8 - 1 /8) + else 2 + (( 32 + 8 - 1 ) / 8) -- IndexTupleData size + IndexAttributeBitMapData size ( max num fields per index + 8 - 1 /8) end as index_tuple_hdr_bm, - /* data len: we remove null values save space using it fractionnal part from stats */ + /* data len: we remove null values save space using its fractional part from stats */ sum((1 - coalesce(s.null_frac, 0)) * coalesce(s.avg_width, 1024)) as nulldatawidth, max(case when a.atttypid = 'pg_catalog.name'::regtype then 1 else 0 end) > 0 as is_na from pg_attribute as a @@ -47,7 +47,7 @@ with step1 as ( s.schemaname = i.nspname and ( (s.tablename = i.tblname and s.attname = pg_catalog.pg_get_indexdef(a.attrelid, a.attnum, true)) -- stats from tbl - OR (s.tablename = i.idxname AND s.attname = a.attname) -- stats from functionnal cols + OR (s.tablename = i.idxname AND s.attname = a.attname) -- stats from functional cols ) join pg_type as t on a.atttypid = t.oid where a.attnum > 0 diff --git a/sql/b3_table_pgstattuple.sql b/sql/b3_table_pgstattuple.sql index 0b00841..73c7a73 100644 --- a/sql/b3_table_pgstattuple.sql +++ b/sql/b3_table_pgstattuple.sql @@ -1,8 +1,8 @@ --Table bloat (requires pgstattuple; expensive) ---https://github.com/dataegret/pg-utils/tree/master/sql ---pgstattuple extension required ---WARNING: without table name/mask query will read all available tables which could cause I/O spikes +-- https://github.com/dataegret/pg-utils/tree/master/sql +-- pgstattuple extension required +-- WARNING: without table name/mask query will read all available tables which could cause I/O spikes select nspname, relname, pg_size_pretty(relation_size + toast_relation_size) as total_size, @@ -22,7 +22,7 @@ from ( left join pg_namespace n on (n.oid = c.relnamespace) where nspname not in ('pg_catalog', 'information_schema') and nspname !~ '^pg_toast' and relkind = 'r' - --put your table name/mask here + -- put your table name/mask here and relname ~ '' ) t order by (toast_free_space + relation_size - (relation_size - free_space)*100/fillfactor) desc diff --git a/sql/b4_btree_pgstattuple.sql b/sql/b4_btree_pgstattuple.sql index 745a911..9487aaa 100644 --- a/sql/b4_btree_pgstattuple.sql +++ b/sql/b4_btree_pgstattuple.sql @@ -1,8 +1,8 @@ --B-tree indexes bloat (requires pgstattuple; expensive) ---https://github.com/dataegret/pg-utils/tree/master/sql ---pgstattuple extension required ---WARNING: without index name/mask query will read all available indexes which could cause I/O spikes +-- https://github.com/dataegret/pg-utils/tree/master/sql +-- pgstattuple extension required +-- WARNING: without index name/mask query will read all available indexes which could cause I/O spikes with data as ( select schemaname as schema_name, @@ -25,7 +25,7 @@ with data as ( join pg_class c_table on p.relid = c_table.oid where pg_get_indexdef(p.indexrelid) like '%USING btree%' - --put your index name/mask here + -- put your index name/mask here and indexrelname ~ '' ) select diff --git a/sql/i2_redundant_indexes.sql b/sql/i2_redundant_indexes.sql index 442f1c7..2ebe852 100644 --- a/sql/i2_redundant_indexes.sql +++ b/sql/i2_redundant_indexes.sql @@ -7,7 +7,7 @@ -- -- so feel free to use it in your clouds (Heroku, AWS RDS, etc) -- (Keep in mind, that on replicas, the whole picture of index usage --- is usually very different from master). +-- is usually very different from the primary). with fk_indexes as ( select @@ -116,12 +116,6 @@ redundant_indexes_tmp_num as ( * from redundant_indexes_tmp_cut order by index_size_bytes desc -), redundant_indexes_grouped as ( - select - distinct(num), - * - from redundant_indexes_tmp_cut - order by index_size_bytes desc ) select schema_name, diff --git a/sql/i4_invalid_indexes.sql b/sql/i4_invalid_indexes.sql index 7da5ab4..9994615 100644 --- a/sql/i4_invalid_indexes.sql +++ b/sql/i4_invalid_indexes.sql @@ -7,7 +7,7 @@ -- -- so feel free to use it in your clouds (Heroku, AWS RDS, etc) -- (Keep in mind, that on replicas, the whole picture of index usage --- is usually very different from master). +-- is usually very different from the primary). select coalesce(nullif(pn.nspname, 'public') || '.', '') || pct.relname as "relation_name", diff --git a/sql/i5_indexes_migration.sql b/sql/i5_indexes_migration.sql index fc8ba46..934d390 100644 --- a/sql/i5_indexes_migration.sql +++ b/sql/i5_indexes_migration.sql @@ -21,9 +21,9 @@ -- It also doesn't do anything except reading system catalogs and -- printing NOTICEs, so you can easily run it on your --- production *master* database. +-- production *primary* database. -- (Keep in mind, that on replicas, the whole picture of index usage --- is usually very different from master). +-- is usually very different from the primary). -- TODO: take into account type of index and opclass -- TODO: schemas diff --git a/sql/l1_lock_trees.sql b/sql/l1_lock_trees.sql index cae0106..91aa358 100644 --- a/sql/l1_lock_trees.sql +++ b/sql/l1_lock_trees.sql @@ -28,7 +28,7 @@ with recursive l as ( select (clock_timestamp() - a.xact_start)::interval(0) as ts_age, (clock_timestamp() - a.state_change)::interval(0) as change_age, a.datname,a.usename,a.client_addr, - --w.obj wait_on_object, + -- w.obj wait_on_object, tree.pid,replace(a.state, 'idle in transaction', 'idletx') state, lvl,(select count(*) from tree p where p.path ~ ('^'||tree.path) and not p.path=tree.path) blocked, case when tree.pid=any(tree.dl) then '!>' else repeat(' .', lvl) end||' '||trim(left(regexp_replace(a.query, e'\\s+', ' ', 'g'),100)) query diff --git a/sql/l2_lock_trees.sql b/sql/l2_lock_trees.sql index bc5c26f..cda1429 100644 --- a/sql/l2_lock_trees.sql +++ b/sql/l2_lock_trees.sql @@ -61,7 +61,7 @@ select format( '%s %s%s', lpad('[' || pid::text || ']', 7, ' '), - repeat('.', level - 1) || case when level > 1 then ' ' end, + repeat('.', level - 1) || case when level > 1 then ' ' else '' end, left(query, 1000) ) as query from tree diff --git a/sql/p1_alignment_padding.sql b/sql/p1_alignment_padding.sql index 567fa0a..c15d96d 100644 --- a/sql/p1_alignment_padding.sql +++ b/sql/p1_alignment_padding.sql @@ -155,7 +155,7 @@ with recursive constants as ( r1.shifts, r2.cols as alt_cols, r2.types as alt_types, - r2.shifts as alt_shits, + r2.shifts as alt_shifts, r1.pads, r1.curleft, r2.pads as alt_pads, diff --git a/sql/s1_pg_stat_statements_top_total.sql b/sql/s1_pg_stat_statements_top_total.sql index e954758..8d04a24 100644 --- a/sql/s1_pg_stat_statements_top_total.sql +++ b/sql/s1_pg_stat_statements_top_total.sql @@ -30,7 +30,6 @@ select round(max(max_plan_time)::numeric, 2) ) as min_max_plan_t, \else - sum(calls) as calls, round(sum(total_time)::numeric, 2) as total_time, round((sum(mean_time * calls) / sum(calls))::numeric, 2) as mean_time, format( diff --git a/sql/s2_pg_stat_statements_report.sql b/sql/s2_pg_stat_statements_report.sql index d49beb3..bdbf99d 100644 --- a/sql/s2_pg_stat_statements_report.sql +++ b/sql/s2_pg_stat_statements_report.sql @@ -46,7 +46,7 @@ with pg_stat_statements_slice as ( select (select datname from pg_database where oid = p.dbid) as database, (select rolname from pg_roles where oid = p.userid) as username, - --select shortest query, replace \n\n-- strings to avoid email clients format text as footer + -- select shortest query, replace \n\n-- strings to avoid email clients formatting text as footer substring( translate( replace( @@ -205,7 +205,7 @@ with pg_stat_statements_slice as ( select (select datname from pg_database where oid = p.dbid) as database, (select rolname from pg_roles where oid = p.userid) as username, - --select shortest query, replace \n\n-- strings to avoid email clients format text as footer + -- select shortest query, replace \n\n-- strings to avoid email clients formatting text as footer substring( translate( replace( diff --git a/sql/t1_tuning.sql b/sql/t1_tuning.sql index 0563b15..c341802 100644 --- a/sql/t1_tuning.sql +++ b/sql/t1_tuning.sql @@ -110,7 +110,6 @@ where 'autovacuum_vacuum_scale_factor', 'autovacuum_work_mem', 'autovacuum_naptime', - 'random_page_cost', 'seq_page_cost' ) order by category, name; diff --git a/sql/v2_autovacuum_progress_and_queue.sql b/sql/v2_autovacuum_progress_and_queue.sql index dc05269..0a2bf7d 100644 --- a/sql/v2_autovacuum_progress_and_queue.sql +++ b/sql/v2_autovacuum_progress_and_queue.sql @@ -30,7 +30,7 @@ with table_opts as ( from pg_stat_progress_vacuum ) select - --vacuum_settings.oid, + -- vacuum_settings.oid, coalesce( coalesce(nullif(vacuum_settings.nspname, 'public') || '.', '') || vacuum_settings.relname, -- current DB format('[something in "%I"]', p.datname)