Get list of tables that would be truncated by a cascade - postgresql

Is there a way to get a list of tables that would also be truncated by a TRUNCATE CASCADE in postgres?
So for example, assuming we have three tables:
a
b (depends on a)
c (depends on b)
TRUNCATE a CASCADE; would also truncate b and c. How could we check this ahead of time?

With the help of this answer you can get the foreign table name by this query
SELECT tc.constraint_name
,tc.table_name
,kcu.column_name
,ccu.table_name AS foreign_table_name
,ccu.column_name AS foreign_column_name
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'FOREIGN KEY' AND ccu.table_name = 'a'
OR
Create a view with another query
CREATE VIEW vdepend_table
AS
SELECT s1.constraint_name
,s1.table_name
,s1.column_name
,s1.ordinal_position
,s2.table_name_ref
,s2.column_name_ref
,s2.ordinal_position_ref
FROM (
SELECT key_column_usage.constraint_name
,key_column_usage.table_name
,key_column_usage.column_name
,columns.ordinal_position
FROM information_schema.key_column_usage
JOIN information_schema.columns USING (
table_name
,column_name
)
) s1
JOIN (
SELECT constraint_column_usage.constraint_name
,constraint_column_usage.table_name AS table_name_ref
,constraint_column_usage.column_name AS column_name_ref
,cols_ref.ordinal_position AS ordinal_position_ref
FROM information_schema.constraint_column_usage
JOIN information_schema.columns cols_ref ON cols_ref.table_name::TEXT = constraint_column_usage.table_name::TEXT
AND cols_ref.column_name::TEXT = constraint_column_usage.column_name::TEXT
) s2 ON s1.constraint_name::TEXT = s2.constraint_name::TEXT
AND NOT s1.table_name::TEXT = s2.table_name_ref::TEXT;
usage:
select table_name from vdepend_table where table_name_ref='a'

Related

Getting the column name of a postgres check constraint from information_schema

I can get the constraint name, the table name and the check_clause of the check constraints on a table using this query:
SELECT tc.constraint_type, tc.constraint_name, tc.table_name, cc.check_clause
FROM information_schema.table_constraints tc
JOIN information_schema.check_constraints cc ON cc.constraint_name = tc.constraint_name
WHERE tc.table_name = $1 and tc.constraint_type = 'CHECK'
From information_schema.constraint_column_usage I can get the columns where a PRIMARY or UNIQUE constraint applies but not a CHECK constraint:
SELECT * FROM information_schema.constraint_column_usage where table_name = $1
This eventually worked following suggestion below:
SELECT
ccu.table_schema,
ccu.table_name,
ccu.constraint_name,
ccu.column_name,
cc.check_clause
FROM information_schema.constraint_column_usage ccu
JOIN information_schema.check_constraints cc ON ccu.constraint_name = cc.constraint_name
WHERE ccu.constraint_name IN
(
SELECT
constraint_name
FROM information_schema.check_constraints
)
AND ccu.table_name = $1;
CHECK constraints can exist on domains as well as tables. To get those that are on table columns only do:
SELECT
table_schema,
table_name,
column_name,
constraint_name
FROM
information_schema.constraint_column_usage
WHERE
constraint_name IN (
SELECT
constraint_name
FROM
information_schema.check_constraints);
The sub select will find all the CHECK constraints in the database and the constraint_name IN will filter to only those that are a column constraint.
UPDATE
An alternative is to use the system catalogs directly:
WITH c AS (
SELECT
conname,
conrelid,
unnest(conkey) AS col_id
FROM
pg_catalog.pg_constraint
WHERE
contype = 'c'
AND conrelid > 0
)
SELECT
conrelid::regclass AS table_name,
conname,
attname
FROM
pg_catalog.pg_attribute
JOIN c ON c.col_id = attnum
AND conrelid = attrelid;
What the above does is restrict the constraint type(contype) to 'c' for CHECK and then to only those CHECK constraints that exist on table conrelid > 0 in the CTE(WITH) and then joins the unnested array of columns(conkey) to the column information in pg_catalog.pg_attribute to get the column names.. The conrelid::regclass turns the conrelid oid value into a table name.
See pg_constraint for more information.

display constraints of Certain table in Postgresql

