Re: Remove redundant MemoryContextSwith in BeginCopyFrom

From: Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>
To: Japin Li <japinli(at)hotmail(dot)com>
Cc: "pgsql-hackers(at)lists(dot)postgresql(dot)org" <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Remove redundant MemoryContextSwith in BeginCopyFrom
Date: 2022-01-19 15:35:09
Message-ID: CALj2ACWEjn2p=8RbFg1uKY-SXTHaxXn3bWZ=dK5qPr_r3F=O3Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, Jan 19, 2022 at 7:51 PM Japin Li <japinli(at)hotmail(dot)com> wrote:
>
>
> Hi, hackers
>
> When I read the code of COPY ... FROM ..., I find there is a redundant
> MemoryContextSwith() in BeginCopyFrom(). In BeginCopyFrom, it creates
> a COPY memory context and then switches to it, in the middle of this
> function, it switches to the oldcontext and immediately switches back to
> COPY memory context, IMO, this is redundant, and can be removed safely.

+1. It looks like a thinko from c532d15d. There's no code in between,
so switching to oldcontext doesn't make sense.

MemoryContextSwitchTo(oldcontext);
<< no code here >>
oldcontext = MemoryContextSwitchTo(cstate->copycontext);

I think we also need to remove MemoryContextSwitchTo(oldcontext); at
the end of BeginCopyTo in copyto.c, because we are not changing memory
contexts in between.

diff --git a/src/backend/commands/copyto.c b/src/backend/commands/copyto.c
index 34c8b80593..5182048e4f 100644
--- a/src/backend/commands/copyto.c
+++ b/src/backend/commands/copyto.c
@@ -742,8 +742,6 @@ BeginCopyTo(ParseState *pstate,

cstate->bytes_processed = 0;

- MemoryContextSwitchTo(oldcontext);
-
return cstate;
}

Regards,
Bharath Rupireddy.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2022-01-19 15:50:44 Re: Refactoring of compression options in pg_basebackup
Previous Message Fabrízio de Royes Mello 2022-01-19 15:34:32 Re: Remove redundant MemoryContextSwith in BeginCopyFrom