# HG changeset patch # User Oleksandr Gavenko # Date 1568495200 -10800 # Node ID 07d81edeaba6f1ea314aa750175594cebbcbc107 # Parent a6ba7fce5ed970ef8ed2bec7df23a188b798fb0f Granting selects. diff -r a6ba7fce5ed9 -r 07d81edeaba6 postgres.rst --- a/postgres.rst Thu May 30 01:20:55 2019 +0300 +++ b/postgres.rst Sun Sep 15 00:06:40 2019 +0300 @@ -84,6 +84,10 @@ https://www.postgresql.org/docs/current/sql-createrole.html ``CREATE ROLE``. +Granting selects:: + + GRANT SELECT ON ALL TABLES IN SCHEMA public TO user2; + Change password =============== :: @@ -221,8 +225,25 @@ SELECT * FROM pg_constraint WHERE conrelid = (SELECT oid FROM pg_class WHERE relname LIKE 'tblName'); select * from pg_constraint where conname like '%conName%'; +Show constraints and columns to given table:: + + select + c.constraint_name, + x.table_schema as schema_name, x.table_name, x.column_name, + y.table_schema as foreign_schema_name, y.table_name as foreign_table_name, y.column_name as foreign_column_name + from information_schema.referential_constraints c + join information_schema.key_column_usage x + on x.constraint_name = c.constraint_name + join information_schema.key_column_usage y + on y.ordinal_position = x.position_in_unique_constraint + and y.constraint_name = c.unique_constraint_name + where y.table_name = 'sa_pnr' + order by c.constraint_name, x.ordinal_position + https://www.postgresql.org/docs/current/catalog-pg-constraint.html pg_constraint. +https://stackoverflow.com/questions/1152260/postgres-sql-to-list-table-foreign-keys + List table foreign keys. List functions:: @@ -265,7 +286,7 @@ \l+ -List tables sizes:: +List tables sizes (and constraints):: \d+ @@ -289,6 +310,13 @@ LEFT JOIN pg_class t ON t.reltoastrelid = p.oid ORDER BY p.relpages DESC; +Largest TOAST:: + + select tbl.relname, toast.relname, toast.relpages + from pg_class tbl, pg_class toast + where tbl.reltoastrelid = toast.oid + order by toast.relpages desc; + Size of types:: SELECT pg_column_size(5::smallint); @@ -314,6 +342,13 @@ select * from pg_stat_user_tables; +Table statistics +================ + +Full table statistics is in ``pg_statistics`` table which is accessible only by superuser. + +Human readable stats are in ``pg_stats`` view. + Using psql client ================= @@ -326,7 +361,7 @@ Connect by:: - $ psql -U $USER -h $HOST $SCHEMA + $ psql -U $USER -h $HOST -p $PORT $SCHEMA How to view execution plan::