Skip to content

Commit 704261e

Browse files
committed
Improve IO accounting for temp relation writes
Both pgstat_database and pgBufferUsage count IO timing for reads of temporary relation blocks into local buffers. However, both failed to count write IO timing for flushes of dirty local buffers. Fix. Additionally, FlushRelationBuffers() seems to have omitted counting write IO (both count and timing) stats for both pgstat_database and pgBufferUsage. Fix. Author: Melanie Plageman <melanieplageman@gmail.com> Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/20230321023451.7rzy4kjj2iktrg2r%40awork3.anarazel.de
1 parent bf5a894 commit 704261e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/backend/storage/buffer/bufmgr.c

+17
Original file line numberDiff line numberDiff line change
@@ -4062,6 +4062,8 @@ FlushRelationBuffers(Relation rel)
40624062
{
40634063
int i;
40644064
BufferDesc *bufHdr;
4065+
instr_time io_start,
4066+
io_time;
40654067

40664068
if (RelationUsesLocalBuffers(rel))
40674069
{
@@ -4087,6 +4089,11 @@ FlushRelationBuffers(Relation rel)
40874089

40884090
PageSetChecksumInplace(localpage, bufHdr->tag.blockNum);
40894091

4092+
if (track_io_timing)
4093+
INSTR_TIME_SET_CURRENT(io_start);
4094+
else
4095+
INSTR_TIME_SET_ZERO(io_start);
4096+
40904097
smgrwrite(RelationGetSmgr(rel),
40914098
BufTagGetForkNum(&bufHdr->tag),
40924099
bufHdr->tag.blockNum,
@@ -4098,6 +4105,16 @@ FlushRelationBuffers(Relation rel)
40984105

40994106
pgstat_count_io_op(IOOBJECT_TEMP_RELATION, IOCONTEXT_NORMAL, IOOP_WRITE);
41004107

4108+
if (track_io_timing)
4109+
{
4110+
INSTR_TIME_SET_CURRENT(io_time);
4111+
INSTR_TIME_SUBTRACT(io_time, io_start);
4112+
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
4113+
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
4114+
}
4115+
4116+
pgBufferUsage.local_blks_written++;
4117+
41014118
/* Pop the error context stack */
41024119
error_context_stack = errcallback.previous;
41034120
}

src/backend/storage/buffer/localbuf.c

+16
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ GetLocalVictimBuffer(void)
176176
int trycounter;
177177
uint32 buf_state;
178178
BufferDesc *bufHdr;
179+
instr_time io_start,
180+
io_time;
179181

180182
ResourceOwnerEnlargeBuffers(CurrentResourceOwner);
181183

@@ -239,6 +241,11 @@ GetLocalVictimBuffer(void)
239241

240242
PageSetChecksumInplace(localpage, bufHdr->tag.blockNum);
241243

244+
if (track_io_timing)
245+
INSTR_TIME_SET_CURRENT(io_start);
246+
else
247+
INSTR_TIME_SET_ZERO(io_start);
248+
242249
/* And write... */
243250
smgrwrite(oreln,
244251
BufTagGetForkNum(&bufHdr->tag),
@@ -252,6 +259,15 @@ GetLocalVictimBuffer(void)
252259

253260
/* Temporary table I/O does not use Buffer Access Strategies */
254261
pgstat_count_io_op(IOOBJECT_TEMP_RELATION, IOCONTEXT_NORMAL, IOOP_WRITE);
262+
263+
if (track_io_timing)
264+
{
265+
INSTR_TIME_SET_CURRENT(io_time);
266+
INSTR_TIME_SUBTRACT(io_time, io_start);
267+
pgstat_count_buffer_write_time(INSTR_TIME_GET_MICROSEC(io_time));
268+
INSTR_TIME_ADD(pgBufferUsage.blk_write_time, io_time);
269+
}
270+
255271
pgBufferUsage.local_blks_written++;
256272
}
257273

0 commit comments

Comments
 (0)