Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nD8zw-00084p-0L for pgsql-hackers@arkaria.postgresql.org; Thu, 27 Jan 2022 17:55:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nD8zu-0003kw-Fl for pgsql-hackers@arkaria.postgresql.org; Thu, 27 Jan 2022 17:55:50 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nD8zu-0003kn-4d for pgsql-hackers@lists.postgresql.org; Thu, 27 Jan 2022 17:55:50 +0000 Received: from lahtoruutu.iki.fi ([185.185.170.37]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nD8zs-0002LA-3v for pgsql-hackers@postgresql.org; Thu, 27 Jan 2022 17:55:49 +0000 Received: from [192.168.1.113] (dsl-hkibng22-54faa4-119.dhcp.inet.fi [84.250.164.119]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 43F721B00266 for ; Thu, 27 Jan 2022 19:55:46 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1643306146; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=inP6uZ9h2fazKHPvabGCPH15VNkvotJT/pN+8Imbl1Y=; b=prFVo8tv2DEevpJeV9x808LXAso/KuJy3SRiBgSIb7lrl9CyApaDOqPJ64nVg4bp/4DesE v2wcRjj9P6A7s1BQgr2izkSHhuPEQnZz7dDoY/ME4YlNqHHlZzQ5x3iSn3rQ1JWD0U09Bp DoqfC/oHYQ+oWhWfnxhgiZKc1eNFEXFj751itXX+Y1DWyFdpiNj98Ob1yrP6pdnbHmNVA6 AkzUZDHHAo8ijYlpOnN+FwGhucIIr5aB3hjL6NBWB2T84H2SHvqcnQCP8IrndHGB7qodC2 DsvmtU5cv284Z0rOrQ5s6Ms2qwKxx431aCuxNxi+npQjYCmvqOEVMTozaVXBPQ== Content-Type: multipart/mixed; boundary="------------6Pv1hP3ToLdc0VlXT2rEXvNP" Message-ID: Date: Thu, 27 Jan 2022 19:55:45 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.1 Content-Language: en-US From: Heikki Linnakangas Subject: Creation of an empty table is not fsync'd at checkpoint To: pgsql-hackers ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Seal: i=1; s=lahtoruutu; d=iki.fi; t=1643306146; a=rsa-sha256; cv=none; b=fhBwTxLXpFYr9kI1WV8RMVaAtoUq5EgFT5TMYTZpdeF2fePyAIu1nnD1Ls81ETYmBs/o1P SS5s6bKmbhPeyJ4HofzHNeiRJdO2SQotB4xPnRFfccjfOa39OeKQ7YY1IRejk+JNf0HLOK YqC+otI36zxmFvxpmEeHv/M9m04iLRv79Vd3bCJuvR7oE6nrBBw5GUWHvLSSF5B0lB4ntj 9qQPlPWjE0//Zk4i50TaoWOY5FmM0HmPXLdmnUSRru1FFSryaErfCT+gQdVhCqYf/0XFFE Ut5MRRRVBOzqxvmkxGsy/xlVrEWtzNWIQLVrUvleYxWyZrqxh4p1JqIou+PtNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1643306146; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=inP6uZ9h2fazKHPvabGCPH15VNkvotJT/pN+8Imbl1Y=; b=rqdwcLhNDcgS/+q9//kwKJT/XwDhJE4MVhuSOVAym1NzwaVHxH822G+4fmejJuqfsdCRP8 y/b7TINfpECzrovKfGRn37RDVd8H/QMEqJbd3rLF9JCZ0ecS8Uc11/TDOQAZp/7+qtt8fw WyNvbPY+tHSFxFfO5oJkP9+66z2lOUHNyp5BQ0eZ5nAyjRi9CjwC8wXnOzz/ukC8r2LFLL hbyfEBRj/WoxlCAdF9v+G05po3PUQPamalmm9Abca3vKqvP3jH1W3tG79bndzkhaAk4rPz 9pjWtSva4PuGG/C91GG+/G+iR0oQfLGQ0uUqIDgUhWQCI3F2KicxJmp9GaHFnA== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------6Pv1hP3ToLdc0VlXT2rEXvNP Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit If you create an empty table, it is not fsync'd. As soon as you insert a row to it, register_dirty_segment() gets called, and after that, the next checkpoint will fsync it. But before that, the creation itself is never fsync'd. That's obviously not great. The lack of an fsync is a bit hard to prove because it requires a hardware failure, or a simulation of it, and can be affected by filesystem options too. But I was able to demonstrate a problem with these steps: 1. Create a VM with two virtual disks. Use ext4, with 'data=writeback' option (I'm not sure if that's required). Install PostgreSQL on one of the virtual disks. 2. Start the server, and create a tablespace on the other disk: CREATE TABLESPACE foospc LOCATION '/data/heikki'; 3. Do this: CREATE TABLE foo (i int) TABLESPACE foospc; CHECKPOINT; 4. Immediately after that, kill the VM. I used: killall -9 qemu-system-x86_64 5. Restart the VM, restart PostgreSQL. Now when you try to use the table, you get an error: postgres=# select * from crashtest ; ERROR: could not open file "pg_tblspc/81921/PG_15_202201271/5/98304": No such file or directory I was not able to reproduce this without the tablespace on a different virtual disk, I presume because ext4 orders the writes so that the checkpoint implicitly always flushes the creation of the file to disk. I tried data=writeback but it didn't make a difference. But with a separate disk, it happens every time. I think the simplest fix is to call register_dirty_segment() from mdcreate(). As in the attached. Thoughts? - Heikki --------------6Pv1hP3ToLdc0VlXT2rEXvNP Content-Type: text/x-patch; charset=UTF-8; name="0001-Ensure-that-creation-of-an-empty-relfile-is-fsync-d-.patch" Content-Disposition: attachment; filename*0="0001-Ensure-that-creation-of-an-empty-relfile-is-fsync-d-.pa"; filename*1="tch" Content-Transfer-Encoding: base64 RnJvbSAwNzViNTlmNGMzMGMwYTlkZTY2ZmU3MmNlNDljYTQwYjVhOTM4OGY5IE1vbiBTZXAg MTcgMDA6MDA6MDAgMjAwMQpGcm9tOiBIZWlra2kgTGlubmFrYW5nYXMgPGhlaWtraS5saW5u YWthbmdhc0Bpa2kuZmk+CkRhdGU6IFRodSwgMjcgSmFuIDIwMjIgMTk6NTI6MzcgKzAyMDAK U3ViamVjdDogW1BBVENIIDEvMV0gRW5zdXJlIHRoYXQgY3JlYXRpb24gb2YgYW4gZW1wdHkg cmVsZmlsZSBpcyBmc3luYydkIGF0CiBjaGVja3BvaW50LgoKSWYgeW91IGNyZWF0ZSBhIHRh YmxlIGFuZCBkb24ndCBpbnNlcnQgYW55IGRhdGEgaW50byBpdCwgdGhlIHJlbGF0aW9uIGZp bGUKaXMgbmV2ZXIgZnN5bmMnZC4gWW91IGRvbid0IGxvc2UgZGF0YSwgYmVjYXVzZSBhbiBl bXB0eSB0YWJsZSBkb2Vzbid0IGhhdmUKYW55IGRhdGEgdG8gYmVnaW4gd2l0aCwgYnV0IGlm IHlvdSBjcmFzaCBhbmQgbG9zZSB0aGUgZmlsZSwgc3Vic2VxdWVudApvcGVyYXRpb25zIG9u IHRoZSB0YWJsZSB3aWxsIGZhaWwgd2l0aCAiY291bGQgbm90IG9wZW4gZmlsZSIgZXJvcnIu CgpUbyBmaXgsIHJlZ2lzdGVyIGFuIGZzeW5jIHJlcXVlc3QgaW4gbWRjcmVhdGUoKSwgbGlr ZSB3ZSBkbyBm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