snippetMinor
How do I swap tables atomically in Oracle?
Viewed 0 times
tableshowswapatomicallyoracle
Problem
How to do I swap two tables in an atomic fashion in Oracle?
I would like to perform the equivalent of:
but what happens if a query needs table foo in between those two lines when there is no table foo? Some lock would be required.
P.S. For a MySQL version of the question, see this question.
I would like to perform the equivalent of:
ALTER TABLE foo RENAME foo_tmp;
ALTER TABLE foo_new RENAME foo;but what happens if a query needs table foo in between those two lines when there is no table foo? Some lock would be required.
P.S. For a MySQL version of the question, see this question.
Solution
Create a public synonym that points to the new table.
Things will get a bit more complicated if you're accessing the tables involved with a user other than the schema owner, but this works if you're only accessing the table with the schema owner.
CREATE PUBLIC SYNONYM foo FOR foo_new;
RENAME foo TO foo_tmp;
-- At this point in time, any DMLs for foo will operate on foo_new, via the synonym
RENAME foo_new TO foo;
DROP PUBLIC SYNONYM foo;Things will get a bit more complicated if you're accessing the tables involved with a user other than the schema owner, but this works if you're only accessing the table with the schema owner.
Code Snippets
CREATE PUBLIC SYNONYM foo FOR foo_new;
RENAME foo TO foo_tmp;
-- At this point in time, any DMLs for foo will operate on foo_new, via the synonym
RENAME foo_new TO foo;
DROP PUBLIC SYNONYM foo;Context
StackExchange Database Administrators Q#177959, answer score: 4
Revisions (0)
No revisions yet.