postgres.git / summary / log / commit / refs

commit    599c50d25f74aae02deb9c953ccf7a9bbb3aaa7b
Author:   Masahiko Sawada <msawada@postgresql.org>
Date:     Wed Oct 21 17:44:20 2026 +0000
First released in: 14.25, 15.20, 16.16, 17.12, 18.7, 19.0

    Reject COPY TO of unlogged tables during recovery.
    
    Unlogged tables have no data on a standby, so reading one there must
    fail. SELECT and COPY (SELECT ...) TO already report a clear error,
    but COPY with a table name read the table directly and failed with a
    low-level error instead:
    
    ERROR:  could not open file "base/5/16384": No such file or directory
    
    Check for this case in COPY TO and report the same error as SELECT. On
    v19 and later, also check each partition when copying from a
    partitioned table, so that an unlogged partition is reported too.
    
    Backpatch to all supported versions, since the low-level error looks
    like data corruption.
    
    Author: Jim Jones <jim.jones@uni-muenster.de>
    Reviewed-by: Zsolt Parragi <zsolt.parragi@percona.com>
    Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>
    Discussion: https://postgr.es/m/b6d71815-d362-4579-bf07-73eca01aee6d@uni-muenster.de
    Backpatch-through: 14


src/backend/commands/copyto.c | 5 +++++ src/test/recovery/t/001_stream_rep.pl | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/backend/commands/copyto.c b/src/backend/commands/copyto.c index 99d65beac6b..24df3490827 100644 --- a/src/backend/commands/copyto.c +++ b/src/backend/commands/copyto.c @@ -400,6 +400,11 @@ BeginCopyTo(ParseState *pstate, RelationGetRelationName(rel)))); } + /* Temporary and unlogged relations are inaccessible during recovery. */ + if (rel != NULL && !RelationIsPermanent(rel) && RecoveryInProgress()) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot access temporary or unlogged relations during recovery"))); /* Allocate workspace and zero all fields */ cstate = (CopyToStateData *) palloc0(sizeof(CopyToStateData)); diff --git a/src/test/recovery/t/001_stream_rep.pl b/src/test/recovery/t/001_stream_rep.pl index 2a1bc81506b..e3d2159345e 100644 --- a/src/test/recovery/t/001_stream_rep.pl +++ b/src/test/recovery/t/001_stream_rep.pl @@ -6,7 +6,7 @@ use strict; use warnings; use PostgresNode; use TestLib; -use Test::More tests => 49; +use Test::More tests => 50; # Initialize primary node my $node_primary = get_new_node('primary'); @@ -62,6 +62,20 @@ $result = print "standby 2: $result\n"; is($result, qq(1002), 'check streamed content on standby 2'); +# Check that COPY TO rejects unlogged tables on standby +$node_primary->safe_psql( + 'postgres', q{ +CREATE UNLOGGED TABLE ultab (a int); +}); +$node_primary->wait_for_catchup($node_standby_1, 'replay', + $node_primary->lsn('flush')); +my ($copy_ret, $copy_stdout, $copy_stderr) = + $node_standby_1->psql('postgres', 'COPY ultab TO STDOUT'); +like( + $copy_stderr, + qr/cannot access temporary or unlogged relations during recovery/, + 'COPY TO of unlogged table on standby 1'); + # Check that only READ-only queries can run on standbys is($node_standby_1->psql('postgres', 'INSERT INTO tab_int VALUES (1)'), 3, 'read-only queries on standby 1'); [parent: d92c551e828f]