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 1qIy6W-0005rN-KX for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Jul 2023 21:07:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1qIy6V-0007BT-Go for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Jul 2023 21:07:31 +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 1qIy6V-0007BK-1m for pgsql-hackers@lists.postgresql.org; Mon, 10 Jul 2023 21:07:31 +0000 Received: from mail-oo1-xc2a.google.com ([2607:f8b0:4864:20::c2a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qIy6R-003LhM-Mb for pgsql-hackers@postgresql.org; Mon, 10 Jul 2023 21:07:30 +0000 Received: by mail-oo1-xc2a.google.com with SMTP id 006d021491bc7-565f3881cbeso3873497eaf.2 for ; Mon, 10 Jul 2023 14:07:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=neon-tech.20221208.gappssmtp.com; s=20221208; t=1689023246; x=1691615246; h=in-reply-to:references:message-id:from:to:subject:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=JZ9Edi4YViEmorvuN3JHH3oewUT5/VtcuA7DEuHbw/U=; b=HKJIrTqNn2GrwHcfKQUeEUEHw2Jwh3pb7Ls/0XI260XxdtHVFFg4Zzte2IVm1k2Mpy R7ya1n7RYNuOmqjMtkxR5KYjwibvQzLVIqaporUBy9XsmlqpmaOzqPqOY9WPp8vQSVYL wmv8F5So5yzEL+h7p/xRQuUDn9WeOERszKhgxfNbyVD3uNqR16DKBNVWsn5RTMzeJqr/ AJjfiCR5RyQ/mTCo4nCWLMpFu37j/0yQ83YbXl0CaTH0kIZVTpV1DVwIZRWEgk3GeaRW of896TaBGB6NtQ/1IsltR2VkwMCeT/RDnLef3bIxMH1F+cYP8v3djOoleMXFN0j7U2xp pRrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689023246; x=1691615246; h=in-reply-to:references:message-id:from:to:subject:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=JZ9Edi4YViEmorvuN3JHH3oewUT5/VtcuA7DEuHbw/U=; b=ie42sV5DKAXvwu29R27jkaRP2mcfAe9qJkaoJbsAHF2wp8kfotBKlRl5yy/N0N61Ko 5JPiaBUZvfky60FychHydiLhr6G2Z+ZOW+fJvIg+mIYmiept5A34mEKv7IbKgeGRrpZu xuosONome/JDHR24Cg8JBxEMjnpqsmXMHMjhYr4nOPUru+Bylkt1dwene9vOV6MLK1WG fGvCLltuBM5gPHC4b65NmKDLb6rLSOpOIl/hJby5wjv+hZJ1u7MszfRgwfsa815gqN2s 1VXasHr8Qt8laWYvqymxBjQXaU0x72+R2Tq4kWmX6aJgIeMcpkEBj2iiYhFNtxtQPAKX vOFA== X-Gm-Message-State: ABy/qLZ9vnYVdsd3MIIYaarl3OAXxbPi0OzFWfFf0J98MBUgoEVmIHFP Uyq80C420Ztj9QNAAwSIYWMqeUuqNqtkUMZUQAbcoA== X-Google-Smtp-Source: APBJJlFlZzjBBL+lyxAY9uTzbMt1ejSGA6jNPxPC/57cwDoWytVhnqHLOmh239WqepHzBAqCs9TF+A== X-Received: by 2002:a4a:2c8b:0:b0:565:83d1:e3cc with SMTP id o133-20020a4a2c8b000000b0056583d1e3ccmr10808457ooo.2.1689023246074; Mon, 10 Jul 2023 14:07:26 -0700 (PDT) Received: from localhost ([2607:fb90:f280:130d:3996:e070:71ee:42fb]) by smtp.gmail.com with ESMTPSA id c8-20020a4a4f08000000b00565ed52196bsm250395oob.6.2023.07.10.14.07.25 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 10 Jul 2023 14:07:25 -0700 (PDT) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 10 Jul 2023 16:07:24 -0500 Subject: Re: Refactoring backend fork+exec code To: "Heikki Linnakangas" , "pgsql-hackers" From: "Tristan Partin" Message-Id: X-Mailer: aerc 0.15.2-83-gab3debc1fad9 References: <7a59b073-5b5b-151e-7ed3-8b01ff7ce9ef@iki.fi> In-Reply-To: <7a59b073-5b5b-151e-7ed3-8b01ff7ce9ef@iki.fi> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > From 1d89eec53c7fefa7a4a8c011c9f19e3df64dc436 Mon Sep 17 00:00:00 2001 > From: Heikki Linnakangas > Date: Mon, 12 Jun 2023 16:33:20 +0300 > Subject: [PATCH 4/9] Use FD_CLOEXEC on ListenSockets > @@ -831,7 +834,8 @@ StreamConnection(pgsocket server_fd, Port *port) > void > StreamClose(pgsocket sock) > { > - closesocket(sock); > + if (closesocket(sock) !=3D 0) > + elog(LOG, "closesocket failed: %m"); > } >=20 > /* Do you think WARNING would be a more appropriate log level? > From 2f518be9e96cfed1a1a49b4af8f7cb4a837aa784 Mon Sep 17 00:00:00 2001 > From: Heikki Linnakangas > Date: Mon, 12 Jun 2023 18:07:54 +0300 > Subject: [PATCH 5/9] Move "too many clients already" et al. checks from > ProcessStartupPacket. This seems like a change you could push already (assuming another maintainer agrees with you), which makes reviews for this patchset even easier. > From c25b67c045018a2bf05e6ff53819d26e561fc83f Mon Sep 17 00:00:00 2001 > From: Heikki Linnakangas > Date: Sun, 18 Jun 2023 14:11:16 +0300 > Subject: [PATCH 6/9] Pass CAC as argument to backend process. Could you expand a bit more on why it is better to pass it as a separate argument? Does it not fit well conceptually in struct Port? > @@ -4498,15 +4510,19 @@ postmaster_forkexec(int argc, char *argv[]) > * returns the pid of the fork/exec'd process, or -1 on failure > */ > static pid_t > -backend_forkexec(Port *port) > +backend_forkexec(Port *port, CAC_state cac) > { > - char *av[4]; > + char *av[5]; > int ac =3D 0; > + char cacbuf[10]; >=20 > av[ac++] =3D "postgres"; > av[ac++] =3D "--forkbackend"; > av[ac++] =3D NULL; /* filled in by interna= l_forkexec */ >=20 > + snprintf(cacbuf, sizeof(cacbuf), "%d", (int) cac); > + av[ac++] =3D cacbuf; Might be worth a sanity check that there wasn't any truncation into cacbuf, which is an impossibility as the code is written, but still useful for catching a future developer error. Is it worth adding a command line option at all instead of having the naked positional argument? It would help anybody who might read the command line what the seemingly random integer stands for. > @@ -4910,7 +4926,10 @@ SubPostmasterMain(int argc, char *argv[]) > /* Run backend or appropriate child */ > if (strcmp(argv[1], "--forkbackend") =3D=3D 0) > { > - Assert(argc =3D=3D 3); /* shouldn't be any m= ore args */ > + CAC_state cac; > + > + Assert(argc =3D=3D 4); > + cac =3D (CAC_state) atoi(argv[3]); Perhaps an assert or full error checking that atoi succeeds would be useful for similar reasons to my previous comment. > From 658cba5cdb2e5c45faff84566906d2fcaa8a3674 Mon Sep 17 00:00:00 2001 > From: Heikki Linnakangas > Date: Mon, 12 Jun 2023 18:03:03 +0300 > Subject: [PATCH 7/9] Remove ConnCreate and ConnFree, and allocate Port in > stack. Again, seems like another patch that could be pushed separately assuming others don't have any comments. > From 65384b9a6cfb3b9b589041526216e0f64d64bea5 Mon Sep 17 00:00:00 2001 > From: Heikki Linnakangas > Date: Sun, 18 Jun 2023 13:56:44 +0300 > Subject: [PATCH 8/9] Introduce ClientSocket, rename some funcs > @@ -1499,7 +1499,7 @@ CloseServerPorts(int status, Datum arg) > { > if (ListenSocket[i] !=3D PGINVALID_SOCKET) > { > - StreamClose(ListenSocket[i]); > + closesocket(ListenSocket[i]); > ListenSocket[i] =3D PGINVALID_SOCKET; > } > } I see you have been adding log messages in the case of closesocket() failing. Do you think it is worth doing here as well? One strange part about this patch is that in patch 4, you edit StreamClose() to emit a log message in the case of closesocket() failure, but then this patch just completely removes it. > @@ -4407,11 +4420,11 @@ BackendInitialize(Port *port, CAC_state cac) > * Doesn't return at all. > */ > static void > -BackendRun(Port *port) > +BackendRun(void) > { > /* > - * Create a per-backend PGPROC struct in shared memory. We must d= o > - * this before we can use LWLocks (in AttachSharedMemoryAndSemaph= ores). > + * Create a per-backend PGPROC struct in shared memory. We must d= o this > + * before we can use LWLocks (in AttachSharedMemoryAndSemaphores)= . > */ > InitProcess(); This comment reflow probably fits better in the patch that added the AttachSharedMemoryAndSemaphores function. > From b33cfeb28a5419045acb659a01410b2b463bea3e Mon Sep 17 00:00:00 2001 > From: Heikki Linnakangas > Date: Sun, 18 Jun 2023 13:59:48 +0300 > Subject: [PATCH 9/9] Refactor postmaster child process launching > - Move code related to launching backend processes to new source file, > process_start.c Since this seems pretty self-contained, my be easier to review if this was its own commit. > - Refactor the mechanism of passing informaton from the parent to > child process. Instead of using different command-line arguments > when launching the child process in EXEC_BACKEND mode, pass a > variable-length blob of data along with all the global > variables. The contents of that blob depends on the kind of child > process being launched. In !EXEC_BACKEND mode, we use the same blob, > but it's simply inherited from the parent to child process. Same with this. Perhaps others would disagree. > +const PMChildEntry entry_kinds[] =3D { > + {"backend", BackendMain, true}, > + > + {"autovacuum launcher", AutoVacLauncherMain, true}, > + {"autovacuum worker", AutoVacWorkerMain, true}, > + {"bgworker", BackgroundWorkerMain, true}, > + {"syslogger", SysLoggerMain, false}, > + > + {"startup", StartupProcessMain, true}, > + {"bgwriter", BackgroundWriterMain, true}, > + {"archiver", PgArchiverMain, true}, > + {"checkpointer", CheckpointerMain, true}, > + {"wal_writer", WalWriterMain, true}, > + {"wal_receiver", WalReceiverMain, true}, > +}; It seems like this could be made static. I didn't see it getting exposed in a header file anywhere, but I also admit that I can be blind at times. I need to spend more time looking at this last patch. Nice work so far! --=20 Tristan Partin Neon (https://neon.tech)