I have to delete records from a table 'project' where internally it has to delete (2000-3000) records from a table 'test_suite' because project_id is foreign key in the test_suite table. The database is Postgres.
I am using the following JPA query to delete the records from table 'test_suite':
testSuiteRepository.deleteInBatchByProjectIdAndAutoGenerated(projectId, true);
This delete is consuming time and delete from the project table ends up throwing an exception that FK constraint violated.
Is there any efficient query for BULK DELETE in POSTGRES?
Related
I have a requirement of deleting records from the Postgres database tables.
We have a Customer table which is the main table, this table contains a primary key which is used in so many other tables as a FOREIGN KEY, I want to delete one of the customers as well as its reference used in other tables. Is there any way to delete the customer from main table as well as from other tables which contains foreign key.
Thanks in Advance.
In the other tables, you want a cascading delete foreign key reference. You can create one in the database using:
alter table othertable add constraint fk_othertable_customerid
foreign key (customerid) references customers(customerid)
on delete cascade;
Note: This assumes that customerid is the name of the column in both tables and that it is already defined in the other tables.
A cascading foreign key constraint does exactly what you specify. When a row is deleted in the reference table, then all related rows are deleted.
If you already have foreign key constraints on customerid, then drop the existing constraint and add the cascading version.
Hi i have one doubt in postgres. In database have 100 tables . few tables have primary key and foreign key relationship and few tables do not have relationship.
Here I want delete all tables useing delete statement.
When I tried delete statement for all tables that time getting error pk and fk violation.
How will delete fk first and pk tables.
Could you tell me how to write a query to achieve this task I postgres.
You can try the CASCADE option.
https://www.postgresql.org/docs/current/ddl-constraints.html -> item 5.3.5
I wanted to remove all foreign key constraints from a schema. I was successful in dropping constraints from most of the tables but in few of them drop foreign key constraint query is getting stuck.
ALTER TABLE table_name DROP CONSTRAINT fkey_name;
I tried truncate cascade but it was also getting stuck. I deleted all rows from both the tables manually. Still getting stuck.
Edits: By getting stuck I mean query continues running for long time without any error message even though tables are empty.
Check for any dead locks using
SELECT * FROM pg_stat_activity;
If any then kill and run below sql,and then drop using
SELECT pg_terminate_backend(pid);
If Not solved check for any virtual transaction
SELECT database, gid FROM pg_prepared_xacts;
Rollback using
ROLLBACK PREPARED 'gid';
Postgres on Linux
When I do the following command.
DELETE FROM some_table;
It also deletes data from another table. How do I found out how?
There is probably a foreign key constraint that points to your table and is defined with ON DELETE CASCADE.
Alternatively, there may be a trigger on the table that deletes the rows.
In psql, use \d some_table to see all such foreign keys and triggers.
Database 1 has foreign tables a and b on database 2.
How can we create indexes on these foreign tables a and b. These foreign tables are wrappers over database2.c and database2.d tables respectively, which do have the necessary indexes in place.
How would you create indexes on foreign tables a and b? Is that even possible?
I get a cannot create index on foreign table a - when I try a simple Create Index command in postgres
You cannot create index on a foreign table, instead write a trigger on foreign table and create a local table in postgres such that whenever an insert, update or delete is happening in your foreign table it will be reflected in your local table and index it.
Joining with a foreign table can result to slow query's.
Since indexes are not a option with foreign table, consider making a materialized view on a foreign table . Materialized views do allow indexing
CREATE FOREIGN TABLE members_fdw(...)
CREATE MATERIALIZED VIEW members AS
select * from members_fdw
WITH DATA
CREATE UNIQUE INDEX "member_id" ON members USING btree ("id");