Skip to content

Commit 78a33bb

Browse files
committed
Improve error message for snapshot import in snapmgr.c, take two
When a snapshot file fails to be read in ImportSnapshot(), it would issue an ERROR as "invalid snapshot identifier" when opening a stream for it in read-only mode. The error handling is improved to be more talkative in failure cases: - If a snapshot identifier uses incorrect characters, complain with the same error as before this commit. - If the snapshot file cannot be found in pg_snapshots/, complain with a "snapshot \"foo\" does not exist" instead. This maps to the case where AllocateFile() fails on ENOENT. Based on a suggestion from Andres Freund. - If AllocateFile() throws something else than ENOENT as errno, report it with more details in %m instead, as these failures are never expected. b29504eeb489 was the first improvement take. The older error message exists since bb446b6 that introduced snapshot imports. Two test cases are added to cover the cases of an identifier with an incorrect format and of a missing snapshot. Author: Bharath Rupireddy Reviewed-by: Andres Freund, Daniel Gustafsson, Michael Paquier Discussion: https://postgr.es/m/CALj2ACWmr=3KdxDkm8h7Zn1XxBoF6hdzq8WQyMn2y1OL5RYFrg@mail.gmail.com
1 parent af5b3c3 commit 78a33bb

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

src/backend/utils/time/snapmgr.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,9 +1390,21 @@ ImportSnapshot(const char *idstr)
13901390

13911391
f = AllocateFile(path, PG_BINARY_R);
13921392
if (!f)
1393-
ereport(ERROR,
1394-
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
1395-
errmsg("invalid snapshot identifier: \"%s\"", idstr)));
1393+
{
1394+
/*
1395+
* If file is missing while identifier has a correct format, avoid
1396+
* system errors.
1397+
*/
1398+
if (errno == ENOENT)
1399+
ereport(ERROR,
1400+
(errcode(ERRCODE_UNDEFINED_OBJECT),
1401+
errmsg("snapshot \"%s\" does not exist", idstr)));
1402+
else
1403+
ereport(ERROR,
1404+
(errcode_for_file_access(),
1405+
errmsg("could not open file \"%s\" for reading: %m",
1406+
path)));
1407+
}
13961408

13971409
/* get the size of the file so that we know how much memory we need */
13981410
if (fstat(fileno(f), &stat_buf))

src/test/regress/expected/transactions.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,17 @@ SELECT * FROM trans_abc ORDER BY 1;
11481148
(3 rows)
11491149

11501150
DROP TABLE trans_abc;
1151+
-- TRANSACTION SNAPSHOT
1152+
-- Incorrect identifier.
1153+
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
1154+
SET TRANSACTION SNAPSHOT 'Incorrect Identifier';
1155+
ERROR: invalid snapshot identifier: "Incorrect Identifier"
1156+
ROLLBACK;
1157+
-- Correct identifier, missing file.
1158+
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
1159+
SET TRANSACTION SNAPSHOT 'FFF-FFF-F';
1160+
ERROR: snapshot "FFF-FFF-F" does not exist
1161+
ROLLBACK;
11511162
-- Test for successful cleanup of an aborted transaction at session exit.
11521163
-- THIS MUST BE THE LAST TEST IN THIS FILE.
11531164
begin;

src/test/regress/sql/transactions.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,15 @@ SELECT * FROM trans_abc ORDER BY 1;
613613

614614
DROP TABLE trans_abc;
615615

616+
-- TRANSACTION SNAPSHOT
617+
-- Incorrect identifier.
618+
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
619+
SET TRANSACTION SNAPSHOT 'Incorrect Identifier';
620+
ROLLBACK;
621+
-- Correct identifier, missing file.
622+
BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ;
623+
SET TRANSACTION SNAPSHOT 'FFF-FFF-F';
624+
ROLLBACK;
616625

617626
-- Test for successful cleanup of an aborted transaction at session exit.
618627
-- THIS MUST BE THE LAST TEST IN THIS FILE.

0 commit comments

Comments
 (0)