Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qYp2H-002orr-JA for pgsql-hackers@arkaria.postgresql.org; Wed, 23 Aug 2023 14:40:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qYp2G-00DWpN-Ga for pgsql-hackers@arkaria.postgresql.org; Wed, 23 Aug 2023 14:40:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qYp2G-00DWpF-2N for pgsql-hackers@lists.postgresql.org; Wed, 23 Aug 2023 14:40:39 +0000 Received: from meesny.iki.fi ([195.140.195.201]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qYp2D-000aOv-0I for pgsql-hackers@postgresql.org; Wed, 23 Aug 2023 14:40:39 +0000 Received: from [192.168.1.115] (dsl-hkibng22-54f8db-125.dhcp.inet.fi [84.248.219.125]) (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 meesny.iki.fi (Postfix) with ESMTPSA id 4RW85X5y2xzyVX; Wed, 23 Aug 2023 17:40:32 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1692801635; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wCHU4KvRAxis9sUHrKdslawL28K/H+P+r6KPFO+IvTQ=; b=jdq9s0Meqtas4p35PJELVSzDXvMWHtgd6g7rIyzvcJxwE1gjHB2TxyXycAUevL8Jz4Mu8i tI6ehur/PciCCG4RF50hhIt84phcYrpb0JTohD+RLT9CtxsxB1aBzPztOmc8SKlmO0Jd8Q WL0Hv99yfoxTroyYctsFyeZBeMpwPjY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=meesny; t=1692801635; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wCHU4KvRAxis9sUHrKdslawL28K/H+P+r6KPFO+IvTQ=; b=dwkjNNq5n7enTNwvZDq3+1s+1HmUzOLas3aWde3+vP/UhjrfTYq70D4tk2djIF7QctxSGS UZRN3S0WZiIaCgCAdKRfpj6tvb4Tz3UsDQD9mAFN7xPdm5QSUEeE5+LTXSVlRbZSUV5Yl5 NP8E3Z1T72xYmhUoaZIXy73vHkt5IR8= ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi ARC-Seal: i=1; s=meesny; d=iki.fi; t=1692801635; a=rsa-sha256; cv=none; b=vNyv6oIWNrD8pwDpqQX5SjhfGAOER6a6YkzK5vvuSm+G04VulxV8Z+Ls43N3dSlCzxTzpk r5UavANmEM2nqF9LPCDCPTNi428IYLrONBK8WjkIMMva+TA5MpDBVX/j0aHMv9O57UmwWk wPUHR7B83/YIU6K1H4Tsp5I3Hl7ME+Y= Message-ID: <0fc6970f-b24d-1f3f-0ebc-2b9a0b14a497@iki.fi> Date: Wed, 23 Aug 2023 17:40:32 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Subject: Re: Unlogged relations and WAL-logging From: Heikki Linnakangas To: Robert Haas Cc: pgsql-hackers References: <6e5bbc08-cdfc-b2b3-9e23-1a914b9850a9@iki.fi> <868b64cf-0d0f-7d49-d985-a0574a8678e1@iki.fi> Content-Language: en-US In-Reply-To: <868b64cf-0d0f-7d49-d985-a0574a8678e1@iki.fi> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 07/07/2023 18:21, Heikki Linnakangas wrote: > On 28/01/2022 15:57, Robert Haas wrote: >>> 4. Also, the smgrwrite() calls are performed before WAL-logging the >>> pages, so the page that's written to disk has 0/0 as the LSN, not the >>> LSN of the WAL record. That's harmless too, but seems a bit sloppy. >> >> That is also a mistake on my part. > > I'm still sitting on these fixes. I think the patch I posted still makes > sense, but I got carried away with a more invasive approach that > introduces a whole new set of functions for bulk-creating a relation, > which would handle WAL-logging, smgrimmedsync() and all that (see > below). We have some repetitive, error-prone code in all the index build > functions for that. But that's not backpatchable, so I'll rebase the > original approach next week. Committed this fix to master and v16. Didn't seem worth backpatching further than that, given that there is no live user-visible issue here. >>> 5. In heapam_relation_set_new_filenode(), we do this: >>> >>>> >>>> /* >>>> * If required, set up an init fork for an unlogged table so that it can >>>> * be correctly reinitialized on restart. An immediate sync is required >>>> * even if the page has been logged, because the write did not go through >>>> * shared_buffers and therefore a concurrent checkpoint may have moved the >>>> * redo pointer past our xlog record. Recovery may as well remove it >>>> * while replaying, for example, XLOG_DBASE_CREATE or XLOG_TBLSPC_CREATE >>>> * record. Therefore, logging is necessary even if wal_level=minimal. >>>> */ >>>> if (persistence == RELPERSISTENCE_UNLOGGED) >>>> { >>>> Assert(rel->rd_rel->relkind == RELKIND_RELATION || >>>> rel->rd_rel->relkind == RELKIND_MATVIEW || >>>> rel->rd_rel->relkind == RELKIND_TOASTVALUE); >>>> smgrcreate(srel, INIT_FORKNUM, false); >>>> log_smgrcreate(newrnode, INIT_FORKNUM); >>>> smgrimmedsync(srel, INIT_FORKNUM); >>>> } >>> >>> The comment doesn't make much sense, we haven't written nor WAL-logged >>> any page here, with nor without the buffer cache. It made more sense >>> before commit fa0f466d53. >> >> Well, it seems to me (and perhaps I am just confused) that complaining >> that there's no page written here might be a technicality. The point >> is that there's no synchronization between the work we're doing here >> -- which is creating a fork, not writing a page -- and any concurrent >> checkpoint. So we both need to log it, and also sync it immediately. > > I see. I pushed the fix from the other thread that makes smgrcreate() > call register_dirty_segment (commit 4b4798e13). I believe that makes > this smgrimmedsync() unnecessary. If a concurrent checkpoint happens > with a redo pointer greater than this WAL record, it must've received > the fsync request created by smgrcreate(). That depends on the fact that > we write the WAL record *after* smgrcreate(). Subtle.. > > Hmm, we have a similar smgrimmedsync() call after index build, because > we have written pages directly with smgrextend(skipFsync=true). If no > checkpoints have occurred during the index build, we could call > register_dirty_segment() instead of smgrimmedsync(). That would avoid > the fsync() latency when creating an index on an empty or small index. > > This is all very subtle to get right though. That's why I'd like to > invent a new bulk-creation facility that would handle this stuff, and > make the callers less error-prone. Having a more generic and less error-prone bulk-creation mechanism is still on my long TODO list.. -- Heikki Linnakangas Neon (https://neon.tech)