Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d4b667e

Browse files
committed
Fix "invalid spinlock number: 0" error in pg_stat_wal_receiver.
Commit 2c8dd05 added the atomic variable writtenUpto into walreceiver's shared memory information. It's initialized only when walreceiver started up but could be read via pg_stat_wal_receiver view anytime, i.e., even before it's initialized. In the server built with --disable-atomics and --disable-spinlocks, this uninitialized atomic variable read could cause "invalid spinlock number: 0" error. This commit changed writtenUpto so that it's initialized at the postmaster startup, to avoid the uninitialized variable read via pg_stat_wal_receiver and fix the error. Also this commit moved the read of writtenUpto after the release of spinlock protecting walreceiver's shared variables. This is necessary to prevent new spinlock from being taken by atomic variable read while holding another spinlock, and to shorten the spinlock duration. This change leads writtenUpto not to be consistent with the other walreceiver's shared variables protected by a spinlock. But this is OK because writtenUpto should not be used for data integrity checks. Back-patch to v13 where commit 2c8dd05 introduced the bug. Author: Fujii Masao Reviewed-by: Michael Paquier, Thomas Munro, Andres Freund Discussion: https://postgr.es/m/7ef8708c-5b6b-edd3-2cf2-7783f1c7c175@oss.nttdata.com
1 parent daf2e70 commit d4b667e

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/backend/replication/walreceiver.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ WalReceiverMain(void)
261261

262262
SpinLockRelease(&walrcv->mutex);
263263

264-
pg_atomic_init_u64(&WalRcv->writtenUpto, 0);
264+
pg_atomic_write_u64(&WalRcv->writtenUpto, 0);
265265

266266
/* Arrange to clean up at walreceiver exit */
267267
on_shmem_exit(WalRcvDie, 0);
@@ -1376,7 +1376,6 @@ pg_stat_get_wal_receiver(PG_FUNCTION_ARGS)
13761376
state = WalRcv->walRcvState;
13771377
receive_start_lsn = WalRcv->receiveStart;
13781378
receive_start_tli = WalRcv->receiveStartTLI;
1379-
written_lsn = pg_atomic_read_u64(&WalRcv->writtenUpto);
13801379
flushed_lsn = WalRcv->flushedUpto;
13811380
received_tli = WalRcv->receivedTLI;
13821381
last_send_time = WalRcv->lastMsgSendTime;
@@ -1396,6 +1395,14 @@ pg_stat_get_wal_receiver(PG_FUNCTION_ARGS)
13961395
if (pid == 0 || !ready_to_display)
13971396
PG_RETURN_NULL();
13981397

1398+
/*
1399+
* Read "writtenUpto" without holding a spinlock. Note that it may not be
1400+
* consistent with the other shared variables of the WAL receiver
1401+
* protected by a spinlock, but this should not be used for data integrity
1402+
* checks.
1403+
*/
1404+
written_lsn = pg_atomic_read_u64(&WalRcv->writtenUpto);
1405+
13991406
/* determine result type */
14001407
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
14011408
elog(ERROR, "return type must be a row type");

src/backend/replication/walreceiverfuncs.c

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ WalRcvShmemInit(void)
6363
MemSet(WalRcv, 0, WalRcvShmemSize());
6464
WalRcv->walRcvState = WALRCV_STOPPED;
6565
SpinLockInit(&WalRcv->mutex);
66+
pg_atomic_init_u64(&WalRcv->writtenUpto, 0);
6667
WalRcv->latch = NULL;
6768
}
6869
}

src/test/regress/expected/sysviews.out

+7
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ select count(*) >= 0 as ok from pg_prepared_xacts;
6767
t
6868
(1 row)
6969

70+
-- We expect no walreceiver running in this test
71+
select count(*) = 0 as ok from pg_stat_wal_receiver;
72+
ok
73+
----
74+
t
75+
(1 row)
76+
7077
-- This is to record the prevailing planner enable_foo settings during
7178
-- a regression test run.
7279
select name, setting from pg_settings where name like 'enable%';

src/test/regress/sql/sysviews.sql

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ select count(*) = 0 as ok from pg_prepared_statements;
3232
-- See also prepared_xacts.sql
3333
select count(*) >= 0 as ok from pg_prepared_xacts;
3434

35+
-- We expect no walreceiver running in this test
36+
select count(*) = 0 as ok from pg_stat_wal_receiver;
37+
3538
-- This is to record the prevailing planner enable_foo settings during
3639
-- a regression test run.
3740
select name, setting from pg_settings where name like 'enable%';

0 commit comments

Comments
 (0)