- Notifications
You must be signed in to change notification settings - Fork4.9k
Commit8a51027
committed
Further optimize nbtree search scan key comparisons.
Postgres 17 commite0b1ee1 added two complementary optimizations tonbtree: the "prechecked" and "firstmatch" optimizations. _bt_readpagewas made to avoid needlessly evaluating keys that are guaranteed to besatisfied by applying page-level context. "prechecked" did this forkeys required in the current scan direction, while "firstmatch" did itfor keys required in the opposite-to-scan direction only.The "prechecked" design had a number of notable issues. It didn'taccount for the fact that an = array scan key's sk_argument field mightneed to advance at the point of the page precheck (it didn't check theprecheck tuple against the key's array, only the key's sk_argument,which needlessly made it ineffective in cases involving stepping to apage having advanced the scan's arrays using a truncated high key)."prechecked" was also completely ineffective when only one scan keywasn't guaranteed to be satisfied by every tuple (it didn't recognizethat it was still safe to avoid evaluating other, earlier keys).The "firstmatch" optimization had similar limitations. It could only beapplied after _bt_readpage found its first matching tuple, regardless ofwhy any earlier tuples failed to satisfy the scan's index quals. Thisallowed unsatisfied non-required scan keys to impede the optimization.Replace both optimizations with a new optimization, without any of theselimitations: the "startikey" optimization. Affected _bt_readpage callsgenerate a page-level key offset ("startikey"), that their _bt_checkkeyscalls can then start at. This is an offset to the first key that isn'tknown to be satisfied by every tuple on the page.Although this is independently useful work, its main goal is to avoidperformance regressions with index scans that use skip arrays, but stillnever manage to skip over irrelevant leaf pages. We must avoid wastingCPU cycles on overly granular skip array maintenance in these cases.The new "startikey" optimization helps with this by selectivelydisabling array maintenance for the duration of a _bt_readpage call.This has no lasting consequences for the scan's array keys (they'llstill reliably track the scan's progress through the index's key spacewhenever the scan is "between pages").Skip scan adds skip arrays during preprocessing using simple, staticrules, and decides how best to navigate/apply the scan's skip arraysdynamically, at runtime. The "startikey" optimization enables thisapproach. As a result of all this, the planner doesn't need to generatedistinct, competing index paths (one path for skip scan, another for anequivalent traditional full index scan). The overall effect is to makescan runtime close to optimal, even when the planner works off anincorrect cardinality estimate. Scans will also perform well given askipped column with data skew: individual groups of pages with manydistinct values (in respect of a skipped column) can be read about asefficiently as before -- without the scan being forced to give up onskipping over other groups of pages that are provably irrelevant.Many scans that cannot possibly skip will still benefit from the use ofskip arrays, since they'll allow the "startikey" optimization to be aseffective as possible (by allowing preprocessing to mark all the scan'skeys as required). A scan that uses a skip array on "a" for a qual"WHERE a BETWEEN 0 AND 1_000_000 AND b = 42" is often much faster now,even when every tuple read by the scan has its own distinct "a" value.However, there are still some remaining regressions, affecting certaintrickier cases.Scans whose index quals have several range skip arrays, each on somehigh cardinality column, can still be slower than they were before theintroduction of skip scan -- even with the new "startikey" optimization.There are also known regressions affecting very selective index scansthat use a skip array. The underlying issue with such selective scansis that they never get as far as reading a second leaf page, and so willnever get a chance to consider applying the "startikey" optimization.In principle, all regressions could be avoided by teaching preprocessingto not add skip arrays whenever they aren't expected to help, but itseems best to err on the side of robust performance.Follow-up to commit92fe23d, which added nbtree skip scan.Author: Peter Geoghegan <pg@bowt.ie>Reviewed-By: Heikki Linnakangas <heikki.linnakangas@iki.fi>Reviewed-By: Masahiro Ikeda <ikedamsh@oss.nttdata.com>Reviewed-By: Matthias van de Meent <boekewurm+postgres@gmail.com>Discussion:https://postgr.es/m/CAH2-Wz=Y93jf5WjoOsN=xvqpMjRy-bxCE037bVFi-EasrpeUJA@mail.gmail.comDiscussion:https://postgr.es/m/CAH2-WznWDK45JfNPNvDxh6RQy-TaCwULaM5u5ALMXbjLBMcugQ@mail.gmail.com1 parent92fe23d commit8a51027
File tree
5 files changed
+483
-153
lines changed- src
- backend/access/nbtree
- include/access
5 files changed
+483
-153
lines changedLines changed: 1 addition & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1390 | 1390 |
| |
1391 | 1391 |
| |
1392 | 1392 |
| |
| 1393 | + | |
1393 | 1394 |
| |
1394 | 1395 |
| |
1395 | 1396 |
| |
|
Lines changed: 1 addition & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
349 | 349 |
| |
350 | 350 |
| |
351 | 351 |
| |
| 352 | + | |
352 | 353 |
| |
353 | 354 |
| |
354 | 355 |
| |
|
Lines changed: 39 additions & 38 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
1648 | 1648 |
| |
1649 | 1649 |
| |
1650 | 1650 |
| |
| 1651 | + | |
| 1652 | + | |
1651 | 1653 |
| |
1652 | 1654 |
| |
1653 | 1655 |
| |
1654 |
| - | |
1655 |
| - | |
1656 | 1656 |
| |
1657 | 1657 |
| |
1658 | 1658 |
| |
1659 |
| - | |
1660 |
| - | |
1661 |
| - | |
1662 |
| - | |
1663 |
| - | |
1664 |
| - | |
1665 |
| - | |
1666 |
| - | |
1667 |
| - | |
1668 |
| - | |
1669 |
| - | |
1670 |
| - | |
1671 |
| - | |
1672 |
| - | |
1673 |
| - | |
1674 |
| - | |
1675 |
| - | |
1676 |
| - | |
1677 |
| - | |
1678 |
| - | |
1679 |
| - | |
1680 |
| - | |
1681 |
| - | |
1682 |
| - | |
1683 |
| - | |
1684 |
| - | |
1685 |
| - | |
1686 |
| - | |
1687 |
| - | |
1688 |
| - | |
1689 |
| - | |
1690 |
| - | |
1691 |
| - | |
1692 | 1659 |
| |
1693 | 1660 |
| |
1694 | 1661 |
| |
| |||
1716 | 1683 |
| |
1717 | 1684 |
| |
1718 | 1685 |
| |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
| 1692 | + | |
1719 | 1693 |
| |
1720 | 1694 |
| |
1721 | 1695 |
| |
| |||
1752 | 1726 |
| |
1753 | 1727 |
| |
1754 | 1728 |
| |
| 1729 | + | |
1755 | 1730 |
| |
1756 | 1731 |
| |
1757 | 1732 |
| |
| |||
1761 | 1736 |
| |
1762 | 1737 |
| |
1763 | 1738 |
| |
1764 |
| - | |
1765 | 1739 |
| |
1766 | 1740 |
| |
1767 | 1741 |
| |
| |||
1816 | 1790 |
| |
1817 | 1791 |
| |
1818 | 1792 |
| |
1819 |
| - | |
| 1793 | + | |
| 1794 | + | |
1820 | 1795 |
| |
1821 | 1796 |
| |
1822 | 1797 |
| |
| |||
1855 | 1830 |
| |
1856 | 1831 |
| |
1857 | 1832 |
| |
| 1833 | + | |
| 1834 | + | |
| 1835 | + | |
| 1836 | + | |
| 1837 | + | |
| 1838 | + | |
| 1839 | + | |
1858 | 1840 |
| |
1859 | 1841 |
| |
1860 | 1842 |
| |
| |||
1894 | 1876 |
| |
1895 | 1877 |
| |
1896 | 1878 |
| |
| 1879 | + | |
| 1880 | + | |
| 1881 | + | |
| 1882 | + | |
| 1883 | + | |
1897 | 1884 |
| |
1898 | 1885 |
| |
1899 | 1886 |
| |
| |||
1905 | 1892 |
| |
1906 | 1893 |
| |
1907 | 1894 |
| |
| 1895 | + | |
1908 | 1896 |
| |
1909 | 1897 |
| |
1910 | 1898 |
| |
| |||
1914 | 1902 |
| |
1915 | 1903 |
| |
1916 | 1904 |
| |
1917 |
| - | |
1918 | 1905 |
| |
1919 | 1906 |
| |
1920 | 1907 |
| |
| |||
1970 | 1957 |
| |
1971 | 1958 |
| |
1972 | 1959 |
| |
| 1960 | + | |
| 1961 | + | |
| 1962 | + | |
| 1963 | + | |
| 1964 | + | |
| 1965 | + | |
| 1966 | + | |
| 1967 | + | |
| 1968 | + | |
| 1969 | + | |
| 1970 | + | |
| 1971 | + | |
| 1972 | + | |
| 1973 | + | |
1973 | 1974 |
| |
1974 | 1975 |
| |
1975 | 1976 |
| |
|
0 commit comments
Comments
(0)