May 17, 2026, 11:40 PM
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
SELECT c.table_schema,
|
||||
c.table_name as table,
|
||||
c.ordinal_position as order,
|
||||
c.column_name as column,
|
||||
-- c.data_type as type,
|
||||
CASE WHEN c.data_type IN ('character', 'varchar') THEN c.data_type || '(' || c.character_maximum_length || ')'
|
||||
WHEN c.data_type IN ('numeric') THEN c.data_type || '(' || c.numeric_precision || ',' || c.numeric_scale || ')'
|
||||
ELSE c.data_type
|
||||
END,
|
||||
-- c.character_maximum_length as max_len,
|
||||
c.is_nullable as null,
|
||||
col_description(t.oid, c.ordinal_position)
|
||||
FROM information_schema.columns c
|
||||
JOIN pg_class t ON (t.relname = c.table_name)
|
||||
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
|
||||
ORDER BY 1, 2, 3;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
SELECT table_schema,
|
||||
table_name,
|
||||
constraint_name as constraint,
|
||||
constraint_type as type
|
||||
FROM information_schema.table_constraints
|
||||
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
|
||||
ORDER BY 1, 2;
|
||||
@@ -0,0 +1,6 @@
|
||||
SELECT table_schema as schema,
|
||||
table_name as table
|
||||
FROM information_schema.tables
|
||||
WHERE table_type = 'BASE TABLE'
|
||||
AND table_schema NOT IN ('pg_catalog', 'information_schema')
|
||||
ORDER BY 1, 2;
|
||||
@@ -0,0 +1,81 @@
|
||||
These are generic SQL queries. In some cases you need to replace variable holders -> <VARIABLE>
|
||||
with the actual value you need.
|
||||
|
||||
ALL_COLUMNS.sql -> Lists all tables, their columns and data type
|
||||
ALL_CONSTRAINTS.sql -> lists all tables and their associated constraints
|
||||
ALL_TABLES.sql -> lists all tables
|
||||
bad_idx.sql -> lists all indexes flagged as invalid
|
||||
blocked_transaction.sql -> shows all blocked queries and transaction causing block
|
||||
cache_hit_ratio.sql -> shows cache hit ratio for all databases
|
||||
cancel_all_queries.sql -> cancels all tranactions for specified <user_name>
|
||||
check_fks.sql -> shows fk status for all tables > 9mb in size or greater than 1000 writes
|
||||
column_name_type_difference.sql -> shows all tables and columns that have same column name but different data type
|
||||
columns_of_type_x.sql -> shows all tables that have columns of type <TYPE>
|
||||
connection_counts.sql -> shows connection totals and totals for users, database and user/database
|
||||
current_locks.sql -> shows current_locks
|
||||
current_queries_blocked.sql -> shows blocked transactions and blocking query
|
||||
current_queries.sql -> shows all current_queries
|
||||
database_info.sql -> shows information from pg_database
|
||||
database_sizes.sql -> shows size of all databases and percentage of total size
|
||||
distinct_data_types.sql -> shows all data types in use in cluster
|
||||
enum_maint.sql -> generic query to show all enums and commented delete / add code
|
||||
enums.sql -> generic query to show all enums
|
||||
filenames_and_tables.sql -> shows physical filenames for all tables
|
||||
gen_create_indexes.sql -> generates sql statements to recreate all indexes
|
||||
gen_drop_indexes.sql -> generates sql statements to drop all indexes
|
||||
gen_get_seq_max.sql -> generates sql to get the max value of all sequences
|
||||
gen_revoke_all.sql -> generates sql to revoke all priviliges from all tables for user <some_user>
|
||||
gen_table_compare_counts.sql -> generates sql to compare reltuples to actual row count for all tables
|
||||
get_slony_schemas.sql -> shows all slony schemas
|
||||
get_trans_min_cnt.sql -> shows trandaction count per minute in cluster
|
||||
groups_users.sql -> shows all groups and assigned users
|
||||
inactive_tables.sql -> shows all tables that have 0 fetch's & 0 read's
|
||||
index_bloat.sql -> shows size of index vs. table
|
||||
index_bloat2.sql > a more specific version of index bloat WHERE realbloat > 50 and wastedbytes > 50000000 (adjust to your requirements)
|
||||
index_comments.sql -> shows comments for all indexes
|
||||
index_sizes.sql -> show the size of all indexes
|
||||
index_tablespaces.sql -> shows the tablespace indexes are using
|
||||
make_alter_index_spc.sql -> generates sql to alter all indexes in public to <new_table_space>
|
||||
make_alter_table_spc.sql -> generates sql to alter all tables in public to <new_table_space>
|
||||
most_active_tables.sql -> sorts and lists all tables in descending order by tuples read
|
||||
parent_and_children.sql -> lists all tables and associated children
|
||||
pg_runtime.sql -> lists start and run time for cluster
|
||||
pg_stat_all_indexes.sql -> lists stats and status for all indexes
|
||||
pg_stat_all_tables.sql -> lists stats and vacuum info for all tables
|
||||
remove_ctrl.sh -> removes control characters from queries (used if query written in windows)
|
||||
role_members.sql -> lists all roles and their members
|
||||
rules.sql -> lists all tables and associated rules
|
||||
sequence_cols.sql -> lists all tables with sequence columns
|
||||
show_dead_tuples.sql -> shows table stats and status of dead tuples
|
||||
table_access.sql -> lists all tables and their acl
|
||||
table_columns.sql -> lists all columns for specified <table>
|
||||
table_comments.sql -> lists comments for all tables and columns
|
||||
table_constraints.sql -> lists all constraints for all tables
|
||||
table_dependents.sql -> lists all tables, children and related constraint name
|
||||
table_grants.sql -> lists all tables and their grants
|
||||
table_in_function.sql -> list all functions that reference <TABLE/COLUMN>
|
||||
table_in_schema.sql -> lists all tables in <schema_name>
|
||||
table_in_trigger.sql -> lists all triggers that reference <table>
|
||||
table_row_counts.sql -> lists all tables with greater than 0 rows
|
||||
tables_and_comments.sql -> lists only tables and columns with comments
|
||||
tables_and_filenames.sql -> lists all tables and physical filenames
|
||||
tables_and_owners.sql -> lists all tables, owners and permits
|
||||
tables_and_pkey.sql -> lists all tables and primary key name
|
||||
table_sizes.sql -> lists all tables, owner, physical filename, tuples, size, total size, size in bytes, total_size in bytes and tablespace
|
||||
table_stats.sql -> lists all tables and indexes, last vacuum, inserted, updated and deleted count
|
||||
tables_with_column_name.sql -> lists all tables with column name LIKE <COLUMN_NAME>
|
||||
tables_with_FKs.sql -> lists all tables that have foreign keys and FK name
|
||||
tables_with_large_index_size.sql -> lists all tables where index size > table size
|
||||
tables_with_no_FK.sql -> lists all tables that have no foreign key
|
||||
tables_with_no_pkey.sql -> lists all tables with no primary key
|
||||
tables_with_no_public_grants.sql -> lists all tables that have no public grants (Only owner has access)
|
||||
table_triggers.sql -> lists all tables with triggers, trigger name, trigger type and tgenabled value
|
||||
tcp_settings.sql -> lists the current tcp settings
|
||||
triggers_and_constraints.sql -> lists tables, their triggers and function called by the trigger
|
||||
useless_indexes.sql -> lists tables and associated indexes scanned < 200 times and are not unique indexes
|
||||
useless_indexes2.sql -> lists tables and associated indexes scanned < 200 times and are not unique indexes or primary keys
|
||||
users.sql -> lists all users/groups and if they are superuser or not
|
||||
user_table_access_detail.sql -> lists all table privileges for user <CKUSER>
|
||||
user_table_access.sql -> lists all tables, acl and position in acl for user <USERNAME>
|
||||
wasted_index_space.sql -> shows space wasted by unused indexes
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
SELECT n.nspname,
|
||||
t.relname as table,
|
||||
c.conname as pk_name
|
||||
FROM pg_class t
|
||||
JOIN pg_constraint c ON ( c.conrelid = t.OID AND c.contype = 'p')
|
||||
JOIN pg_namespace n ON (n.oid = t.relnamespace)
|
||||
WHERE relkind = 'r'
|
||||
AND t.relname NOT LIKE 'pg_%'
|
||||
AND t.relname NOT LIKE 'sql_%'
|
||||
ORDER BY n.nspname, t.relname, c.conname;
|
||||
@@ -0,0 +1,26 @@
|
||||
SELECT n.nspname as schema,
|
||||
t.relname as table,
|
||||
count(c.conname)
|
||||
FROM pg_class t
|
||||
JOIN pg_namespace n ON n.oid = t.relnamespace
|
||||
JOIN pg_constraint c ON ( c.conrelid = t.OID AND c.contype = 'f')
|
||||
WHERE relkind = 'r'
|
||||
AND t.relname NOT LIKE 'pg_%'
|
||||
AND t.relname NOT LIKE 'sql_%'
|
||||
GROUP BY n.nspname,
|
||||
t.relname
|
||||
ORDER BY count, n.nspname, t.relname;
|
||||
|
||||
|
||||
SELECT n.nspname as schema,
|
||||
t.relname as table,
|
||||
c.conname as fk_name
|
||||
FROM pg_class t
|
||||
JOIN pg_namespace n ON n.oid = t.relnamespace
|
||||
JOIN pg_constraint c ON ( c.conrelid = t.OID AND c.contype = 'f')
|
||||
WHERE relkind = 'r'
|
||||
AND t.relname NOT LIKE 'pg_%'
|
||||
AND t.relname NOT LIKE 'sql_%'
|
||||
ORDER BY n.nspname,
|
||||
t.relname,
|
||||
c.conname;
|
||||
@@ -0,0 +1,15 @@
|
||||
SELECT n.nspname as schema,
|
||||
relname as table
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON ( n.oid = c.relnamespace )
|
||||
WHERE relkind = 'r'
|
||||
AND relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND c.oid NOT IN
|
||||
(SELECT conrelid
|
||||
FROM pg_constraint
|
||||
WHERE contype = 'f'
|
||||
AND contype <> 'p'
|
||||
AND contype <> 'c')
|
||||
ORDER BY n.nspname,
|
||||
relname;
|
||||
@@ -0,0 +1,16 @@
|
||||
SELECT n.nspname,
|
||||
i.relname,
|
||||
i.indexrelname,
|
||||
CASE WHEN idx.indisprimary
|
||||
THEN 'pkey'
|
||||
WHEN idx.indisunique
|
||||
THEN 'uidx'
|
||||
ELSE 'idx'
|
||||
END AS type,
|
||||
'INVALID'
|
||||
FROM pg_stat_all_indexes i
|
||||
JOIN pg_class c ON (c.oid = i.relid)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_index idx ON (idx.indexrelid = i.indexrelid )
|
||||
WHERE idx.indisvalid = FALSE
|
||||
ORDER BY 1, 2;
|
||||
@@ -0,0 +1,15 @@
|
||||
/* Requires PostgreSQL version is 9.2 or Greater */
|
||||
SELECT
|
||||
w.query as waiting_query,
|
||||
w.pid as w_pid,
|
||||
w.usename as w_user,
|
||||
l.query as locking_query,
|
||||
l.pid as l_pid,
|
||||
l.usename as l_user,
|
||||
t.schemaname || '.' || t.relname as tablename
|
||||
FROM pg_stat_activity w
|
||||
JOIN pg_locks l1 ON (w.pid = l1.pid and not l1.granted)
|
||||
JOIN pg_locks l2 on (l1.relation = l2.relation and l2.granted)
|
||||
JOIN pg_stat_activity l ON (l2.pid = l.pid)
|
||||
JOIN pg_stat_user_tables t ON (l1.relation = t.relid)
|
||||
WHERE w.waiting;
|
||||
@@ -0,0 +1,13 @@
|
||||
SELECT pg_stat_database.datname,
|
||||
pg_stat_database.blks_read,
|
||||
pg_stat_database.blks_hit,
|
||||
round((pg_stat_database.blks_hit::double precision
|
||||
/ (pg_stat_database.blks_read
|
||||
+ pg_stat_database.blks_hit
|
||||
+1)::double precision * 100::double precision)::numeric, 2) AS cachehitratio
|
||||
FROM pg_stat_database
|
||||
WHERE pg_stat_database.datname !~ '^(template(0|1)|postgres)$'::text
|
||||
ORDER BY round((pg_stat_database.blks_hit::double precision
|
||||
/ (pg_stat_database.blks_read
|
||||
+ pg_stat_database.blks_hit
|
||||
+ 1)::double precision * 100::double precision)::numeric, 2) DESC;
|
||||
@@ -0,0 +1,4 @@
|
||||
SELECT pg_cancel_backend(procpid),
|
||||
pg_terminate_backend(procpid)
|
||||
FROM pg_stat_activity
|
||||
WHERE usename = '<user_name>';
|
||||
@@ -0,0 +1,116 @@
|
||||
WITH fk_actions ( code, action ) AS (
|
||||
VALUES ( 'a', 'error' ),
|
||||
( 'r', 'restrict' ),
|
||||
( 'c', 'cascade' ),
|
||||
( 'n', 'set null' ),
|
||||
( 'd', 'set default' )
|
||||
),
|
||||
fk_list AS (
|
||||
SELECT pg_constraint.oid as fkoid, conrelid, confrelid as parentid,
|
||||
conname, relname, nspname,
|
||||
fk_actions_update.action as update_action,
|
||||
fk_actions_delete.action as delete_action,
|
||||
conkey as key_cols
|
||||
FROM pg_constraint
|
||||
JOIN pg_class ON conrelid = pg_class.oid
|
||||
JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid
|
||||
JOIN fk_actions AS fk_actions_update ON confupdtype = fk_actions_update.code
|
||||
JOIN fk_actions AS fk_actions_delete ON confdeltype = fk_actions_delete.code
|
||||
WHERE contype = 'f'
|
||||
),
|
||||
fk_attributes AS (
|
||||
SELECT fkoid, conrelid, attname, attnum
|
||||
FROM fk_list
|
||||
JOIN pg_attribute
|
||||
ON conrelid = attrelid
|
||||
AND attnum = ANY( key_cols )
|
||||
ORDER BY fkoid, attnum
|
||||
),
|
||||
fk_cols_list AS (
|
||||
SELECT fkoid, array_agg(attname) as cols_list
|
||||
FROM fk_attributes
|
||||
GROUP BY fkoid
|
||||
),
|
||||
index_list AS (
|
||||
SELECT indexrelid as indexid,
|
||||
pg_class.relname as indexname,
|
||||
indrelid,
|
||||
indkey,
|
||||
indpred is not null as has_predicate,
|
||||
pg_get_indexdef(indexrelid) as indexdef
|
||||
FROM pg_index
|
||||
JOIN pg_class ON indexrelid = pg_class.oid
|
||||
WHERE indisvalid
|
||||
),
|
||||
fk_index_match AS (
|
||||
SELECT fk_list.*,
|
||||
indexid,
|
||||
indexname,
|
||||
indkey::int[] as indexatts,
|
||||
has_predicate,
|
||||
indexdef,
|
||||
array_length(key_cols, 1) as fk_colcount,
|
||||
array_length(indkey,1) as index_colcount,
|
||||
round(pg_relation_size(conrelid)/(1024^2)::numeric) as table_mb,
|
||||
cols_list
|
||||
FROM fk_list
|
||||
JOIN fk_cols_list USING (fkoid)
|
||||
LEFT OUTER JOIN index_list
|
||||
ON conrelid = indrelid
|
||||
AND (indkey::int2[])[0:(array_length(key_cols,1) -1)] @> key_cols
|
||||
),
|
||||
fk_perfect_match AS (
|
||||
SELECT fkoid
|
||||
FROM fk_index_match
|
||||
WHERE (index_colcount - 1) <= fk_colcount
|
||||
AND NOT has_predicate
|
||||
AND indexdef LIKE '%USING btree%'
|
||||
),
|
||||
fk_index_check AS (
|
||||
SELECT 'no index' as issue, *, 1 as issue_sort
|
||||
FROM fk_index_match
|
||||
WHERE indexid IS NULL
|
||||
UNION ALL
|
||||
SELECT 'questionable index' as issue, *, 2
|
||||
FROM fk_index_match
|
||||
WHERE indexid IS NOT NULL
|
||||
AND fkoid NOT IN (
|
||||
SELECT fkoid
|
||||
FROM fk_perfect_match)
|
||||
),
|
||||
parent_table_stats AS (
|
||||
SELECT fkoid, tabstats.relname as parent_name,
|
||||
(n_tup_ins + n_tup_upd + n_tup_del + n_tup_hot_upd) as parent_writes,
|
||||
round(pg_relation_size(parentid)/(1024^2)::numeric) as parent_mb
|
||||
FROM pg_stat_user_tables AS tabstats
|
||||
JOIN fk_list
|
||||
ON relid = parentid
|
||||
),
|
||||
fk_table_stats AS (
|
||||
SELECT fkoid,
|
||||
(n_tup_ins + n_tup_upd + n_tup_del + n_tup_hot_upd) as writes,
|
||||
seq_scan as table_scans
|
||||
FROM pg_stat_user_tables AS tabstats
|
||||
JOIN fk_list
|
||||
ON relid = conrelid
|
||||
)
|
||||
SELECT nspname as schema_name,
|
||||
relname as table_name,
|
||||
conname as fk_name,
|
||||
issue,
|
||||
table_mb,
|
||||
writes,
|
||||
table_scans,
|
||||
parent_name,
|
||||
parent_mb,
|
||||
parent_writes,
|
||||
cols_list,
|
||||
indexdef
|
||||
FROM fk_index_check
|
||||
JOIN parent_table_stats USING (fkoid)
|
||||
JOIN fk_table_stats USING (fkoid)
|
||||
/*WHERE table_mb > 9
|
||||
AND ( writes > 1000
|
||||
OR parent_writes > 1000
|
||||
OR parent_mb > 10 )
|
||||
*/ORDER BY issue_sort, table_mb DESC, table_name, fk_name;
|
||||
@@ -0,0 +1,26 @@
|
||||
SELECT sa.nspname as schemaa,
|
||||
ca.relname as tablea,
|
||||
a.attname as columna, ta.typname,
|
||||
sb.nspname as schemab,
|
||||
cb.relname as tableb,
|
||||
a.attname as columnb, tb.typname
|
||||
FROM pg_attribute a
|
||||
JOIN pg_type ta ON (ta.oid = a.atttypid)
|
||||
JOIN pg_class ca ON (ca.oid = a.attrelid)
|
||||
JOIN pg_catalog.pg_namespace sa ON (ca.relnamespace = sa.oid),
|
||||
pg_attribute b
|
||||
JOIN pg_type tb ON (tb.oid = b.atttypid)
|
||||
JOIN pg_class cb ON (cb.oid = b.attrelid)
|
||||
JOIN pg_catalog.pg_namespace sb ON (cb.relnamespace = sb.oid)
|
||||
WHERE a.attname = b.attname
|
||||
AND ta.typname <> tb.typname
|
||||
AND ca.relname NOT LIKE 'pg_%'
|
||||
AND ca.relname NOT LIKE 'information%'
|
||||
AND ca.relname NOT LIKE 'sql_%'
|
||||
AND ca.relkind = 'r'
|
||||
AND cb.relname NOT LIKE 'pg_%'
|
||||
AND cb.relname NOT LIKE 'information%'
|
||||
AND cb.relname NOT LIKE 'sql_%'
|
||||
AND cb.relkind = 'r'
|
||||
AND 'rollback' NOT IN (sa.nspname, sb.nspname)
|
||||
AND 'id' NOT IN (a.attname, b.attname);
|
||||
@@ -0,0 +1,26 @@
|
||||
--SELECT DISTINCT (c.data_type),
|
||||
-- COALESCE(character_maximum_length::text, 'N/A'),
|
||||
-- c.numeric_precision,
|
||||
-- c.numeric_scale
|
||||
-- FROM information_schema.columns c;
|
||||
|
||||
SELECT c.table_schema as schema,
|
||||
c.table_name as table,
|
||||
c.column_name as column,
|
||||
c.data_type as type,
|
||||
case when c.data_type LIKE '%char%'
|
||||
then COALESCE(character_maximum_length::text, 'N/A')
|
||||
when c.data_type LIKE '%numeric%'
|
||||
then '(' || c.numeric_precision::text || ', ' || c.numeric_scale::text || ')'
|
||||
when c.data_type LIKE '%int%'
|
||||
then c.numeric_precision::text
|
||||
else COALESCE(character_maximum_length::text, 'N/A')
|
||||
end as size
|
||||
FROM information_schema.columns c
|
||||
WHERE c.table_schema NOT LIKE 'pg_%'
|
||||
AND c.table_schema NOT LIKE 'information%'
|
||||
AND c.table_name NOT LIKE 'sql_%'
|
||||
AND c.data_type LIKE '%<TYPE>%'
|
||||
AND c.is_updatable = 'YES'
|
||||
ORDER BY c.table_name, c.column_name;
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
SELECT COUNT(*)
|
||||
FROM pg_stat_activity;
|
||||
|
||||
SELECT usename,
|
||||
count(*)
|
||||
FROM pg_stat_activity
|
||||
GROUP BY 1
|
||||
ORDER BY 1;
|
||||
|
||||
SELECT datname,
|
||||
usename,
|
||||
count(*)
|
||||
FROM pg_stat_activity
|
||||
GROUP BY 1, 2
|
||||
ORDER BY 1, 2;
|
||||
|
||||
SELECT usename,
|
||||
datname,
|
||||
count(*)
|
||||
FROM pg_stat_activity
|
||||
GROUP BY 1, 2
|
||||
ORDER BY 1, 2;
|
||||
@@ -0,0 +1,18 @@
|
||||
SELECT database,
|
||||
relation,
|
||||
n.nspname,
|
||||
c.relname,
|
||||
pid,
|
||||
a.usename,
|
||||
locktype,
|
||||
mode,
|
||||
granted,
|
||||
tuple
|
||||
FROM pg_locks l
|
||||
JOIN pg_class c ON (c.oid = l.relation)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_stat_activity a ON (a.procpid = l.pid)
|
||||
ORDER BY database,
|
||||
relation,
|
||||
pid;
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
SELECT a.datname,
|
||||
a.procpid as pid,
|
||||
CASE WHEN a.client_addr IS NULL
|
||||
THEN 'local'
|
||||
ELSE a.client_addr::text
|
||||
END as client_addr,
|
||||
a.usename as user,
|
||||
a.waiting,
|
||||
l.procpid as blocking_pid,
|
||||
l.usename as blicking_user,
|
||||
a.current_query,
|
||||
a.query_start,
|
||||
current_timestamp - a.query_start as duration
|
||||
FROM pg_stat_activity a
|
||||
LEFT JOIN pg_locks l1 ON (a.procpid = l1.pid )
|
||||
LEFT JOIN pg_locks l2 on (l1.relation = l2.relation )
|
||||
LEFT JOIN pg_stat_activity l ON (l2.pid = l.procpid)
|
||||
LEFT JOIN pg_stat_user_tables t ON (l1.relation = t.relid)
|
||||
WHERE pg_backend_pid() <> a.procpid
|
||||
ORDER BY a.datname,
|
||||
a.query_start;
|
||||
|
||||
|
||||
SELECT
|
||||
w.current_query as waiting_query,
|
||||
w.procpid as w_pid,
|
||||
w.usename as w_user,
|
||||
l.current_query as locking_query,
|
||||
l.procpid as l_pid,
|
||||
l.usename as l_user,
|
||||
t.schemaname || '.' || t.relname as tablename
|
||||
FROM pg_stat_activity w
|
||||
JOIN pg_locks l1 ON (w.procpid = l1.pid and not l1.granted)
|
||||
JOIN pg_locks l2 on (l1.relation = l2.relation and l2.granted)
|
||||
JOIN pg_stat_activity l ON (l2.pid = l.procpid)
|
||||
JOIN pg_stat_user_tables t ON (l1.relation = t.relid)
|
||||
WHERE w.waiting;
|
||||
SELECT datname,
|
||||
procpid as pid,
|
||||
client_addr,
|
||||
usename as user,
|
||||
current_query,
|
||||
CASE WHEN waiting = TRUE
|
||||
THEN 'BLOCKED'
|
||||
ELSE 'no'
|
||||
END as waiting,
|
||||
query_start,
|
||||
current_timestamp - query_start as duration
|
||||
FROM pg_stat_activity
|
||||
WHERE pg_backend_pid() <> procpid
|
||||
ORDER BY datname,
|
||||
query_start;
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
/* Use for PostgreSQL 9.2 or greater */
|
||||
SELECT c.datname,
|
||||
c.pid as pid,
|
||||
c.client_addr,
|
||||
c.usename as user,
|
||||
c.query,
|
||||
CASE WHEN c.waiting = TRUE
|
||||
THEN 'BLOCKED'
|
||||
ELSE 'no'
|
||||
END as waiting,
|
||||
l.pid as blocked_by,
|
||||
current_timestamp - c.query_start as duration
|
||||
FROM pg_stat_activity c
|
||||
LEFT JOIN pg_locks l1 ON (c.pid = l1.pid and not l1.granted)
|
||||
LEFT JOIN pg_locks l2 on (l1.relation = l2.relation and l2.granted)
|
||||
LEFT JOIN pg_stat_activity l ON (l2.pid = l.pid)
|
||||
LEFT JOIN pg_stat_user_tables t ON (l1.relation = t.relid)
|
||||
WHERE pg_backend_pid() <> c.pid
|
||||
ORDER BY datname,
|
||||
query_start;
|
||||
|
||||
/* Use for PostgreSQL 9.1 or less */
|
||||
/*
|
||||
SELECT
|
||||
w.current_query as waiting_query,
|
||||
w.procpid as w_pid,
|
||||
w.usename as w_user,
|
||||
l.current_query as locking_query,
|
||||
l.procpid as l_pid,
|
||||
l.usename as l_user,
|
||||
t.schemaname || '.' || t.relname as tablename
|
||||
FROM pg_stat_activity w
|
||||
JOIN pg_locks l1 ON (w.procpid = l1.pid and not l1.granted)
|
||||
JOIN pg_locks l2 on (l1.relation = l2.relation and l2.granted)
|
||||
JOIN pg_stat_activity l ON (l2.pid = l.procpid)
|
||||
JOIN pg_stat_user_tables t ON (l1.relation = t.relid)
|
||||
WHERE w.waiting;
|
||||
*/
|
||||
@@ -0,0 +1,13 @@
|
||||
SELECT db.datname,
|
||||
au.rolname as datdba,
|
||||
pg_encoding_to_char(db.encoding) as encoding,
|
||||
db.datallowconn,
|
||||
db.datconnlimit,
|
||||
db.datfrozenxid,
|
||||
tb.spcname as tblspc,
|
||||
-- db.datconfig,
|
||||
db.datacl
|
||||
FROM pg_database db
|
||||
JOIN pg_authid au ON au.oid = db.datdba
|
||||
JOIN pg_tablespace tb ON tb.oid = db.dattablespace
|
||||
ORDER BY 1;
|
||||
@@ -0,0 +1,9 @@
|
||||
SELECT datname,
|
||||
pg_size_pretty(pg_database_size(datname))as size_pretty,
|
||||
pg_database_size(datname) as size,
|
||||
(SELECT pg_size_pretty (SUM( pg_database_size(datname))::bigint)
|
||||
FROM pg_database) AS total,
|
||||
((pg_database_size(datname) / (SELECT SUM( pg_database_size(datname))
|
||||
FROM pg_database) ) * 100)::numeric(6,3) AS pct
|
||||
FROM pg_database
|
||||
ORDER BY datname;
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
SELECT DISTINCT t.typname
|
||||
FROM pg_class c
|
||||
JOIN pg_attribute a ON ( a.attrelid = c.oid )
|
||||
JOIN pg_type t ON (t.oid = a.atttypid)
|
||||
JOIN pg_catalog.pg_namespace s ON (c.relnamespace = s.oid)
|
||||
WHERE c.relname NOT LIKE 'pg_%'
|
||||
AND c.relname NOT LIKE 'information%'
|
||||
AND c.relname NOT LIKE 'sql_%'
|
||||
AND c.relkind = 'r'
|
||||
ORDER BY t.typname;
|
||||
@@ -0,0 +1,18 @@
|
||||
SELECT t.typname,
|
||||
e.enumlabel,
|
||||
e.enumsortorder,
|
||||
e.enumtypid
|
||||
FROM pg_type t
|
||||
JOIN pg_enum e ON e.enumtypid = t.oid
|
||||
WHERE t.typtype = 'e'
|
||||
ORDER BY 1, enumsortorder;
|
||||
|
||||
--DELETE FROM pg_enum
|
||||
--WHERE enumtypid = 9999999
|
||||
-- AND enumsortorder = 4;
|
||||
|
||||
--DELETE FROM pg_enum
|
||||
--WHERE enumtypid = 9999999
|
||||
-- AND enumlabel = 'medium';
|
||||
|
||||
--ALTER TYPE some_enum ADD VALUE 'medium' AFTER 'small';
|
||||
@@ -0,0 +1,11 @@
|
||||
SELECT n.nspname as schema,
|
||||
t.typname as enum,
|
||||
e.enumsortorder as order,
|
||||
e.enumlabel as value,
|
||||
t.oid as oid
|
||||
FROM pg_type t
|
||||
JOIN pg_namespace n ON ( n.oid = t.typnamespace )
|
||||
JOIN pg_enum e ON ( e.enumtypid = t.oid )
|
||||
WHERE typtype = 'e'
|
||||
ORDER BY 1, 2, 3;
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
SELECT (SELECT oid
|
||||
FROM pg_database
|
||||
WHERE datname = current_database()) AS database,
|
||||
c.relfilenode as filename,
|
||||
n.nspname as schema,
|
||||
c.relname as table
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON ( n.oid = c.relnamespace )
|
||||
WHERE relname NOT LIKE 'pg_%' AND
|
||||
relname NOT LIKE 'information%' AND
|
||||
relname NOT LIKE 'sql_%' AND
|
||||
relkind = 'r'
|
||||
ORDER BY relfilenode;
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
SELECT pg_get_indexdef(idx.indexrelid) || ';'
|
||||
FROM pg_stat_all_indexes i
|
||||
JOIN pg_class c ON (c.oid = i.relid)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_index idx ON (idx.indexrelid = i.indexrelid )
|
||||
WHERE NOT idx.indisprimary
|
||||
AND NOT idx.indisunique
|
||||
AND i.relname NOT LIKE 'pg_%'
|
||||
AND i.idx_scan = 0
|
||||
ORDER BY n.nspname,
|
||||
i.relname;
|
||||
@@ -0,0 +1,8 @@
|
||||
SELECT 'DROP INDEX IF EXISTS "' || n.nspname || '"' || '.' || '"' || i.indexrelname || '"' ||';'
|
||||
FROM pg_stat_all_indexes i
|
||||
JOIN pg_class c ON (c.oid = i.relid)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_index idx ON (idx.indexrelid = i.indexrelid )
|
||||
WHERE NOT idx.indisprimary
|
||||
AND i.relname NOT LIKE 'pg_%'
|
||||
ORDER BY i.indexrelname;
|
||||
@@ -0,0 +1,4 @@
|
||||
SELECT 'SELECT ''' || relname || ''' ,
|
||||
(SELECT max_value FROM ' || relname || ');'
|
||||
FROM pg_class
|
||||
WHERE relkind = 'S';
|
||||
@@ -0,0 +1,7 @@
|
||||
SELECT 'REVOKE ALL PRIVILEGES FROM TABLE public."' || relname || '" FROM <some_user>;'
|
||||
FROM pg_class
|
||||
WHERE relname NOT LIKE 'pg_%' AND
|
||||
relname NOT LIKE 'information%' AND
|
||||
relname NOT LIKE 'sql_%' AND
|
||||
relkind = 'r'
|
||||
ORDER BY relname;
|
||||
@@ -0,0 +1,10 @@
|
||||
SELECT 'SELECT ''' ||
|
||||
c.relname || ''' as table, ' ||
|
||||
c.reltuples::int4 || ' as rows, ' ||
|
||||
'(SELECT COUNT(*) FROM ' || s.nspname || '."' || c.relname || '") as cnt;'
|
||||
FROM pg_catalog.pg_class c
|
||||
JOIN pg_catalog.pg_namespace s ON (c.relnamespace = s.oid)
|
||||
WHERE nspname = 'public'
|
||||
AND relkind = 'r'
|
||||
ORDER BY c.reltuples::int4 DESC;
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
SELECT nspname AS "Schema Name"
|
||||
FROM pg_namespace nsp
|
||||
JOIN pg_proc pro ON pronamespace=nsp.oid AND proname = 'slonyversion'
|
||||
ORDER BY nspname;
|
||||
@@ -0,0 +1,27 @@
|
||||
BEGIN;
|
||||
|
||||
DROP TABLE IF EXISTS tmp_trans_stats;
|
||||
|
||||
CREATE TEMP TABLE tmp_trans_stats AS
|
||||
SELECT 'start_cnt'::varchar(10) AS taken,
|
||||
SUM(xact_commit + xact_rollback) AS cnt
|
||||
FROM pg_stat_database;
|
||||
|
||||
COMMIT;
|
||||
|
||||
SELECT pg_sleep(60);
|
||||
|
||||
INSERT INTO tmp_trans_stats
|
||||
SELECT 'end_cnt'::varchar(10) AS taken,
|
||||
SUM(xact_commit + xact_rollback) AS cnt
|
||||
FROM pg_stat_database;
|
||||
|
||||
SELECT (
|
||||
(SELECT cnt
|
||||
FROM tmp_trans_stats
|
||||
WHERE taken = 'end_cnt')
|
||||
- (SELECT cnt
|
||||
FROM tmp_trans_stats
|
||||
WHERE taken = 'start_cnt')
|
||||
) as tot_trans;
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
SELECT g.rolname as group,
|
||||
u.rolname as user,
|
||||
r.admin_option as admin,
|
||||
g.rolsuper as g_super,
|
||||
u.rolsuper as u_super
|
||||
FROM pg_auth_members r
|
||||
JOIN pg_authid g ON (r.roleid = g.oid)
|
||||
JOIN pg_authid u ON (r.member = u.oid)
|
||||
WHERE u.rolsuper = TRUE
|
||||
OR g.rolsuper = TRUE
|
||||
ORDER BY 1, 2;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
SELECT schemaname as schema,
|
||||
relname as table,
|
||||
idx_tup_fetch + seq_tup_read as TotalReads
|
||||
FROM pg_stat_all_tables
|
||||
WHERE idx_tup_fetch + seq_tup_read = 0
|
||||
AND schemaname NOT IN ('pg_catalog', 'pg_toast' )
|
||||
order by schemaname,
|
||||
relname;
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
SELECT N.nspname,
|
||||
T.relname AS "table",
|
||||
C.relname AS idx_name,
|
||||
round(100 * pg_relation_size(indexrelid) / pg_relation_size(indrelid) ) / 100 AS "index_ratio %",
|
||||
pg_size_pretty(pg_relation_size(indexrelid) ) AS index_size,
|
||||
pg_size_pretty(pg_relation_size(indrelid) ) AS table_size_size
|
||||
FROM pg_index I
|
||||
LEFT JOIN pg_class C ON (c.oid = I.indexrelid)
|
||||
LEFT JOIN pg_class T ON (T.oid = I.indrelid)
|
||||
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
|
||||
WHERE nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
|
||||
AND c.relkind = 'i'
|
||||
AND pg_relation_size(indrelid) > 0
|
||||
ORDER BY 4 desc, 1;
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
WITH btree_index_atts AS (
|
||||
SELECT nspname,
|
||||
relname,
|
||||
reltuples,
|
||||
relpages,
|
||||
indrelid,
|
||||
relam,
|
||||
regexp_split_to_table(indkey::text, ' ')::smallint AS attnum,
|
||||
indexrelid as index_oid
|
||||
FROM pg_index
|
||||
JOIN pg_class ON pg_class.oid=pg_index.indexrelid
|
||||
JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
|
||||
JOIN pg_am ON pg_class.relam = pg_am.oid
|
||||
WHERE pg_am.amname = 'btree'
|
||||
),
|
||||
index_item_sizes AS (
|
||||
SELECT i.nspname,
|
||||
i.relname,
|
||||
i.reltuples,
|
||||
i.relpages,
|
||||
i.relam,
|
||||
s.starelid,
|
||||
a.attrelid AS table_oid,
|
||||
index_oid,
|
||||
current_setting('block_size')::numeric AS bs,
|
||||
/* MAXALIGN: 4 on 32bits, 8 on 64bits (and mingw32 ?) */
|
||||
CASE WHEN version() ~ 'mingw32' OR version() ~ '64-bit'
|
||||
THEN 8
|
||||
ELSE 4
|
||||
END AS maxalign,
|
||||
24 AS pagehdr,
|
||||
/* per tuple header: add index_attribute_bm if some cols are null-able */
|
||||
CASE WHEN max(coalesce(s.stanullfrac,0)) = 0
|
||||
THEN 2
|
||||
ELSE 6
|
||||
END AS index_tuple_hdr,
|
||||
/* data len: we remove null values save space using it fractionnal part from stats */
|
||||
sum( (1-coalesce(s.stanullfrac, 0)) * coalesce(s.stawidth, 2048) ) AS nulldatawidth
|
||||
FROM pg_attribute AS a
|
||||
JOIN pg_statistic AS s ON s.starelid=a.attrelid AND s.staattnum = a.attnum
|
||||
JOIN btree_index_atts AS i ON i.indrelid = a.attrelid AND a.attnum = i.attnum
|
||||
WHERE a.attnum > 0
|
||||
GROUP BY 1, 2, 3, 4, 5, 6, 7, 8, 9
|
||||
),
|
||||
index_aligned AS (
|
||||
SELECT maxalign,
|
||||
bs,
|
||||
nspname,
|
||||
relname AS index_name,
|
||||
reltuples,
|
||||
relpages,
|
||||
relam,
|
||||
table_oid,
|
||||
index_oid,
|
||||
( 2 + maxalign -
|
||||
/* Add padding to the index tuple header to align on MAXALIGN */
|
||||
CASE WHEN index_tuple_hdr%maxalign = 0
|
||||
THEN maxalign
|
||||
ELSE index_tuple_hdr%maxalign
|
||||
END
|
||||
+ nulldatawidth + maxalign -
|
||||
/* Add padding to the data to align on MAXALIGN */
|
||||
CASE WHEN nulldatawidth::integer%maxalign = 0
|
||||
THEN maxalign
|
||||
ELSE nulldatawidth::integer%maxalign
|
||||
END
|
||||
)::numeric AS nulldatahdrwidth,
|
||||
pagehdr
|
||||
FROM index_item_sizes AS s1
|
||||
),
|
||||
otta_calc AS
|
||||
(
|
||||
SELECT bs,
|
||||
nspname,
|
||||
table_oid,
|
||||
index_oid,
|
||||
index_name,
|
||||
relpages,
|
||||
coalesce( ceil((reltuples*(4+nulldatahdrwidth))/(bs-pagehdr::float)) +
|
||||
CASE WHEN am.amname IN ('hash','btree')
|
||||
THEN 1
|
||||
ELSE 0
|
||||
END ,
|
||||
0 -- btree and hash have a metadata reserved block
|
||||
) AS otta
|
||||
FROM index_aligned AS s2
|
||||
LEFT JOIN pg_am am ON s2.relam = am.oid
|
||||
),
|
||||
raw_bloat AS
|
||||
(
|
||||
SELECT current_database() as dbname,
|
||||
nspname, c.relname AS table_name,
|
||||
index_name,
|
||||
bs*(sub.relpages)::bigint AS totalbytes,
|
||||
CASE WHEN sub.relpages <= otta
|
||||
THEN 0
|
||||
ELSE bs*(sub.relpages-otta)::bigint
|
||||
END AS wastedbytes,
|
||||
CASE WHEN sub.relpages <= otta
|
||||
THEN 0
|
||||
ELSE bs*(sub.relpages-otta)::bigint * 100 / (bs*(sub.relpages)::bigint)
|
||||
END AS realbloat,
|
||||
pg_relation_size(sub.table_oid) as table_bytes,
|
||||
stat.idx_scan as index_scans
|
||||
FROM otta_calc AS sub
|
||||
JOIN pg_class AS c ON c.oid=sub.table_oid
|
||||
JOIN pg_stat_user_indexes AS stat ON sub.index_oid = stat.indexrelid
|
||||
)
|
||||
SELECT dbname as database_name,
|
||||
nspname as schema_name,
|
||||
table_name,
|
||||
index_name,
|
||||
round(realbloat, 1) as bloat_pct,
|
||||
wastedbytes as bloat_bytes,
|
||||
pg_size_pretty(wastedbytes::bigint) as bloat_size,
|
||||
totalbytes as index_bytes,
|
||||
pg_size_pretty(totalbytes::bigint) as index_size,
|
||||
table_bytes,
|
||||
pg_size_pretty(table_bytes) as table_size,
|
||||
index_scans
|
||||
FROM raw_bloat
|
||||
WHERE ( realbloat > 50 and wastedbytes > 50000000 )
|
||||
ORDER BY wastedbytes DESC;
|
||||
@@ -0,0 +1,14 @@
|
||||
SELECT n.nspname as schema,
|
||||
c.relname,
|
||||
d.description as comment
|
||||
FROM pg_class c
|
||||
LEFT JOIN pg_description d ON (d.objoid = c.oid)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE n.nspname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
-- AND relname LIKE '%IDX%'
|
||||
AND relkind = 'i'
|
||||
AND d.description IS NOT NULL
|
||||
ORDER BY 2;
|
||||
@@ -0,0 +1,21 @@
|
||||
SELECT n.nspname as schema,
|
||||
c.relname as index,
|
||||
a.rolname as owner,
|
||||
c.relfilenode as filename,
|
||||
CASE WHEN indisprimary
|
||||
THEN 'pkey'
|
||||
WHEN indisunique
|
||||
THEN 'uidx'
|
||||
ELSE'idx'
|
||||
END as type,
|
||||
pg_size_pretty(pg_total_relation_size(n.nspname|| '.' || c.relname)) as total_size,
|
||||
pg_total_relation_size(n.nspname|| '.' || c.relname) as total_size_bytes
|
||||
FROM pg_index i
|
||||
JOIN pg_class c ON (c.oid = i.indexrelid)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_authid a ON ( a.oid = c.relowner )
|
||||
WHERE relname NOT LIKE 'pg_%' AND
|
||||
relname NOT LIKE 'information%' AND
|
||||
relname NOT LIKE 'sql_%' AND
|
||||
relkind IN ('i')
|
||||
ORDER BY 7 DESC, 1, 2;
|
||||
@@ -0,0 +1,20 @@
|
||||
SELECT n.nspname as schema,
|
||||
c.relname as table,
|
||||
c.reltablespace,
|
||||
CASE WHEN c.reltablespace > 0 THEN (SELECT t.spcname
|
||||
FROM pg_tablespace
|
||||
WHERE oid = c.reltablespace)
|
||||
ELSE '' END
|
||||
FROM pg_class c
|
||||
JOIN pg_authid a ON ( a.OID = c.relowner )
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_tablespace t ON ( CASE WHEN c.reltablespace > 0 THEN t.oid = c.reltablespace
|
||||
ELSE NULL END)
|
||||
WHERE c.relname NOT LIKE 'pg_%'
|
||||
AND n.nspname NOT LIKE 'information%'
|
||||
AND n.nspname NOT LIKE 'sql_%'
|
||||
AND c.relkind = 'i'
|
||||
-- AND n.nspname = '<target_schema>'
|
||||
-- AND c.relname = '<table_name>'
|
||||
ORDER BY n.nspname, c.relname;
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
SELECT 'ALTER INDEX ' || i.indexrelname
|
||||
|| ' SET TABLESPACE <new_table_space>;'
|
||||
FROM pg_stat_all_indexes i
|
||||
JOIN pg_class c ON (c.oid = i.relid)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_index idx ON (idx.indexrelid = i.indexrelid )
|
||||
WHERE n.nspname = 'public'
|
||||
ORDER BY 1;
|
||||
@@ -0,0 +1,9 @@
|
||||
SELECT 'ALTER TABLE ' || c.relname || ' SET TABLESPACE <new_table_space>;'
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND n.nspname = 'public'
|
||||
AND relkind = 'r'
|
||||
ORDER BY relname;
|
||||
@@ -0,0 +1,8 @@
|
||||
SELECT schemaname,
|
||||
relname,
|
||||
idx_tup_fetch + seq_tup_read as TotalReads
|
||||
FROM pg_stat_all_tables
|
||||
WHERE idx_tup_fetch + seq_tup_read != 0
|
||||
AND schemaname NOT IN ( 'pg_catalog', 'pg_toast' )
|
||||
order by TotalReads desc
|
||||
LIMIT 10;
|
||||
@@ -0,0 +1,16 @@
|
||||
SELECT DISTINCT n1.nspname || '.' || p.relname AS parent, n2.nspname || '.' || c.relname AS child
|
||||
-- , cstr.conname
|
||||
FROM pg_class c
|
||||
JOIN pg_constraint cstr ON (cstr.conrelid = c.oid AND c.relkind = 'r' AND cstr.contype = 'f')
|
||||
JOIN pg_namespace n2 ON (n2.oid = c.relnamespace)
|
||||
JOIN pg_class p ON (p.oid = cstr.confrelid)
|
||||
JOIN pg_namespace n1 ON (n1.oid = p.relnamespace)
|
||||
WHERE p.relkind = 'r'
|
||||
-- AND n1.nspname = '<schema_name>'
|
||||
-- AND p.relname = '<paren_table>'
|
||||
AND p.relname NOT LIKE 'pg_%'
|
||||
AND p.relname NOT LIKE 'sql_%'
|
||||
AND p.oid IN (SELECT confrelid
|
||||
FROM pg_constraint)
|
||||
ORDER BY parent, child;
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
SELECT pg_postmaster_start_time() as pg_start,
|
||||
current_timestamp - pg_postmaster_start_time() as runtime;
|
||||
@@ -0,0 +1,39 @@
|
||||
SELECT n.nspname as schema,
|
||||
i.relname as table,
|
||||
i.indexrelname as index,
|
||||
i.idx_scan,
|
||||
i.idx_tup_read,
|
||||
i.idx_tup_fetch,
|
||||
CASE WHEN idx.indisprimary
|
||||
THEN 'pkey'
|
||||
WHEN idx.indisunique
|
||||
THEN 'uidx'
|
||||
ELSE 'idx'
|
||||
END AS type,
|
||||
/*
|
||||
pg_get_indexdef(idx.indexrelid, 1, FALSE) as idxcol1,
|
||||
pg_get_indexdef(idx.indexrelid, 2, FALSE) as idxcol2,
|
||||
pg_get_indexdef(idx.indexrelid, 3, FALSE) as idxcol3,
|
||||
pg_get_indexdef(idx.indexrelid, 4, FALSE) as idxcol4,
|
||||
|
||||
*/
|
||||
CASE WHEN idx.indisvalid
|
||||
THEN 'valid'
|
||||
ELSE 'INVALID'
|
||||
END as statusi,
|
||||
pg_relation_size( quote_ident(n.nspname) || '.' || quote_ident(i.relname) ) as size_in_bytes,
|
||||
pg_size_pretty(pg_relation_size(quote_ident(n.nspname) || '.' || quote_ident(i.relname) )) as size
|
||||
FROM pg_stat_all_indexes i
|
||||
JOIN pg_class c ON (c.oid = i.relid)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_index idx ON (idx.indexrelid = i.indexrelid )
|
||||
WHERE i.relname LIKE '%%'
|
||||
AND n.nspname NOT LIKE 'pg_%'
|
||||
-- AND idx.indisunique = TRUE
|
||||
-- AND NOT idx.indisprimary
|
||||
-- AND i.indexrelname LIKE 'tmp%'
|
||||
-- AND idx.indisvalid IS false
|
||||
/* AND NOT idx.indisprimary
|
||||
AND NOT idx.indisunique
|
||||
AND idx_scan = 0
|
||||
*/ ORDER BY 1, 2, 3;
|
||||
@@ -0,0 +1,29 @@
|
||||
SELECT n.nspname,
|
||||
s.relname,
|
||||
c.reltuples::bigint,
|
||||
-- n_live_tup,
|
||||
n_tup_ins,
|
||||
n_tup_upd,
|
||||
n_tup_del,
|
||||
date_trunc('second', last_vacuum) as last_vacuum,
|
||||
date_trunc('second', last_autovacuum) as last_autovacuum,
|
||||
date_trunc('second', last_analyze) as last_analyze,
|
||||
date_trunc('second', last_autoanalyze) as last_autoanalyze
|
||||
,
|
||||
round( current_setting('autovacuum_vacuum_threshold')::integer + current_setting('autovacuum_vacuum_scale_factor')::numeric * C.reltuples) AS av_threshold
|
||||
/* ,CASE WHEN reltuples > 0
|
||||
THEN round(100.0 * n_dead_tup / (reltuples))
|
||||
ELSE 0
|
||||
END AS pct_dead,
|
||||
CASE WHEN n_dead_tup > round( current_setting('autovacuum_vacuum_threshold')::integer + current_setting('autovacuum_vacuum_scale_factor')::numeric * C.reltuples)
|
||||
THEN 'VACUUM'
|
||||
ELSE 'ok'
|
||||
END AS "av_needed"
|
||||
*/
|
||||
FROM pg_stat_all_tables s
|
||||
JOIN pg_class c ON c.oid = s.relid
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE s.relname NOT LIKE 'pg_%'
|
||||
AND s.relname NOT LIKE 'sql_%'
|
||||
-- AND s.relname LIKE '%TBL%'
|
||||
ORDER by 1, 2;
|
||||
@@ -0,0 +1,12 @@
|
||||
SELECT a.rolname as role,
|
||||
u.rolname as member,
|
||||
CASE
|
||||
WHEN m.admin_option THEN
|
||||
'YES'
|
||||
ELSE
|
||||
'no'
|
||||
END as admin
|
||||
FROM pg_auth_members m
|
||||
JOIN pg_authid a ON (a.oid = m.roleid)
|
||||
JOIN pg_authid u ON (u.oid = m.member)
|
||||
ORDER BY 1, 2
|
||||
@@ -0,0 +1,10 @@
|
||||
SELECT n.nspname,
|
||||
c.relname,
|
||||
r.rulename
|
||||
FROM pg_rewrite r
|
||||
JOIN pg_class c ON (c.oid = ev_class)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE rulename <> '_RETURN'
|
||||
-- AND n.nspname <> 'inv'
|
||||
ORDER BY 1, 2, 3;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
SELECT table_schema as schema,
|
||||
table_name as table,
|
||||
column_name as column,
|
||||
column_default,
|
||||
data_type
|
||||
FROM information_schema.columns
|
||||
WHERE column_default LIKE 'nextval%'
|
||||
ORDER BY 1, 2, 3;
|
||||
|
||||
SELECT tbl.relname,
|
||||
col.attname,
|
||||
col.attnum
|
||||
FROM pg_attrdef def
|
||||
JOIN pg_class tbl ON tbl.oid = def.adrelid
|
||||
JOIN pg_attribute col ON col.attrelid = tbl.oid
|
||||
WHERE def.adsrc LIKE 'nextval%'
|
||||
AND col.attnum = def.adnum
|
||||
ORDER BY 1;
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
SELECT *,
|
||||
n_dead_tup > av_threshold AS "av_needed",
|
||||
CASE WHEN reltuples > 0
|
||||
THEN round(100.0 * n_dead_tup / (reltuples))
|
||||
ELSE 0
|
||||
END AS pct_dead
|
||||
FROM (SELECT
|
||||
N.nspname || '.' || C.relname AS "relation",
|
||||
pg_stat_get_tuples_inserted(C.oid) AS n_tup_ins,
|
||||
pg_stat_get_tuples_updated(C.oid) AS n_tup_upd,
|
||||
pg_stat_get_tuples_deleted(C.oid) AS n_tup_del,
|
||||
CASE WHEN pg_stat_get_tuples_updated(C.oid) > 0
|
||||
THEN pg_stat_get_tuples_hot_updated(C.oid)::real / pg_stat_get_tuples_updated(C.oid)
|
||||
ELSE 0
|
||||
END AS HOT_update_ratio,
|
||||
pg_stat_get_live_tuples(C.oid) AS n_live_tup,
|
||||
pg_stat_get_dead_tuples(C.oid) AS n_dead_tup,
|
||||
C.reltuples AS reltuples,
|
||||
round( current_setting('autovacuum_vacuum_threshold')::integer + current_setting('autovacuum_vacuum_scale_factor')::numeric * C.reltuples) AS av_threshold,
|
||||
date_trunc('minute',greatest(pg_stat_get_last_vacuum_time(C.oid),pg_stat_get_last_autovacuum_time(C.oid))) AS last_vacuum,
|
||||
date_trunc('minute',greatest(pg_stat_get_last_analyze_time(C.oid),pg_stat_get_last_analyze_time(C.oid))) AS last_analyze
|
||||
FROM pg_class C
|
||||
LEFT JOIN pg_index I ON C.oid = I.indrelid
|
||||
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
|
||||
WHERE C.relkind IN ('r', 't')
|
||||
AND N.nspname NOT IN ('pg_catalog', 'information_schema')
|
||||
AND N.nspname !~ '^pg_toast'
|
||||
) AS av
|
||||
ORDER BY av_needed DESC,
|
||||
n_dead_tup DESC;
|
||||
@@ -0,0 +1,16 @@
|
||||
--r=SELECT
|
||||
--r=SELECT
|
||||
--a=INSERT
|
||||
--w=UPDATE
|
||||
--d=DELETE
|
||||
--x=REFERENCES
|
||||
|
||||
SELECT n.nspname as schema,
|
||||
c.relname,
|
||||
array_to_string(c.relacl, ' ')
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE c.relkind = 'r'
|
||||
AND n.nspname = 'public'
|
||||
ORDER BY c.relname;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
-- Returns all columns for a specified table
|
||||
SELECT c.relname as table,
|
||||
a.attname as column
|
||||
FROM pg_class c
|
||||
JOIN pg_attribute a ON ( a.attrelid = c.oid )
|
||||
WHERE relname = '<table>'
|
||||
AND attnum > 0
|
||||
AND NOT attisdropped
|
||||
ORDER BY attnum;
|
||||
@@ -0,0 +1,40 @@
|
||||
SELECT DISTINCT ON (c.relname)
|
||||
n.nspname as schema,
|
||||
c.relname,
|
||||
a.rolname as owner,
|
||||
0 as col_seq,
|
||||
'' as column,
|
||||
d.description as comment
|
||||
FROM pg_class c
|
||||
LEFT JOIN pg_attribute col ON (col.attrelid = c.oid)
|
||||
LEFT JOIN pg_description d ON (d.objoid = col.attrelid AND d.objsubid = 0)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_authid a ON ( a.OID = c.relowner )
|
||||
WHERE n.nspname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
-- AND relname LIKE '<TABLE>'
|
||||
AND relkind = 'r'
|
||||
-- AND d.description IS NOT NULL
|
||||
UNION
|
||||
SELECT n.nspname as schema,
|
||||
c.relname,
|
||||
'' as owner,
|
||||
col.attnum as col_seq,
|
||||
col.attname as column,
|
||||
d.description
|
||||
FROM pg_class c
|
||||
JOIN pg_attribute col ON (col.attrelid = c.oid)
|
||||
LEFT JOIN pg_description d ON (d.objoid = col.attrelid AND d.objsubid = col.attnum)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_authid a ON ( a.OID = c.relowner )
|
||||
WHERE n.nspname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
-- AND relname LIKE '<TABLE>'
|
||||
AND relkind = 'r'
|
||||
-- AND d.description IS NOT NULL
|
||||
AND col.attnum >= 0
|
||||
ORDER BY 1, 2, 4;
|
||||
@@ -0,0 +1,10 @@
|
||||
SELECT rel.relname,
|
||||
con.conname,
|
||||
con.contype,
|
||||
con.consrc
|
||||
FROM pg_class rel
|
||||
JOIN pg_constraint con ON (con.conrelid = rel.oid)
|
||||
--WHERE contype = 'f'
|
||||
ORDER by relname,
|
||||
contype,
|
||||
conname;
|
||||
@@ -0,0 +1,11 @@
|
||||
SELECT n.nspname as schema,
|
||||
pa.relname as parent,
|
||||
ch.relname as dependent,
|
||||
cn.conname as constraint_name
|
||||
FROM pg_constraint cn
|
||||
JOIN pg_class pa ON (pa.oid = cn.confrelid)
|
||||
JOIN pg_class ch ON (ch.oid = cn.conrelid)
|
||||
JOIN pg_namespace n ON (n.oid = pa.relnamespace)
|
||||
WHERE pa.relname LIKE '%%'
|
||||
AND contype = 'f'
|
||||
ORDER BY 1, 2, 3;
|
||||
@@ -0,0 +1,7 @@
|
||||
SELECT table_schema,
|
||||
table_name,
|
||||
grantee,
|
||||
privilege_type
|
||||
FROM information_schema.role_table_grants
|
||||
WHERE table_schema NOT IN ('information_schema', 'pg_catalog')
|
||||
ORDER BY table_schema, table_name, grantee;
|
||||
@@ -0,0 +1,13 @@
|
||||
SELECT p.proname,
|
||||
CASE WHEN p.proretset = TRUE
|
||||
THEN 'SET OF '
|
||||
ELSE ''
|
||||
END ||
|
||||
UPPER( (SELECT typname
|
||||
FROM pg_type
|
||||
WHERE oid = p.prorettype) ) AS return_type,
|
||||
proargnames as parameters
|
||||
FROM pg_proc p
|
||||
WHERE prosrc ILIKE ('%<TABLE/COLUMN>%')
|
||||
AND proname NOT LIKE 'pg%'
|
||||
ORDER BY p.proname;
|
||||
@@ -0,0 +1,12 @@
|
||||
SELECT n.nspname,
|
||||
c.relname,
|
||||
pg_size_pretty (pg_total_relation_size(c.oid)) as size
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE c.relname NOT LIKE 'pg_%'
|
||||
AND c.relname NOT LIKE 'information%'
|
||||
AND c.relname NOT LIKE 'sql_%'
|
||||
AND c.relkind = 'r'
|
||||
AND n.nspname = '<schema_name>'
|
||||
ORDER BY n.nspname;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
SELECT c.relname as table,
|
||||
t.tgname as tg_name,
|
||||
p.proname as tg_function
|
||||
FROM pg_proc p
|
||||
JOIN pg_trigger t ON (t.tgfoid = p.oid)
|
||||
JOIN pg_class c ON (c.oid = t.tgrelid)
|
||||
WHERE prosrc LIKE ('%<table>%')
|
||||
ORDER BY c.relname;
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
SELECT cc.relname as table,
|
||||
ci.relname as index
|
||||
FROM pg_index i
|
||||
JOIN pg_class cc ON (cc.oid = i.indrelid)
|
||||
JOIN pg_class ci ON (ci.oid = i.indexrelid)
|
||||
WHERE ci.relname = 'idx_normal_distro_name'
|
||||
ORDER BY 1;
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
SELECT s.nspname, c.relname as table, c.reltuples::int4 as rows
|
||||
FROM pg_catalog.pg_class c
|
||||
JOIN pg_catalog.pg_namespace s ON (c.relnamespace = s.oid)
|
||||
WHERE relkind = 'r'
|
||||
AND c.reltuples::int4 > 0
|
||||
ORDER BY rows DESC;
|
||||
@@ -0,0 +1,22 @@
|
||||
SELECT n.nspname as schema,
|
||||
c.relname as table,
|
||||
a.rolname as owner,
|
||||
c.relfilenode as filename,
|
||||
c.reltuples,
|
||||
pg_size_pretty(pg_relation_size(quote_ident(n.nspname) || '.' || quote_ident(c.relname) )) as size,
|
||||
pg_size_pretty(pg_total_relation_size(quote_ident(n.nspname) || '.' || quote_ident(c.relname) )) as total_size,
|
||||
pg_relation_size(quote_ident(n.nspname) || '.' || quote_ident(c.relname) ) as size_bytes,
|
||||
pg_total_relation_size(quote_ident(n.nspname) || '.' || quote_ident(c.relname) ) as total_size_bytes,
|
||||
CASE WHEN c.reltablespace = 0
|
||||
THEN 'pg_default'
|
||||
ELSE (SELECT t.spcname
|
||||
FROM pg_tablespace t WHERE (t.oid = c.reltablespace) )
|
||||
END as tablespace
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_authid a ON ( a.oid = c.relowner )
|
||||
WHERE relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND relkind IN ('r')
|
||||
ORDER BY 7 DESC, 1, 2;
|
||||
@@ -0,0 +1,10 @@
|
||||
SELECT n.nspname,
|
||||
c.relname,
|
||||
pg_stat_get_last_vacuum_time(c.oid) as last_vacuum,
|
||||
pg_stat_get_tuples_inserted(c.oid) as inserted,
|
||||
pg_stat_get_tuples_updated(c.oid) as updated,
|
||||
pg_stat_get_tuples_deleted(c.oid) as deleted
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE nspname NOT IN ('information_schema', 'pg_toast', 'pg_catalog')
|
||||
ORDER BY 1, 2;
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
SELECT n.nspname as schema,
|
||||
c.relname as table,
|
||||
t.tgname as trigger,
|
||||
t.tgtype as "type"
|
||||
FROM pg_trigger t
|
||||
JOIN pg_class c ON (c.oid = t.tgrelid)
|
||||
JOIN pg_namespace n ON n.oid = c.relnamespace
|
||||
ORDER BY 1, 2;
|
||||
*/
|
||||
|
||||
SELECT n.nspname as schema,
|
||||
r.relname,
|
||||
t.tgname,
|
||||
t.tgtype,
|
||||
t.tgenabled
|
||||
FROM pg_class r
|
||||
JOIN pg_namespace n ON n.oid = r.relnamespace
|
||||
JOIN pg_trigger t ON (t.tgrelid = r.oid)
|
||||
-- WHERE t.tgenabled = 'D' -- Show only DISABLED triggers
|
||||
ORDER BY 1, 2;
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
SELECT DISTINCT ON (c.relname)
|
||||
n.nspname as schema,
|
||||
c.relname,
|
||||
a.rolname as owner,
|
||||
0 as col_seq,
|
||||
'' as column,
|
||||
d.description as comment
|
||||
-- c.oid
|
||||
FROM pg_class c
|
||||
JOIN pg_attribute col ON (col.attrelid = c.oid)
|
||||
JOIN pg_description d ON (d.objoid = col.attrelid AND d.objsubid = 0)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_authid a ON ( a.OID = c.relowner )
|
||||
UNION
|
||||
SELECT n.nspname as schema,
|
||||
c.relname,
|
||||
'' as owner,
|
||||
col.attnum as col_seq,
|
||||
col.attname as column,
|
||||
d.description as comment
|
||||
-- c.oid
|
||||
FROM pg_class c
|
||||
JOIN pg_attribute col ON (col.attrelid = c.oid)
|
||||
JOIN pg_description d ON (d.objoid = col.attrelid AND d.objsubid = col.attnum)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_authid a ON ( a.OID = c.relowner )
|
||||
WHERE relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND relkind = 'r'
|
||||
AND d.objsubid >= 0
|
||||
AND col.attnum >= 0
|
||||
ORDER BY 1, 2, 4;
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
SELECT n.nspname,
|
||||
c.relname as table,
|
||||
(SELECT oid FROM pg_database WHERE datname = current_database() ) as db_dir,
|
||||
c.relfilenode as filename
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE relname NOT LIKE 'pg_%' AND
|
||||
relname NOT LIKE 'information%' AND
|
||||
relname NOT LIKE 'sql_%' AND
|
||||
relkind = 'r'
|
||||
ORDER BY 1, relname;
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
SELECT n.nspname as schema,
|
||||
c.relname as table,
|
||||
a.rolname as owner,
|
||||
c.relacl as permits
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON ( n.oid = c.relnamespace )
|
||||
JOIN pg_authid a ON ( a.OID = c.relowner )
|
||||
WHERE relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND relkind = 'r'
|
||||
--AND position('cp' in ARRAY_TO_STRING(c.relacl, '') )> 0
|
||||
ORDER BY n.nspname,
|
||||
c.relname;
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
SELECT n.nspname as schema,
|
||||
t.relname as table,
|
||||
c.conname as pk_name
|
||||
FROM pg_class t
|
||||
JOIN pg_namespace n ON ( n.oid = t.relnamespace )
|
||||
JOIN pg_constraint c ON ( c.conrelid = t.OID AND c.contype = 'p')
|
||||
WHERE relkind = 'r'
|
||||
AND t.relname NOT LIKE 'pg_%'
|
||||
AND t.relname NOT LIKE 'sql_%'
|
||||
ORDER BY t.relname, c.conname;
|
||||
@@ -0,0 +1,20 @@
|
||||
SELECT c.table_schema as schema,
|
||||
c.table_name as table,
|
||||
c.column_name as column,
|
||||
c.data_type as type,
|
||||
case when c.data_type LIKE '%char%'
|
||||
then COALESCE(character_maximum_length::text, 'N/A')
|
||||
when c.data_type LIKE '%numeric%'
|
||||
then '(' || c.numeric_precision::text || ', ' || c.numeric_scale::text || ')'
|
||||
when c.data_type LIKE '%int%'
|
||||
then c.numeric_precision::text
|
||||
else COALESCE(character_maximum_length::text, 'N/A')
|
||||
end as size
|
||||
FROM information_schema.columns c
|
||||
WHERE c.table_schema NOT LIKE 'pg_%'
|
||||
AND c.table_schema NOT LIKE 'information%'
|
||||
AND c.table_name NOT LIKE 'sql_%'
|
||||
AND c.is_updatable = 'YES'
|
||||
AND c.column_name LIKE '%<COLUMN_NAME>%'
|
||||
ORDER BY c.table_name, c.column_name;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
-- TABLES with_large_index_size
|
||||
SELECT c.oid, s.nspname,
|
||||
c.relname as table,
|
||||
c.reltuples::int4 as rows,
|
||||
c.relpages as pages,
|
||||
pg_size_pretty (pg_relation_size(c.oid)) as size,
|
||||
(SELECT pg_size_pretty (SUM (pg_relation_size(indexrelid) )::INT8)
|
||||
FROM pg_index
|
||||
WHERE indrelid = c.oid) as index_size,
|
||||
pg_size_pretty (pg_total_relation_size(c.oid))
|
||||
FROM pg_catalog.pg_class c
|
||||
JOIN pg_catalog.pg_namespace s ON (relnamespace = s.oid)
|
||||
WHERE nspname = 'public'
|
||||
AND relkind = 'r'
|
||||
GROUP BY 1, 2, 3, 4, 5, 6
|
||||
HAVING pg_relation_size(c.oid) < (SELECT SUM (pg_relation_size(indexrelid) )
|
||||
FROM pg_index
|
||||
WHERE indrelid = c.oid)
|
||||
ORDER BY (SELECT SUM (pg_relation_size(indexrelid) )::INT8 FROM pg_index
|
||||
WHERE indrelid = c.oid) DESC
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
SELECT n.nspname as schema,
|
||||
c.relname as table
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid =c.relnamespace )
|
||||
WHERE relkind = 'r' AND
|
||||
relname NOT LIKE 'pg_%' AND
|
||||
relname NOT LIKE 'sql_%' AND
|
||||
relhaspkey = FALSE
|
||||
ORDER BY n.nspname, c.relname;
|
||||
@@ -0,0 +1,5 @@
|
||||
SELECT relname, array_to_string(ARRAY[relacl], '|') as permits
|
||||
FROM pg_class
|
||||
WHERE relkind = 'r'
|
||||
AND position('|=' in array_to_string(ARRAY[relacl], '|')) = 0
|
||||
ORDER BY 1;
|
||||
@@ -0,0 +1,4 @@
|
||||
SELECT name, setting, short_desc
|
||||
FROM pg_settings
|
||||
WHERE name LIKE 'tcp%';
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
SELECT n.nspname AS schema,
|
||||
c.relname,
|
||||
t.tgname,
|
||||
p.proname AS function_called,
|
||||
-- t.tgisconstraint AS is_constraint,
|
||||
-- t.tgconstrname AS constraint_nm,
|
||||
CASE WHEN t.tgconstrrelid > 0
|
||||
THEN (SELECT relname
|
||||
FROM pg_class
|
||||
WHERE oid = t.tgconstrrelid)
|
||||
ELSE ''
|
||||
END AS constr_tbl,
|
||||
t.tgenabled
|
||||
FROM pg_trigger t
|
||||
INNER JOIN pg_proc p ON ( p.oid = t.tgfoid)
|
||||
INNER JOIN pg_class c ON (c.oid = t.tgrelid)
|
||||
INNER JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE tgname NOT LIKE 'pg_%'
|
||||
AND tgname NOT LIKE 'RI_%' -- < comment out to see constraints
|
||||
-- AND t.tgenabled = FALSE
|
||||
ORDER BY 1;
|
||||
@@ -0,0 +1,16 @@
|
||||
SELECT idstat.schemaname AS schema,
|
||||
idstat.relname AS table_name,
|
||||
indexrelname AS index_name,
|
||||
idstat.idx_scan AS times_used,
|
||||
pg_size_pretty(pg_relation_size(quote_ident(idstat.schemaname) || '.' || quote_ident(idstat.relname))) AS table_size,
|
||||
pg_size_pretty(pg_relation_size(quote_ident(idstat.schemaname) || '.' || quote_ident(indexrelname))) AS index_size,
|
||||
n_tup_upd + n_tup_ins + n_tup_del as num_writes,
|
||||
indexdef AS definition
|
||||
FROM pg_stat_user_indexes AS idstat
|
||||
JOIN pg_indexes ON indexrelname = indexname
|
||||
JOIN pg_stat_user_tables AS tabstat ON idstat.relname = tabstat.relname
|
||||
WHERE idstat.idx_scan < 200
|
||||
AND indexdef !~* 'unique'
|
||||
ORDER BY idstat.schemaname,
|
||||
idstat.relname,
|
||||
indexrelname;
|
||||
@@ -0,0 +1,19 @@
|
||||
SELECT n.nspname as schema,
|
||||
i.relname as table,
|
||||
i.indexrelname as index,
|
||||
i.idx_scan,
|
||||
i.idx_tup_read,
|
||||
i.idx_tup_fetch,
|
||||
pg_size_pretty(pg_relation_size(quote_ident(n.nspname) || '.' || quote_ident(i.relname))) AS table_size,
|
||||
pg_size_pretty(pg_relation_size(quote_ident(n.nspname) || '.' || quote_ident(i.indexrelname))) AS index_size,
|
||||
pg_get_indexdef(idx.indexrelid) as idx_definition
|
||||
FROM pg_stat_all_indexes i
|
||||
JOIN pg_class c ON (c.oid = i.relid)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_index idx ON (idx.indexrelid = i.indexrelid )
|
||||
WHERE i.idx_scan < 200
|
||||
AND NOT idx.indisprimary
|
||||
AND NOT idx.indisunique
|
||||
ORDER BY 1, 2, 3;
|
||||
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
SELECT n.nspname,
|
||||
c.relname,
|
||||
array_to_string(ARRAY[c.relacl], '|') as permits,
|
||||
position('<USERNAME>' in array_to_string(ARRAY[relacl], '|'))
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE n.nspname not like 'pg_%'
|
||||
AND n.nspname not like 'inform_%'
|
||||
AND relkind = 'r'
|
||||
AND (position('<USERNAME>' in array_to_string(ARRAY[relacl], '|')) > 0
|
||||
OR position( (SELECT g.rolname
|
||||
FROM pg_auth_members r
|
||||
JOIN pg_authid g ON (r.roleid = g.oid)
|
||||
JOIN pg_authid u ON (r.member = u.oid)
|
||||
AND u.rolname LIKE '%<USERNAME>%') in array_to_string(ARRAY[relacl], '|')) > 0 )
|
||||
ORDER BY 1;
|
||||
@@ -0,0 +1,33 @@
|
||||
SELECT n.nspname,
|
||||
c.relname,
|
||||
CASE WHEN has_table_privilege('<CKUSER>', n.nspname || '.' || c.relname, 'SELECT')
|
||||
THEN 'YES'
|
||||
ELSE 'no'
|
||||
END AS select,
|
||||
CASE WHEN has_table_privilege('<CKUSER>', n.nspname || '.' || c.relname, 'INSERT')
|
||||
THEN 'YES'
|
||||
ELSE 'no'
|
||||
END AS insert,
|
||||
CASE WHEN has_table_privilege('<CKUSER>', n.nspname || '.' || c.relname, 'UPDATE')
|
||||
THEN 'YES'
|
||||
ELSE 'no'
|
||||
END AS update,
|
||||
CASE WHEN has_table_privilege('<CKUSER>', n.nspname || '.' || c.relname, 'DELETE')
|
||||
THEN 'YES'
|
||||
ELSE 'no'
|
||||
END AS delete,
|
||||
array_to_string(ARRAY[c.relacl], '|') as permits
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE n.nspname not like 'pg_%'
|
||||
AND n.nspname not like 'inform_%'
|
||||
AND relkind = 'r'
|
||||
/*
|
||||
AND (position('$CKUSER' in array_to_string(ARRAY[relacl], '|')) > 0
|
||||
OR position( (SELECT g.rolname
|
||||
FROM pg_auth_members r
|
||||
JOIN pg_authid g ON (r.roleid = g.oid)
|
||||
JOIN pg_authid u ON (r.member = u.oid)
|
||||
AND u.rolname LIKE '$CKUSER%') in array_to_string(ARRAY[relacl], '|')) > 0 )
|
||||
*/
|
||||
ORDER BY 1, 2;
|
||||
@@ -0,0 +1,10 @@
|
||||
SELECT rolname as user,
|
||||
CASE WHEN rolcanlogin THEN 'user'
|
||||
ELSE 'group'
|
||||
END,
|
||||
CASE WHEN rolsuper THEN 'SUPERUSER'
|
||||
ELSE 'normal'
|
||||
END AS super
|
||||
FROM pg_authid
|
||||
ORDER BY rolcanlogin ,
|
||||
rolname;
|
||||
@@ -0,0 +1,26 @@
|
||||
/* Requires PostgreSQL 8.4 or greater */
|
||||
WITH s AS(
|
||||
SELECT SUM(pg_relation_size(quote_ident(n.nspname) || '.' || quote_ident(c.relname))::bigint) AS table_size,
|
||||
pg_size_pretty(SUM(pg_relation_size(quote_ident(n.nspname) || '.' || quote_ident(c.relname))::bigint)::bigint) AS table_size_pretty
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE c.relkind = 'r'
|
||||
AND c.relname NOT LIKE 'pg_%'
|
||||
AND c.relname NOT LIKE 'sql%'
|
||||
)
|
||||
SELECT s.table_size,
|
||||
s.table_size_pretty,
|
||||
SUM(pg_relation_size(quote_ident(n.nspname) || '.' || quote_ident(i.indexrelname))::bigint) AS unused_idx_size,
|
||||
pg_size_pretty(SUM(pg_relation_size(quote_ident(n.nspname) || '.' || quote_ident(i.indexrelname))::bigint)::bigint) AS unused_idx_size_pretty,
|
||||
pg_database_size(current_database()) as db_size,
|
||||
pg_size_pretty(pg_database_size(current_database()))as db_size_pretty,
|
||||
pg_size_pretty(pg_database_size(current_database()) - SUM(pg_relation_size(quote_ident(n.nspname) || '.' || quote_ident(i.indexrelname))::bigint)::bigint) as db_minus_wasted_space
|
||||
|
||||
FROM s, pg_stat_all_indexes i
|
||||
JOIN pg_class c ON (c.oid = i.relid)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_index idx ON (idx.indexrelid = i.indexrelid )
|
||||
WHERE i.idx_scan = 0
|
||||
AND NOT idx.indisprimary
|
||||
AND NOT idx.indisunique
|
||||
GROUP BY table_size, table_size_pretty;
|
||||
Reference in New Issue
Block a user