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 1rhdoT-00Ccjw-8r for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Mar 2024 23:03:09 +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 1rhdoQ-003SQ3-HX for pgsql-hackers@arkaria.postgresql.org; Tue, 05 Mar 2024 23:03:07 +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 1rhdoQ-003SPu-5Y for pgsql-hackers@lists.postgresql.org; Tue, 05 Mar 2024 23:03:06 +0000 Received: from mail-oo1-xc2e.google.com ([2607:f8b0:4864:20::c2e]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rhdoI-002yIy-IA for pgsql-hackers@postgresql.org; Tue, 05 Mar 2024 23:03:05 +0000 Received: by mail-oo1-xc2e.google.com with SMTP id 006d021491bc7-5a142375987so623207eaf.3 for ; Tue, 05 Mar 2024 15:02:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=neon.tech; s=google; t=1709679777; x=1710284577; darn=postgresql.org; h=in-reply-to:references:subject:cc:from:to:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=q3BaTQAem+LUELmP5P5zplbfzkbT7ddyyLLlMV0QG+4=; b=l0aSXx0Q+4h3mXzkuRon/n8sLV95puM7H7wTV5jEyRSl2l/BZypL/RUKl/dtPu2e+/ aziz3v7dxEbiuCv5XKfMYDhfu8wwm0WW0hgk+OLkEsBR2yXBqcNGRkyLlLZ3mqc/A2Vi bLJJMhzJuWehnChNg9Z16g/LMoeR5Iws3ovH4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709679777; x=1710284577; h=in-reply-to:references:subject:cc:from:to:message-id:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=q3BaTQAem+LUELmP5P5zplbfzkbT7ddyyLLlMV0QG+4=; b=UOJZl5wig/NdVKuUsFcFpla+xzXt0SB6EmjnijykX9JkDGvTIPtbJRjZGIts+WPbz/ 7NlE2+OW4NYd8hFUFyq2rdE46c8pQic9GDe9zkEAIfxWinqjl9bIOAU8KJN0IjWGBu7S /qhiz+jQIW9Bk07hVLlq5c5VuMwyzzL8+l7qJE/jjVF7nrcEICnZOunj7+u4eyb0bNb2 EYP72eOsADE+lR94jQjYHR/xlP7ftVgBw40RjCCCO2e61ge+RQiZ45GhaUOHCe50cn6h N+abwYHvjEUIYhv/gMRmhPRMe6o0CnH3lvG/xJJkVoAtbM+p0HLDSc2khhzd07o65CN5 8bVg== X-Forwarded-Encrypted: i=1; AJvYcCV8TBsJdUxZuQRDbE3YFlCBYXa/ru03B5/IlU9BTgU81oTM9zEo/zC4tLDhWUekytCrk6yRQE5526IPt/ML0jBqhHscO05ZELN9WNMg X-Gm-Message-State: AOJu0YxA6Jjki/QCV9OgKHSBYMf5tPgN+22zGRt1/WdZmFHjp3Zqaezu OItglBcTBE8HroBuqPwSAYCi/CDbmwAKlGV2HZwa3zuPJkQ8gjuzVNY2Eu9WQoc= X-Google-Smtp-Source: AGHT+IEWMA1bj3SGZLPQNN/oKhaKrnvInA++uM9/FdrENoFevN4p9XmfrCpUCIsXNTj/jvS2r+al4A== X-Received: by 2002:a05:6820:1ca0:b0:5a1:2209:8e04 with SMTP id ct32-20020a0568201ca000b005a122098e04mr3735205oob.7.1709679776787; Tue, 05 Mar 2024 15:02:56 -0800 (PST) Received: from localhost ([2607:fb91:121c:5d6:3996:e070:71ee:42fb]) by smtp.gmail.com with ESMTPSA id b19-20020a4a9bd3000000b005a0a55ba943sm2653503ook.35.2024.03.05.15.02.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 05 Mar 2024 15:02:56 -0800 (PST) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8; format=Flowed Date: Tue, 05 Mar 2024 17:02:55 -0600 Message-Id: To: "Heikki Linnakangas" From: "Tristan Partin" Cc: , "Thomas Munro" , "Robert Haas" , "Andres Freund" , "pgsql-hackers" Subject: Re: Refactoring backend fork+exec code X-Mailer: aerc 0.17.0-13-g7c7a7ba8027e References: <59527c9b-c3f7-4d0c-a3f1-00d0ed5cc186@iki.fi> <20240122210740.7vq5fd4woixpwx3f@awork3.anarazel.de> <8171f1aa-496f-46a6-afc3-c46fe7a9b407@iki.fi> <20240123195009.44wtutga24p5sh3s@awork3.anarazel.de> <08eff756-beeb-4a11-9eb5-d45b6af38998@iki.fi> <5997ff317a5827496537580078bcd19f4441f8d2.camel@crunchydata.com> <606a62d6-321c-4967-92c1-2d4e20a2c136@iki.fi> <20240207182521.wyt5dmd75l4v27zk@awork3.anarazel.de> <2136fb4f-1630-43e9-9cf6-b32772b4b0a0@iki.fi> <20240214213711.2gjlezqll37mg7lg@awork3.anarazel.de> <8f695109-8341-4f7c-8b0d-3cadc7eddc9e@iki.fi> <0c247a78-9dac-4584-8e87-39f2b45bec68@iki.fi> <861f42c1-01c4-42e6-ab2f-d5743ff69607@iki.fi> In-Reply-To: <861f42c1-01c4-42e6-ab2f-d5743ff69607@iki.fi> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon Mar 4, 2024 at 3:05 AM CST, Heikki Linnakangas wrote: > I've now completed many of the side-quests, here are the patches that=20 > remain. > > The first three patches form a logical unit. They move the=20 > initialization of the Port struct from postmaster to the backend=20 > process. Currently, that work is split between the postmaster and the=20 > backend process so that postmaster fills in the socket and some other=20 > fields, and the backend process fills the rest after reading the startup= =20 > packet. With these patches, there is a new much smaller ClientSocket=20 > struct that is passed from the postmaster to the child process, which=20 > contains just the fields that postmaster initializes. The Port struct is= =20 > allocated in the child process. That makes the backend startup easier to= =20 > understand. I plan to commit those three patches next if there are no=20 > objections. > > That leaves the rest of the patches. I think they're in pretty good=20 > shape too, and I've gotten some review on those earlier and have=20 > addressed the comments I got so far, but would still appreciate another= =20 > round of review. > - * *MyProcPort, because ConnCreate() allocated that space with m= alloc() > - * ... else we'd need to copy the Port data first. Also, subsid= iary data > - * such as the username isn't lost either; see ProcessStartupPac= ket(). > + * *MyProcPort, because that space is allocated in stack ... els= e we'd > + * need to copy the Port data first. Also, subsidiary data such= as the > + * username isn't lost either; see ProcessStartupPacket(). s/allocated in/allocated on the The first 3 patches seem good to go, in my opinion. > @@ -225,14 +331,13 @@ internal_forkexec(int argc, char *argv[], ClientSoc= ket *client_sock, BackgroundW > return -1; > } >=20 > - /* Make sure caller set up argv properly */ > - Assert(argc >=3D 3); > - Assert(argv[argc] =3D=3D NULL); > - Assert(strncmp(argv[1], "--fork", 6) =3D=3D 0); > - Assert(argv[2] =3D=3D NULL); > - > - /* Insert temp file name after --fork argument */ > + /* set up argv properly */ > + argv[0] =3D "postgres"; > + snprintf(forkav, MAXPGPATH, "--forkchild=3D%s", child_kind); > + argv[1] =3D forkav; > + /* Insert temp file name after --forkchild argument */ > argv[2] =3D tmpfilename; > + argv[3] =3D NULL; Should we use postgres_exec_path instead of the naked "postgres" here? > + /* in postmaster, fork failed ... */ > + ereport(LOG, > + (errmsg("could not fork worker process: = %m"))); > + /* undo what assign_backendlist_entry did */ > + ReleasePostmasterChildSlot(rw->rw_child_slot); > + rw->rw_child_slot =3D 0; > + pfree(rw->rw_backend); > + rw->rw_backend =3D NULL; > + /* mark entry as crashed, so we'll try again later */ > + rw->rw_crashed_at =3D GetCurrentTimestamp(); > + return false; I think the error message should include the word "background." It would=20 be more consistent with the log message above it. > +typedef struct > +{ > + int syslogFile; > + int csvlogFile; > + int jsonlogFile; > +} syslogger_startup_data; It would be nice if all of these startup data structs were named=20 similarly. For instance, a previous one was BackendStartupInfo. It would=20 help with greppability. I noticed there were a few XXX comments left that you created. I'll=20 highlight them here for more visibility. > +/* XXX: where does this belong? */ > +extern bool LoadedSSL; Perhaps near the My* variables or maybe in the Port struct? > +#ifdef EXEC_BACKEND > + > + /* > + * Need to reinitialize the SSL library in the backend, since th= e context > + * structures contain function pointers and cannot be passed thr= ough the > + * parameter file. > + * > + * If for some reason reload fails (maybe the user installed bro= ken key > + * files), soldier on without SSL; that's better than all connec= tions > + * becoming impossible. > + * > + * XXX should we do this in all child processes? For the moment= it's > + * enough to do it in backend children. XXX good question indeed > + */ > +#ifdef USE_SSL > + if (EnableSSL) > + { > + if (secure_initialize(false) =3D=3D 0) > + LoadedSSL =3D true; > + else > + ereport(LOG, > + (errmsg("SSL configuration could= not be loaded in child process"))); > + } > +#endif > +#endif Here you added the "good question indeed." I am not sure what the best=20 answer is either! :) > + /* XXX: translation? */ > + ereport(LOG, > + (errmsg("could not fork %s process: %m",= PostmasterChildName(type)))); I assume you are referring to the child name here? > XXX: We now have functions called AuxiliaryProcessInit() and > InitAuxiliaryProcess(). Confusing. Based on my analysis, the *Init() is called in the Main functions, while=20 Init*() is called before the Main functions. Maybe=20 AuxiliaryProcessInit() could be renamed to AuxiliaryProcessStartup()?=20 Rename the other to AuxiliaryProcessInit(). --=20 Tristan Partin Neon (https://neon.tech)