Re: Please help me understand unlogged tables

From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: Alexander Stoddard <alexander(dot)stoddard(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Please help me understand unlogged tables
Date: 2018-02-01 00:45:28
Message-ID: 20180201004528.GA27440@paquier.xyz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Jan 31, 2018 at 10:52:38AM -0600, Alexander Stoddard wrote:
> If a table is set to unlogged is it inherently non-durable? That, is any
> crash or unsafe shutdown _must_ result in truncation upon recovery?

Yes, they are designed like that. Upon recovery all unlogged tables are
re-initialized. What happens internally is that their init fork files
gets copied to become the one in use.

> I can imagine a table that is bulk loaded in a warehousing scenario and
> then sitting statically could be safe, but maybe the question becomes how
> could the system know it is unchanged if it isn't logged...
>
> Would a correct solution be to bulk load into an unlogged table and then
> (fairly cheaply?) set the table back to being logged?

Note that switching a table from unlogged to logged using ALTER TABLE
generates a large amount of WAL, as you need to make this table's data
consistent it is necessary to copy a full image of it.

> If I my mental model is correct the table would be liable to being lost to
> a crash during loading but once set back to logged it is durable. Is that
> correct?

Yes, ALTER TABLE makes that possible but be careful about the WAL
generated because of the table rewrite.

> Is it the case that setting a large table to logged status after loading it
> unlogged is a reasonably cheap operation?

If you load a logged table the cost would be the same as switching it
from unlogged to logged. So what matters is how much operation is
generated between the time you created the table, like random writes on
it. For an initial load on a very large table, you could reduce
wal_level temporarily to minimal, and make the WAL generated less
painful. For a one-time load this can matter.
--
Michael

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Steven Lembark 2018-02-01 03:48:22 Re: PG Sharding
Previous Message Jeff Janes 2018-01-31 22:44:43 Re: Please help me understand unlogged tables