Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd4b667e

Browse files
committed
Fix "invalid spinlock number: 0" error in pg_stat_wal_receiver.
Commit2c8dd05 added the atomic variable writtenUpto intowalreceiver's shared memory information. It's initialized onlywhen walreceiver started up but could be read via pg_stat_wal_receiverview anytime, i.e., even before it's initialized. In the server builtwith --disable-atomics and --disable-spinlocks, this uninitializedatomic variable read could cause "invalid spinlock number: 0" error.This commit changed writtenUpto so that it's initialized atthe postmaster startup, to avoid the uninitialized variable readvia pg_stat_wal_receiver and fix the error.Also this commit moved the read of writtenUpto after the releaseof spinlock protecting walreceiver's shared variables. This isnecessary to prevent new spinlock from being taken by atomicvariable read while holding another spinlock, and to shortenthe spinlock duration. This change leads writtenUpto not to beconsistent with the other walreceiver's shared variables protectedby a spinlock. But this is OK because writtenUpto should not beused for data integrity checks.Back-patch to v13 where commit2c8dd05 introduced the bug.Author: Fujii MasaoReviewed-by: Michael Paquier, Thomas Munro, Andres FreundDiscussion:https://postgr.es/m/7ef8708c-5b6b-edd3-2cf2-7783f1c7c175@oss.nttdata.com
1 parentdaf2e70 commitd4b667e

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

‎src/backend/replication/walreceiver.c

Lines changed: 9 additions & 2 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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
selectcount(*)>=0as okfrom pg_prepared_xacts;
3434

35+
-- We expect no walreceiver running in this test
36+
selectcount(*)=0as okfrom 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, settingfrom pg_settingswhere namelike'enable%';

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp