pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pgsql: Use pre-fetching for ANALYZE
4+ messages / 2 participants
[nested] [flat]

* pgsql: Use pre-fetching for ANALYZE
@ 2021-03-16 18:48  Stephen Frost <sfrost@snowman.net>
  0 siblings, 1 reply; 4+ messages in thread

From: Stephen Frost @ 2021-03-16 18:48 UTC (permalink / raw)
  To: pgsql-committers@lists.postgresql.org

Use pre-fetching for ANALYZE

When we have posix_fadvise() available, we can improve the performance
of an ANALYZE by quite a bit by using it to inform the kernel of the
blocks that we're going to be asking for.  Similar to bitmap index
scans, the number of buffers pre-fetched is based off of the
maintenance_io_concurrency setting (for the particular tablespace or,
if not set, globally, via get_tablespace_maintenance_io_concurrency()).

Reviewed-By: Heikki Linnakangas, Tomas Vondra
Discussion: https://www.postgresql.org/message-id/VI1PR0701MB69603A433348EDCF783C6ECBF6EF0%40VI1PR0701MB6960.eur...

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/c6fc50cb40285141fad401321ae21becbaea1c59

Modified Files
--------------
src/backend/commands/analyze.c | 73 ++++++++++++++++++++++++++++++++++++++++--
1 file changed, 71 insertions(+), 2 deletions(-)



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: pgsql: Use pre-fetching for ANALYZE
@ 2022-06-03 02:30  Andres Freund <andres@anarazel.de>
  parent: Stephen Frost <sfrost@snowman.net>
  0 siblings, 1 reply; 4+ messages in thread

From: Andres Freund @ 2022-06-03 02:30 UTC (permalink / raw)
  To: Stephen Frost <sfrost@snowman.net>; pgsql-hackers@postgresql.org

Hi,

On 2021-03-16 18:48:08 +0000, Stephen Frost wrote:
> Use pre-fetching for ANALYZE
> 
> When we have posix_fadvise() available, we can improve the performance
> of an ANALYZE by quite a bit by using it to inform the kernel of the
> blocks that we're going to be asking for.  Similar to bitmap index
> scans, the number of buffers pre-fetched is based off of the
> maintenance_io_concurrency setting (for the particular tablespace or,
> if not set, globally, via get_tablespace_maintenance_io_concurrency()).

I just looked at this as part of debugging a crash / hang in the AIO patch.

The code does:

		block_accepted = table_scan_analyze_next_block(scan, targblock, vac_strategy);

#ifdef USE_PREFETCH

		/*
		 * When pre-fetching, after we get a block, tell the kernel about the
		 * next one we will want, if there's any left.
		 *
		 * We want to do this even if the table_scan_analyze_next_block() call
		 * above decides against analyzing the block it picked.
		 */
		if (prefetch_maximum && prefetch_targblock != InvalidBlockNumber)
			PrefetchBuffer(scan->rs_rd, MAIN_FORKNUM, prefetch_targblock);
#endif

I.e. we lock a buffer and *then* we prefetch another buffer. That seems like a
quite bad idea to me. Why are we doing IO while holding a content lock, if we
can avoid it?

Greetings,

Andres Freund





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: pgsql: Use pre-fetching for ANALYZE
@ 2022-06-03 02:35  Andres Freund <andres@anarazel.de>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 1 reply; 4+ messages in thread

From: Andres Freund @ 2022-06-03 02:35 UTC (permalink / raw)
  To: Stephen Frost <sfrost@snowman.net>; pgsql-hackers@postgresql.org

Hi,

On 2022-06-02 19:30:16 -0700, Andres Freund wrote:
> On 2021-03-16 18:48:08 +0000, Stephen Frost wrote:
> > Use pre-fetching for ANALYZE
> > 
> > When we have posix_fadvise() available, we can improve the performance
> > of an ANALYZE by quite a bit by using it to inform the kernel of the
> > blocks that we're going to be asking for.  Similar to bitmap index
> > scans, the number of buffers pre-fetched is based off of the
> > maintenance_io_concurrency setting (for the particular tablespace or,
> > if not set, globally, via get_tablespace_maintenance_io_concurrency()).
> 
> I just looked at this as part of debugging a crash / hang in the AIO patch.
> 
> The code does:
> 
> 		block_accepted = table_scan_analyze_next_block(scan, targblock, vac_strategy);
> 
> #ifdef USE_PREFETCH
> 
> 		/*
> 		 * When pre-fetching, after we get a block, tell the kernel about the
> 		 * next one we will want, if there's any left.
> 		 *
> 		 * We want to do this even if the table_scan_analyze_next_block() call
> 		 * above decides against analyzing the block it picked.
> 		 */
> 		if (prefetch_maximum && prefetch_targblock != InvalidBlockNumber)
> 			PrefetchBuffer(scan->rs_rd, MAIN_FORKNUM, prefetch_targblock);
> #endif
> 
> I.e. we lock a buffer and *then* we prefetch another buffer. That seems like a
> quite bad idea to me. Why are we doing IO while holding a content lock, if we
> can avoid it?

