>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().
You are right about freopen(), but stderr works just because it's being parsed as a file, not a stream, by parse_slash_copy().
I attached new patch version.
I would keep the memset(buf, 0, sizeof(*buf)) for the members we are not setting.