Skip to content

Instantly share code, notes, and snippets.

View graphbear's full-sized avatar

John Graber graphbear

  • Gray Bear, Inc.
  • USA • WI • MKE
  • 19:52 (UTC -05:00)
  • LinkedIn in/johngraber
View GitHub Profile
@graphbear
graphbear / find_toast_table_parent.sql
Created August 27, 2020 16:12
find parent table of a toast table in postgresql
select
relname
from
pg_class
where
reltoastrelid = ( select oid from pg_class where relname = 'toast_table_name' );
# you can encrypt the whole cluster in one go
read -sp "Postgres passphrase: " PGENCRYPTIONKEY
export PGENCRYPTIONKEY=$PGENCRYPTIONKEY
initdb –data-encryption pgcrypto --data-checksums -D cryptotest
@graphbear
graphbear / pg_find_locks.sql
Created May 14, 2018 18:20
what's locking?
SELECT
waiting.locktype AS waiting_locktype,
waiting.relation::regclass AS waiting_table,
waiting_stm.query AS waiting_query,
waiting.mode AS waiting_mode,
waiting.pid AS waiting_pid,
other.locktype AS other_locktype,
other.relation::regclass AS other_table,
other_stm.query AS other_query,
other.mode AS other_mode,
@graphbear
graphbear / tablespace_objects.sql
Last active September 29, 2017 17:29
what's in a postgres tablespace
SELECT
c.relname,
c.reltype,
t.spcname
FROM
pg_class c
JOIN pg_tablespace t ON c.reltablespace = t.oid
ORDER BY
t.spcname,
c.relname;
@graphbear
graphbear / table_privileges.sql
Created July 12, 2017 01:13
list all tables for which user or role has privileges
SELECT
schemaname||'.'||tablename
FROM
pg_tables
WHERE
has_table_privilege (
'user or role name',
schemaname||'.'||tablename,
'select'
)
@graphbear
graphbear / org-mode-line-wrap.el
Created June 20, 2017 03:40
org-mode word wrap
;; add this to your emacs.d
;; wraps the lines in org-mode
(setq org-startup-truncated nil)
;; or in the case of spacemacs...
(org :variables
org-startup-truncated nil)
@graphbear
graphbear / tables_by_size.sql
Created May 18, 2017 21:26
postgres tables sorted by size
SELECT
nspname || '.' || relname AS "relation",
pg_size_pretty(pg_relation_size(C.oid)) AS "size"
FROM
pg_class C
LEFT JOIN
pg_namespace N ON (N.oid = C.relnamespace)
WHERE
nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY
@graphbear
graphbear / indexes_by_size.sql
Created May 18, 2017 21:24
postgres indexes sorted by size
SELECT
relname AS name,
pg_size_pretty(sum(relpages::bigint*current_setting('block_size')::bigint)::bigint) AS size
FROM
pg_class
WHERE
reltype=0
GROUP BY
relname
ORDER BY
-- http://stackoverflow.com/questions/7336413/query-grants-for-a-table-in-postgres
SELECT grantee, privilege_type
FROM information_schema.role_table_grants
WHERE table_name='mytable'
@graphbear
graphbear / tables_identical.sql
Created May 1, 2017 14:03
check for identical data in two postgres tables
-- https://dba.stackexchange.com/questions/72641/checking-whether-two-tables-have-identical-content-in-postgresql
-- more sql for other cases and good discussion in reply to this question
(TABLE a EXCEPT TABLE b)
UNION ALL
(TABLE b EXCEPT TABLE a) ;