It also seems decidedly not great from a layering POV to do the IO in
analyze.c. There's no guarantee that the tableam maps blocks in a way that's
compatible with PrefetchBuffer().  Yes, the bitmap heap scan code does
something similar, but a) that is opt in by the AM, b) there's a comment
saying it's quite crufty and should be fixed.

Greetings,

Andres Freund





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: pgsql: Use pre-fetching for ANALYZE
@ 2022-06-06 18:52  Stephen Frost <sfrost@snowman.net>
  parent: Andres Freund <andres@anarazel.de>
  0 siblings, 0 replies; 4+ messages in thread

From: Stephen Frost @ 2022-06-06 18:52 UTC (permalink / raw)
  To: Andres Freund <andres@anarazel.de>; +Cc: pgsql-hackers@postgresql.org

Greetings,

* Andres Freund (andres@anarazel.de) wrote:
> On 2022-06-02 19:30:16 -0700, Andres Freund wrote:
> > On 2021-03-16 18:48:08 +0000, Stephen Frost wrote:
> > > Use pre-fetching for ANALYZE
> > > 
> > > When we have posix_fadvise() available, we can improve the performance
> > > of an ANALYZE by quite a bit by using it to inform the kernel of the
> > > blocks that we're going to be asking for.  Similar to bitmap index
> > > scans, the number of buffers pre-fetched is based off of the
> > > maintenance_io_concurrency setting (for the particular tablespace or,
> > > if not set, globally, via get_tablespace_maintenance_io_concurrency()).
> > 
> > I just looked at this as part of debugging a crash / hang in the AIO patch.
> > 
> > The code does:
> > 
> > 		block_accepted = table_scan_analyze_next_block(scan, targblock, vac_strategy);
> > 
> > #ifdef USE_PREFETCH
> > 
> > 		/*
> > 		 * When pre-fetching, after we get a block, tell the kernel about the
> > 		 * next one we will want, if there's any left.
> > 		 *
> > 		 * We want to do this even if the table_scan_analyze_next_block() call
> > 		 * above decides against analyzing the block it picked.
> > 		 */
> > 		if (prefetch_maximum && prefetch_targblock != InvalidBlockNumber)
> > 			PrefetchBuffer(scan->rs_rd, MAIN_FORKNUM, prefetch_targblock);
> > #endif
> > 
> > I.e. we lock a buffer and *then* we prefetch another buffer. That seems like a
> > quite bad idea to me. Why are we doing IO while holding a content lock, if we
> > can avoid it?

At the end, we're doing a posix_fadvise() which is a kernel call but
hopefully wouldn't do actual IO when we call it.  Still, agreed that
it'd be better to do that without holding locks and no objection to
making such a change.

> It also seems decidedly not great from a layering POV to do the IO in
> analyze.c. There's no guarantee that the tableam maps blocks in a way that's
> compatible with PrefetchBuffer().  Yes, the bitmap heap scan code does
> something similar, but a) that is opt in by the AM, b) there's a comment
> saying it's quite crufty and should be fixed.

Certainly open to suggestions.  Are you thinking it'd make sense to add
a 'prefetch_block' method to TableAmRoutine?  Or did you have another
thought?

Thanks!

Stephen

Attachments:

  signature.asc (application/pgp-signature, 818B) download

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2022-06-06 18:52 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-03-16 18:48 pgsql: Use pre-fetching for ANALYZE Stephen Frost <sfrost@snowman.net>
2022-06-03 02:30 ` Andres Freund <andres@anarazel.de>
2022-06-03 02:35   ` Andres Freund <andres@anarazel.de>
2022-06-06 18:52     ` Stephen Frost <sfrost@snowman.net>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox