Fix incorrect memory context switch in COPY TO execution
authorMichael Paquier
Sat, 29 Jan 2022 01:23:17 +0000 (10:23 +0900)
committerMichael Paquier
Sat, 29 Jan 2022 01:23:17 +0000 (10:23 +0900)
c532d15 has split the logic of COPY commands into multiple files, one
change being to move the internals of BeginCopy() to BeginCopyTo().
Originally the code was written so as we'd switch back-and-forth between
the current execution memory context and the dedicated memory context
for the COPY command, and this refactoring has introduced an extra
switch to the current memory context from the COPY context once
BeginCopyTo() is done with the past logic coming from BeginCopy().

The code was correctly doing the analyze, rewrite and planning phases in
the COPY context, but it was not assigning "copy_file" (FILE* used when
copying to a source file) and "filename" in the COPY context, making the
COPY status data inconsistent.

Author: Bharath Rupireddy
Reviewed-by: Japin Li
Discussion: https://postgr.es/m/CALj2ACWvVa69foi9jhHFY=2BuHxAoYboyE+vXQTARwxZcJnVrQ@mail.gmail.com
Backpatch-through: 14

src/backend/commands/copyto.c

index b6eacd5baa8932afe4b0eee7e04c18c1de22f437..b6a4b77053a889c1c0baae9c3fefcda578d0275d 100644 (file)
@@ -657,8 +657,6 @@ BeginCopyTo(ParseState *pstate,
 
    cstate->copy_dest = COPY_FILE;  /* default */
 
-   MemoryContextSwitchTo(oldcontext);
-
    if (pipe)
    {
        progress_vals[1] = PROGRESS_COPY_TYPE_PIPE;