Re: track_planning causing performance regression

From: Fujii Masao <masao(dot)fujii(at)oss(dot)nttdata(dot)com>
To: Ants Aasma <ants(at)cybertec(dot)at>, Julien Rouhaud <rjuju123(at)gmail(dot)com>
Cc: "Tharakan, Robins" <tharar(at)amazon(dot)com>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: track_planning causing performance regression
Date: 2020-06-30 05:43:39
Message-ID: 5725cc95-6aea-d3a8-6ea5-36acf780a55d@oss.nttdata.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On 2020/06/29 22:23, Ants Aasma wrote:
> On Mon, 29 Jun 2020 at 12:17, Julien Rouhaud <rjuju123(at)gmail(dot)com <mailto:rjuju123(at)gmail(dot)com>> wrote:
>
> On Mon, Jun 29, 2020 at 10:55 AM Fujii Masao
> <masao(dot)fujii(at)oss(dot)nttdata(dot)com <mailto:masao(dot)fujii(at)oss(dot)nttdata(dot)com>> wrote:
> >
> > On 2020/06/29 16:05, Julien Rouhaud wrote:
> > > On Mon, Jun 29, 2020 at 7:49 AM Tharakan, Robins <tharar(at)amazon(dot)com <mailto:tharar(at)amazon(dot)com>> wrote:
> > >>
> > >> During fully-cached SELECT-only test using pgbench, Postgres v13Beta1 shows
> >
> > Thanks for the benchmark!
> >
> >
> > >> ~45% performance drop [2] at high DB connection counts (when compared with v12.3)
> >
> > That's bad :(
> >
> >
> > >>
> > >> Disabling pg_stat_statements.track_planning (which is 'On' by default)
> > >> brings the TPS numbers up to v12.3 levels.
> > >>
> > >> The inflection point (in this test-case) is 128 Connections, beyond which the
> > >> TPS numbers are consistently low. Looking at the mailing list [1], this issue
> > >> didn't surface earlier possibly since the regression is trivial at low connection counts.
> > >>
> > >> It would be great if this could be optimized further, or track_planning
> > >> disabled (by default) so as to not trip users upgrading from v12 with pg_stat_statement
> > >> enabled (but otherwise not particularly interested in track_planning).
> >
> > Your benchmark result seems to suggest that the cause of the problem is
> > the contention of per-query spinlock in pgss_store(). Right?
> > This lock contention is likely to happen when multiple sessions run
> > the same queries.
> >
> > One idea to reduce that lock contention is to separate per-query spinlock
> > into two; one is for planning, and the other is for execution. pgss_store()
> > determines which lock to use based on the given "kind" argument.
> > To make this idea work, also every pgss counters like shared_blks_hit
> > need to be separated into two, i.e., for planning and execution.
>
> This can probably remove some overhead, but won't it eventually hit
> the same issue when multiple connections try to plan the same query,
> given the number of different queries and very low execution runtime?
> It'll also quite increase the shared memory consumption.
>
> I'm wondering if we could instead use atomics to store the counters.
> The only downside is that we won't guarantee per-row consistency
> anymore, which may be problematic.
>
>
>
> The problem looks to be that spinlocks are terrible with overloaded CPU and a contended spinlock. A process holding the spinlock might easily get scheduled out leading to excessive spinning by everybody. I think a simple thing to try would be to replace the spinlock with LWLock.

Yes. Attached is the POC patch that replaces per-counter spinlock with LWLock.

>
> I did a prototype patch that replaces spinlocks with futexes, but was not able to find a workload where it mattered.

I'm not familiar with futex, but could you tell me why you used futex instead
of LWLock that we already have? Is futex portable?

> We have done a great job at eliminating spinlocks from contended code paths. Robins, perhaps you could try it to see if it reduces the regression you are observing.

Yes. Also we need to check that this change doesn't increase performance
overhead in other workloads.

Regards,

--
Fujii Masao
Advanced Computing Technology Center
Research and Development Headquarters
NTT DATA CORPORATION

Attachment Content-Type Size
pgss_lwlock_v1.patch text/plain 4.4 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Amit Langote 2020-06-30 05:53:55 Re: POC: postgres_fdw insert batching
Previous Message Dipesh Pandit 2020-06-30 05:15:43 Re: refactoring basebackup.c