I have created an empty UNLOGGED
table to faster copy a large amount of data (over 1 Billion rows). Loading the data took around 4 hours.
Now I want to set the table to LOGGED
to make it safe for unexpected shutdowns and crashes. This process takes a long time. In fact it takes longer than loading the data. Is this normal, or is there a way to speed it up?
Best Answer
Problem
I believe currently
SET LOGGED
rewrites the table using the WAL (essentially doing the whole operation), and rewrites the indexes.So I found a thread about this on the lists
You can see the patch that added the
SET (LOGGED|UNLOGGED)
. The implementation hasn't changed much, though there was a plan to fix it that acknowledged the problemsBut it seems no more work was done. Looking at the code you can see it,
Checking that comment,
So you can see, that a rewrite is still required. I guess.
Potential solution
You may be better of copying all of the data into the table on the same transactions that creates the table. Which disables the rewrite, and is mentioned in the docs.
This would skip the WAL write and heap rewrite.