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 1ua7vE-00GNGO-AY for pgsql-bugs@arkaria.postgresql.org; Fri, 11 Jul 2025 07:11:52 +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 1ua7vC-002RWT-D8 for pgsql-bugs@arkaria.postgresql.org; Fri, 11 Jul 2025 07:11:51 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ua7vC-002RWL-3U for pgsql-bugs@lists.postgresql.org; Fri, 11 Jul 2025 07:11:50 +0000 Received: from mail.postgrespro.ru ([93.174.132.70]) by makus.postgresql.org with smtp (Exim 4.96) (envelope-from ) id 1ua7v9-006ifX-1J for pgsql-bugs@lists.postgresql.org; Fri, 11 Jul 2025 07:11:49 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1752217904; bh=JBBAVNP+ITacPiMUCUg19v4bl/A4cL98OUCcDS4aTiQ=; h=Date:From:To:Subject:In-Reply-To:References:Message-ID:From; b=JiE568/OtpzrVy4zoaysMDYk5sxnXdVByxA+ojz7GP1OwyMHwMfOaeAzKRfPH0xlZ ri6KXB/RRSCHWEnsIKTjphaxyvsAidNIuIGSnaK0OT+LaKNdG8DafqKdHEp21ahG8L C/UFCvzwtR5wv14bzja7MSyc1TX6Fg/wCpGu43bInoi35RTw0MU+uH/+8Sb3Kmye2r jJfsQjE81qbjF/YS85RQR+IgCQ+tWyaC2SPhpC9QKeyIlxCVThbKE66ttSYgJZeqJ0 BhrlAh5XJ7ulWgl4xmV6BKrH7t+3BDa6stzMrmc8+6tgWQOy5zdYZed2NjgrNDl0dC ETaLPG6dVMnCA== Received: from mail.postgrespro.ru (webmail-master-mstn.l.postgrespro.ru [192.168.2.26]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: a.mitrokhin@postgrespro.ru) by mail.postgrespro.ru (Postfix/587) with ESMTPSA id ED82160EF4 for ; Fri, 11 Jul 2025 10:11:43 +0300 (MSK) MIME-Version: 1.0 Date: Fri, 11 Jul 2025 14:11:43 +0700 From: a.mitrokhin@postgrespro.ru To: pgsql-bugs@lists.postgresql.org Subject: Re: Starting a PostgreSQL server on a dynamic port (parameter port=0) In-Reply-To: <1523824.1752158490@sss.pgh.pa.us> References: <824bbf6be5fa0edf6a7d4c8371ce67ce@postgrespro.ru> <1523824.1752158490@sss.pgh.pa.us> Message-ID: <50a78b5c9a4fe5f0731c632c61eb6df9@postgrespro.ru> X-Sender: a.mitrokhin@postgrespro.ru Content-Type: text/plain; charset=UTF-8; format=flowed X-KSMG-AntiPhishing: NotDetected X-KSMG-AntiSpam-Interceptor-Info: not scanned X-KSMG-AntiSpam-Status: not scanned, disabled by settings X-KSMG-AntiVirus: Kaspersky Secure Mail Gateway, version 2.1.0.7854, bases: 2025/07/11 06:17:00 #27634361 X-KSMG-AntiVirus-Status: NotDetected, skipped X-KSMG-LinksScanning: not scanned, disabled by settings X-KSMG-Message-Action: skipped X-KSMG-Rule-ID: 1 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Tom Lane =D0=BF=D0=B8=D1=81=D0=B0=D0=BB(=D0=B0) 2025-07-10 21:41: > a.mitrokhin@postgrespro.ru writes: >> Postgres does not allow starting the server with port=3D0, which=20 >> prevents >> it from obtaining an arbitrary free port from the kernel. >=20 > Others have already pointed out problems with this proposal, but > the one I see is: what do you do next? How will clients know > what port to connect to? >=20 > The only way I could see to do that is to read the port out of > postmaster.pid, which works only for clients running as the same > userid as the postmaster, which is surely an unfit-for-production > approach. >=20 > If you're only interested in this for testing purposes, we > already have perfectly good free-port-selection algorithms > embedded in our test harnesses. For that matter, in a test > environment it's usually best (most secure) to use a Unix > socket in a private directory, in which case there is little > need to worry about port number conflicts in the first place. >=20 I looked at get_free_port(). Here's how it works: - It selects a port from the range 10200..32767, - Checks that the port is not in use, and - Verifies there=E2=80=99s no file created for that reserved port. There=E2=80=99s a race condition between bind() and the actual process launch, and it=E2=80=99s not handled (I don=E2=80=99t think there=E2=80=99= s a simple solution with this approach). The port could be taken by any connection in the system or by another developer running their own tests on the same host (and they likely have their own directory with port reservation files). It would be much simpler to just start the server and use the port assigned by the OS kernel. The vast majority of test scenarios would work fine this way (I believe).