Re: BUG #17288: PSQL bug with COPY command (Windows)

Dmitry Koval <d.koval@postgrespro.ru>

From: Dmitry Koval <d.koval@postgrespro.ru>
To: Michael Paquier <michael@paquier.xyz>
Cc: pgsql-bugs@lists.postgresql.org
Date: 2021-11-18T13:23:54Z
Lists: pgsql-bugs

Attachments

 >_fstat64() and _stat64() have proved to be tricky to work with and 
 >rather unworkable across all the build systems we support

I agree, it is better not use them.

 >I think that we had better never call GetFileInformationByHandle() if
 >we use a fileno that maps to stdin, stdout or stderr.

Probably we should call GetFileInformationByHandle() for case standard
streams stdin/stdout/stderr are redirected to files.
See file src\backend\utils\error\elog.c, for example. It contains
lines:

if (!freopen(OutputFileName, "a", stderr))
if (!freopen(OutputFileName, "a", stdout))

And this command with "stderr" works in PSQL without crash (in
contrast to "stdout"):

\copy (SELECT 1) TO stderr

(it put resullt into file with name "stderr").
We can emulate stats for stdin/stdout/stderr after call
GetFileInformationByHandle().

 >Just to be sure, this is the code path in psql's copy.c where we check 
 >that a specified copystream is not a directory, right?

Yes, fstat() called from file src/bin/psql/copy.c:

/* make sure the specified file is not a directory */
if ((result = fstat(fileno(copystream), &st)) < 0)

I attached new patch version.

With best regards,
Dmitry Koval.

Commits

  1. Fix compatibility thinko for fstat() on standard streams in win32stat.c

  2. Fix fstat() emulation on Windows with standard streams