Skip to content

Latest commit

 

History

History
108 lines (73 loc) · 4.29 KB

20231220_01.md

File metadata and controls

108 lines (73 loc) · 4.29 KB

PostgreSQL 17 preview - WAL锁竞争优化 - reading WAL buffer contents without a lock, Additional write barrier in AdvanceXLInsertBuffer()

作者

digoal

日期

2023-12-20

标签

PostgreSQL , PolarDB , DuckDB , WAL , lock , barrier , pg_atomic_write_u64


背景

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=766571be16598b401b5527208847145edc6be1f4

Additional write barrier in AdvanceXLInsertBuffer(). master github/master  
  
author	Jeff Davis <jdavis@postgresql.org>	  
Wed, 20 Dec 2023 01:35:54 +0000 (17:35 -0800)  
committer	Jeff Davis <jdavis@postgresql.org>	  
Wed, 20 Dec 2023 01:35:54 +0000 (17:35 -0800)  
commit	766571be16598b401b5527208847145edc6be1f4  
tree	8b996fa24f62b557b285ff07123a1d7e79a56092	tree  
parent	c3a8e2a7cb16d55e3b757934b538cb8b8a0eab02	commit | diff  
Additional write barrier in AdvanceXLInsertBuffer().  
  
First, mark the xlblocks member with InvalidXLogRecPtr, then issue a  
write barrier, then initialize it. That ensures that the xlblocks  
member doesn't appear valid while the contents are being initialized.  
  
In preparation for reading WAL buffer contents without a lock.  
  
Author: Bharath Rupireddy  
Discussion: https://postgr.es/m/CALj2ACVfFMfqD5oLzZSQQZWfXiJqd-NdX0_317veP6FuB31QWA@mail.gmail.com  
Reviewed-by: Andres Freund  
+       /*  
+        * Mark the xlblock with InvalidXLogRecPtr and issue a write barrier  
+        * before initializing. Otherwise, the old page may be partially  
+        * zeroed but look valid.  
+        */  
+       pg_atomic_write_u64(&XLogCtl->xlblocks[nextidx], InvalidXLogRecPtr);  
+       pg_write_barrier();  
+  

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=c3a8e2a7cb16d55e3b757934b538cb8b8a0eab02

Use 64-bit atomics for xlblocks array elements.  
  
author	Jeff Davis <jdavis@postgresql.org>	  
Wed, 20 Dec 2023 01:35:42 +0000 (17:35 -0800)  
committer	Jeff Davis <jdavis@postgresql.org>	  
Wed, 20 Dec 2023 01:35:42 +0000 (17:35 -0800)  
commit	c3a8e2a7cb16d55e3b757934b538cb8b8a0eab02  
tree	d22d792b0b99be09bb267399ede72c5f16419de8	tree  
parent	1301c80b2167feb658a738fa4ceb1c23d0991e23	commit | diff  
Use 64-bit atomics for xlblocks array elements.  
  
In preparation for reading the contents of WAL buffers without a  
lock. Also, avoids the previously-needed comment in GetXLogBuffer()  
explaining why it's safe from torn reads.  
  
Author: Bharath Rupireddy  
Discussion: https://postgr.es/m/CALj2ACVfFMfqD5oLzZSQQZWfXiJqd-NdX0_317veP6FuB31QWA@mail.gmail.com  
Reviewed-by: Andres Freund  
-   XLogRecPtr *xlblocks;       /* 1st byte ptr-s + XLOG_BLCKSZ */  
+   pg_atomic_uint64 *xlblocks; /* 1st byte ptr-s + XLOG_BLCKSZ */  
  
+    * We don't hold a lock while we read the value. If someone is just about  
+    * to initialize or has just initialized the page, it's possible that we  
+    * get InvalidXLogRecPtr. That's ok, we'll grab the mapping lock (in  
+    * AdvanceXLInsertBuffer) and retry if we see anything other than the page  
+    * we're looking for.  

digoal's wechat