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 1kd6WH-00039n-8z for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Nov 2020 06:55:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kd6WG-0006Mf-8e for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Nov 2020 06:55:44 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kd6WF-0006MX-UD for pgsql-hackers@lists.postgresql.org; Thu, 12 Nov 2020 06:55:44 +0000 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kd6WD-0003Jz-Nf for pgsql-hackers@lists.postgresql.org; Thu, 12 Nov 2020 06:55:42 +0000 Received: by mail-pg1-x533.google.com with SMTP id e21so3355316pgr.11 for ; Wed, 11 Nov 2020 22:55:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:message-id:to:cc:subject:from:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=4o0RsGKT3/6BV6i4U/h6tAVocA4hSVLf6zKXcikd6mk=; b=aGZ64sW2zgSdWfysbJyMHDCxl51UzBDlBu67+/wBO6y56Bdc7s/L7VdKNjn5gde+ZQ 2c5IfWSJWb3BWHl9ep/ZwhsgdCG15wSriPcBpbcnDym2t/MzSXzxNFA6OLHVoiPu2RDE BPipfD8yA03CdfaKqLBmNNg7+Whj+KJB/+DBSE7iGjewWeMJWdu4W5FZHZ8nbSCggIeU dAy3xJH7n7N7Xx+Zg+6TJ//GLt1ZGCzc3+Gno1uX0W5q88zmE4cxp96OT72S5IkWhdZ3 Wn5KLGWmdQMfR113RUvHLyzOTqUNk3/Hhy0UtASQ20QDzLHLfmDQ7TNtUsTt8IAgY2LD fSbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:to:cc:subject:from:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=4o0RsGKT3/6BV6i4U/h6tAVocA4hSVLf6zKXcikd6mk=; b=bh9ChHIPMdi1ANyUcEptEXmLGFTHdj65pO4M0HHtpAxgZQ/JTn0h+Nv6WczAWsx0IZ 0n95YLijofqgpaSAlMWY+lC5go49jI9p8++Ekujz28XSlxqcqWxrX3+mZJK9c9o92KFR l/p5f1eDWFCsAsFuzpGqpfQWZ6OUBChdgfQj6pCWPvtjeRKNJTwgfFywVhcAW8DfAcBX JVl7IN44e2MWA/uN4fVuOw/QYlDo6gXalggN+CroVfaWFZs8vBd9jK92zDTppP+6SGnQ N+xsM3PDzh5FqVVTT5QjSI8y89SY/kk3PgfX/NPDc0yGvJoItbBXmjn6zVPPcWlAXce4 l35g== X-Gm-Message-State: AOAM530UScNGjantgRpFMwYtAfFYreLGRHGhrpgDqNF6bfV/kf59QWt3 cl1+jzVcwBxAR5qVL8Ed550= X-Google-Smtp-Source: ABdhPJzDtUt9zyqehXa3dj9h6ZyC60xLIlYUWL16OxLFfBRCGhTOOITSejS+ZgX12aNLHMmP8pPn0A== X-Received: by 2002:a17:90a:a48c:: with SMTP id z12mr7874533pjp.108.1605164140555; Wed, 11 Nov 2020 22:55:40 -0800 (PST) Received: from localhost (KD036014041111.ppp-bb.dion.ne.jp. [36.14.41.111]) by smtp.gmail.com with ESMTPSA id x123sm5108550pfb.212.2020.11.11.22.55.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Nov 2020 22:55:40 -0800 (PST) Date: Thu, 12 Nov 2020 15:55:37 +0900 (JST) Message-Id: <20201112.155537.905170546612920882.horikyota.ntt@gmail.com> To: sfrost@snowman.net Cc: osumi.takamichi@fujitsu.com, masao.fujii@oss.nttdata.com, ashutosh.bapat.oss@gmail.com, tsunakawa.takay@fujitsu.com, pgsql-hackers@lists.postgresql.org Subject: Re: In-place persistance change of a relation From: Kyotaro Horiguchi In-Reply-To: <20201111145644.GY16415@tamriel.snowman.net> References: <20201111.173317.460890039962481381.horikyota.ntt@gmail.com> <20201111145644.GY16415@tamriel.snowman.net> User-Agent: Mew version 6.8 on Emacs 26.1 Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk At Wed, 11 Nov 2020 09:56:44 -0500, Stephen Frost wrote in > Greetings, > > * Kyotaro Horiguchi (horikyota.ntt@gmail.com) wrote: > > At Tue, 10 Nov 2020 09:33:12 -0500, Stephen Frost wrote in > > > * Kyotaro Horiguchi (horikyota.ntt@gmail.com) wrote: > > > > For fuel(?) of the discussion, I tried a very-quick PoC for in-place > > > > ALTER TABLE SET LOGGED/UNLOGGED and resulted as attached. After some > > > > trials of several ways, I drifted to the following way after poking > > > > several ways. > > > > > > > > 1. Flip BM_PERMANENT of active buffers > > > > 2. adding/removing init fork > > > > 3. sync files, > > > > 4. Flip pg_class.relpersistence. > > > > > > > > It always skips table copy in the SET UNLOGGED case, and only when > > > > wal_level=minimal in the SET LOGGED case. Crash recovery seems > > > > working by some brief testing by hand. > > > > > > Somehow missed that this patch more-or-less does what I was referring to > > > down-thread, but I did want to mention that it looks like it's missing a > > > necessary FlushRelationBuffers() call before the sync, otherwise there > > > could be dirty buffers for the relation that's being set to LOGGED (with > > > wal_level=minimal), which wouldn't be good. See the comments above > > > smgrimmedsync(). > > > > Right. Thanks. However, since SetRelFileNodeBuffersPersistence() > > called just above scans shared buffers so I don't want to just call > > FlushRelationBuffers() separately. Instead, I added buffer-flush to > > SetRelFileNodeBuffersPersistence(). > > Maybe I'm missing something, but it sure looks like in the patch that > SetRelFileNodeBuffersPersistence() is being called after the > smgrimmedsync() call, and I don't think you get to just switch the order > of those- the sync is telling the kernel to make sure it's written to > disk, while the FlushBuffer() is just writing it into the kernel but > doesn't provide any guarantee that the data has actually made it to > disk. We have to FlushBuffer() first, and then call smgrimmedsync(). > Perhaps there's a way to avoid having to go through shared buffers > twice, and I generally agreed it'd be good if we could avoid doing so, > but this approach doesn't look like it actually works. Yeah, sorry for the rare-baked version.. I was confused about the order at the time. The next version works like this: LOGGED->UNLOGGED for each relations: minimal> if it is index call ambuildempty() (which syncs the init fork) else WAL-log smgr_create then sync the init file. ... commit time: abort time: UNLOGGED->LOGGED for each relations: minimal> ... commit time: abort time: > > FWIW this is a revised version of the PoC, which has some known > > problems. > > > > - Flipping of Buffer persistence is not WAL-logged nor even be able to > > be safely roll-backed. (It might be better to drop buffers). > > Not sure if it'd be better to drop buffers or not, but figuring out how > to deal with rollback seems pretty important. How is the persistence > change in the catalog not WAL-logged though..? Rollback works as the above. Buffer persistence change is registered in pending-deletes. Persistence change in catalog is rolled back in the ordinary way (or automatically). If wal_level > minimal, persistence change of buffers is propagated to standbys by WAL. However I'm not sure we need wal-logging otherwise, the next version emits WAL since SMGR_CREATE is always logged by existing code. > > - This version handles indexes but not yet handle toast relatins. > > Would need to be fixed, of course. Fixed. > > - tableAMs are supposed to support this feature. (but I'm not sure > > it's worth allowing them not to do so). > > Seems like they should. Init fork of index relations needs a call to ambuildempty() instead of "log_smgrcreate-smgrimmedsync" after smgrcreate. Instead of adding similar interface in indexAm, I reverted changes of tableam and make RelationCreate/DropInitFork() directly do that. That introduces new include of amapi.h to storage.c, which is a bit uneasy. The previous version give up the in-place persistence change in the case where wal_level > minimal and SET LOGGED since that needs WAL to be emitted. However, in-place change still has the advantage of not running a table copy. So the next verson always runs persistence change in-place. As suggested by Andres, I'll send a summary of this patch. The patch will be attached to the coming mail. regards. -- Kyotaro Horiguchi NTT Open Source Software Center