Skip to content

Commit f421bf8

Browse files
BRupireddy2Satyanarayana Narlapuram
authored andcommitted
Make autoprewarm yield to conflicting lock requests.
The autoprewarm worker opens every relation it has recorded blocks for and holds AccessShareLock on it until all of those blocks have been read back into shared buffers. On a large relation that takes a long time, and anything that needs a conflicting lock in the meantime, TRUNCATE or DROP for instance, waits behind the worker for the whole of it. Nothing bounds that wait, and it buys nothing, since prewarming is best-effort work done for the benefit of later queries. Fix this by having the worker check from time to time whether a conflicting request is waiting on the relation, and give the relation up when one is. It closes the relation, which releases the lock, and moves on to the next one rather than coming back to finish later, because once a conflicting strong lock has been taken the blocks recorded for that relation may no longer be the ones worth reading. Heap truncation in VACUUM already yields to waiters the same way. The check is throttled by both a block count and a time interval, so it costs nothing measurable while nobody is waiting. Note that the pg_prewarm() function is left as it is. Its caller asked for a bounded amount of work and is waiting on the result, which is the same reason a foreground VACUUM does not deprioritize itself. Author: Bharath Rupireddy <bharath.rupireddyforpostgres@gmail.com> Co-authored-by: Satyanarayana Narlapuram <satyanarlapuram@gmail.com> Reviewed-by: Robert Haas <robertmhaas@gmail.com> Reviewed-by: Michael Paquier <michael@paquier.xyz> Reviewed-by: Palak Chaturvedi <chaturvedipalak1911@gmail.com> Discussion: https://postgr.es/m/CAHg+QDfdoR=7iqEAvLW9qtzV0Sx1wp2FuALeamqcCdiVEmMF-Q@mail.gmail.com
1 parent 404affb commit f421bf8

1 file changed

Lines changed: 89 additions & 20 deletions

File tree

‎contrib/pg_prewarm/autoprewarm.c‎

Lines changed: 89 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "access/relation.h"
3232
#include "access/xact.h"
3333
#include "pgstat.h"
34+
#include "portability/instr_time.h"
3435
#include "postmaster/bgworker.h"
3536
#include "postmaster/interrupt.h"
3637
#include "storage/buf_internals.h"
@@ -39,6 +40,7 @@
3940
#include "storage/fd.h"
4041
#include "storage/ipc.h"
4142
#include "storage/latch.h"
43+
#include "storage/lmgr.h"
4244
#include "storage/lwlock.h"
4345
#include "storage/procsignal.h"
4446
#include "storage/read_stream.h"
@@ -52,6 +54,14 @@
5254

5355
#define AUTOPREWARM_FILE "autoprewarm.blocks"
5456