I want to know how to display all constraints of a certain table(all columns) by the SELECT statement in PostgreSQL. I tried this code:
SELECT * FROM information_schema.table_constraints;
SELECT * FROM information_schema.referential_constraints;
Actually, I want the primary keys and foreign keys of the tables
Following should do the trick. Just change table-name-goes-here with your table name.
SELECT
tc.table_schema, tc.constraint_name, tc.table_name, kcu.column_name,
ccu.table_schema AS foreign_table_schema,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name
FROM
information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type in ('PRIMARY KEY', 'FOREIGN KEY')
AND tc.table_name='table-name-goes-here';

How to get the table a FK refers to in Postgres sql

For a given table, I am trying to get all the columns, to include their name, type, whether the column is a primary key, or a foreign key, and if it is a FK, what table it points to. I wrote the query below, but it seems to be give me the what the columns are referenced by, instead of the other way around:
select c.column_name, c.udt_name, constraint_type, kcu.table_name as references from information_schema.columns c
left outer join information_schema.constraint_column_usage u on c.column_name=u.column_name
left outer join information_schema.table_constraints t on u.constraint_name=t.constraint_name
left outer join information_schema.key_column_usage AS kcu on t.constraint_name = kcu.constraint_name
where c.table_name=#name
I am not too worried about edge cases, I am just trying to inverse the references column. Thanks for your time.
Here is a query to get all the foreign keys from a given table along with what they point to:
SELECT c.conname,
t1.relname AS from_table,
a1.attname AS from_column,
t2.relname AS to_table,
a2.attname AS to_column
FROM pg_catalog.pg_constraint c,
pg_catalog.pg_class t1,
pg_catalog.pg_class t2,
pg_catalog.pg_attribute a1,
pg_catalog.pg_attribute a2,
pg_catalog.pg_namespace n1,
pg_catalog.pg_namespace n2
WHERE c.conrelid = t1.oid
AND c.confrelid = t2.oid
AND c.contype = 'f'
AND a1.attrelid = t1.oid
AND a1.attnum = ANY(c.conkey)
AND a2.attrelid = t2.oid
AND a2.attnum = ANY(c.confkey)
AND t1.relkind = 'r'
AND t2.relkind = 'r'
AND n1.oid = t1.relnamespace
AND n2.oid = t2.relnamespace
AND n1.nspname NOT IN ('pg_catalog', 'pg_toast')
AND n2.nspname NOT IN ('pg_catalog', 'pg_toast')
AND pg_catalog.pg_table_is_visible(t1.oid)
AND pg_catalog.pg_table_is_visible(t2.oid)
AND t1.relname = #name;
I see you are using information_schema instead of pg_catalog, so the easiest way might be to turn my query into a CTE or subquery, then join to it based on your table and column name.
EDIT: It sounds like you just need some help joining my query to what you already have. Actually your query has a lot of errors in how it does the joins, so I've rewritten it to include the correct foreign key results:
SELECT c.column_name,
c.udt_name,
t.constraint_type,
t.constraint_name,
x.to_table
FROM information_schema.columns c
LEFT OUTER JOIN (
SELECT t.constraint_type,
t.constraint_catalog,
t.constraint_schema,
t.constraint_name,
t.table_catalog,
t.table_schema,
t.table_name,
u.column_name
FROM information_schema.constraint_column_usage u
LEFT OUTER JOIN information_schema.table_constraints t
ON t.table_catalog = u.table_catalog
AND t.table_schema = u.table_schema
AND t.table_name = u.table_name
AND t.constraint_catalog = u.constraint_catalog
AND t.constraint_schema = u.constraint_schema
AND t.constraint_name = u.constraint_name
WHERE t.constraint_type IS DISTINCT FROM 'FOREIGN KEY'
) t
ON c.table_catalog = t.table_catalog
AND c.table_schema = t.table_schema
AND c.table_name = t.table_name
AND c.column_name = t.column_name
LEFT OUTER JOIN (
SELECT c.conname,
t1.relname AS from_table,
a1.attname AS from_column,
t2.relname AS to_table,
a2.attname AS to_column
FROM pg_catalog.pg_constraint c,
pg_catalog.pg_class t1,
pg_catalog.pg_class t2,
pg_catalog.pg_attribute a1,
pg_catalog.pg_attribute a2,
pg_catalog.pg_namespace n1,
pg_catalog.pg_namespace n2
WHERE c.conrelid = t1.oid
AND c.confrelid = t2.oid
AND c.contype = 'f'
AND a1.attrelid = t1.oid
AND a1.attnum = ANY(c.conkey)
AND a2.attrelid = t2.oid
AND a2.attnum = ANY(c.confkey)
AND t1.relkind = 'r'
AND t2.relkind = 'r'
AND n1.oid = t1.relnamespace
AND n2.oid = t2.relnamespace
AND n1.nspname NOT IN ('pg_catalog', 'pg_toast')
AND n2.nspname NOT IN ('pg_catalog', 'pg_toast')
AND pg_catalog.pg_table_is_visible(t1.oid)
AND pg_catalog.pg_table_is_visible(t2.oid)
) x
ON x.from_table = c.table_name
AND x.from_column = c.column_name
WHERE c.table_name = #cards
;
This version also prevents lots of duplicate rows if you have NOT NULL or CHECK constraints.
I am not sure if you know how to programming with other languages(such as java, C#) besides writing Sql queries
If you know any of them, you can use the function which are int the JDBC/ODBC drivers to get the things you want
Here is an example of using jdbc
you open a connection to the database
from the connection you get, and you can get the meta data, here is the code
conn=dataSource.getConnection();
DatabaseMetaData dbmeta = conn.getMetaData();
After you get the meta data, you can get all the columns, primary keys, foreign keys and even indexed. See the sample codes below
ResultSet rsKey = dbmeta.getPrimaryKeys(null, schemaName, tableName);
ResultSet rs = dbmeta.getColumns(null, schemaName, tableName, "%");
ResultSet rsForeign=meta.getExportedKeys(null, schemaName, tableName);

List columns with indexes in Amazon Redshift

I need to query Redshift metadata to get a list of table columns that includes information whether the column is part of primary key or not.
There is a post already List columns with indexes in PostgreSQL that has an answer for PostgreSQL, however unfortunately, it fails on Redshift with "ERROR: 42809: op ANY/ALL (array) requires array on right side"
I figured out how to do it with the help of this https://bitbucket.org/zzzeek/sqlalchemy/pull-request/6/sqlalchemy-to-support-postgresql-80/diff
SELECT attname column_name, attnotnull,
format_type(atttypid, atttypmod) as column_type, atttypmod,
i.indisprimary as primary_key,
col_description(attrelid, attnum) as description
FROM pg_attribute c
LEFT OUTER JOIN pg_index i
ON c.attrelid = i.indrelid AND i.indisprimary AND
c.attnum = ANY(string_to_array(textin(int2vectorout(i.indkey)), ' '))
where c.attnum > 0 AND NOT c.attisdropped AND c.attrelid = :tableOid
order by attnum
The following worked for me:
SELECT n.nspname as schema_name,
t.relname as table_name,
i.relname as index_name,
c.contype as index_type,
a.attname as column_name,
a.attnum AS column_position
FROM pg_class t
INNER JOIN pg_index AS ix ON t.oid = ix.indrelid
INNER JOIN pg_constraint AS c ON ix.indrelid = c.conrelid
INNER JOIN pg_class AS i ON i.oid = ix.indexrelid
INNER JOIN pg_attribute AS a ON a.attrelid = t.oid
AND a.attnum= ANY(string_to_array(textin(int2vectorout(ix.indkey)),' ')::int[])
INNER JOIN pg_namespace AS n ON n.oid = t.relnamespace;
You can leverage the table DDL view AWS published a few months ago (https://github.com/awslabs/amazon-redshift-utils/blob/master/src/AdminViews/v_generate_tbl_ddl.sql) by picking out the constraint component and parsing out the key columns:
select schemaname,tablename, substring(ddl,charindex('(',ddl)+1, charindex(')',ddl)-1-charindex('(',ddl))
from
(
SELECT
n.nspname AS schemaname
,c.relname AS tablename
,200000000 + CAST(con.oid AS INT) AS seq
,'\t,' + pg_get_constraintdef(con.oid) AS ddl
FROM
pg_constraint AS con
INNER JOIN pg_class AS c
ON c.relnamespace = con.connamespace
AND c.relfilenode = con.conrelid
INNER JOIN pg_namespace AS n
ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
ORDER BY seq
)
Note that this query also gives you foreign key columns. It's easy enough to filter those out by appending the query with
where ddl like '%PRIMARY KEY%'
Use below query:
select * from pg_table_def where tablename = 'mytablename'
This will give you all columns for table along with their data type , encoding and if it has sort key or dist key.

Find primary key of table in Postgresql from information_schema with only SELECT

I am using the following query to discover (1) the primary key columns and (2) if the columns have a default value from the information_schema in Postgresql 9.1.
SELECT kcu.column_name, (c.column_default is not null) AS has_default
FROM information_schema.key_column_usage kcu
JOIN information_schema.table_constraints tc ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.columns c on c.column_name = kcu.column_name and c.table_name = kcu.table_name
WHERE tc.constraint_type = 'PRIMARY KEY' AND kcu.table_name like :tablename
It works fine when run as the database owner, but when I run it as a "read-only" user (which I need to do in my application), it returns no data. Some research revealed that the problem is the information.table_constraints view; from the documentation:
The view table_constraints contains all constraints belonging to tables that the current user owns or has some non-SELECT privilege on.
So in order to retrieve table_constraints, my login role needs more than SELECT on the table? Is there no way to get the information from information_schema without giving write permissions to the login role?
Use pg_* views instead of information_schema views. pg_* views display all information regardles of granted privileges.
Try this query:
select
t.relname as table_name,
i.relname as index_name,
a.attname as column_name,
d.adsrc as default_value
from
pg_class t
join pg_attribute a on a.attrelid = t.oid
join pg_index ix on t.oid = ix.indrelid AND a.attnum = ANY(ix.indkey)
join pg_class i on i.oid = ix.indexrelid
left join pg_attrdef d on d.adrelid = t.oid and d.adnum = a.attnum
where
t.relkind = 'r'
and t.relname in ( 'aa', 'bb', 'cc' )
order by
t.relname,
i.relname,
a.attnum;
An example of the query results:
create table aa(
x int primary KEY
);
create table bb(
x int default 1,
constraint pk primary key ( x )
);
create table cc(
x int default 20,
y varchar(10) default 'something',
constraint cc_pk primary key ( x, y )
);
table_name | index_name | column_name | default_value
------------+------------+-------------+--------------------------------
aa | aa_pkey | x |
bb | pk | x | 1
cc | cc_pk | x | 20
cc | cc_pk | y | 'something'::character varying
This is correct, the official postgresql query is below
http://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
if schema is needed the query is as follows
SELECT
pg_attribute.attname,
format_type(pg_attribute.atttypid, pg_attribute.atttypmod)
FROM pg_index, pg_class, pg_attribute, pg_namespace
WHERE
pg_class.oid = 'MY TABLE'::regclass AND
indrelid = pg_class.oid AND
nspname = 'MY CLASS' AND
pg_class.relnamespace = pg_namespace.oid AND
pg_attribute.attrelid = pg_class.oid AND
pg_attribute.attnum = any(pg_index.indkey)
AND indisprimary
The difference can be up to 6000~7000 times. The pg_ one runs often in 0.56ms where the schema based one can run up 6500ms. This is a huge difference especially if you have a high load on the server.
There is another way to provide access for data in information_schema.
A. Envelop SQL in a function with SECURITY DEFINER modifier
CREATE FUNCTION fn_inf(name)
RETURNS TABLE (column_name information_schema.sql_identifier, has_default bool)
LANGUAGE SQL
SECURITY DEFINER
AS $$
SELECT kcu.column_name, (c.column_default is not null) AS has_default
FROM information_schema.key_column_usage kcu
JOIN information_schema.table_constraints tc ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.columns c on c.column_name = kcu.column_name and c.table_name = kcu.table_name
WHERE tc.constraint_type = 'PRIMARY KEY' AND kcu.table_name like $1;
$$;
B. GRANT EXECUTE to user read_only
GRANT EXECUTE ON FUNCTION fn_inf to read_only;
C. Use as user read_only
SELECT * FROM fn_inf('spatial_ref_sys');
column_name
has_default
srid
false