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 1sTpDf-002q5o-4p for pgsql-hackers@arkaria.postgresql.org; Tue, 16 Jul 2024 20:56:19 +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 1sTpDd-004CrE-Rz for pgsql-hackers@arkaria.postgresql.org; Tue, 16 Jul 2024 20:56:17 +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 1sTpDd-004Cr6-Hq for pgsql-hackers@lists.postgresql.org; Tue, 16 Jul 2024 20:56:17 +0000 Received: from mail-ed1-x533.google.com ([2a00:1450:4864:20::533]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sTpDa-002TPY-7q for pgsql-hackers@lists.postgresql.org; Tue, 16 Jul 2024 20:56:16 +0000 Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-5a015a79e80so132636a12.2 for ; Tue, 16 Jul 2024 13:56:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; t=1721163372; x=1721768172; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=YUmTtM51bOOepwirtaBJuXtexXpEYJQ1O0KxtTxJQ8s=; b=D1z7xoUFH6IP8z4wfkQ52SRqBfjzb+A4yfOaI+WW53Um85ErTaunjLf9zil0H7YwUK Kee9WVX2NTTp8MKA73k+0xpcIbirfFi7ip5T1wO85VmbVDCiqc2zXto8Hz32/KKYcwUz D4LP6RW31PwMqNuG53ZQJPeUmWKodWfukZOWFus58X+imU44D0zclUe2jHqIPJkMv795 912w0RHuJ3o7UK3/pq/WM8styUKY95VOkzb+pKB4k69wECOv7UfaH3nvzpx71RtLDisW h+bGvxPeuCeqljufVid7sDeUzQ97iFJ6Pc2wtbpXyCqW7MjNz44ZFHRsvrJtV5e/aold TX4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721163372; x=1721768172; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=YUmTtM51bOOepwirtaBJuXtexXpEYJQ1O0KxtTxJQ8s=; b=lvln7mzqEZ8bD+srE+B51NhiE+OVSCxXTXBtVnRE/DH5G5yGBUFi6e3BQQLkX3qRwG fodQ5/sFa2DS3BWAefRpBXyNgirEQRnNckF7sWVMS4Hz9oioo7qc09K5CBGjPJusSW9r gxbA93KG3vHgfNzE9ZRW3tZ2q/uhe//6FE2O36iqqoFpV087Tj4+fWSLtSLDSELkcHnd OIDKhJBZXuC81hD/08CFG8DUFM3YFHaCIX/sKOCjD9mnbw1cGqcOlyjYPv/5uT9Ln6sa gbq4dlkK9oSBijj8FU/fcy8fbi39K1wRFVTEnXWuGtDoSB9psdhJhc6euO6ZJrSEAmt0 Gb9A== X-Forwarded-Encrypted: i=1; AJvYcCXj7X5yuYxhAYyz2Nmjc1/knJmDH1dn9bBq6SVWkvDROepnTm01NMqYqFQIUnWwSAyTrQm7GLLAfiWmFEkH3KGOOvbAJiWSUGKjgeGy/+A3Y2ud X-Gm-Message-State: AOJu0YyUEfYcctjiypSQWY28BcrszvUpHC4EfEzuUMqHpo7mSYHDWTuF biMbTkkaDcDT6ucbSrJm/OxmjYMq/DoopvNWFiKoAe9f3lsNi+0uNnMHczK98A== X-Google-Smtp-Source: AGHT+IHvKf0eOeShfMpuEfW4DL+/RuDqN9eh9fQrEM/8mHmaa88iP0/XwNmrtsZu5YBDSNX0JdbgqA== X-Received: by 2002:a50:ab11:0:b0:58b:1a5e:c0e7 with SMTP id 4fb4d7f45d1cf-59ef05b7155mr2601561a12.35.1721163372544; Tue, 16 Jul 2024 13:56:12 -0700 (PDT) Received: from [10.137.0.18] (ip-86-49-229-30.bb.vodafone.cz. [86.49.229.30]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-59b24f56ccasm5487181a12.22.2024.07.16.13.56.12 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 16 Jul 2024 13:56:12 -0700 (PDT) Message-ID: Date: Tue, 16 Jul 2024 22:56:11 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: walsender.c fileheader comment To: Peter Smith , PostgreSQL Hackers References: Content-Language: en-US From: Tomas Vondra In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 6/11/24 04:35, Peter Smith wrote: > Hi, > > I was reading the walsender.c fileheader comment while studying > another thread. I think if there is logical replication in progress > then the PROCSIG_WALSND_INIT_STOPPING handler will *always* switch to > a "stopping" state: e.g., > > /* > * Handle PROCSIG_WALSND_INIT_STOPPING signal. > */ > void > HandleWalSndInitStopping(void) > { > Assert(am_walsender); > > /* > * If replication has not yet started, die like with SIGTERM. If > * replication is active, only set a flag and wake up the main loop. It > * will send any outstanding WAL, wait for it to be replicated to the > * standby, and then exit gracefully. > */ > if (!replication_active) > kill(MyProcPid, SIGTERM); > else > got_STOPPING = true; > } > > ~~~ > > But the walsender.c fileheader comment seems to be saying something > slightly different. IIUC, some minor rewording of the comment is > needed so it describes the code better. > > HEAD > ... > * shutdown, if logical replication is in progress all existing WAL records > * are processed followed by a shutdown. Otherwise this causes the walsender > * to switch to the "stopping" state. In this state, the walsender will reject > * any further replication commands. The checkpointer begins the shutdown > ... > > SUGGESTION > .. shutdown. If logical replication is in progress, the walsender > switches to a "stopping" state. In this state, the walsender will > reject any further replication commands - but all existing WAL records > are processed - followed by a shutdown. > > ~~~ > > I attached a patch for the above-suggested change. > > Thoughts? I did look at this, and while the explanation in the current comment may seem a bit confusing, I'm not sure the suggested changes improve the situation very much. This suggests the two comments somehow disagree, but it does not say in what exactly, so perhaps I just missed it :-( ISTM there's a bit of confusion what is meant by "stopping" state - you seem to be interpreting it as a general concept, where the walsender is requested to stop (through the signal), and starts doing stuff to exit. But the comments actually talk about WalSnd->state, where "stopping" means it needs to be set to WALSNDSTATE_STOPPING. And we only ever switch to that state in two places - in WalSndPhysical and exec_replication_command. And that does not happen in regular logical replication (which is what "logical replication is in progress" refers to) - if you have a walsender just replicating DML, it will never see the WALSNDSTATE_STOPPING state. It will simply do the cleanup while still in WALSNDSTATE_STREAMING state, and then just exit. So from this point of view, the suggestion is actually wrong. To conclude, I think this probably makes the comments more confusing. If we want to make it clearer, I'd probably start by clarifying what the "stopping" state means. Also, it's a bit surprising we may not actually go through the "stopping" state during shutdown. regards -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company