Quantcast
Channel: Planet PostgreSQL
Viewing all articles
Browse latest Browse all 9645

Egor Spivac: How to get some information about PostgreSQL structure (Part 2)

$
0
0

Schemas

How to get a list of schemes:
SELECT 
CASE
WHEN nspname LIKE E'pg\_temp\_%' THEN 1
WHEN (nspname LIKE E'pg\_%') THEN 0
ELSE 3
END AS nsptyp, nsp.nspname, nsp.oid, pg_get_userbyid(nspowner)
AS namespaceowner,
nspacl, description,
has_schema_privilege(nsp.oid, 'CREATE') as cancreate
FROM pg_namespace nsp
LEFT OUTER JOIN pg_description des ON des.objoid=nsp.oid
WHERE NOT ((nspname = 'pg_catalog' AND EXISTS
(SELECT 1 FROM pg_class
WHERE relname = 'pg_class'
AND relnamespace = nsp.oid LIMIT 1)) OR
(nspname = 'information_schema' AND
EXISTS (SELECT 1 FROM pg_class
WHERE relname = 'tables'
AND relnamespace = nsp.oid LIMIT 1)) OR
(nspname LIKE '_%' AND
EXISTS (SELECT 1 FROM pg_proc
WHERE proname='slonyversion'
AND pronamespace = nsp.oid LIMIT 1)) OR
(nspname = 'dbo' AND
EXISTS (SELECT 1 FROM pg_class
WHERE relname = 'systables'
AND relnamespace = nsp.oid LIMIT 1)) OR
(nspname = 'sys' AND
EXISTS (SELECT 1 FROM pg_class
WHERE relname = 'all_tables'
AND relnamespace = nsp.oid LIMIT 1))
)
AND nspname NOT LIKE E'pg\_temp\_%'
AND nspname NOT LIKE E'pg\_toast_temp\_%'
ORDER BY 1, nspname

Tables

Get all tables for schema "public"
SELECT n.nspname as "Schema",  c.relname AS datname,  
CASE c.relkind
WHEN 'r' THEN 'table'
WHEN 'v' THEN 'view'
WHEN 'i' THEN 'index'
WHEN 'S' THEN 'sequence'
WHEN 's' THEN 'special'
END as "Type", u.usename as "Owner",
(SELECT obj_description(c.oid, 'pg_class')) AS comment
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_user u ON u.usesysid = c.relowner
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname='public' AND c.relkind IN ('r','')
AND n.nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema')
ORDER BY datname ASC 

Fields

Get all fields for table "table1", with additional information (type, default value, not null flag, length, comment, foreign key name, primary key name)
SELECT pg_tables.tablename, pg_attribute.attname AS field, 
format_type(pg_attribute.atttypid, -1) AS "type",
pg_attribute.atttypmod AS len,
(SELECT col_description(pg_attribute.attrelid,
pg_attribute.attnum)) AS comment,
CASE pg_attribute.attnotnull
WHEN false THEN 1 ELSE 0
END AS "notnull",
pg_constraint.conname AS "key", pc2.conname AS ckey,
(SELECT pg_attrdef.adsrc FROM pg_attrdef
WHERE pg_attrdef.adrelid = pg_class.oid
AND pg_attrdef.adnum = pg_attribute.attnum) AS def
FROM pg_tables, pg_class
JOIN pg_attribute ON pg_class.oid = pg_attribute.attrelid
AND pg_attribute.attnum > 0
LEFT JOIN pg_constraint ON pg_constraint.contype = 'p'::"char"
AND pg_constraint.conrelid = pg_class.oid AND
(pg_attribute.attnum = ANY (pg_constraint.conkey))
LEFT JOIN pg_constraint AS pc2 ON pc2.contype = 'f'::"char"
AND pc2.conrelid = pg_class.oid
AND (pg_attribute.attnum = ANY (pc2.conkey))
WHERE pg_class.relname = pg_tables.tablename
AND pg_tables.tableowner = "current_user"()
AND pg_attribute.atttypid <> 0::oid
AND tablename='table1'
ORDER BY field ASC 
 
 
See First part

Viewing all articles
Browse latest Browse all 9645

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>