Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.92) (envelope-from ) id 1jIawE-0004q6-3X for pgsql-hackers@arkaria.postgresql.org; Sun, 29 Mar 2020 16:37:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1jIawB-0007cs-9D for pgsql-hackers@arkaria.postgresql.org; Sun, 29 Mar 2020 16:37:27 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1jIawA-0007cf-TD for pgsql-hackers@lists.postgresql.org; Sun, 29 Mar 2020 16:37:27 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jIaw4-0003s6-1s for pgsql-hackers@postgresql.org; Sun, 29 Mar 2020 16:37:25 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id 02TGb5fR027065; Sun, 29 Mar 2020 12:37:05 -0400 From: Tom Lane To: Justin Pryzby cc: pgsql-hackers@postgresql.org, Fabien COELHO , Alvaro Herrera , David Steele , "Bossart, Nathan" , Thomas Munro Subject: Re: pg11+: pg_ls_*dir LIMIT 1: temporary files .. not closed at end-of-transaction In-reply-to: <28906.1585422475@sss.pgh.pa.us> References: <27334.1583692669@sss.pgh.pa.us> <20200308191456.GD1357@telsasoft.com> <5679.1583696409@sss.pgh.pa.us> <20200311111921.GQ29065@telsasoft.com> <21724.1583955158@sss.pgh.pa.us> <20200312121156.GB29065@telsasoft.com> <20200316155306.GM26184@telsasoft.com> <3061.1584409130@sss.pgh.pa.us> <20200317020017.GT26184@telsasoft.com> <24244.1585415634@sss.pgh.pa.us> <20200328183904.GI20103@telsasoft.com> <28906.1585422475@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Sat, 28 Mar 2020 15:07:55 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <27037.1585499800.0@sss.pgh.pa.us> Date: Sun, 29 Mar 2020 12:37:05 -0400 Message-ID: <27064.1585499825@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <27037.1585499800.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable I wrote: > Justin Pryzby writes: >> Maybe we should lstat() the file to determine if it's a dangling link; = if >> lstat() fails, then skip it. Currently, we use stat(), which shows met= data of >> a link's *target*. Maybe we'd change that. > Hm, good point that ENOENT could refer to a symlink's target. Still, > I'm not sure it's worth going out of our way to disambiguate that, > given that these directories aren't really supposed to contain symlinks. > (And on the third hand, if they aren't supposed to, then maybe these > functions needn't look through any symlinks? In which case just > substituting lstat for stat would resolve the ambiguity.) After looking at the callers of pg_ls_dir_files, and noticing that it's already defined to ignore anything that's not a regular file, I think switching to lstat makes sense. I also grepped the other uses of ReadDir[Extended], and didn't see any other ones that seemed desperately in need of changing. So the attached seems like a sufficient fix. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="ignore-concurrent-removals-in-pg_ls_dir_files.patch"; charset="us-ascii" Content-ID: <27037.1585499800.2@sss.pgh.pa.us> Content-Description: ignore-concurrent-removals-in-pg_ls_dir_files.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/genfile.c b/src/backend/utils/adt/genfi= le.c index 01185f2..8429a12 100644 --- a/src/backend/utils/adt/genfile.c +++ b/src/backend/utils/adt/genfile.c @@ -596,10 +596,15 @@ pg_ls_dir_files(FunctionCallInfo fcinfo, const char = *dir, bool missing_ok) = /* Get the file info */ snprintf(path, sizeof(path), "%s/%s", dir, de->d_name); - if (stat(path, &attrib) < 0) + if (lstat(path, &attrib) < 0) + { + /* Ignore concurrently-deleted files, else complain */ + if (errno =3D=3D ENOENT) + continue; ereport(ERROR, (errcode_for_file_access(), errmsg("could not stat file \"%s\": %m", path))); + } = /* Ignore anything but regular files */ if (!S_ISREG(attrib.st_mode)) ------- =_aaaaaaaaaa0--