Skip to content

Instantly share code, notes, and snippets.

@dolunay
Created February 2, 2025 11:39
Show Gist options
  • Save dolunay/4e4cf646c2047f29779eb9ba86ef43a6 to your computer and use it in GitHub Desktop.
Save dolunay/4e4cf646c2047f29779eb9ba86ef43a6 to your computer and use it in GitHub Desktop.
Postgresql informationschema DDL samples
-- list all functions in schema
SELECT proname, prosrc
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_proc p
ON pronamespace = n.oid
WHERE nspname = 'public';
-- list all tables in schema
SELECT * FROM information_schema.tables
WHERE table_schema = 'public'
order by table_name ;
-- list all tables with descriptions in schema
SELECT t.table_name, pg_catalog.obj_description(pgc.oid, 'pg_class')
FROM information_schema.tables t
INNER JOIN pg_catalog.pg_class pgc
ON t.table_name = pgc.relname
WHERE t.table_type='BASE TABLE'
AND t.table_schema='public' order by table_name ;
SELECT pgc.*, obj_description(oid)
FROM pg_class pgc;
--WHERE relkind = 'r';
-- list all tables with record count in schema
SELECT
pgClass.relname as tableName,
pgClass.reltuples as rowCount
FROM
pg_class pgClass
INNER JOIN
pg_namespace pgNamespace ON (pgNamespace.oid = pgClass.relnamespace)
WHERE
pgNamespace.nspname NOT IN ('pg_catalog', 'information_schema') AND
pgClass.relkind='r' and pgNamespace.nspname='public'
order by pgclass.relname;
-- list all tables with no record in schema
SELECT
pgClass.relname as tableName,
pg_catalog.obj_description(pgClass.oid, 'pg_class')
-- ,pgClass.reltuples as rowCount
FROM
pg_class pgClass
INNER JOIN
pg_namespace pgNamespace ON (pgNamespace.oid = pgClass.relnamespace)
WHERE
pgNamespace.nspname NOT IN ('pg_catalog', 'information_schema') AND
pgClass.relkind='r' and pgNamespace.nspname='public' and pgClass.reltuples=-1
order by pgclass.relname;
-- list all columns in tables in schema
SELECT *
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'test'
;
-- list all columns typed with float8 in schema
SELECT *
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'test' and udt_name ='float8'
;
-- list all columns typed with text in schema
SELECT *
FROM information_schema.columns
WHERE table_schema = 'public'
and udt_name ='text'
;
-- find varchar typed columns with no length in schema
SELECT *
FROM information_schema.columns
WHERE table_schema = 'public'
and udt_name ='varchar' and character_maximum_length is null
order by table_name , column_name ;
-- list all columns typed with int4 in schema
SELECT *
FROM information_schema.columns
WHERE table_schema = 'public' and
udt_name ='int4'
order by table_name , column_name;
-- list all columns typed with int8 in schema
SELECT *
FROM information_schema.columns
WHERE
table_schema = 'public' and
udt_name ='int8' and
column_name not in ('id','created_by','updated_by','concurrency_data','national_identifier') and
column_name not like '%_id'
order by table_name , column_name;
-- list all columns typed with float8 in schema
SELECT *
FROM information_schema.columns
WHERE table_schema = 'public' and
udt_name ='float8'
order by table_name , column_name;
-- finding ConcurrencyStamp columns in schema.
SELECT *
FROM information_schema.columns
WHERE
table_schema = 'public' and
column_name ='ConcurrencyStamp'
order by table_name , column_name;
-- list all column information in schema
select col_description((table_schema||'.'||table_name)::regclass::oid, ordinal_position) as column_comment
, * from information_schema.columns
WHERE table_schema = 'public' ;
--and table_name = 'table_name';
-- Checking ConcurrencyStamp column comment
select table_schema , table_name , column_name , column_comment from (select col_description((table_schema||'."'||table_name||'"')::regclass::oid, ordinal_position) as column_comment
, * from information_schema.columns
WHERE table_schema = 'public') as cd where cd.column_name ='ConcurrencyStamp' and cd.column_comment<>'Tutarlılık Verisi';
--and table_name = 'table_name';
-- inspecting incorrect column names
select table_schema , table_name , column_name , column_comment from (select col_description((table_schema||'.'||table_name)::regclass::oid, ordinal_position) as column_comment
, * from information_schema.columns
) as cd where cd.column_name like '% %';
--and table_name = 'table_name';
--select max(scale(cast(x.numeric_colun as numeric))) from the_table x;
--select length(cast(max(abs(trunc(cast(x.numeric_colun as numeric)))) as text)) from the_table x;
-- find tables in schema
SELECT t.table_schema,t.table_name FROM information_schema.TABLES t WHERE t.table_schema='applicationmanagement' ;
-- find foreign keys
SELECT * FROM (SELECT
pc.conname as constraint_name,
conrelid as child_table_id,
pclsc.relname as child_table,
pc.conkey as child_column_id,
pac.attname as child_column,
confrelid as parent_table_id,
pclsp.relname as parent_table,
pc.confkey as parent_column_id,
pap.attname as parent_column,
nspname as schema_name
FROM
(
SELECT
connamespace,conname, unnest(conkey) as "conkey", unnest(confkey)
as "confkey" , conrelid, confrelid, contype
FROM
pg_constraint
) pc
JOIN pg_namespace pn ON pc.connamespace = pn.oid
-- and pn.nspname = 'panmydesk4400'
JOIN pg_class pclsc ON pc.conrelid = pclsc.oid
JOIN pg_class pclsp ON pc.confrelid = pclsp.oid
JOIN pg_attribute pac ON pc.conkey = pac.attnum and pac.attrelid =pclsc.oid
JOIN pg_attribute pap ON pc.confkey = pap.attnum and pap.attrelid = pclsp.oid
ORDER BY pclsc.relname) t
WHERE t.constraint_name='fk_compnay_taxofficeam';
-- find constraints in schema
select kcu.table_schema,
kcu.table_name,
tco.constraint_name,
kcu.ordinal_position as position,
kcu.column_name as key_column,
tco.constraint_type
from information_schema.table_constraints tco
join information_schema.key_column_usage kcu
on kcu.constraint_name = tco.constraint_name
and kcu.constraint_schema = tco.constraint_schema
and kcu.constraint_name = tco.constraint_name
--where tco.constraint_type = 'PRIMARY KEY'
order by kcu.table_schema,
kcu.table_name,
position;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment