Re: Does PostgreSQL use atomic file creation of FS?

From: Thomas Munro <thomas(dot)munro(at)enterprisedb(dot)com>
To: dilaz03(at)gmail(dot)com
Cc: Karsten(dot)Hilbert(at)gmx(dot)net, "pgsql-generallists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Does PostgreSQL use atomic file creation of FS?
Date: 2018-12-12 21:34:27
Message-ID: CAEepm=0PBwCGLDXx7FtcyOeXc_vWrhycTguq5sw7b6q=EzRRDA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Dec 12, 2018 at 11:52 PM Dmitry Lazurkin <dilaz03(at)gmail(dot)com> wrote:
> Thank you. But I have read this. I said about network file system only
> for example. I would like to known how PostgreSQL handles this specific
> case (of course if someone knowns a answer):
>
> fd = open(file, "w");
> write(fd, data);
> // crash and now I have empty file which isn't correct
> fsync(fd);
>
> PS. I think PostgreSQL doesn't have this problem.

It depends on the context, but in general PostgreSQL knows about that
sort of thing. When the cluster shuts down, it records that it shut
down cleanly, meaning that everything that should be on disk is on
disk. When you start the cluster up, if it sees that it didn't shut
down cleanly, it enters recovery. During recovery it tolerates files
being too short while it's replaying the WAL to get back to a
consistent state. See the comment in mdread() for example:

https://github.com/postgres/postgres/blob/master/src/backend/storage/smgr/md.c#L755

It's called "write-ahead log" because we log our intention before we
write to data files (and make sure it's on disk first), so we'll
always replay the same effects again if we're interrupted. The WAL is
a magic source of reliability (we can do it again if things go wrong)
and also performance (IO becomes serial, optimised for the storage
hardware).

https://www.postgresql.org/docs/current/wal-intro.html

--
Thomas Munro
http://www.enterprisedb.com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Ravi Krishna 2018-12-12 22:37:47 explain analyze cost
Previous Message Laurenz Albe 2018-12-12 21:26:36 Re: Does PostgreSQL use atomic file creation of FS?