I use Postgresql 9.4 to synchronize two database,
I use the extension postgres_fdw everything work great except, i have one table where i need to disable foreign key check, i use this statement :
ALTER TABLE foo DISABLE TRIGGER ALL;
It's work great on my local database but on my remote database the following statement do nothing :
ALTER FOREIGN TABLE foo1 DISABLE TRIGGER ALL;
However in the documentation the feature is present, someone know if they are a limitation or a bug on this version of pgsql.
https://www.postgresql.org/docs/9.4/static/sql-alterforeigntable.html
The second statement will disable all triggers defined on the foreign table on the local database.
To disable triggers in the remote database, you must login there and run ALTER DATABASE there.
Related
Precondition:
I am using Liquibase with SQL scripts in my app. I started testing with Oracle DB, but now I need to switch to PostgreSQL DB.
Problem:
When I added constraints, I didn't add the names of the constraints.
Liquibase changelog contains a script for dropping unique and primary key constraints:
alter table SCENARIO drop primary key/
alter table SCENARIO drop unique (OWNER_ID)/
This syntax doesn't sync with PostgreSQL
Could you give some advice, on how to resolve this problem?
Screenshots:
I have not been able to find an example of altering the underlying query defined for a MQT.
Are you supposed to drop the table and recreate it?
If you can would this be the correct way?
ALTER TABLE SCHEMA.MY_TABLE
(SELECT...)
DATA INITIALLY DEFERRED
REFRESH DEFERRED
MAINTAINED BY SYSTEM
I'm using AWS Aurora Postgres and using DMS to migrate from RDS postgres to Aurora PG. In order to perform the FULL LOAD I want to disable foreign key constraints and triggers on all the objects. I'm able to disable triggers but couldn't find a way to disable constraints.
Below doesn't work:
ALTER TABLE so_items DISABLE CONSTRAINT so_items_so_id_fkey;
It throws:
ERROR: syntax error at or near "CONSTRAINT"
LINE 1: ALTER TABLE so_items DISABLE CONSTRAINT so_items_so_id_fkey;
^
SQL state: 42601
Character: 30
Setting "session_replication_role" = "replica" in the parameter group didn't work. While the DMS task tries to truncate the table part of preparation it still fails with foreign key violation errors.
Please advise any workarounds.
Note: I couldn't do below since in RDS I do not have permissions to do so even with master account:
alter table so_items disable trigger ALL;
ERROR: permission denied: "RI_ConstraintTrigger_c_16520" is a system trigger
SQL state: 42501
You shouldn't modify the triggers a Postgres constraint relies on. This is an implementation detail for which you shouldn't care about.
You cannot disable constraints, really.
To turn constraints temporarily off, you can defer the constraint check to the end of transactions:
ALTER TABLE so_items ALTER CONSTRAINT so_items_so_id_fkey DEFERRABLE INITIALLY DEFERRED;
With that modification the constraint is evaluated after a modification at the end of the current transaction. This will allow you to break the constraint inside of a transaction.
You may DROP CONSTRAINTs
ALTER TABLE so_items DROP CONSTRAINT so_items_so_id_fkey;
which will delete it permanently.
Edit: It is also possible to disable the triggers which also affects the foreign key constraints of the table
ALTER TABLE so_items DISABLE TRIGGER ALL;
But when you are re-enabling the triggers afterwards, the foreign keys are not checked. This might lead to invalid / inconsistent foreign keys in the database.
For Postgres :-
It is easier to disable all triggers with:
SET session_replication_role = 'replica';
And after migration reenable all with
SET session_replication_role = 'origin';
I'm trying to refresh some data that's referred to by other data - I want to truncate and reload the ms_automobile table, but the rm_automobile table has a foreign key to it.
It looks like the 'DISABLE TRIGGER' statements are working (run as postgres, a superuser):
mobilesurvey=# ALTER TABLE ms_automobile DISABLE TRIGGER ALL;
ALTER TABLE
mobilesurvey=# ALTER TABLE rm_automobile DISABLE TRIGGER ALL;
ALTER TABLE
But I can't then truncate the ms_automobile table:
mobilesurvey=# TRUNCATE TABLE ms_automobile;
ERROR: cannot truncate a table referenced in a foreign key constraint
DETAIL: Table "rm_automobile" references "ms_automobile".
HINT: Truncate table "rm_automobile" at the same time, or use TRUNCATE ... CASCADE.
Again, I do not want to lose the rm_automobile data; after the TRUNCATE I'm planning on doing a pg_restore that includes the missing ms_automobile data.
If possible, I'd like to disable instead of dropping the constraints - there are more of them, and maintaining disable/enable seems a lot less error-prone than maintaining drop/add.
So, how can I actually disable the foreign keys here?
Disabling triggers works as you expect on DELETE (and not on TRUNCATE).
DELETE FROM ms_automobile;
TRUNCATE is implemented in the specific way different from INSERT/UPDATE/DELETE. It doesn't use triggers but checks referential integrity once before its execution.
I need to make an update on a table (sql 2008 server).
I use sqlcmd and my update is on a file named update.sql and i use sqlcmd for run it.
In this update, i need to replace a value for an other.
Example:
use db
go
update db SET fab=9 where fab=10
Go
update db set fab=7 where fab=8
GO
update db set fab=6 where fab=17
GO
update db set fab=11 where fab=12
GO
And I have this message :
Message 2627, Level 14, State 1 sqlserveur Server, Line 1 Violation of PRIMARY KEY constraint PK_database can not insert duplicate key in object dbo.database. duplicate key value is <2.9 the statement has been terminated
Could you please tell me how I can use THE NOCHEK CONTRAINT, because I've try it but it's does work.
thank you very Much!!!!!
ALTER TABLE Test_Table
NOCHECK CONSTRAINT your_Constraint_Name
GO
This is one way of disabling your constraints but it only works for Foreign keys and check constraints.
Your Error Message says Violation of PRIMARY KEY constraint this cant be disabled you will need to drop the Primary Key.