57+
/*
58+
* How often the prewarm loop checks for a conflicting lock request: probe the
59+
* shared lock table at most once per interval (in ms), and only consult the
60+
* clock once every so many blocks.
61+
*/
62+
#define PREWARM_LOCK_CHECK_INTERVAL 20 /* ms */
63+
#define PREWARM_LOCK_CHECK_BLOCKS 32
64+
5565
/* Metadata for each block we dump. */
5666
typedef struct BlockInfoRecord
5767
{
@@ -493,6 +503,73 @@ apw_read_stream_next_block(ReadStream *stream,
493503
return InvalidBlockNumber;
494504
}
495505

506+
/*
507+
* Prewarm the blocks of one fork by draining the read stream, and return true
508+
* if a conflicting lock request showed up while doing so. On such a waiter we
509+
* stop early rather than reacquiring the lock and resuming; prewarming is
510+
* best-effort, and the caller gives up the relation to let the waiter proceed.
511+
* The read stream is always shut down before returning.
512+
*/
513+
static bool
514+
apw_prewarm_blocks(Relation rel, struct AutoPrewarmReadStreamData *p)
515+
{
516+
ReadStream *stream;
517+
Buffer buf;
518+
instr_time starttime;
519+
int blocks_since_check = 0;
520+
bool waiter_detected = false;
521+
522+
stream = read_stream_begin_relation(READ_STREAM_MAINTENANCE |
523+
READ_STREAM_DEFAULT |
524+
READ_STREAM_USE_BATCHING,
525+
NULL,
526+
rel,
527+
p->forknum,
528+
apw_read_stream_next_block,
529+
p,
530+
0);
531+
532+
INSTR_TIME_SET_CURRENT(starttime);
533+
534+
while ((buf = read_stream_next_buffer(stream, NULL)) != InvalidBuffer)
535+
{
536+
apw_state->prewarmed_blocks++;
537+
ReleaseBuffer(buf);
538+
539+
/*
540+
* Check for a conflicting lock waiter, but keep the clock reads and
541+
* lock table probes rare: only look at the clock every
542+
* PREWARM_LOCK_CHECK_BLOCKS blocks, and only probe once
543+
* PREWARM_LOCK_CHECK_INTERVAL has elapsed since the last probe.
544+
*/
545+
if (++blocks_since_check >= PREWARM_LOCK_CHECK_BLOCKS)
546+
{
547+
instr_time currenttime;
548+
instr_time elapsed;
549+
550+
blocks_since_check = 0;
551+
552+
INSTR_TIME_SET_CURRENT(currenttime);
553+
elapsed = currenttime;
554+
INSTR_TIME_SUBTRACT(elapsed, starttime);
555+
if ((INSTR_TIME_GET_MICROSEC(elapsed) / 1000)
556+
>= PREWARM_LOCK_CHECK_INTERVAL)
557+
{
558+
if (LockHasWaitersRelation(rel, AccessShareLock))
559+
{
560+
waiter_detected = true;
561+
break;
562+
}
563+
starttime = currenttime;
564+
}
565+
}
566+
}
567+
568+
read_stream_end(stream);
569+
570+
return waiter_detected;
571+
}
572+
496573
/*
497574
* Prewarm all blocks for one database (and possibly also global objects, if
498575
* those got grouped with this database).
@@ -577,8 +654,6 @@ autoprewarm_database_main(Datum main_arg)
577654
ForkNumber forknum;
578655
BlockNumber nblocks;
579656
struct AutoPrewarmReadStreamData p;
580-
ReadStream *stream;
581-
Buffer buf;
582657

583658
blk = block_info[i];
584659

@@ -627,29 +702,23 @@ autoprewarm_database_main(Datum main_arg)
627702
.nblocks = nblocks,
628703
};
629704

630-
stream = read_stream_begin_relation(READ_STREAM_MAINTENANCE |
631-
READ_STREAM_DEFAULT |
632-
READ_STREAM_USE_BATCHING,
633-
NULL,
634-
rel,
635-
p.forknum,
636-
apw_read_stream_next_block,
637-
&p,
638-
0);
639-
640705
/*
641-
* Loop until we've prewarmed all the blocks from this fork. The
642-
* read stream callback will check that we still have free buffers
643-
* before requesting each block from the read stream API.
706+
* On a conflicting lock waiter, give up the whole relation: skip
707+
* its remaining blocks and break out so we close it (releasing
708+
* the lock) and move on to the next one.
644709
*/
645-
while ((buf = read_stream_next_buffer(stream, NULL)) != InvalidBuffer)
710+
if (apw_prewarm_blocks(rel, &p))
646711
{
647-
apw_state->prewarmed_blocks++;
648-
ReleaseBuffer(buf);
712+
for (i = p.pos; i < apw_state->prewarm_stop_idx; i++)
713+
{
714+
blk = block_info[i];
715+
if (blk.tablespace != tablespace ||
716+
blk.filenumber != filenumber)
717+
break;
718+
}
719+
break;
649720
}
650721

651-
read_stream_end(stream);
652-
653722
/*
654723
* Advance i past all the blocks just prewarmed. Note that the
655724
* callback might have advanced the index beyond the last valid

0 commit comments

Comments
 (0)