Skip to content

Instantly share code, notes, and snippets.

View StevenJL's full-sized avatar

Steven Li StevenJL

  • Fountain Inc.
  • SF Bay Area
  • 23:25 (UTC -07:00)
View GitHub Profile
@StevenJL
StevenJL / low_use_indices.sql
Last active April 22, 2021 05:19
low use indices
WITH table_scans as (
SELECT relid,
tables.idx_scan + tables.seq_scan as all_scans,
( tables.n_tup_ins + tables.n_tup_upd + tables.n_tup_del ) as writes,
pg_relation_size(relid) as table_size
FROM pg_stat_user_tables as tables
),
all_writes as (
SELECT sum(writes) as total_writes
FROM table_scans
@StevenJL
StevenJL / init.vim
Last active February 21, 2021 06:33
~/.config/nvim/init.vim
" map kj to ESC
imap kj <ESC>
" Show line numbers in editor
set number
" Shows file name in terminal window title bar
set title
" When line length exceeds editor width, content does not wrap around
@StevenJL
StevenJL / shared_buffers.sql
Created February 8, 2021 03:42
Show Shared Buffers
SHOW SHARED_BUFFERS;
# shared_buffers
# ----------------
# 128MB
# (1 row)
@StevenJL
StevenJL / psql_cache_hit_ratio_index.sql
Last active February 8, 2021 02:50
PSQL index cache hit ratio
# See Cache Hit Ratio for a given index
SELECT
(heap_blks_hit::decimal / (heap_blks_hit + heap_blks_read)) as cache_hit_ratio
FROM
pg_statio_user_indexes
WHERE indexrelname = 'some_high_use_index';
# See overall Cache Hit Ratio across all indexes
SELECT
sum(idx_blks_hit)/(sum(idx_blks_read) + sum(idx_blks_hit)) as cache_hit_ratio
@StevenJL
StevenJL / psql_cache_hit_ratio.psql
Created January 31, 2021 04:30
Psql Overall Cache Hit Ratio
SELECT
SUM(heap_blks_hit) / (SUM(heap_blks_hit) + SUM(heap_blks_read)) as cache_hit_ratio;
FROM
pg_statio_user_tables;
@StevenJL
StevenJL / cache_hit_ratio_for_table.sql
Created January 31, 2021 04:25
Cache Hit Ratio for psql table
SELECT
(heap_blks_hit::decimal / (heap_blks_hit + heap_blks_read)) as cache_hit_ratio
FROM
pg_statio_user_tables
WHERE relname = 'large_table';
@StevenJL
StevenJL / psql_query_most_rows.sql
Last active August 16, 2021 06:30
PSQL query most rows
# See the 10 SELECT queries which touch the most number of rows.
# These queries may benefit from adding indexes to reduce the number
# of rows retrieved/affected.
SELECT
rows, # rows is the number of rows retrieved/affected by the query
query
FROM pg_stat_statements
WHERE query iLIKE '%SELECT%'
ORDER BY rows DESC
LIMIT 10;
@StevenJL
StevenJL / psql_slowest_average_queries.sql
Last active August 16, 2021 06:29
PSQL Slowest Average Queries
# See the 10 slowest queries with over a 1000 calls
SELECT query, calls, (total_time/calls)::integer AS avg_time_ms
FROM pg_stat_statements
WHERE calls > 1000
ORDER BY avg_time_ms DESC
LIMIT 10;
# query | calls | avg_time_ms
# ----------+---------+-------------
# INSERT .. | 52323 | 12
@StevenJL
StevenJL / psql_max_connections.sql
Created January 23, 2021 19:44
psql max connections
/* See the number of max connections the server supports */
SHOW max_connections;
/*
The number of connections being used.
This includes both client connections
as well as connections to WAL sender
processes (used in replication)
*/
SELECT COUNT(*) FROM
@StevenJL
StevenJL / write_queries_to_table.sql
Created January 23, 2021 08:32
Write Queries to table
SELECT * FROM pg_stat_activity
WHERE state != 'idle'
AND query NOT ILIKE '%SELECT%'
AND query ILIKE '%some_big_table%'
AND query NOT ILIKE '%pg_stat_activity%';