dba:postgresql:postgresql_basic_querys
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
dba:postgresql:postgresql_basic_querys [2022/03/22 16:33] – [Sessions] dodger | dba:postgresql:postgresql_basic_querys [2023/10/09 09:10] – [list functions] dodger | ||
---|---|---|---|
Line 57: | Line 57: | ||
===== autocommit ===== | ===== autocommit ===== | ||
- | <code sql> | + | *WARNING* '' |
- | \set AUTOCOMMIT off | + | \\ |
- | </ | + | '' |
Check: | Check: | ||
Line 108: | Line 109: | ||
===== list schemas ===== | ===== list schemas ===== | ||
+ | Sortcode: | ||
< | < | ||
\dn | \dn | ||
+ | </ | ||
+ | |||
+ | Query: | ||
+ | <code sql> | ||
+ | SELECT n.nspname AS " | ||
+ | pg_catalog.pg_get_userbyid(n.nspowner) AS " | ||
+ | FROM pg_catalog.pg_namespace n | ||
+ | WHERE n.nspname !~ ' | ||
+ | ORDER BY 1; | ||
</ | </ | ||
Line 150: | Line 161: | ||
===== list functions ===== | ===== list functions ===== | ||
+ | |||
+ | <code sql> | ||
+ | \df | ||
+ | </ | ||
+ | or | ||
+ | |||
+ | <code sql> | ||
+ | SELECT n.nspname as " | ||
+ | p.proname as " | ||
+ | -- pg_catalog.pg_get_function_result(p.oid) as " | ||
+ | -- pg_catalog.pg_get_function_arguments(p.oid) as " | ||
+ | CASE p.prokind | ||
+ | WHEN ' | ||
+ | WHEN ' | ||
+ | WHEN ' | ||
+ | ELSE ' | ||
+ | END as " | ||
+ | FROM pg_catalog.pg_proc p | ||
+ | LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace | ||
+ | WHERE pg_catalog.pg_function_is_visible(p.oid) | ||
+ | AND n.nspname <> ' | ||
+ | AND n.nspname <> ' | ||
+ | ORDER BY 1, 2; | ||
+ | </ | ||
+ | Note: | ||
+ | " | ||
+ | |||
+ | ==== Aggregate functions ==== | ||
+ | |||
<code sql> | <code sql> | ||
\da | \da | ||
Line 269: | Line 309: | ||
WHERE TABLE_NAME | WHERE TABLE_NAME | ||
order by ordinal_position | order by ordinal_position | ||
+ | ; | ||
+ | </ | ||
+ | ==== dba_tab_columns ==== | ||
+ | |||
+ | inefficient but who cares | ||
+ | <code sql> | ||
+ | SELECT * from | ||
+ | ( | ||
+ | SELECT table_catalog DB_NAME, | ||
+ | table_schema SCHEMA_NAME, | ||
+ | table_name, | ||
+ | column_name, | ||
+ | ordinal_position, | ||
+ | data_type, | ||
+ | character_maximum_length, | ||
+ | column_default, | ||
+ | is_nullable | ||
+ | FROM information_schema.columns | ||
+ | order by ordinal_position | ||
+ | ) as dba_tab_columns | ||
+ | WHERE COLUMN_NAME LIKE ' | ||
; | ; | ||
</ | </ | ||
Line 282: | Line 343: | ||
FROM INFORMATION_SCHEMA.STATISTICS | FROM INFORMATION_SCHEMA.STATISTICS | ||
WHERE TABLE_SCHEMA = ' | WHERE TABLE_SCHEMA = ' | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== List column index ===== | ||
+ | ==== dba_ind_columns ==== | ||
+ | |||
+ | |||
+ | <code sql> | ||
+ | SELECT | ||
+ | np.nspname AS schema_name, | ||
+ | t.relname AS table_name, | ||
+ | a.attname AS column_name, | ||
+ | i.relname AS index_name | ||
+ | FROM | ||
+ | pg_class t, | ||
+ | pg_class i, | ||
+ | pg_index ix, | ||
+ | pg_attribute a, | ||
+ | pg_namespace np | ||
+ | WHERE | ||
+ | t.oid = ix.indrelid | ||
+ | AND i.oid = ix.indexrelid | ||
+ | AND a.attrelid = t.oid | ||
+ | AND a.attnum = ANY(ix.indkey) | ||
+ | AND np.oid = t.relnamespace | ||
+ | AND t.relkind = ' | ||
+ | AND a.attname like ' | ||
+ | AND i.relname like ' | ||
+ | AND t.relname like ' | ||
+ | ORDER BY | ||
+ | t.relname, | ||
+ | a.attname; | ||
</ | </ | ||
Line 514: | Line 608: | ||
+ | |||
+ | ===== List Foreign keys (FK) ===== | ||
+ | |||
+ | From [[https:// | ||
+ | <code sql> | ||
+ | select kcu.table_schema || ' | ||
+ | '> | ||
+ | | ||
+ | | ||
+ | | ||
+ | from information_schema.table_constraints tco | ||
+ | join information_schema.key_column_usage kcu | ||
+ | on tco.constraint_schema = kcu.constraint_schema | ||
+ | and tco.constraint_name = kcu.constraint_name | ||
+ | join information_schema.referential_constraints rco | ||
+ | on tco.constraint_schema = rco.constraint_schema | ||
+ | and tco.constraint_name = rco.constraint_name | ||
+ | join information_schema.table_constraints rel_tco | ||
+ | on rco.unique_constraint_schema = rel_tco.constraint_schema | ||
+ | and rco.unique_constraint_name = rel_tco.constraint_name | ||
+ | where tco.constraint_type = ' | ||
+ | group by kcu.table_schema, | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | order by kcu.table_schema, | ||
+ | | ||
+ | |||
+ | </ | ||
====== Section: VACUUM ====== | ====== Section: VACUUM ====== | ||
Line 652: | Line 776: | ||
<code sql> | <code sql> | ||
select * from pgagent.pga_job ; | select * from pgagent.pga_job ; | ||
+ | </ | ||
+ | |||
+ | Less info: | ||
+ | <code sql> | ||
+ | select jobid, | ||
+ | jobname, | ||
+ | jobdesc, | ||
+ | jobenabled | ||
+ | from pgagent.pga_job ; | ||
</ | </ | ||
dba/postgresql/postgresql_basic_querys.txt · Last modified: 2024/04/19 07:37 by dodger