Skip to content

Commit 942b8ea

Browse files
macdiceCommitfest Bot
authored andcommitted
Improve API for storing data in read streams.
Read stream callbacks receive a void pointer into the per-buffer data queue so that can store data there for later retrieval by the buffer consumer. We can improve readability and safety a bit by changing cast-and-assign or raw memcpy() to: read_stream_put_value(stream, per_buffer_data, my_int); This form infers the size and asserts that the storage space matches, generally mirroring the read_stream_get_buffer_and_value() call used for retrieving the streamed data later.
1 parent 85b3620 commit 942b8ea

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/backend/access/heap/vacuumlazy.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,7 @@ heap_vac_scan_next_block(ReadStream *stream,
16491649
*/
16501650
vacrel->current_block = next_block;
16511651
blk_info |= VAC_BLK_ALL_VISIBLE_ACCORDING_TO_VM;
1652-
*((uint8 *) per_buffer_data) = blk_info;
1652+
read_stream_put_value(stream, per_buffer_data, blk_info);
16531653
return vacrel->current_block;
16541654
}
16551655
else
@@ -1665,7 +1665,7 @@ heap_vac_scan_next_block(ReadStream *stream,
16651665
blk_info |= VAC_BLK_ALL_VISIBLE_ACCORDING_TO_VM;
16661666
if (vacrel->next_unskippable_eager_scanned)
16671667
blk_info |= VAC_BLK_WAS_EAGER_SCANNED;
1668-
*((uint8 *) per_buffer_data) = blk_info;
1668+
read_stream_put_value(stream, per_buffer_data, blk_info);
16691669
return vacrel->current_block;
16701670
}
16711671
}
@@ -2711,7 +2711,7 @@ vacuum_reap_lp_read_stream_next(ReadStream *stream,
27112711
* Save the TidStoreIterResult for later, so we can extract the offsets.
27122712
* It is safe to copy the result, according to TidStoreIterateNext().
27132713
*/
2714-
memcpy(per_buffer_data, iter_result, sizeof(*iter_result));
2714+
read_stream_put_value(stream, per_buffer_data, *iter_result);
27152715

27162716
return iter_result->blkno;
27172717
}

src/include/storage/read_stream.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,13 @@ read_stream_get_buffer_and_value_with_size(ReadStream *stream,
157157
(AssertMacro(sizeof(**(pointer)) <= read_stream_per_buffer_data_size(stream)), \
158158
read_stream_next_buffer((stream), ((void **) (pointer))))
159159

160+
/*
161+
* Set the per-buffer data by value. This can be called from inside a
162+
* callback that is returning block numbers. It asserts that the value's size
163+
* matches the available space.
164+
*/
165+
#define read_stream_put_value(stream, per_buffer_data, value) \
166+
(AssertMacro(sizeof(value) == read_stream_per_buffer_data_size(stream)), \
167+
memcpy((per_buffer_data), &(value), sizeof(value)))
168+
160169
#endif /* READ_STREAM_H */

0 commit comments

Comments
 (0)