Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TMNz2-0005ci-Bk for pgsql-bugs@postgresql.org; Thu, 11 Oct 2012 18:59:48 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TMNyw-0004ep-Ml for pgsql-bugs@postgresql.org; Thu, 11 Oct 2012 18:59:46 +0000 Received: from sc9-mailhost1.vmware.com (sc9-mailhost1.vmware.com [10.113.161.71]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id 8C7B6281A6; Thu, 11 Oct 2012 11:59:39 -0700 (PDT) Received: from zcs-prod-mta-2.vmware.com (zcs-prod-mta-2.vmware.com [10.113.163.64]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id 88F71183D6; Thu, 11 Oct 2012 11:59:39 -0700 (PDT) Received: from localhost (localhost.localdomain [127.0.0.1]) by zcs-prod-mta-2.vmware.com (Postfix) with ESMTP id 94223E3239; Thu, 11 Oct 2012 12:00:46 -0700 (PDT) X-Virus-Scanned: amavisd-new at zcs-prod-mta-2.vmware.com Received: from zcs-prod-mta-2.vmware.com ([127.0.0.1]) by localhost (zcs-prod-mta-2.vmware.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id XpMrA3t6PfoV; Thu, 11 Oct 2012 12:00:46 -0700 (PDT) Received: from [10.113.224.84] (unknown [10.113.160.14]) by zcs-prod-mta-2.vmware.com (Postfix) with ESMTPSA id 970D3E321A; Thu, 11 Oct 2012 12:00:45 -0700 (PDT) Message-ID: <50771713.8000908@vmware.com> Date: Thu, 11 Oct 2012 21:59:31 +0300 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:10.0.7) Gecko/20120922 Icedove/10.0.7 MIME-Version: 1.0 To: Tom Lane CC: Pg Bugs , Dave Vitek Subject: Re: Fwd: race in pg_ctl start -w References: <5076F626.2090404@grammatech.com> <5076FF23.6020704@iki.fi> <22107.1349976592@sss.pgh.pa.us> In-Reply-To: <22107.1349976592@sss.pgh.pa.us> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -9.0 (---------) X-Archive-Number: 201210/97 X-Sequence-Number: 34397 On 11.10.2012 20:29, Tom Lane wrote: > Heikki Linnakangas writes: >> A straightforward fix would be to just allocate one large-enough buffer >> to begin with, e.g 8k, and read the whole file in one go. I'll write up >> a patch for that. > > This makes the readfile function very usage-specific though. The fix > I was thinking about was to modify the second loop to force it to fall > out once the predetermined number of lines had been read. > > Or maybe we should use just one loop with realloc, instead of reading > the file twice. Hmm, starting with 9.3, postmaster can not only create and append to the end of file, it can also inject a line in the middle, shifting the following lines forwards. In theory, if a new line is injected into the middle of the file between fgets() calls, readfile() could read part of the same line twice. Not sure what consequences that could have; pg_ctl might try to connect to wrong address or socket directory. Although in practice, fgets() is buffered, and the buffer is probably large enough to hold the whole file, so it probably gets slurped into memory as one unit anyway. Then again, I don't think read/write on a file is guaranteed to be atomic either, so I guess there's always the theoretical possibility of a partial read. This makes me a bit uncomfortable with the 9.3 change that postmaster.pid file is no longer strictly append-only (commit c9b0cbe9). Could we delay appending the socket directory and listen address information to the file until we know both, and then append both in one call after that? Gah, how can a trivial thing like this be so complicated.. - Heikki