This is my query
SELECT p.book FROM customers_books p
INNER JOIN books b ON p.book = b.id
INNER JOIN bookprices bp ON bp.book = p.book
WHERE b.status = 'PUBLISHED' AND bp.currency_code = 'GBP'
AND p.book NOT IN (SELECT cb.book FROM customers_books cb WHERE cb.customer = 1)
GROUP BY p.book, p.created_date ORDER BY p.created_date DESC
This is the data in my customers_books table,
I expect only 8,6,1 of books IDs to return but query is returning 8,6,1,1
table structures are here
CREATE TABLE "public"."customers_books" (
"id" int8 NOT NULL,
"created_date" timestamp(6),
"book" int8,
"customer" int8,
);
CREATE TABLE "public"."books" (
"id" int8 NOT NULL,
"created_date" timestamp(6),
"status" varchar(255) COLLATE "pg_catalog"."default",
)
CREATE TABLE "public"."bookprices" (
"id" int8 NOT NULL,
"currency_code" varchar(255) COLLATE "pg_catalog"."default",
"book" int8
)
what do you think I am doing wrong here.
I really dont want to use p.created_date in group by but I was forced to use because of order by
You have too many joins in the outer query:
SELECT b.book
FROM books b INNER JOIN
bookprices bp
ON bp.book = p.book
WHERE b.status = 'PUBLISHED' AND bp.currency_code = 'GBP' AND
NOT EXISTS (SELECT 1
FROM customers_books cb
WHERE cb.book = p.book AND cb.customer = 1
) ;
Note that I replaced the NOT IN with NOT EXISTS. I strongly, strongly discourage you from using NOT IN with a subquery. If the subquery returns any NULL values, then NOT IN returns no rows at all. It is better to sidestep this issue just by using NOT EXISTS.
i have 2 postgres DB's with the same table X, and primary key PKEY (i'm not creator of this).
When im looking on it using my client (i try 2 different) or extracting ddl, i got identical source like this:
...
CONSTRAINT pkey PRIMARY KEY(column1, column2)
...
The problem is what i see in pg_attribute.attname - first DB have correct values (column1 and column2) but the second have column1 and id (?). The rest of data (attnum and other parameters) are identical...It's interesting that column id not exists on this table (X)...maybe one day it existed, but i'm not sure how to check it).
This is a production environment, so recreating index etc it's not easy...Have you met with a similar situation?
comparison method:
select cls.oid,
nsp.nspname as object_schema,
cls.relname as object_name,
a.attname,
case cls.relkind
when 'r' then 'TABLE'
when 'm' then 'MATERIALIZED_VIEW'
when 'i' then 'INDEX'
when 'S' then 'SEQUENCE'
when 'v' then 'VIEW'
when 'c' then 'TYPE'
else cls.relkind::text
end as type
from pg_class cls
join pg_roles rol on rol.oid = cls.relowner
join pg_namespace nsp on nsp.oid = cls.relnamespace
join pg_catalog.pg_attribute a on a.attrelid = cls.oid
left outer join pg_attrdef ad on (ad.adrelid = cls.oid and ad.adnum = a.attnum)
left outer join pg_constraint con on cls.oid = con.conrelid
where nsp.nspname not in ('information_schema', 'pg_catalog')
and nsp.nspname not like 'pg_toast%'
and a.attnum > 0
AND NOT a.attisdropped
and cls.relname like '%my_pkey_real_name%'
order by 1, 2
This query returns 'column1' and 'column2' in attname column on DB1 and 'column1' and 'id' on DB2.
As i wrote - the problem is that the column 'id' don't exists...when i'm extract ddl i'm getting somthing like:
ALTER TABLE X
ADD CONSTRAINT pkey
PRIMARY KEY (column1, column2) NOT DEFERRABLE;
on both DB's
So, i know the reason but not a solution :(
Let's create a test table:
CREATE TABLE test.test_x (
id VARCHAR(128) NOT NULL,
CONSTRAINT pkey PRIMARY KEY(id)
)
WITH (oids = false);
then query pg_class and pg_attribute:
select c.oid, c.* from pg_class c where relname = 'pkey'
select * from pg_attribute where attrelid = 854514857
the result of second query is:
next reneme the primary key column:
ALTER TABLE test.test_x RENAME COLUMN id TO id_x;
the name of this column have changed, ddl is ok, value in pg_class (queried for the table context) is ok (id_x, not id), but in pg_attribute is still the older one:
i tried reindex this table (or only index) but it didn't help
I'm looking for query to get the parent table details(name) using child table name and child table schema.
I browsed over the web but didn't get any query.
CREATE TABLE smt.items (
item_code INTEGER PRIMARY KEY DEFAULT '1001'
,item_name CHARACTER(35) NOT NULL
,purchase_unit CHARACTER(10)
,sale_unit CHARACTER(10)
,purchase_price NUMERIC(10, 2)
,sale_price NUMERIC(10, 2)
);
CREATE TABLE smt.sub_items (
sub_item_id INTEGER PRIMARY KEY
,sub_items_name CHARACTER(35) NOT NULL
) inherits (smt.items);
Something like this:
select bt.relname as table_name, bns.nspname as table_schema
from pg_class ct
join pg_namespace cns on ct.relnamespace = cns.oid
join pg_inherits i on i.inhrelid = ct.oid
join pg_class bt on i.inhparent = bt.oid
join pg_namespace bns on bt.relnamespace = bns.oid
where bt.relkind <> 'p'
and cns.nspname = 'public'
and ct.relname = 'child_table_name';
For example: Say, I have 2 schemas and 4 tables created as
create table a.foo(
id integer primary key,
val integer);
create table b.main(
id serial primary key,
val integer references a.foo(id));
create table b.foo(
k integer primary key references b.main(id),
v timestamp with time zone);
create table b.bar(
k integer primary key references b.main(id),
v timestamp with time zone);
Psuedo code for what I'm looking for: select all tables where b.main.id is referenced;
The results would look like:
b.main.id | b.main.val | b.foo.k | b.foo.v | b.bar.k | b.bar.v
1 1 1 TimeStamp 1 TimeStamp
2 1 2 .... 2 ....
Right now I have implemented this query as:
select * from b.main,
(select *
from b.foo,
(select * from b.bar where b.bar.v > somedate) as morebar
where b.bar.k = b.foo.k) as morefoo
where b.main.id = b.foo.k;
Back to the question. Is there a feature in Postgres that allows us to perform a select on all tables that reference a primary key? In my case, all tables that reference b.main.id.
I've searched through the Postgresql documentation, but have yet to find what I'm looking for. Suggestions?
I have yet to find myself a way to automatically select other tables that referenced to a table's primary key. It seems you still have to mention/type the other table manually in your query, but here's a short version of your query using join:
select *
from b.main a, b.foo b, b.bar c
where a.id = b.k
and a.id = c.k
or
select a.*, b.*, c.*
from b.main as a
inner join b.foo as b on a.id = b.k
inner join b.bar as c on a.id = c.k
You can try:
SELECT tc.constraint_name, tc.table_name, kcu.column_name,
ccu.table_schema AS foreign_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 = 'FOREIGN KEY'
AND ccu.table_schema = 'b'
AND ccu.table_name='main'
AND ccu.column_name = 'id';
I'm using PostgreSQL and I'm trying to list all the tables that have a particular column from a table as a foreign-key/reference. Can this be done? I'm sure this information is stored somewhere in information_schema but I have no idea how to start querying it.
SELECT
r.table_name
FROM information_schema.constraint_column_usage u
INNER JOIN information_schema.referential_constraints fk
ON u.constraint_catalog = fk.unique_constraint_catalog
AND u.constraint_schema = fk.unique_constraint_schema
AND u.constraint_name = fk.unique_constraint_name
INNER JOIN information_schema.key_column_usage r
ON r.constraint_catalog = fk.constraint_catalog
AND r.constraint_schema = fk.constraint_schema
AND r.constraint_name = fk.constraint_name
WHERE
u.column_name = 'id' AND
u.table_catalog = 'db_name' AND
u.table_schema = 'public' AND
u.table_name = 'table_a'
This uses the full catalog/schema/name triplet to identify a db table from all 3 information_schema views. You can drop one or two as required.
The query lists all tables that have a foreign key constraint against the column 'a' in table 'd'
The other solutions are not guaranteed to work in postgresql, as the constraint_name is not guaranteed to be unique; thus you will get false positives. PostgreSQL used to name constraints silly things like '$1', and if you've got an old database you've been maintaining through upgrades, you likely still have some of those around.
Since this question was targeted AT PostgreSQL and that is what you are using, then you can query the internal postgres tables pg_class and pg_attribute to get a more accurate result.
NOTE: FKs can be on multiple columns, thus the referencing column (attnum of pg_attribute) is an ARRAY, which is the reason for using array_agg in the answer.
The only thing you need plug in is the TARGET_TABLE_NAME:
select
(select r.relname from pg_class r where r.oid = c.conrelid) as table,
(select array_agg(attname) from pg_attribute
where attrelid = c.conrelid and ARRAY[attnum] <# c.conkey) as col,
(select r.relname from pg_class r where r.oid = c.confrelid) as ftable
from pg_constraint c
where c.confrelid = (select oid from pg_class where relname = 'TARGET_TABLE_NAME');
If you want to go the other way (list all of the things a specific table refers to), then just change the last line to:
where c.conrelid = (select oid from pg_class where relname = 'TARGET_TABLE_NAME');
Oh, and since the actual question was to target a specific column, you can specify the column name with this one:
select (select r.relname from pg_class r where r.oid = c.conrelid) as table,
(select array_agg(attname) from pg_attribute
where attrelid = c.conrelid and ARRAY[attnum] <# c.conkey) as col,
(select r.relname from pg_class r where r.oid = c.confrelid) as ftable
from pg_constraint c
where c.confrelid = (select oid from pg_class where relname = 'TARGET_TABLE_NAME') and
c.confkey #> (select array_agg(attnum) from pg_attribute
where attname = 'TARGET_COLUMN_NAME' and attrelid = c.confrelid);
This query requires only the referenced table name and column name, and produces a result set containing both sides of the foreign key.
select confrelid::regclass, af.attname as fcol,
conrelid::regclass, a.attname as col
from pg_attribute af, pg_attribute a,
(select conrelid,confrelid,conkey[i] as conkey, confkey[i] as confkey
from (select conrelid,confrelid,conkey,confkey,
generate_series(1,array_upper(conkey,1)) as i
from pg_constraint where contype = 'f') ss) ss2
where af.attnum = confkey and af.attrelid = confrelid and
a.attnum = conkey and a.attrelid = conrelid
AND confrelid::regclass = 'my_table'::regclass AND af.attname = 'my_referenced_column';
Example result set:
confrelid | fcol | conrelid | col
----------+----------------------+---------------+-------------
my_table | my_referenced_column | some_relation | source_type
my_table | my_referenced_column | some_feature | source_type
All credit to Lane and Krogh at the PostgreSQL forum.
Personally, I prefer to query based on the referenced unique constraint rather than the column. That would look something like this:
SELECT rc.constraint_catalog,
rc.constraint_schema||'.'||tc.table_name AS table_name,
kcu.column_name,
match_option,
update_rule,
delete_rule
FROM information_schema.referential_constraints AS rc
JOIN information_schema.table_constraints AS tc USING(constraint_catalog,constraint_schema,constraint_name)
JOIN information_schema.key_column_usage AS kcu USING(constraint_catalog,constraint_schema,constraint_name)
WHERE unique_constraint_catalog='catalog'
AND unique_constraint_schema='schema'
AND unique_constraint_name='constraint name';
Here is a version that allows querying by column name:
SELECT rc.constraint_catalog,
rc.constraint_schema||'.'||tc.table_name AS table_name,
kcu.column_name,
match_option,
update_rule,
delete_rule
FROM information_schema.referential_constraints AS rc
JOIN information_schema.table_constraints AS tc USING(constraint_catalog,constraint_schema,constraint_name)
JOIN information_schema.key_column_usage AS kcu USING(constraint_catalog,constraint_schema,constraint_name)
JOIN information_schema.key_column_usage AS ccu ON(ccu.constraint_catalog=rc.unique_constraint_catalog AND ccu.constraint_schema=rc.unique_constraint_schema AND ccu.constraint_name=rc.unique_constraint_name)
WHERE ccu.table_catalog='catalog'
AND ccu.table_schema='schema'
AND ccu.table_name='name'
AND ccu.column_name='column';
SELECT
main_table.table_name AS main_table_table_name,
main_table.column_name AS main_table_column_name,
main_table.constraint_name AS main_table_constraint_name,
info_other_table.table_name AS info_other_table_table_name,
info_other_table.constraint_name AS info_other_table_constraint_name,
info_other_table.column_name AS info_other_table_column_name
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE main_table
INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS other_table
ON other_table.unique_constraint_name = main_table.constraint_name
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE info_other_table
ON info_other_table.constraint_name = other_table.constraint_name
WHERE main_table.table_name = 'MAIN_TABLE_NAME';
A simple request for recovered the names of foreign key as well as the names of the tables:
SELECT CONSTRAINT_NAME, table_name
FROM
information_schema.table_constraints
WHERE table_schema='public' and constraint_type='FOREIGN KEY'
If you use the psql client, you can simply issue the \d table_name command to see which tables reference the given table. From the linked documentation page:
\d[S+] [ pattern ]
For each relation (table, view, materialized view, index, sequence, or foreign table) or composite type matching the pattern,
show all columns, their types, the tablespace (if not the default) and
any special attributes such as NOT NULL or defaults. Associated
indexes, constraints, rules, and triggers are also shown. For foreign
tables, the associated foreign server is shown as well.
Table constraints can include multiple columns. The trick to getting this right is to join each column by their constraint ordinal positions. If you don't join correctly your script will blow up with duplicate rows 😥 whenever a table has multiple columns in a unique constraint.
Query
Lists all foreign key columns and their references.
select
-- unique reference info
ref.table_catalog as ref_database,
ref.table_schema as ref_schema,
ref.table_name as ref_table,
ref.column_name as ref_column,
refd.constraint_type as ref_type, -- e.g. UNIQUE or PRIMARY KEY
-- foreign key info
fk.table_catalog as fk_database,
fk.table_schema as fk_schema,
fk.table_name as fk_table,
fk.column_name as fk_column,
map.update_rule as fk_on_update,
map.delete_rule as fk_on_delete
-- lists fk constraints and maps them to pk constraints
from information_schema.referential_constraints as map
-- join unique constraints (e.g. PKs constraints) to ref columns info
inner join information_schema.key_column_usage as ref
on ref.constraint_catalog = map.unique_constraint_catalog
and ref.constraint_schema = map.unique_constraint_schema
and ref.constraint_name = map.unique_constraint_name
-- optional: to include reference constraint type
left join information_schema.table_constraints as refd
on refd.constraint_catalog = ref.constraint_catalog
and refd.constraint_schema = ref.constraint_schema
and refd.constraint_name = ref.constraint_name
-- join fk columns to the correct ref columns using ordinal positions
inner join information_schema.key_column_usage as fk
on fk.constraint_catalog = map.constraint_catalog
and fk.constraint_schema = map.constraint_schema
and fk.constraint_name = map.constraint_name
and fk.position_in_unique_constraint = ref.ordinal_position --IMPORTANT!
Helpful links
information_schema.table_constraints
information_schema.referential_constraints
information_schema.key_column_usage
Explanation
consider the relationship between these to tables.
create table foo (
a int,
b int,
primary key (a,b)
);
create table bar (
c int,
d int,
foreign key (c,d) references foo (b,a) -- i flipped a,b to make a point later.
);
get table constraint names
select * from information_schema.table_constraints where table_name in ('foo','bar');
| constraint_name | table_name | constraint_type |
| --------------- | ---------- | --------------- |
| foo_pkey | foo | PRIMARY KEY |
| bar_c_d_fkey | bar | FOREIGN KEY |
constraint references
select * from information_schema.referential_constraints where constraint_name in ('bar_c_d_fkey');
| constraint_name | unique_constraint_name |
| --------------- | ---------------------- |
| bar_c_d_fkey | foo_pkey |
constraint ordinal_position of column.
select * from information_schema.key_column_usage where table_name in ('foo','bar');
| constraint_name | table_name | column_name | ordinal_position | position_in_unique_constraint |
| --------------- | ---------- | ----------- | ---------------- | ----------------------------- |
| foo_pkey | foo | a | 1 | null |
| foo_pkey | foo | b | 2 | null |
| bar_c_d_fkey | bar | c | 1 | 2 |
| bar_c_d_fkey | bar | d | 2 | 1 |
Now all that's left is to join them together. The main query above is one way you could do so.
I turned #Tony K's answer into a reusable function that takes in a schema/table/column tuple and returns all tables that have a foreign key relationship: https://gist.github.com/colophonemes/53b08d26bdd219e6fc11677709e8fc6c
I needed something like this in order to implement a script that merged two records into a single record.
Function:
CREATE SCHEMA utils;
-- Return type for the utils.get_referenced_tables function
CREATE TYPE utils.referenced_table_t AS (
constraint_name name,
schema_name name,
table_name name,
column_name name[],
foreign_schema_name name,
foreign_table_name name
);
/*
A function to get all downstream tables that are referenced to a table via a foreign key relationship
The function looks at all constraints that contain a reference to the provided schema-qualified table column
It then generates a list of the schema/table/column tuples that are the target of these references
Idea based on https://stackoverflow.com/a/21125640/7114675
Postgres built-in reference:
- pg_namespace => schemas
- pg_class => tables
- pg_attribute => table columns
- pg_constraint => constraints
*/
CREATE FUNCTION utils.get_referenced_tables (schema_name name, table_name name, column_name name)
RETURNS SETOF utils.referenced_table_t AS $$
-- Wrap the internal query in a select so that we can order it more easily
SELECT * FROM (
-- Get human-readable names for table properties by mapping the OID's stored on the pg_constraint
-- table to the underlying value on their relevant table.
SELECT
-- constraint name - we get this directly from the constraints table
pg_constraint.conname AS constraint_name,
-- schema_name
(
SELECT pg_namespace.nspname FROM pg_namespace
WHERE pg_namespace.oid = pg_constraint.connamespace
) as schema_name,
-- table_name
(
SELECT pg_class.relname FROM pg_class
WHERE pg_class.oid = pg_constraint.conrelid
) as table_name,
-- column_name
(
SELECT array_agg(attname) FROM pg_attribute
WHERE attrelid = pg_constraint.conrelid
AND ARRAY[attnum] <# pg_constraint.conkey
) AS column_name,
-- foreign_schema_name
(
SELECT pg_namespace.nspname FROM pg_namespace
WHERE pg_namespace.oid = (
SELECT pg_class.relnamespace FROM pg_class
WHERE pg_class.oid = pg_constraint.confrelid
)
) AS foreign_schema_name,
-- foreign_table_name
(
SELECT pg_class.relname FROM pg_class
WHERE pg_class.oid = pg_constraint.confrelid
) AS foreign_table_name
FROM pg_constraint
-- confrelid = constraint foreign relation id = target schema + table
WHERE confrelid IN (
SELECT oid FROM pg_class
-- relname = target table name
WHERE relname = get_referenced_tables.table_name
-- relnamespace = target schema
AND relnamespace = (
SELECT oid FROM pg_namespace
WHERE nspname = get_referenced_tables.schema_name
)
)
-- confkey = constraint foreign key = the column on the foreign table linked to the target column
AND confkey #> (
SELECT array_agg(attnum) FROM pg_attribute
WHERE attname = get_referenced_tables.column_name
AND attrelid = pg_constraint.confrelid
)
) a
ORDER BY
schema_name,
table_name,
column_name,
foreign_table_name,
foreign_schema_name
;
$$ LANGUAGE SQL STABLE;
Example usage:
/*
Function to merge two people into a single person
The primary person (referenced by primary_person_id) will be retained, the secondary person
will have all their records re-referenced to the primary person, and then the secondary person
will be deleted
Note that this function may be destructive! For most tables, the records will simply be merged,
but in cases where merging would violate a UNIQUE or EXCLUSION constraint, the secondary person's
respective records will be dropped. For example, people cannot have overlapping pledges (on the
pledges.pledge table). If the secondary person has a pledge that overlaps with a pledge that is
on record for the primary person, the secondary person's pledge will just be deleted.
*/
CREATE FUNCTION utils.merge_person (primary_person_id BIGINT, secondary_person_id BIGINT)
RETURNS people.person AS $$
DECLARE
_referenced_table utils.referenced_table_t;
_col name;
_exec TEXT;
_primary_person people.person;
BEGIN
-- defer all deferrable constraints
SET CONSTRAINTS ALL DEFERRED;
-- This loop updates / deletes all referenced tables, setting the person_id (or equivalent)
-- From secondary_person_id => primary_person_id
FOR _referenced_table IN (SELECT * FROM utils.get_referenced_tables('people', 'person', 'id')) LOOP
-- the column_names are stored as an array, so we need to loop through these too
FOREACH _col IN ARRAY _referenced_table.column_name LOOP
RAISE NOTICE 'Merging %.%(%)', _referenced_table.schema_name, _referenced_table.table_name, _col;
-- FORMAT allows us to safely build a dynamic SQL string
_exec = FORMAT(
$sql$ UPDATE %s.%s SET %s = $1 WHERE %s = $2 $sql$,
_referenced_table.schema_name,
_referenced_table.table_name,
_col,
_col
);
RAISE NOTICE 'SQL: %', _exec;
-- wrap the execution in a block so that we can handle uniqueness violations
BEGIN
EXECUTE _exec USING primary_person_id, secondary_person_id;
RAISE NOTICE 'Merged %.%(%) OK!', _referenced_table.schema_name, _referenced_table.table_name, _col;
EXCEPTION
-- Error codes are Postgres built-ins, see https://www.postgresql.org/docs/9.6/errcodes-appendix.html
WHEN unique_violation OR exclusion_violation THEN
RAISE NOTICE 'Cannot merge record with % = % on table %.%, falling back to deletion!', _col, secondary_person_id, _referenced_table.schema_name, _referenced_table.table_name;
_exec = FORMAT(
$sql$ DELETE FROM %s.%s WHERE %s = $1 $sql$,
_referenced_table.schema_name,
_referenced_table.table_name,
_col
);
RAISE NOTICE 'SQL: %', _exec;
EXECUTE _exec USING secondary_person_id;
RAISE WARNING 'Deleted record with % = % on table %.%', _col, secondary_person_id, _referenced_table.schema_name, _referenced_table.table_name;
END;
END LOOP;
END LOOP;
-- Once we've updated all the tables, we can safely delete the secondary person
RAISE WARNING 'Deleted person with id = %', secondary_person_id;
-- Get our primary person so that we can return them
SELECT * FROM people.person WHERE id = primary_person_id INTO _primary_person;
RETURN _primary_person;
END
$$ LANGUAGE plpgsql VOLATILE;
Note the use of SET CONSTRAINTS ALL DEFERRED; in the function, which ensures that foreign key relationships are checked at the end of the merge. You may need to update your constraints to be DEFERRABLE INITIALLY DEFERRED:
ALTER TABLE settings.contact_preference
DROP CONSTRAINT contact_preference_person_id_fkey,
DROP CONSTRAINT person_id_current_address_id_fkey,
ADD CONSTRAINT contact_preference_person_id_fkey
FOREIGN KEY (person_id)
REFERENCES people.person(id)
ON UPDATE CASCADE ON DELETE CASCADE
DEFERRABLE INITIALLY IMMEDIATE,
ADD CONSTRAINT person_id_current_address_id_fkey
FOREIGN KEY (person_id, current_address_id)
REFERENCES people.address(person_id, id)
DEFERRABLE INITIALLY IMMEDIATE
;