Skip to content

Commit e9f9fc6

Browse files
authored
Merge pull request #2 from ongres/checksfor13plus
adding reviewed queries
2 parents ca7e80c + 04f7952 commit e9f9fc6

File tree

15 files changed

+59
-31
lines changed

15 files changed

+59
-31
lines changed

sql/AutoVacuum/candidates-for-vacuum-freeze.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ On `percent_towards_emergency_autovacuum` in 100%, autovacuum will run in aggres
1818
but also, will force a block read to disk.
1919

2020
The sizes are useful for estimate how long the execution will take, but this is also relative to the concurrency, indexes and other factors.
21+
22+
The default value is too conservative, (10%) probably you can set autovacuum_freeze_max_age for this table in a higer value (~25%).
23+
https://postgresqlco.nf/doc/en/param/autovacuum_freeze_max_age/13/

sql/BackgroundWriter/10_secs_bgwriter.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- 10 second iteration for calculate bgwriter activity
2-
WITH tara AS (
3-
SELECT
2+
with tara as (
3+
select
44
checkpoints_timed ,
55
checkpoints_req ,
66
checkpoint_write_time ,
@@ -13,7 +13,7 @@ WITH tara AS (
1313
buffers_alloc
1414
from pg_stat_bgwriter, pg_sleep(10)
1515
)
16-
SELECT
16+
select
1717
pgb.checkpoints_timed - tara.checkpoints_timed,
1818
pgb.checkpoints_req - tara.checkpoints_req,
1919
pgb.checkpoint_write_time - tara.checkpoint_write_time,
@@ -24,5 +24,5 @@ SELECT
2424
pgb.buffers_backend - tara.buffers_backend,
2525
pgb.buffers_backend_fsync - tara.buffers_backend_fsync,
2626
pgb.buffers_alloc - tara.buffers_alloc
27-
FROM pg_stat_bgwriter pgb,tara
27+
from pg_stat_bgwriter pgb,tara
2828
;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
-- Shows the cache effectivity on each table (1 row per table)
22
-- "_hits" and "_reads" includes "regular" blocks, indexes blocks, toast and tidx blocks
3+
4+
select
5+
schemaname, relname,
6+
heap_blks_read as heap_read,
7+
heap_blks_hit as heap_hit,
8+
heap_blks_hit / (heap_blks_hit + heap_blks_read::float) as ratio
9+
from
10+
pg_statio_user_tables
11+
where (heap_blks_hit + heap_blks_read) > 0;

sql/DatabaseCapacity/size_growth_interval.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
WITH timeset AS (
55
select pg_database_size(datname) num,
66
pg_size_pretty(pg_database_size(datname)) size
7-
from pg_database where datname = 'db'
7+
from pg_database where datname = current_database()
88
UNION ALL
99
select pg_database_size(datname) num,
1010
pg_size_pretty(pg_database_size(datname)) size
11-
from pg_database, pg_sleep(10) where datname = 'db'
11+
from pg_database, pg_sleep(10) where datname = current_database()
1212
)
1313
SELECT pg_size_pretty((num - lag(num,1)
1414
OVER (ORDER BY num))/10) transfer_per_second,

sql/Indexes/unused_indexes.sql

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
SELECT s.schemaname,
2-
s.relname AS tablename,
3-
s.indexrelname AS indexname,
4-
pg_relation_size(s.indexrelid) AS index_size
5-
FROM pg_catalog.pg_stat_user_indexes s
6-
JOIN pg_catalog.pg_index i ON s.indexrelid = i.indexrelid
7-
WHERE s.idx_scan = 0 -- has never been scanned
8-
AND 0 <>ALL (i.indkey) -- no index column is an expression
9-
AND NOT i.indisunique -- is not a UNIQUE index
10-
AND NOT EXISTS -- does not enforce a constraint
11-
(SELECT 1 FROM pg_catalog.pg_constraint c
12-
WHERE c.conindid = s.indexrelid)
13-
ORDER BY pg_relation_size(s.indexrelid) DESC;
1+
select s.schemaname,
2+
s.relname as tablename,
3+
s.indexrelname as indexname,
4+
pg_relation_size(s.indexrelid) as index_size,
5+
pg_size_pretty(pg_relation_size(s.indexrelid)) as index_size_human
6+
from pg_catalog.pg_stat_user_indexes s
7+
join pg_catalog.pg_index i on s.indexrelid = i.indexrelid
8+
where --s.idx_scan = 0 -- has never been scanned
9+
s.idx_scan < 50 -- almost never used
10+
and pg_relation_size(relid) > 5 * 8192 -- skip empty objects
11+
and 0 <>all (i.indkey) -- no index column is an expression
12+
and not i.indisunique -- is not a unique index
13+
and not exists -- does not enforce a constraint
14+
(select 1 from pg_catalog.pg_constraint c
15+
where c.conindid = s.indexrelid)
16+
order by pg_relation_size(s.indexrelid) desc;

sql/Indexes/worst_index_hits.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@ WITH worst_index_hit_ratio AS (
1616
GROUP BY indexrelname, relid
1717
ORDER BY hit_ratio ASC
1818
)
19-
SELECT * FROM worst_index_hit_ratio WHERE idx_read = 0 and idx_hit = 0; -- < 0.9;
19+
SELECT * FROM worst_index_hit_ratio
20+
WHERE
21+
-- idx_read = 0 and idx_hit = 0; -- < 0.9;
22+
hit_ratio < 0.8;
2023

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- This query returns a list of active session holding multixact slots and their ages
2+
-- The age here is not tied to a time constraint, but rather to the transaction
3+
-- amount distance from the relminmxid and now.
4+
SELECT pid, datname, usename, state, txid_current(), backend_xmin,
5+
txid_current()::text::int - backend_xmin::text::int difference
6+
, statement_timestamp() - query_start elapsed , pg_blocking_pids(pid) as blocked_by, query::varchar(150) query FROM pg_stat_activity WHERE backend_xmin IS NOT NULL ORDER BY age(backend_xmin) ;

sql/MultiXact/get_oldest_multixact.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ SELECT relnamespace::regnamespace, relname, relkind, relminmxid,
55
(select next_multixact_id::text::bigint FROM pg_control_checkpoint()) - relminmxid::TEXT::BIGINT AS age
66
FROM pg_class
77
WHERE relminmxid::TEXT::BIGINT <> 0
8-
ORDER BY relminmxid::TEXT::BIGINT ASC;
8+
ORDER BY relminmxid::TEXT::BIGINT ASC;

0 commit comments

Comments
 (0)