|
| 1 | +## PostgreSQL 18 preview - 窗口查询性能提升40%(100万个窗口时) |
| 2 | + |
| 3 | +### 作者 |
| 4 | +digoal |
| 5 | + |
| 6 | +### 日期 |
| 7 | +2024-09-06 |
| 8 | + |
| 9 | +### 标签 |
| 10 | +PostgreSQL , PolarDB , DuckDB , 窗口查询 , 性能 , 窗口数 |
| 11 | + |
| 12 | +---- |
| 13 | + |
| 14 | +## 背景 |
| 15 | +PostgreSQL 18窗口查询性能提升优化, 100万个窗口时性能提升40%, 窗口越多性能提升越明显. 代码优化参考如下说明, 看起来就是把tuplestore_clear的返回指针回传了, 减少了重复调用. |
| 16 | + |
| 17 | +类似 `update/delete returning` 减少重复查询? |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=908a968612f9ed61911d8ca0a185b262b82f1269 |
| 22 | +``` |
| 23 | +Optimize WindowAgg's use of tuplestores |
| 24 | +author David Rowley <[email protected]> |
| 25 | +Thu, 5 Sep 2024 04:18:30 +0000 (16:18 +1200) |
| 26 | +committer David Rowley <[email protected]> |
| 27 | +Thu, 5 Sep 2024 04:18:30 +0000 (16:18 +1200) |
| 28 | +commit 908a968612f9ed61911d8ca0a185b262b82f1269 |
| 29 | +tree bf1957ccad3aab816fc5b0175c2294c099dc6680 tree |
| 30 | +parent 19b861f880166fbdb67d268955e590881376f876 commit | diff |
| 31 | + |
| 32 | +Optimize WindowAgg's use of tuplestores |
| 33 | + |
| 34 | +When WindowAgg finished one partition of a PARTITION BY, it previously |
| 35 | +would call tuplestore_end() to purge all the stored tuples before again |
| 36 | +calling tuplestore_begin_heap() and carefully setting up all of the |
| 37 | +tuplestore read pointers exactly as required for the given frameOptions. |
| 38 | +Since the frameOptions don't change between partitions, this part does |
| 39 | +not make much sense. For queries that had very few rows per partition, |
| 40 | +the overhead of this was very large. |
| 41 | + |
| 42 | +It seems much better to create the tuplestore and the read pointers once |
| 43 | +and simply call tuplestore_clear() at the end of each partition. |
| 44 | +tuplestore_clear() moves all of the read pointers back to the start |
| 45 | +position and deletes all the previously stored tuples. |
| 46 | + |
| 47 | +A simple test query with 1 million partitions and 1 tuple per partition |
| 48 | +has been shown to run around 40% faster than without this change. The |
| 49 | +additional effort seems to have mostly been spent in malloc/free. |
| 50 | + |
| 51 | +Making this work required adding a new bool field to WindowAggState |
| 52 | +which had the unfortunate effect of being the 9th bool field in a group |
| 53 | +resulting in the struct being enlarged. Here we shuffle the fields |
| 54 | +around a little so that the two bool fields for runcondition relating |
| 55 | +stuff fit into existing padding. Also, move the "runcondition" field to |
| 56 | +be near those. This frees up enough space with the other bool fields so |
| 57 | +that the newly added one fits into the padding bytes. This was done to |
| 58 | +address a very small but apparent performance regression with queries |
| 59 | +containing a large number of rows per partition. |
| 60 | + |
| 61 | +Reviewed-by: Ashutosh Bapat <[email protected]> |
| 62 | +Reviewed-by: Tatsuo Ishii <[email protected]> |
| 63 | +Discussion: https://postgr.es/m/CAHoyFK9n-QCXKTUWT_xxtXninSMEv%2BgbJN66-y6prM3f4WkEHw%40mail.gmail.com |
| 64 | +``` |
| 65 | + |
0 commit comments