Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am mcorsen on github.
  • I am macorsen (https://keybase.io/macorsen) on keybase.
  • I have a public key ASBv2-qM9sxjyd5CjC0uhCx9L01sra9fA9bc90MJNmQlmAo

To claim this, I am signing this object:

-- Size of all schemas ordered by percent of total size
SELECT schema_name,
pg_size_pretty(sum(table_size)::bigint),
(sum(table_size) / pg_database_size(current_database())) * 100 AS percent
FROM (
SELECT pg_catalog.pg_namespace.nspname as schema_name,
pg_relation_size(pg_catalog.pg_class.oid) as table_size
FROM pg_catalog.pg_class
JOIN pg_catalog.pg_namespace ON relnamespace = pg_catalog.pg_namespace.oid
) t
@mcorsen
mcorsen / table_sizes.sql
Created June 6, 2014 16:29
Postgresql 9.1 query to find table sizes
SELECT
table_name,
pg_size_pretty(table_size) AS table_size,
pg_size_pretty(indexes_size) AS indexes_size,
pg_size_pretty(total_size) AS total_size
FROM (
SELECT
table_name,
pg_table_size(table_name) AS table_size,
pg_indexes_size(table_name) AS indexes_size,
@mcorsen
mcorsen / database_sizes.sql
Created June 6, 2014 16:27
Postgresql 9.1 query to list database sizes
SELECT pg_database.datname,
pg_size_pretty(pg_database_size(pg_database.datname)) AS size
FROM pg_database
ORDER BY pg_database_size(pg_database.datname) DESC;
@mcorsen
mcorsen / current_queries.sql
Created June 6, 2014 16:25
Postgresql 9.1 query to list current queries
SELECT usename, client_addr, waiting, backend_start, query_start, procpid, current_query
FROM pg_stat_activity
ORDER BY usename, client_addr, backend_start;
@mcorsen
mcorsen / blocked_queries.sql
Created June 6, 2014 16:22
Postgresql 9.1 query to find queries that are blocked by locks
SELECT
l1.relation::regclass AS blocked_relation,
l1.pid AS blocked_pid,
a1.usename AS blocked_user,
a1.current_query AS blocked_query,
now() - a1.query_start AS blocked_time,
l2.pid AS blocking_pid,
a2.usename AS blocking_user,
a2.current_query AS blocking_query,
l2.virtualtransaction,
@mcorsen
mcorsen / init_script.sh
Created May 13, 2014 23:39
LSB compliant init script that actually works. Tested on Linux Mint 15 and CentOS 6.5.
#!/bin/bash
#
# chkconfig: 2345 90 12
# description: Description of service here
#
### BEGIN INIT INFO
# Provides: Short service name here
# Required-Start: $remote_fs $network $syslog
# Required-Stop: $remote_fs $network $syslog
# Default-Start: 3 5
@mcorsen
mcorsen / clean_files.sh
Last active September 3, 2015 14:34
Clean old log files and directories
#!/bin/bash
if [[ $# -lt 3 ]]
then
echo "Usage: $0: <dir> <file pattern> <num days old>"
exit 1
fi
dir=$1
file_pattern=$2