diff --git a/src/backend/storage/file/README b/src/backend/storage/file/README index 26c9a5b5120..ed29ae8bbee 100644 --- a/src/backend/storage/file/README +++ b/src/backend/storage/file/README @@ -66,7 +66,8 @@ These run when the API is called, so the file or directory can be used within the same transaction, and each registers an abort-time undo action that reverses it if the transaction rolls back: - FileOpsCreate -- create() now; UNDO/pending-delete on abort + FileOpsCreate -- create() now, an ERROR if the file exists; UNDO/pending-delete + on abort FileOpsMkdir -- mkdir() now; UNDO rmdir on abort FileOpsMkdirTree -- mkdir() now; UNDO removes the whole tree on abort FileOpsChmod -- chmod() now; UNDO restore old mode on abort diff --git a/src/backend/storage/file/fileops.c b/src/backend/storage/file/fileops.c index 386eb618910..8ad7ab281ff 100644 --- a/src/backend/storage/file/fileops.c +++ b/src/backend/storage/file/fileops.c @@ -431,6 +431,11 @@ FileOpsCancelPendingDelete(const char *path, bool at_commit) * and logs the creation to WAL. If register_delete is true, the file will * be deleted if the transaction aborts. * + * A path that already exists is an ERROR (ERRCODE_DUPLICATE_FILE), as for + * FileOpsMkdir(), FileOpsLink() and FileOpsRename(). The UNDO record + * unlinks the path, so the path must name a file this call created: opening + * an existing file here would let a rollback remove a committed one. + * * Platform handling: * - Linux/FreeBSD: O_DIRECT passed directly to open() * - macOS: F_NOCACHE fcntl applied after open() @@ -446,7 +451,7 @@ FileOpsCreate(const char *path, int flags, mode_t mode, bool register_delete) Assert(!IsInParallelMode()); - fd = OpenTransientFilePerm(path, flags | O_CREAT, mode); + fd = OpenTransientFilePerm(path, flags | O_CREAT | O_EXCL, mode); if (fd < 0) ereport(ERROR, (errcode_for_file_access(), diff --git a/src/test/modules/test_fileops/meson.build b/src/test/modules/test_fileops/meson.build index e14676912a4..e9c51446bed 100644 --- a/src/test/modules/test_fileops/meson.build +++ b/src/test/modules/test_fileops/meson.build @@ -44,6 +44,7 @@ tests += { 't/018_fileops_rename.pl', 't/023_fileops_inline_abort_ctx.pl', 't/024_fileops_recovery_apply_failure.pl', + 't/025_fileops_create_existing.pl', ], }, } diff --git a/src/test/modules/test_fileops/t/025_fileops_create_existing.pl b/src/test/modules/test_fileops/t/025_fileops_create_existing.pl new file mode 100644 index 00000000000..a0db2dc29f0 --- /dev/null +++ b/src/test/modules/test_fileops/t/025_fileops_create_existing.pl @@ -0,0 +1,56 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# FileOpsCreate() on a path that already exists, then ROLLBACK. The UNDO +# record of a create unlinks the path, so if the call opened an existing file +# a rollback would remove a committed one. The committed file must survive +# on the primary, on a standby replaying the abort record, and on the primary +# after crash recovery replays it. +use strict; +use warnings FATAL => 'all'; +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; + +my $primary = PostgreSQL::Test::Cluster->new('primary'); +$primary->init(allows_streaming => 1); +$primary->start; +$primary->safe_psql('postgres', 'CREATE EXTENSION test_fileops'); +$primary->backup('backup'); +my $standby = PostgreSQL::Test::Cluster->new('standby'); +$standby->init_from_backup($primary, 'backup', has_streaming => 1); +$standby->start; + +$primary->safe_psql('postgres', q{ +BEGIN; +SELECT test_fileops_create('existing.dat', 384); +SELECT test_fileops_write('existing.dat', 0, 'committed'); +COMMIT; +}); + +my ($ret, $out, $err) = $primary->psql('postgres', q{ +BEGIN; +SELECT test_fileops_create('existing.dat', 384); +ROLLBACK; +}); +like($err, qr/could not create file "existing.dat": File exists/, + 'creating an existing path is an ERROR'); + +$primary->wait_for_catchup($standby); +for my $stage ('online', 'after crash') +{ + if ($stage eq 'after crash') + { + $primary->stop('immediate'); + $primary->start; + } + my $f = $primary->data_dir . '/existing.dat'; + is(-e $f ? slurp_file($f) : '(missing)', + 'committed', "$stage: primary keeps the committed file"); +} +my $sf = $standby->data_dir . '/existing.dat'; +is(-e $sf ? slurp_file($sf) : '(missing)', + 'committed', 'standby keeps the committed file'); + +$standby->stop; +$primary->stop; +done_testing();