fix pg_mkdir_p to tolerate concurrent directory creation

From: Andrew Dunstan <andrew(at)dunslane(dot)net>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: fix pg_mkdir_p to tolerate concurrent directory creation
Date: 2026-06-18 13:18:37
Message-ID: 3ca004de-e49b-4471-b8aa-fd656e70f68c@dunslane.net
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


While working on the pytest stuff, I found this issue when making it
work on Windows, but the issue can exist everywhere. pg_mkdir_p can fail
if there is a concurrent directory creation. This patch makes it
tolerant of concurrent directory creation. To make it more robust it
stops using stat() on Windows and instead uses GetFileAttributes().

I think this makes the function actually meet the contract set out in
the file's header comment:

 * pg_mkdir_p --- create a directory and, if necessary, parent directories
 *
 * This is equivalent to "mkdir -p" except we don't complain if the target
 * directory already exists.

Thanks to Bryan Green for help with the Windows part.

There might be a few call sites that actually work around this bug that
we can clean up.

cheers

andrew

--
Andrew Dunstan
EDB: https://www.enterprisedb.com

Attachment Content-Type Size
0001-Make-pg_mkdir_p-tolerant-of-a-concurrent-directory-c.patch text/x-patch 3.6 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2026-06-18 13:35:59 Re: Fix pg_restore --no-globals SQL output for pg_dumpall archives
Previous Message Vitaly Davydov 2026-06-18 13:11:27 Re: Deadlock detector fails to activate on a hot standby replica