From aa4f1fc810c8617d78dec75adad9951faf929909 Mon Sep 17 00:00:00 2001 From: Craig Ringer Date: Fri, 12 Aug 2016 15:42:12 +0800 Subject: [PATCH] Emit a HINT when COPY can't find a file Users often get confused between COPY and \copy and try to use client-side paths with COPY. The server of course cannot find the file. Emit a HINT in the most common cases to help users out. Craig Ringer, review by Tom Lane and Christoph Berg --- src/backend/commands/copy.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/backend/commands/copy.c b/src/backend/commands/copy.c index f45b330..fe44bd9 100644 --- a/src/backend/commands/copy.c +++ b/src/backend/commands/copy.c @@ -1753,6 +1753,7 @@ BeginCopyTo(Relation rel, { mode_t oumask; /* Pre-existing umask value */ struct stat st; + int save_errno; /* * Prevent write to relative path ... too easy to shoot oneself in @@ -1761,16 +1762,23 @@ BeginCopyTo(Relation rel, if (!is_absolute_path(filename)) ereport(ERROR, (errcode(ERRCODE_INVALID_NAME), - errmsg("relative path not allowed for COPY to file"))); + errmsg("relative path not allowed for COPY to file"), + errhint("COPY copies to a file on the PostgreSQL server, not on the client. " + "You may want a client-side facility such as psql's \\copy."))); oumask = umask(S_IWGRP | S_IWOTH); cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W); + save_errno = errno; umask(oumask); + if (cstate->copy_file == NULL) ereport(ERROR, (errcode_for_file_access(), errmsg("could not open file \"%s\" for writing: %m", - cstate->filename))); + cstate->filename), + (save_errno == ENOENT ? + errhint("COPY copies to a file on the PostgreSQL server, not on the client. " + "You may want a client-side facility such as psql's \\copy.") : 0))); if (fstat(fileno(cstate->copy_file), &st)) ereport(ERROR, @@ -2790,13 +2798,21 @@ BeginCopyFrom(Relation rel, else { struct stat st; + int save_errno; cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_R); + + /* in case something in ereport changes errno: */ + save_errno = errno; + if (cstate->copy_file == NULL) ereport(ERROR, (errcode_for_file_access(), errmsg("could not open file \"%s\" for reading: %m", - cstate->filename))); + cstate->filename), + (save_errno == ENOENT ? + errhint("COPY copies from a file on the PostgreSQL server, not on the client. " + "You may want a client-side facility such as psql's \\copy.") : 0))); if (fstat(fileno(cstate->copy_file), &st)) ereport(ERROR, -- 2.5.5