In PostgreSQL 13, I am loading data to a Staging table which has the same schema structure as the old table i.e. Target table and once all rows are loaded I am Dropping the old table and renaming Staging to the old table name.
The above works, But my concern is even if the table rename command should not take a lot of time still the worst-case scenario, What if someone tries to query the old table at the same time while it was executing the DROP old table query? my understanding is their application will fail basically some sort of table lock issue.
So how can we avoid that or how can we safely rename/switch the table without such issues?
Thanks!
UPDATE
In MS SQL Server we can do table switching instead of renaming tables which is explained very well in the below link. So do we have similar option in Postgresql as well?
https://littlekendra.com/2017/01/19/why-you-should-switch-in-staging-tables-instead-of-renaming/