Skip to content

Commit 9aca915

Browse files
author
Daniel Shelepanov
committed
intermediate
1 parent 7c4dcfd commit 9aca915

File tree

4 files changed

+44
-37
lines changed

4 files changed

+44
-37
lines changed

engine.c

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ ptrackMapReadFromFile(const char *ptrack_path)
258258
* postmaster is the only user right now.
259259
*/
260260
elog(DEBUG1, "ptrack read map: crc %u, file_crc %u, init_lsn %X/%X",
261-
crc, *file_crc, (uint32) (ptrack_map->init_lsn.value >> 32), (uint32) ptrack_map->init_lsn.value);
261+
crc, *file_crc, (uint16) (ptrack_map->init_lsn.value >> 16), (uint16) ptrack_map->init_lsn.value);
262262

263263
if (!EQ_CRC32C(*file_crc, crc))
264264
{
@@ -330,7 +330,7 @@ ptrackMapInit(void)
330330
* Fill entries with InvalidXLogRecPtr
331331
* (InvalidXLogRecPtr is actually 0)
332332
*/
333-
memset(ptrack_map->entries, 0, PtrackContentNblocks * sizeof(pg_atomic_uint64));
333+
memset(ptrack_map->entries, 0, PtrackContentNblocks * sizeof(pg_atomic_uint32));
334334
/*
335335
* Last part of memory representation of ptrack_map (crc) is actually unused
336336
* so leave it as it is
@@ -348,8 +348,8 @@ ptrackCheckpoint(void)
348348
pg_crc32c crc;
349349
char ptrack_path[MAXPGPATH];
350350
char ptrack_path_tmp[MAXPGPATH];
351-
XLogRecPtr init_lsn;
352-
pg_atomic_uint64 buf[PTRACK_BUF_SIZE];
351+
uint32 init_lsn;
352+
pg_atomic_uint32 buf[PTRACK_BUF_SIZE];
353353
struct stat stat_buf;
354354
uint64 i = 0;
355355
uint64 j = 0;
@@ -408,20 +408,23 @@ ptrackCheckpoint(void)
408408
ptrack_write_chunk(ptrack_tmp_fd, &crc, (char *) ptrack_map,
409409
offsetof(PtrackMapHdr, init_lsn));
410410

411-
init_lsn = pg_atomic_read_u64(&ptrack_map->init_lsn);
411+
init_lsn = pg_atomic_read_u32(&ptrack_map->init_lsn);
412412

413413
/* Set init_lsn during checkpoint if it is not set yet */
414414
if (init_lsn == InvalidXLogRecPtr)
415415
{
416416
XLogRecPtr new_init_lsn;
417+
uint32 new_init_lsn32;
417418

418419
if (RecoveryInProgress())
419420
new_init_lsn = GetXLogReplayRecPtr(NULL);
420421
else
421422
new_init_lsn = GetXLogInsertRecPtr();
422423

423-
pg_atomic_write_u64(&ptrack_map->init_lsn, new_init_lsn);
424-
init_lsn = new_init_lsn;
424+
new_init_lsn32 = (uint32)(new_init_lsn >> 16);
425+
426+
pg_atomic_write_u32(&ptrack_map->init_lsn, new_init_lsn32);
427+
init_lsn = new_init_lsn32;
425428
}
426429

427430
/* Put init_lsn in the same buffer */
@@ -435,7 +438,7 @@ ptrackCheckpoint(void)
435438
*/
436439
while (i < PtrackContentNblocks)
437440
{
438-
XLogRecPtr lsn;
441+
uint32 lsn;
439442

440443
/*
441444
* We store LSN values as pg_atomic_uint64 in the ptrack map, but
@@ -445,7 +448,7 @@ ptrackCheckpoint(void)
445448
*
446449
* TODO: is it safe and can we do any better?
447450
*/
448-
lsn = pg_atomic_read_u64(&ptrack_map->entries[i]);
451+
lsn = pg_atomic_read_u32(&ptrack_map->entries[i]);
449452
buf[j].value = lsn;
450453

451454
i++;
@@ -472,7 +475,7 @@ ptrackCheckpoint(void)
472475
/* Write if anything left */
473476
if ((i + 1) % PTRACK_BUF_SIZE != 0)
474477
{
475-
size_t writesz = sizeof(pg_atomic_uint64) * j;
478+
size_t writesz = sizeof(pg_atomic_uint32) * j;
476479

477480
ptrack_write_chunk(ptrack_tmp_fd, &crc, (char *) buf, writesz);
478481
elog(DEBUG5, "ptrack checkpoint: final i " UINT64_FORMAT ", j " UINT64_FORMAT ", writesz %zu PtrackContentNblocks " UINT64_FORMAT,
@@ -684,12 +687,13 @@ ptrack_mark_block(RelFileNodeBackend smgr_rnode,
684687
size_t slot1;
685688
size_t slot2;
686689
XLogRecPtr new_lsn;
690+
uint32 new_lsn32;
687691
/*
688692
* We use pg_atomic_uint64 here only for alignment purposes, because
689693
* pg_atomic_uint64 is forcedly aligned on 8 bytes during the MSVC build.
690694
*/
691-
pg_atomic_uint64 old_lsn;
692-
pg_atomic_uint64 old_init_lsn;
695+
pg_atomic_uint32 old_lsn;
696+
pg_atomic_uint32 old_init_lsn;
693697

694698
if (ptrack_map_size == 0
695699
|| ptrack_map == NULL
@@ -710,25 +714,27 @@ ptrack_mark_block(RelFileNodeBackend smgr_rnode,
710714
else
711715
new_lsn = GetXLogInsertRecPtr();
712716

717+
new_lsn32 = (uint32)(new_lsn >> 16);
718+
713719
/* Atomically assign new init LSN value */
714-
old_init_lsn.value = pg_atomic_read_u64(&ptrack_map->init_lsn);
720+
old_init_lsn.value = pg_atomic_read_u32(&ptrack_map->init_lsn);
715721
if (old_init_lsn.value == InvalidXLogRecPtr)
716722
{
717-
elog(DEBUG1, "ptrack_mark_block: init_lsn " UINT64_FORMAT " <- " UINT64_FORMAT, old_init_lsn.value, new_lsn);
723+
elog(DEBUG1, "ptrack_mark_block: init_lsn %u <- %u", old_init_lsn.value, new_lsn32);
718724

719-
while (old_init_lsn.value < new_lsn &&
720-
!pg_atomic_compare_exchange_u64(&ptrack_map->init_lsn, (uint64 *) &old_init_lsn.value, new_lsn));
725+
while (old_init_lsn.value < new_lsn32 &&
726+
!pg_atomic_compare_exchange_u32(&ptrack_map->init_lsn, (uint32 *) &old_init_lsn.value, new_lsn32));
721727
}
722728

723729
/* Atomically assign new LSN value to the first slot */
724-
old_lsn.value = pg_atomic_read_u64(&ptrack_map->entries[slot1]);
725-
elog(DEBUG3, "ptrack_mark_block: map[%zu]=" UINT64_FORMAT " <- " UINT64_FORMAT, slot1, old_lsn.value, new_lsn);
726-
while (old_lsn.value < new_lsn &&
727-
!pg_atomic_compare_exchange_u64(&ptrack_map->entries[slot1], (uint64 *) &old_lsn.value, new_lsn));
730+
old_lsn.value = pg_atomic_read_u32(&ptrack_map->entries[slot1]);
731+
elog(DEBUG3, "ptrack_mark_block: map[%zu]=%u <- %u", slot1, old_lsn.value, new_lsn32);
732+
while (old_lsn.value < new_lsn32 &&
733+
!pg_atomic_compare_exchange_u32(&ptrack_map->entries[slot1], (uint32 *) &old_lsn.value, new_lsn32));
728734

729735
/* And to the second */
730-
old_lsn.value = pg_atomic_read_u64(&ptrack_map->entries[slot2]);
731-
elog(DEBUG3, "ptrack_mark_block: map[%zu]=" UINT64_FORMAT " <- " UINT64_FORMAT, slot2, old_lsn.value, new_lsn);
732-
while (old_lsn.value < new_lsn &&
733-
!pg_atomic_compare_exchange_u64(&ptrack_map->entries[slot2], (uint64 *) &old_lsn.value, new_lsn));
736+
old_lsn.value = pg_atomic_read_u32(&ptrack_map->entries[slot2]);
737+
elog(DEBUG3, "ptrack_mark_block: map[%zu]=%u <- %u", slot2, old_lsn.value, new_lsn32);
738+
while (old_lsn.value < new_lsn32 &&
739+
!pg_atomic_compare_exchange_u32(&ptrack_map->entries[slot2], (uint32 *) &old_lsn.value, new_lsn32));
734740
}

engine.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ typedef struct PtrackMapHdr
6666
uint32 version_num;
6767

6868
/* LSN of the moment, when map was last enabled. */
69-
pg_atomic_uint64 init_lsn;
69+
pg_atomic_uint32 init_lsn;
7070

7171
/* Followed by the actual map of LSNs */
72-
pg_atomic_uint64 entries[FLEXIBLE_ARRAY_MEMBER];
72+
pg_atomic_uint32 entries[FLEXIBLE_ARRAY_MEMBER];
7373

7474
/*
7575
* At the end of the map CRC of type pg_crc32c is stored.
@@ -80,11 +80,11 @@ typedef PtrackMapHdr * PtrackMap;
8080

8181
/* Number of elements in ptrack map (LSN array) */
8282
#define PtrackContentNblocks \
83-
((ptrack_map_size - offsetof(PtrackMapHdr, entries) - sizeof(pg_crc32c)) / sizeof(pg_atomic_uint64))
83+
((ptrack_map_size - offsetof(PtrackMapHdr, entries) - sizeof(pg_crc32c)) / sizeof(pg_atomic_uint32))
8484

8585
/* Actual size of the ptrack map, that we are able to fit into ptrack_map_size */
8686
#define PtrackActualSize \
87-
(offsetof(PtrackMapHdr, entries) + PtrackContentNblocks * sizeof(pg_atomic_uint64) + sizeof(pg_crc32c))
87+
(offsetof(PtrackMapHdr, entries) + PtrackContentNblocks * sizeof(pg_atomic_uint32) + sizeof(pg_crc32c))
8888

8989
/* CRC32 value offset in order to directly access it in the shared memory chunk */
9090
#define PtrackCrcOffset (PtrackActualSize - sizeof(pg_crc32c))

ptrack.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ ptrack_init_lsn(PG_FUNCTION_ARGS)
508508
{
509509
if (ptrack_map != NULL)
510510
{
511-
XLogRecPtr init_lsn = pg_atomic_read_u64(&ptrack_map->init_lsn);
511+
XLogRecPtr init_lsn = (XLogRecPtr) (pg_atomic_read_u32(&ptrack_map->init_lsn) << 16);
512512

513513
PG_RETURN_LSN(init_lsn);
514514
}
@@ -541,13 +541,14 @@ ptrack_get_pagemapset(PG_FUNCTION_ARGS)
541541
if (SRF_IS_FIRSTCALL())
542542
{
543543
TupleDesc tupdesc;
544+
XLogRecPtr lsn = PG_GETARG_LSN(0);
544545

545546
funcctx = SRF_FIRSTCALL_INIT();
546547

547548
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
548549

549550
ctx = (PtScanCtx *) palloc0(sizeof(PtScanCtx));
550-
ctx->lsn = PG_GETARG_LSN(0);
551+
ctx->lsn = (uint32)(lsn >> 16);
551552
ctx->filelist = NIL;
552553

553554
/* Make tuple descriptor */
@@ -597,8 +598,8 @@ ptrack_get_pagemapset(PG_FUNCTION_ARGS)
597598
uint64 hash;
598599
size_t slot1;
599600
size_t slot2;
600-
XLogRecPtr update_lsn1;
601-
XLogRecPtr update_lsn2;
601+
uint32 update_lsn1;
602+
uint32 update_lsn2;
602603

603604
/* Stop traversal if there are no more segments */
604605
if (ctx->bid.blocknum >= ctx->relsize)
@@ -641,22 +642,22 @@ ptrack_get_pagemapset(PG_FUNCTION_ARGS)
641642
hash = BID_HASH_FUNC(ctx->bid);
642643
slot1 = (size_t)(hash % PtrackContentNblocks);
643644

644-
update_lsn1 = pg_atomic_read_u64(&ptrack_map->entries[slot1]);
645+
update_lsn1 = pg_atomic_read_u32(&ptrack_map->entries[slot1]);
645646

646647
if (update_lsn1 != InvalidXLogRecPtr)
647648
elog(DEBUG3, "ptrack: update_lsn1 %X/%X of blckno %u of file %s",
648-
(uint32) (update_lsn1 >> 32), (uint32) update_lsn1,
649+
(uint16) (update_lsn1 >> 16), (uint16) update_lsn1,
649650
ctx->bid.blocknum, ctx->relpath);
650651

651652
/* Only probe the second slot if the first one is marked */
652653
if (update_lsn1 >= ctx->lsn)
653654
{
654655
slot2 = (size_t)(((hash << 32) | (hash >> 32)) % PtrackContentNblocks);
655-
update_lsn2 = pg_atomic_read_u64(&ptrack_map->entries[slot2]);
656+
update_lsn2 = pg_atomic_read_u32(&ptrack_map->entries[slot2]);
656657

657658
if (update_lsn2 != InvalidXLogRecPtr)
658659
elog(DEBUG3, "ptrack: update_lsn2 %X/%X of blckno %u of file %s",
659-
(uint32) (update_lsn1 >> 32), (uint32) update_lsn2,
660+
(uint16) (update_lsn1 >> 16), (uint16) update_lsn2,
660661
ctx->bid.blocknum, ctx->relpath);
661662

662663
/* Block has been changed since specified LSN. Mark it in the bitmap */

ptrack.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ typedef struct PtBlockId
6262
*/
6363
typedef struct PtScanCtx
6464
{
65-
XLogRecPtr lsn;
65+
uint32 lsn;
6666
PtBlockId bid;
6767
uint32 relsize;
6868
char *relpath;

0 commit comments

Comments
 (0)