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 1sWFSs-00E3ZD-0J for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Jul 2024 13:22:02 +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 1sWFSp-00DXZL-R0 for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Jul 2024 13:22:00 +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 1sWFSp-00DXZC-Bh for pgsql-hackers@lists.postgresql.org; Tue, 23 Jul 2024 13:21:59 +0000 Received: from lahtoruutu.iki.fi ([185.185.170.37]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sWFSm-0013rm-VR for pgsql-hackers@postgresql.org; Tue, 23 Jul 2024 13:21:59 +0000 Received: from [192.168.27.172] (mobile-access-5d6a04-205.dhcp.inet.fi [93.106.4.205]) (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 4WSyVB1yqlz49Q0F; Tue, 23 Jul 2024 16:21:53 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1721740914; 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=L2geJD54VAqf9TS83aZI8Qpo9bk7F5nRx1+h3DFYL+0=; b=wR3x/Q5VwMbjeAtBeMaGbi5Ak+FmXYzG5mRfaipwm2SNcKQN8Kqvfk+U/hFyZ/qz4g3UI0 KjfWp8iwZSARjpN9320HUXqKmgEq1kjmnx/zv8Ht7ji2Kc+lihZ/HeLKiiTUxYgXxW2Plb /48HPqj8jsYOcXmtZk9u3AiOqCZEuhcooqUWOwdx3g3zChzOyr5jXJNb9khd9tj5Pp5glR 6aG2BsOiWP/wMn0SylxLtJvRx4IzGrcJkIADnEaWOIoJJ/BElDKPZ7mIXfWv+wA660vRRx Nkpcd0/kVDNER5KXf1nFOiQwizgTAzatmsxdMGbJRKyjDsPAMb3SN5ftWXkoaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1721740914; 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=L2geJD54VAqf9TS83aZI8Qpo9bk7F5nRx1+h3DFYL+0=; b=nG3u/I69nMifUZNuawqsymlYa/AwcMPss0Ii4cHw7dbDSSK5Vgf1ix+NXjnSWoNxNTpQKB hWm1KRm9BYBoP8/5s+Gil517bfBMnfueskdTpUPWGiLhxp6IHDfe+E1tUFbGNT/RiwsYCZ BOKWTY/iobId+kbXFlTjjnbqcE2EU1Yie4YClGU4Ag1gsVGSkaVuN8n5rfZGA+XJ11frm5 4XZlGfOxZaJaEQF+zEhNasbdB1LKxbVZwRqALfGMglItvVzjsze+PAVDOwZpijJJr+U+QK F2vCDPrgeA1pNjAlRDg8zZ7zt0nPpvnyhWgPrAQEENzJKLjsntdpBaG0+BNRAQ== ARC-Seal: i=1; s=lahtoruutu; d=iki.fi; t=1721740914; a=rsa-sha256; cv=none; b=YKGEEeHvBmtBg1UACgXAcEhPe4urKdR+fJcpAXcTze1WFPqSboehKLqjr5/iChye5iRciI TTnPWDuZkLv0fKwayNNcDJTGqB9nZFOgIEJR1QzWAaaESLQDga8mDlL78XR6HSYyqRBK0p nm4UZSRBHpuuOqasjmrCg4ViqJF8nChrMFz8+Wh/dfiKmoGDNRjP0JnQGB64GfdLBTpv4x g1Pcsb2NA2M4ntWG+hnBSgi/ckX+Ugi783PCZGxt/g9Z6IJwBIryNUf7fPLG3IMQowVQs4 IGpvZQBuQ1HKWFPYozm+F8Ffc4Yo2cIlCAk0Qu4FfbFlNQTRuQzWAzvlE0URow== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi Message-ID: <3f732849-0f60-4a64-be84-ee41e239d560@iki.fi> Date: Tue, 23 Jul 2024 16:21:53 +0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Comments on Custom RMGRs To: Michael Paquier , Robert Haas Cc: Jeff Davis , Danil Anisimow , Andres Freund , PostgreSQL Hackers References: <0892cd00635c8bcd458de6d43d31cf61953da1b2.camel@j-davis.com> <727b0f3b48aec2a4f968bf11c6fa8ca6382b6cca.camel@j-davis.com> <22e756affaad88b77a52d67cb532ed2a544f2e36.camel@j-davis.com> <1e2256b744836aeb485c61954e9d8272f80141a0.camel@j-davis.com> Content-Language: en-US From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 27/05/2024 21:20, Michael Paquier wrote: > On Fri, May 17, 2024 at 04:25:15PM -0400, Robert Haas wrote: >> On Fri, May 17, 2024 at 4:20 PM Jeff Davis wrote: >>> Regarding this particular change: the checkpointing hook seems more >>> like a table AM feature, so I agree with you that we should have a good >>> idea how a real table AM might use this, rather than only >>> pg_stat_statements. >> >> I would even be OK with a pg_stat_statements example that is fully >> working and fully explained. I just don't want to have no example at >> all. The original proposal has been changed twice because of >> complaints that the hook wasn't quite useful enough, but I think that >> only proves that v3 is closer to being useful than v1. If v1 is 40% of >> the way to useful and v3 is 120% of the way to useful, wonderful! But >> if v1 is 20% of the way to being useful and v3 is 60% of the way to >> being useful, it's not time to commit anything yet. I don't know which >> is the case, and I think if someone wants this to be committed, they >> need to explain clearly why it's the first and not the second. > > Please note that I've been studying ways to have pg_stat_statements > being plugged in directly with the shared pgstat APIs to get it backed > by a dshash to give more flexibility and scaling, giving a way for > extensions to register their own stats kind. In this case, the flush > of the stats would be controlled with a callback in the stats > registered by the extensions, conflicting with what's proposed here. > pg_stat_statements is all about stats, at the end. I don't want this > argument to act as a barrier if a checkpoint hook is an accepted > consensus here, but a checkpoint hook used for this code path is not > the most intuitive solution I can think of in the long-term. On the topic of concrete uses for this API: We have a bunch of built-in resource managers that could be refactored to use this API. CheckPointGuts currently looks like this: > /* > * Flush all data in shared memory to disk, and fsync > * > * This is the common code shared between regular checkpoints and > * recovery restartpoints. > */ > static void > CheckPointGuts(XLogRecPtr checkPointRedo, int flags) > { > CheckPointRelationMap(); > CheckPointReplicationSlots(flags & CHECKPOINT_IS_SHUTDOWN); > CheckPointSnapBuild(); > CheckPointLogicalRewriteHeap(); > CheckPointReplicationOrigin(); > > /* Write out all dirty data in SLRUs and the main buffer pool */ > TRACE_POSTGRESQL_BUFFER_CHECKPOINT_START(flags); > CheckpointStats.ckpt_write_t = GetCurrentTimestamp(); > CheckPointCLOG(); > CheckPointCommitTs(); > CheckPointSUBTRANS(); > CheckPointMultiXact(); > CheckPointPredicate(); > > RmgrCheckpoint(flags, RMGR_CHECKPOINT_BEFORE_BUFFERS); > > CheckPointBuffers(flags); > > RmgrCheckpoint(flags, RMGR_CHECKPOINT_AFTER_BUFFERS); > > /* Perform all queued up fsyncs */ > TRACE_POSTGRESQL_BUFFER_CHECKPOINT_SYNC_START(); > CheckpointStats.ckpt_sync_t = GetCurrentTimestamp(); > ProcessSyncRequests(); > CheckpointStats.ckpt_sync_end_t = GetCurrentTimestamp(); > TRACE_POSTGRESQL_BUFFER_CHECKPOINT_DONE(); > > /* We deliberately delay 2PC checkpointing as long as possible */ > CheckPointTwoPhase(checkPointRedo); > } Of these calls, CheckPointCLOG would be natural as the rmgr_callback of the clog rmgr. Similarly for CheckPointMultiXact and maybe a few others. However, let's look at the pg_stat_statements patch (pgss_001.v1.patch): It's now writing a new WAL record for every call to pgss_store(), turning even simple queries into WAL-logged operations. That's a non-starter. It will also not work on a standby. This needs to be redesigned so that the data is updated in memory, and written to disk and/or WAL-logged only periodically. Perhaps at checkpoints, but you could do it more frequently too. I'm not convinced that the stats should be WAL-logged. Do you want them to be replicated and included in backups? Maybe, but maybe not. It's certainly a change to how it currently works. If we don't WAL-log the stats, we don't really need a custom RMGR for it. We just need a checkpoint hook to flush the stats to disk, but we don't need a registered RMGR ID for it. So, I got a feeling that adding this to the rmgr interface is not quite right. The rmgr callbacks are for things that run when WAL is *replayed*, while checkpoints are related to how WAL is generated. Let's design this as an independent hook, separate from rmgrs. Another data point: In Neon, we actually had to add a little code to checkpoints, to WAL-log some exta data. That was a quick hack and might not be the right design in the first place, but these hooks would not have been useful for our purposes. We wanted to write a new WAL record at shutdown, and in CheckPointGuts(), it's already too late for that. It needs to be done earlier, before starting to the shutdown checkpoint. Similar to LogStandbySnapshot(), except that LogStandbySnapshot() is not called at shutdown like we wanted to. For a table AM, the point of a checkpoint hook is probably to fsync() data that is managed outside of the normal buffer manager and CheckPointGuts() is the right place for that, but other extensions might want to hook into checkpoints for other reasons. -- Heikki Linnakangas Neon (https://neon.tech)