You don't need the expensive VACUUM FULL
at all. Right after your big INSERT
there is nothing to clean up. No dead tuples, and all indexes are in pristine condition.
It can make sense to run ANALYZE
though to update completely changed table statistics right away.
For small tables DELETE
can be faster than TRUNCATE
, but for medium to large tables TRUNCATE
is typically faster.
And do it all in a single transaction. INSERT
after TRUNCATE
in the same transaction does not have to write to WAL and is much faster:
BEGIN;
TRUNCATE table1;
INSERT INTO table1 TABLE table2;
TRUNCATE table2; -- do you need this?
ANALYZE table1; -- might be useful
COMMIT;
Details:
If you have any indexes on table1
it pays to drop them first and recreate them after the INSERT
for big tables.
If you don't have depending objects, you might also just drop table1
and rename table2
, which would be much faster.
Either method requires an exclusive lock on the table. But you have been using VACUUM FULL
previously which takes an exclusive lock as well:
VACUUM FULL
rewrites the entire contents of the table into a new disk
file with no extra space, allowing unused space to be returned to the
operating system. This form is much slower and requires an exclusive
lock on each table while it is being processed.
So it's safe to assume an exclusive lock is ok for you.