%3CLINGO-SUB%20id%3D%22lingo-sub-1806462%22%20slang%3D%22en-US%22%3EImproving%20Postgres%20Connection%20Scalability%3A%20Snapshots%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1806462%22%20slang%3D%22en-US%22%3E%3CP%3EI%20recently%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fanalyzing-the-limits-of-connection-scalability-in-postgres%2Fba-p%2F1757266%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Eanalyzed%20the%20limits%20of%20connection%20scalability%3C%2FA%3E%2C%20to%20understand%20the%20most%20effective%20way%20to%20improve%20Postgres'%20handling%20of%20large%20numbers%20of%20connections%2C%20and%20why%20that%20is%20important.%20I%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fanalyzing-the-limits-of-connection-scalability-in-postgres%2Fba-p%2F1757266%23conclusion-start-by-improving-snapshot-scalability-in-postgres%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Econcluded%3C%2FA%3E%20that%20the%20most%20pressing%20issue%20is%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fanalyzing-the-limits-of-connection-scalability-in-postgres%2Fba-p%2F1757266%23snapshot-scalability%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Esnapshot%20scalability%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20post%20details%20the%20improvements%20I%20recently%20contributed%20to%20Postgres%2014%20(to%20be%20released%20Q3%20of%202021)%2C%20significantly%20reducing%20the%20identified%20snapshot%20scalability%20bottleneck.%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pgbench-before-and-after-performance-comparison-Postgres-snapshot-scalability-v2-1920x1080.jpg%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F228383i00393979FBE65C85%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22pgbench-before-and-after-performance-comparison-Postgres-snapshot-scalability-v2-1920x1080.jpg%22%20alt%3D%22pgbench-before-and-after-performance-comparison-Postgres-snapshot-scalability-v2-1920x1080.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EAs%20the%20explanation%20of%20the%20implementation%20details%20is%20fairly%20long%2C%20I%20thought%20it%E2%80%99d%20be%20more%20fun%20for%20of%20you%20if%20I%20start%20with%20the%20results%20of%20the%20work%2C%20instead%20of%20the%20technical%20details%20(I%E2%80%99m%20cheating%2C%20I%20know%20%3B)).%3C%2FP%3E%0A%3CHR%20%2F%3E%0A%3CDIV%20class%3D%22toc%22%3E%3CNAV%20id%3D%22TableOfContents%22%3E%0A%3COL%3E%0A%3CLI%3E%3CA%20href%3D%22%23first-performance-improvements%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EFirst%3A%20Performance%20Improvements%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23unfortunately-a-minimal-transaction-visibility-primer%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EUnfortunately%3A%20A%20Minimal%20Transaction%20Visibility%20Primer%3C%2FA%3E%3COL%3E%0A%3CLI%3E%3CA%20href%3D%22%23multi-version-concurrency-control-mvcc%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMulti%20Version%20Concurrency%20Control%20(MVCC)%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23snapshots%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESnapshots%3F%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23snapping-snapshots%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESnapping%20Snapshots%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23past-optimizations%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPast%20Optimizations%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23finally-addressing-bottlenecks%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EFinally%3A%20Addressing%20Bottlenecks%3C%2FA%3E%3COL%3E%0A%3CLI%3E%3CA%20href%3D%22%23bottleneck-1-ping-pong%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EBottleneck%201%3A%20Ping%20Pong%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23interlude-removing-the-need-for-recentglobalxminhorizon%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EInterlude%3A%20Removing%20the%20need%20for%3CCODE%3ERecentGlobalXminHorizon%3C%2FCODE%3E%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23bottleneck-2-density%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EBottleneck%202%3A%20Density%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23bottleneck-3-caching%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EBottleneck%203%3A%20Caching%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23conclusion-one-bottleneck-down-in-pg-14-others-in-sight%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EConclusion%3A%20One%20bottleneck%20down%20in%20PG%2014%2C%20others%20in%20sight%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23from-easy-to-hard-opportunities-for-further-improvements%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EFrom%20easy%20to%20hard%3A%20Opportunities%20for%20further%20improvements%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FNAV%3E%3C%2FDIV%3E%0A%3CHR%20%2F%3E%0A%3CH2%20id%3D%22first-performance-improvements%22%20id%3D%22toc-hId--1215879544%22%20id%3D%22toc-hId--1215879548%22%3E%3CBR%20%2F%3EFirst%3A%20Performance%20Improvements%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20all%20of%20these%20benchmarks%2C%20I%20compared%20the%20Postgres%20development%20tree%20just%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dcommit%3Bh%3D1f42d35a1d6144a23602b2c0bc7f97f3046cf890%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ebefore%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dcommit%3Bh%3D07f32fcd23ac81898ed47f88beb569c631a2f223%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eafter%3C%2FA%3E%20all%20the%20connection%20scalability%20changes%20have%20been%20merged.%20There%20are%20a%20few%20other%20changes%20interspersed%2C%20but%20none%20that%20are%20likely%20to%20affect%20performance%20in%20a%20significant%20way.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%2C%20a%20before%20%2F%20after%20comparison%20of%20a%20read-only%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fcurrent%2Fpgbench.htm%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Epgbench%3C%2FA%3E%20benchmark%2C%20on%20an%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fvirtual-machines%2Ffsv2-series%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20F72s_v2%3C%2FA%3E%20VM%3A%3C%2FP%3E%0A%3CFIGURE%3E%0A%3CDIV%20id%3D%22tinyMceEditorandresfreund_0%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pgbench-read-only-log-scale-prepost.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F228387i9DE217F911A02BA7%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22pgbench-read-only-log-scale-prepost.png%22%20alt%3D%22Figure%201%3A%20Benchmark%20(read-only%20pgbench)%20results%20comparison%2C%20showing%20the%20effects%20of%20the%20snapshot%20scalability%20improvements.%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFigure%201%3A%20Benchmark%20(read-only%20pgbench)%20results%20comparison%2C%20showing%20the%20effects%20of%20the%20snapshot%20scalability%20improvements.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CFIGCAPTION%3E%0A%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EThese%20results%20%3C%2FSPAN%3E%3CSUP%20id%3D%22fnref%3A1%22%20style%3D%22font-family%3A%20inherit%3B%22%3E%3CA%20class%3D%22footnote-ref%22%20role%3D%22doc-noteref%22%20href%3D%22%23fn%3A1%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E1%3C%2FA%3E%3C%2FSUP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E%20show%20a%20significant%20improvement%20after%20the%20snapshot%20scalability%20changes%20I%E2%80%99ll%20be%20talking%20about%20in%20this%20post%2C%20with%20little%20evidence%20of%20scalability%20issues%20even%20at%20very%20high%20connection%20counts.%20The%20dip%20starting%20around%20100%20connections%E2%80%94for%20both%20the%20pre%2Fpost%20changes%20runs%E2%80%94%20appears%20to%20be%20caused%20by%20OS%20task%20scheduling%2C%20rather%20than%20Postgres%20directly.%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FFIGCAPTION%3E%0A%3C%2FFIGURE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%20up%2C%20a%20repeat%20of%20the%20benchmarks%20I%20used%20in%20my%20last%20post%20on%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fanalyzing-the-limits-of-connection-scalability-in-postgres%2Fba-p%2F1757266%23snapshot-scalability%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Eanalyzing%20connection%20scalability%3C%2FA%3E%20to%20identify%20snapshot%20scalability%20as%20the%20primary%20bottleneck%20(again%20executed%20on%20my%20workstation%3CSUP%20id%3D%22fnref%3A2%22%3E%3CA%20class%3D%22footnote-ref%22%20role%3D%22doc-noteref%22%20href%3D%22%23fn%3A2%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E2%3C%2FA%3E%3C%2FSUP%3E).%3C%2FP%3E%0A%3CFIGURE%3E%0A%3CDIV%20id%3D%22tinyMceEditorandresfreund_1%22%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22performance-impact-of-idle-connections-1active-prepost.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F228385iFCCFB2066ACBB491%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22performance-impact-of-idle-connections-1active-prepost.png%22%20alt%3D%22Figure%202%3A%20Benchmark%20result%20comparison%20(1%20active%20connection%20running%20read-only%20pgbench%2C%20in%20presence%20of%20a%20variable%20number%20of%20idle%20connections)%2C%20showing%20the%20effects%20of%20the%20snapshot%20scalability%20improvements.%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFigure%202%3A%20Benchmark%20result%20comparison%20(1%20active%20connection%20running%20read-only%20pgbench%2C%20in%20presence%20of%20a%20variable%20number%20of%20idle%20connections)%2C%20showing%20the%20effects%20of%20the%20snapshot%20scalability%20improvements.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CFIGCAPTION%3E%3C%2FFIGCAPTION%3E%0A%3C%2FFIGURE%3E%0A%3CFIGURE%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22performance-impact-of-idle-connections-48active-prepost.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F228386iFC9CDAE22E9B903B%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22performance-impact-of-idle-connections-48active-prepost.png%22%20alt%3D%22Figure%203%3A%20Benchmark%20result%20comparison%20(48%20active%20connections%20running%20read-only%20pgbench%2C%20in%20presence%20of%20a%20variable%20number%20of%20idle%20connections)%2C%20showing%20the%20effects%20of%20the%20snapshot%20scalability%20improvements.%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EFigure%203%3A%20Benchmark%20result%20comparison%20(48%20active%20connections%20running%20read-only%20pgbench%2C%20in%20presence%20of%20a%20variable%20number%20of%20idle%20connections)%2C%20showing%20the%20effects%20of%20the%20snapshot%20scalability%20improvements.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CFIGCAPTION%3E%0A%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EThese%20results%20(%3C%2FSPAN%3E%3CSUP%20id%3D%22fnref%3A3%22%20style%3D%22font-family%3A%20inherit%3B%22%3E%3CA%20class%3D%22footnote-ref%22%20role%3D%22doc-noteref%22%20href%3D%22%23fn%3A3%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E3%3C%2FA%3E%3C%2FSUP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E%2C%20%3C%2FSPAN%3E%3CSUP%20id%3D%22fnref%3A4%22%20style%3D%22font-family%3A%20inherit%3B%22%3E%3CA%20class%3D%22footnote-ref%22%20role%3D%22doc-noteref%22%20href%3D%22%23fn%3A4%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E4%3C%2FA%3E%3C%2FSUP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E)%20show%20the%20extreme%20difference%20in%20scalability%20between%20the%20%3C%2FSPAN%3E%3CEM%20style%3D%22font-family%3A%20inherit%3B%22%3Efixed%3C%2FEM%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E%20and%20%3C%2FSPAN%3E%3CEM%20style%3D%22font-family%3A%20inherit%3B%22%3Eunfixed%3C%2FEM%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E%20version%20of%20Postgres.%20More%20so%20than%20the%20results%20above%2C%20as%20the%20benchmark%20is%20chosen%20to%20highlight%20the%20snapshot%20scalability%20issue.%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FFIGCAPTION%3E%0A%3C%2FFIGURE%3E%0A%3CH2%20id%3D%22unfortunately-a-minimal-transaction-visibility-primer%22%20id%3D%22toc-hId-1271633289%22%20id%3D%22toc-hId-1271633285%22%3E%3CBR%20%2F%3EUnfortunately%3A%20A%20Minimal%20Transaction%20Visibility%20Primer%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20seen%20in%20the%20before%20%26amp%3B%20after%20charts%20above%2C%20the%20performance%20%2F%20scalability%20effects%20of%20the%20changes%20are%20substantial.%20To%2C%20hopefully%2C%20make%20it%20a%20bit%20easier%20to%20follow%20along%2C%20the%20next%20section%20is%20an%20attempt%20in%20providing%20some%20of%20the%20necessary%20background.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPostgres%20implements%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fdevel%2Fmvcc-intro.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Etransaction%20isolation%3C%2FA%3E%2C%20i.e.%20the%20visibility%20changes%20made%20concurrent%20with%20a%20transactions%2C%20using%20snapshot%20based%20%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FMultiversion_concurrency_control%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMulti%20Version%20Concurrency%20Control%3C%2FA%3E%20(MVCC).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBrandur%20has%20a%20good%20post%20on%20%3CA%20href%3D%22https%3A%2F%2Fbrandur.org%2Fpostgres-atomicity%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehow%20Postgres%20makes%20transactions%20atomic%3C%2FA%3E%20explaining%20how%20this%20works%20in%20more%20detail.%20A%20lot%20of%20low-level%20details%20of%20how%20this%20works%20in%20Postgres%20are%20explained%20in%20the%20relevant%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dblob%3Bf%3Dsrc%2Fbackend%2Faccess%2Ftransam%2FREADME%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EREADME%3C%2FA%3E%20inside%20the%20Postgres%20source%20code.%3C%2FP%3E%0A%3CH3%20id%3D%22multi-version-concurrency-control-mvcc%22%20id%3D%22toc-hId-1962194763%22%20id%3D%22toc-hId-1962194759%22%3E%3CBR%20%2F%3EMulti%20Version%20Concurrency%20Control%20(MVCC)%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20brief%2C%20both%20%3CEM%3EMVCC%3C%2FEM%3E%20and%20%3CEM%3Esnapshots%3C%2FEM%3E%20are%20some%20of%20the%20building%20blocks%20used%20to%20implement%20part%20of%20concurrency%20control%20in%20Postgres.%20%3CEM%3EMVCC%3C%2FEM%3E%20boils%20down%20to%20having%20the%20ability%20to%20have%20multiple%20row%20versions%20for%20the%20same%20logical%20row%2C%20with%20different%20versions%20visible%20to%20different%20transactions%2C%20increasing%20concurrency.%26nbsp%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EE.g.%20imagine%20one%20query%20starting%20to%20scan%20a%20large%20table%2C%20and%20subsequently%20another%20query%20updating%20a%20row%20in%20that%20table.%20%3C%2FSPAN%3E%3CEM%20style%3D%22font-family%3A%20inherit%3B%22%3EMVCC%3C%2FEM%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E%20allows%20the%20update%20to%20proceed%20without%20affecting%20the%20query%20results%20by%20the%20table%20scan%2C%20by%20keeping%20the%20original%20row%20version%20available%20for%20the%20scan%2C%20and%20making%20a%20new%20row%20version%20with%20the%20updated%20contents.%20That%20obviously%20is%20good%20for%20concurrency.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EConceptually%20this%20works%20by%20each%20row%20version%20having%20a%20%E2%80%9Cvisible%20since%E2%80%9D%20(xmin%20in%20Postgres)%20and%20a%20%E2%80%9Cvisible%20until%E2%80%9D%20(xmax%20in%20Postgres)%20%E2%80%9Ctimestamp%E2%80%9D%20(not%20really%20a%20timestamp%20in%20Postgres%2C%20but%20rather%20a%20transaction%20identifier).%20That%20way%20a%20scan%20can%20ignore%20modifications%20that%20have%20been%20made%20after%20the%20scan%20started%2C%20by%20(a)%20considering%20row%20versions%20that%20have%20since%20been%20deleted%20to%20be%20visible%20and%20(b)%20considering%20row%20versions%20created%20after%20the%20scan%20started%20to%20be%20invisible.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CH3%20id%3D%22snapshots%22%20id%3D%22toc-hId-154740300%22%20id%3D%22toc-hId-154740296%22%3ESnapshots%3F%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EJust%20having%20two%20such%20%E2%80%9Ctimestamps%E2%80%9D%20associated%20with%20each%20row%20version%20is%20not%20enough%2C%20however.%20Isolation%20rules%20consider%20which%20effects%20by%20other%20transactions%20should%20be%20visible%20to%20a%20transaction%20not%20by%20the%20time%20the%20other%20transactions%20started%2C%20but%20by%20the%20time%20the%20other%20transaction%20commits.%20Therefore%2C%20fundamentally%2C%20a%20timestamp%20like%20%E2%80%9Cvisible%20since%E2%80%9D%20and%20%E2%80%9Cvisible%20until%E2%80%9D%20attached%20to%20row%20versions%20at%20the%20time%20of%20modification%20cannot%20alone%20be%20sufficient%3A%20The%20order%20in%20which%20transaction%20commit%20is%20not%20yet%20known%3CSUP%20id%3D%22fnref%3A5%22%3E%3CA%20class%3D%22footnote-ref%22%20role%3D%22doc-noteref%22%20href%3D%22%23fn%3A5%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E5%3C%2FA%3E%3C%2FSUP%3E.%20That%20is%20where%20%3CEM%3Esnapshots%3C%2FEM%3E%20come%20into%20play.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPostgres%20uses%20snapshots%20to%20identify%20which%20which%20transactions%20were%20running%20at%20the%20time%20of%20snapshot%E2%80%99s%20creation.%20That%20allows%20statements%20(e.g.%20in%3CCODE%3EREAD%20COMMITTED%3C%2FCODE%3Emode)%20or%20entire%20transactions%20(e.g.%20in%3CCODE%3EREPEATABLE%20READ%3C%2FCODE%3Emode)%20to%20decide%20which%20rows%20created%20by%20other%20transactions%20should%20be%20visible%2C%20and%20which%20not.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%22%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-C%22%20data-lang%3D%22C%22%3E%3CSPAN%3Etypedef%3C%2FSPAN%3E%20%3CSPAN%3Estruct%3C%2FSPAN%3E%20SnapshotData%0A%7B%0A%3CSPAN%3E%E2%80%A6%3C%2FSPAN%3E%0A%20TransactionId%20xmin%3B%20%20%20%3CSPAN%3E%2F*%20all%20XID%20%26lt%3B%20xmin%20are%20visible%20to%20me%20*%2F%3C%2FSPAN%3E%0A%20TransactionId%20xmax%3B%20%20%20%3CSPAN%3E%2F*%20all%20XID%20%26gt%3B%3D%20xmax%20are%20invisible%20to%20me%20*%2F%3C%2FSPAN%3E%0A%3CSPAN%3E%E2%80%A6%3C%2FSPAN%3E%0A%20%3CSPAN%3E%2F*%0A%3C%2FSPAN%3E%3CSPAN%3E%20%20*%20For%20normal%20MVCC%20snapshot%20this%20contains%20the%20all%20xact%20IDs%20that%20are%20in%0A%3C%2FSPAN%3E%3CSPAN%3E%20%20*%20progress%2C%20unless%20the%20snapshot%20was%20taken%20during%20recovery%20in%20which%20case%0A%3C%2FSPAN%3E%3CSPAN%3E%20%20*%20it's%20empty.%20%E2%80%A6%0A%3C%2FSPAN%3E%3CSPAN%3E%20%20*%20note%3A%20all%20ids%20in%20xip%5B%5D%20satisfy%20xmin%20%26lt%3B%3D%20xip%5Bi%5D%20%26lt%3B%20xmax%0A%3C%2FSPAN%3E%3CSPAN%3E%20%20*%2F%3C%2FSPAN%3E%0A%20TransactionId%20%3CSPAN%3E*%3C%2FSPAN%3Exip%3B%0A%20uint32%20%20xcnt%3B%20%20%20%3CSPAN%3E%2F*%20%23%20of%20xact%20ids%20in%20xip%5B%5D%20*%2F%3C%2FSPAN%3E%0A%3CSPAN%3E%E2%80%A6%3C%2FSPAN%3E%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CP%3EThe%3CCODE%3Exip%3C%2FCODE%3Earray%20contains%20all%20the%20transaction%20IDs%20(which%20Postgres%20uses%20instead%20of%20plain%20timestamps)%20that%20were%20running%20at%20the%20time%20the%20snapshot%20was%20taken.%20When%20encountering%20a%20row%20version%20with%20a%20certain%20xmin%2C%20it%20will%20be%20invisible%20if%20that%20transaction%20was%20still%20running%20when%20the%20snapshot%20was%20taken%20and%20conversely%20may%20be%20visible%20if%20xmin%20is%20a%20transaction%20that%20already%20had%20finished%20at%20that%20time.%20And%20conversely%2C%20a%20row%20version%20with%20an%20xmax%20is%20still%20visible%20if%20the%20associated%20transaction%20that%20was%20running%20at%20the%20time%20of%20the%20snapshot%20was%20taken%2C%20invisible%20otherwise.%3C%2FP%3E%0A%3CH3%20id%3D%22snapping-snapshots%22%20id%3D%22toc-hId--1652714163%22%20id%3D%22toc-hId--1652714167%22%3E%3CBR%20%2F%3ESnapping%20Snapshots%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20understand%20the%20performance%20problems%20and%20the%20improvements%20it%20is%20necessary%20to%20understand%20how%20snapshots%20were%20built%20before.%20The%20core%20routine%20for%20this%20is%3CCODE%3EGetSnapshotData()%3C%2FCODE%3E%2C%20which%20unsurprisingly%20is%20the%20function%20we%20saw%20high%20up%20in%20profiles%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fanalyzing-the-limits-of-connection-scalability-in-postgres%2Fba-p%2F1757266%23cause%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Eearlier%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEvery%20connection%20to%20Postgres%20has%20an%20associated%3CCODE%3Estruct%20PGPROC%3C%2FCODE%3Eand%2C%20until%20now%2C%20a%3CCODE%3Estruct%20PGXACT%3C%2FCODE%3Eentry.%20These%20structs%20are%20pre-allocated%20at%20server%20based%20on%3CCODE%3Emax_connections%3C%2FCODE%3E(and%3CCODE%3Emax_prepared_xacts%3C%2FCODE%3E%2C%3CCODE%3Emax_autovacuum_workers%3C%2FCODE%3E%2C%20%E2%80%A6).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%22%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-C%22%20data-lang%3D%22C%22%3E%3CSPAN%3Etypedef%3C%2FSPAN%3E%20%3CSPAN%3Estruct%3C%2FSPAN%3E%20ProcArrayStruct%0A%7B%0A%20%3CSPAN%3Eint%3C%2FSPAN%3E%20%20%20numProcs%3B%20%20%3CSPAN%3E%2F*%20number%20of%20valid%20procs%20entries%20*%2F%3C%2FSPAN%3E%0A%3CSPAN%3E%E2%80%A6%3C%2FSPAN%3E%0A%20%3CSPAN%3E%2F*%20indexes%20into%20allPgXact%5B%5D%2C%20has%20PROCARRAY_MAXPROCS%20entries%20*%2F%3C%2FSPAN%3E%0A%20%3CSPAN%3Eint%3C%2FSPAN%3E%20%20%20pgprocnos%5BFLEXIBLE_ARRAY_MEMBER%5D%3B%0A%3CSPAN%3E%E2%80%A6%3C%2FSPAN%3E%0A%7D%20ProcArrayStruct%3B%0A%0A%0A%3CSPAN%3Estruct%3C%2FSPAN%3E%20PGPROC%0A%7B%0A%3CSPAN%3E%E2%80%A6%3C%2FSPAN%3E%0A%7D%0A%3CSPAN%3E%E2%80%A6%3C%2FSPAN%3E%0A%0A%3CSPAN%3E%2F*%0A%3C%2FSPAN%3E%3CSPAN%3E%20*%20Prior%20to%20PostgreSQL%209.2%2C%20the%20fields%20below%20were%20stored%20as%20part%20of%20the%0A%3C%2FSPAN%3E%3CSPAN%3E%20*%20PGPROC.%20%20However%2C%20benchmarking%20revealed%20that%20packing%20these%20particular%0A%3C%2FSPAN%3E%3CSPAN%3E%20*%20members%20into%20a%20separate%20array%20as%20tightly%20as%20possible%20sped%20up%20GetSnapshotData%0A%3C%2FSPAN%3E%3CSPAN%3E%20*%20considerably%20on%20systems%20with%20many%20CPU%20cores%2C%20by%20reducing%20the%20number%20of%0A%3C%2FSPAN%3E%3CSPAN%3E%20*%20cache%20lines%20needing%20to%20be%20fetched.%20%20Thus%2C%20think%20very%20carefully%20before%20adding%0A%3C%2FSPAN%3E%3CSPAN%3E%20*%20anything%20else%20here.%0A%3C%2FSPAN%3E%3CSPAN%3E%20*%2F%3C%2FSPAN%3E%0A%3CSPAN%3Etypedef%3C%2FSPAN%3E%20%3CSPAN%3Estruct%3C%2FSPAN%3E%20PGXACT%0A%7B%0A%20TransactionId%20xid%3B%20%20%20%3CSPAN%3E%2F*%20id%20of%20top-level%20transaction%20currently%20being%0A%3C%2FSPAN%3E%3CSPAN%3E%20%20%20%20%20%20%20%20%20*%20executed%20by%20this%20proc%2C%20if%20running%20and%20XID%0A%3C%2FSPAN%3E%3CSPAN%3E%20%20%20%20%20%20%20%20%20*%20is%20assigned%3B%20else%20InvalidTransactionId%20*%2F%3C%2FSPAN%3E%0A%0A%20TransactionId%20xmin%3B%20%20%20%3CSPAN%3E%2F*%20minimal%20running%20XID%20as%20it%20was%20when%20we%20were%0A%3C%2FSPAN%3E%3CSPAN%3E%20%20%20%20%20%20%20%20%20*%20starting%20our%20xact%2C%20excluding%20LAZY%20VACUUM%3A%0A%3C%2FSPAN%3E%3CSPAN%3E%20%20%20%20%20%20%20%20%20*%20vacuum%20must%20not%20remove%20tuples%20deleted%20by%0A%3C%2FSPAN%3E%3CSPAN%3E%20%20%20%20%20%20%20%20%20*%20xid%20%26gt%3B%3D%20xmin%20!%20*%2F%3C%2FSPAN%3E%0A%0A%20uint8%20%20vacuumFlags%3B%20%3CSPAN%3E%2F*%20vacuum-related%20flags%2C%20see%20above%20*%2F%3C%2FSPAN%3E%0A%20%3CSPAN%3Ebool%3C%2FSPAN%3E%20%20overflowed%3B%0A%0A%20uint8%20%20nxids%3B%0A%7D%20PGXACT%3B%0A%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20avoid%20needing%20to%20grovel%20through%20all%20PGPROC%20%2F%20PGXACT%20entries%3CCODE%3EProcArrayStruct-%26gt%3Bpgprocnos%3C%2FCODE%3Eis%20a%20sorted%20array%20of%20the%3CCODE%3E-%26gt%3BmaxProc%3C%2FCODE%3Eestablished%20connections.%20Each%20array%20entry%20is%20the%20index%20into%3CCODE%3EPGPROC%3C%2FCODE%3E%2F%3CCODE%3EPGXACT%3C%2FCODE%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20build%20a%20snapshot%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dblob%3Bf%3Dsrc%2Fbackend%2Fstorage%2Fipc%2Fprocarray.c%3Bhb%3D5cbfce562f7cd2aab0cdc4694ce298ec3567930e%23l1468%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EGetSnapshotData()%3C%2FA%3E%20iterates%20over%20all%3CCODE%3EmaxProc%3C%2FCODE%3Eentries%20in%3CCODE%3Epgprocnos%3C%2FCODE%3E%2C%20collecting%3CCODE%3EPGXACT-%26gt%3Bxid%3C%2FCODE%3Efor%20all%20connections%20with%20an%20assigned%20transaction%20ID.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20are%20a%20few%20aspects%20making%20this%20slightly%20more%20complicated%20than%20the%20simple%20loop%20I%20described%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3EBecause%20it%20was%2C%20at%20some%20point%2C%20convenient%2C%3CCODE%3EGetSnapshotData()%3C%2FCODE%3Ealso%20computes%20the%20globally%20oldest%3CCODE%3EPGXACT-%26gt%3Bxmin%3C%2FCODE%3E.%20That%20is%2C%20most%20importantly%2C%20used%20to%20remove%20dead%20tuples%20on%20access.%3C%2FLI%3E%0A%3CLI%3ETo%20implement%3CCODE%3ESAVEPOINT%3C%2FCODE%3E%2C%20a%20backend%20can%20have%20multiple%20assigned%20transaction%20IDs.%20A%20certain%20number%20of%20these%20are%20stored%20as%20part%20of%3CCODE%3EPGPROC%3C%2FCODE%3E.%3C%2FLI%3E%0A%3CLI%3ESome%20backends%2C%20e.g.%20ones%20executing%3CCODE%3EVACUUM%3C%2FCODE%3E%2C%20are%20ignored%20when%20building%20a%20snapshot%2C%20for%20efficiency%20purposes.%3C%2FLI%3E%0A%3CLI%3EOn%20a%20replica%2C%20the%20snapshot%20computation%20works%20quite%20differently.%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CH3%20id%3D%22past-optimizations%22%20id%3D%22toc-hId-834798670%22%20id%3D%22toc-hId-834798666%22%3E%3CBR%20%2F%3EPast%20Optimizations%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%202011%3CCODE%3EGetSnapshotData()%3C%2FCODE%3Ewas%20seen%20as%20a%20bottleneck.%20At%20that%20point%20all%20the%20relevant%20data%20to%20build%20a%20snapshot%20was%20stored%20in%3CCODE%3EPGPROC%3C%2FCODE%3E.%20That%20caused%20performance%20problems%2C%20primarily%20because%20multiple%20cache-lines%20were%20accessed%20for%20each%20established%20connection.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20was%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dcommit%3Bh%3Ded0b409d223%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eimproved%3C%2FA%3E%20by%20splitting%20out%20the%20most%20important%20fields%20into%20a%20new%20data-structure%3CCODE%3EPGXACT%3C%2FCODE%3E.%20That%20significantly%20decreases%20the%20total%20number%20of%20cache-lines%20that%20need%20to%20be%20accessed%20to%20build%20a%20snapshot.%20Additionally%20the%20order%20of%20accesses%20to%3CCODE%3EPGXACT%3C%2FCODE%3Ewas%20improved%20to%20be%20in%20increasing%20memory%20order%20(previously%20it%20was%20determined%20by%20the%20order%20in%20which%20connections%20are%20established%20and%20disconnect).%3C%2FP%3E%0A%3CH2%20id%3D%22finally-addressing-bottlenecks%22%20id%3D%22toc-hId-824295566%22%20id%3D%22toc-hId-824295562%22%3E%3CBR%20%2F%3EFinally%3A%20Addressing%20Bottlenecks%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIt%E2%80%99s%20not%20too%20hard%20to%20see%20that%20the%20approach%20described%20above%2C%20i.e.%20iterating%20over%20an%20array%20containing%20all%20established%20connections%2C%20has%20a%20%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FTime_complexity%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ecomplexity%3C%2FA%3E%20of%3CCODE%3EO(%23connections)%3C%2FCODE%3E%2C%20i.e.%20the%20snapshot%20computation%20cost%20increases%20linearly%20with%20the%20number%20of%20connections.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20are%20two%20fundamental%20approaches%20to%20improving%20scalability%20here%3A%20First%2C%20finding%20an%20algorithm%20that%20improves%20the%20complexity%2C%20so%20that%20each%20additional%20connection%20does%20%3CEM%3Enot%3C%2FEM%3E%20increase%20the%20snapshot%20computation%20costs%20linearly.%20Second%2C%20perform%20less%20work%20for%20each%20connection%2C%20hopefully%20reducing%20the%20total%20time%20taken%20so%20much%20that%20even%20at%20high%20connection%20counts%20the%20total%20time%20is%20still%20small%20enough%20to%20not%20matter%20much%20(i.e.%20reduce%20the%20constant%20factor).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20approach%20to%20improve%20the%20algorithmic%20complexity%20of%3CCODE%3EGetSnapshotData()%3C%2FCODE%3Ethat%20has%20been%20worked%20on%20in%20the%20Postgres%20community%20for%20quite%20a%20few%20years%20are%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fmessage-id%2FCA%252BCSw_tEpJ%253Dmd1zgxPkjH6CWDnTDft4gBi%253D%252BP9SnoC%252BWy3pKdA%2540mail.gmail.com%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ecommit%20sequence%20number%3C%2FA%3E%20based%20snapshots%20(also%20called%20CSN%20based%20snapshots).%20Unfortunately%20implementing%20CSN%20snapshots%20has%20proven%20to%20be%20a%20very%20large%20project%2C%20with%20many%20open%20non-trivial%20problems%20that%20need%20to%20be%20solved.%20As%20I%20was%20looking%20for%20improvements%20that%20could%20be%20completed%20in%20a%20shorter%20time%20frame%2C%20I%20discarded%20pursuing%20that%20approach%2C%20and%20other%20similarly%20fundamental%20changes.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBack%20in%202015%2C%20I%20had%20previously%20tried%20to%20attack%20this%20problem%20by%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fmessage-id%2F20150202152706.GD9201%2540alap3.anarazel.de%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ecaching%20snapshots%3C%2FA%3E%2C%20but%20that%20turned%20out%20to%20not%20be%20easy%20either%20(at%20least%20not%20yet%E2%80%A6).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETherefore%20I%20chose%20to%20first%20focus%20on%20improving%20the%20cost%20each%20additional%20connection%20adds.%20Iterating%20over%20an%20array%20of%20a%20few%20thousand%20elements%20and%20dereferencing%20fairly%20small%20content%20obviously%20is%20not%20free%2C%20but%20compared%20to%20the%20other%20work%20done%20as%20part%20of%20query%20processing%2C%20it%20should%20not%20be%20quite%20as%20prominent%20as%20in%20the%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fanalyzing-the-limits-of-connection-scalability-in-postgres%2Fba-p%2F1757266%23cause%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3ECPU%20profile%3C%2FA%3E%20from%20the%20previous%20post%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22profile_1active_with_5000_idle.png%22%20style%3D%22width%3A%20707px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F228392i1943EFD21C8FEC3D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22profile_1active_with_5000_idle.png%22%20alt%3D%22Profile%20of%20one%20active%20connection%20running%20read-only%20pgbench%20concurrently%20with%205000%20idle%20connections%2C%20bottleneck%20is%20clearly%20in%20%60GetSnapshotData()%60%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EProfile%20of%20one%20active%20connection%20running%20read-only%20pgbench%20concurrently%20with%205000%20idle%20connections%2C%20bottleneck%20is%20clearly%20in%20%60GetSnapshotData()%60%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20core%20snapshot%20computation%20boils%20down%20to%2C%20in%20pseudo%20code%2C%20the%20following%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%22%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-C%22%20data-lang%3D%22C%22%3Exmin%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20global_xmin%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20inferred_maximum_possible%3B%0A%3CSPAN%3Efor%3C%2FSPAN%3E%20(i%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20%3CSPAN%3E0%3C%2FSPAN%3E%3B%20i%20%3CSPAN%3E%26lt%3B%3C%2FSPAN%3E%20%3CSPAN%3E%23%3C%2FSPAN%3Econnections%3B%20i%3CSPAN%3E%2B%2B%3C%2FSPAN%3E)%0A%7B%0A%20%20%20%20%3CSPAN%3Eint%3C%2FSPAN%3E%20procno%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20shared_memory%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Econnection_offsets%5Bi%5D%3B%0A%20%20%20%20PGXACT%20%3CSPAN%3E*%3C%2FSPAN%3Epgxact%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20shared_memory%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Eall_connections%5Bprocno%5D%3B%0A%0A%20%20%20%20%3CSPAN%3E%2F%2F%20compute%20global%20xmin%20minimum%0A%3C%2FSPAN%3E%20%3CSPAN%3Eif%3C%2FSPAN%3E%20(pgxact%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Exmin%20%3CSPAN%3E%26amp%3B%26amp%3B%3C%2FSPAN%3E%20pgxact%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Exmin%20%3CSPAN%3E%26lt%3B%3C%2FSPAN%3E%20global_xmin)%0A%20%20%20%20%20global_xmin%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20pgxact%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Exmin%3B%0A%0A%20%3CSPAN%3E%2F%2F%20nothing%20to%20do%20if%20backend%20has%20transaction%20id%20assigned%0A%3C%2FSPAN%3E%20%3CSPAN%3Eif%3C%2FSPAN%3E%20(%3CSPAN%3E!%3C%2FSPAN%3Epgxact%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Exid)%0A%20%20%20%20%20%3CSPAN%3Econtinue%3C%2FSPAN%3E%3B%0A%0A%20%3CSPAN%3E%2F%2F%20the%20global%20xmin%20minimum%20also%20needs%20to%20include%20assigned%20transaction%20ids%0A%3C%2FSPAN%3E%20%3CSPAN%3Eif%3C%2FSPAN%3E%20(pxact%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Exid%20%3CSPAN%3E%26lt%3B%3C%2FSPAN%3E%20global_xmin)%0A%20%20global_xmin%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20pgxact%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Exid%3B%0A%0A%20%3CSPAN%3E%2F%2F%20add%20the%20xid%20to%20the%20snapshot%0A%3C%2FSPAN%3E%20snapshot%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Exip%5Bsnapshot%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Excnt%3CSPAN%3E%2B%2B%3C%2FSPAN%3E%5D%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20pgxact%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Exid%3B%0A%0A%20%3CSPAN%3E%2F%2F%20compute%20minimum%20xid%20in%20snapshot%0A%3C%2FSPAN%3E%20%3CSPAN%3Eif%3C%2FSPAN%3E%20(pgxact%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Exid%20%3CSPAN%3E%26lt%3B%3C%2FSPAN%3E%20xmin)%0A%20%20xmin%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20pgxact%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Exid%3B%0A%0A%7D%0A%0Asnapshot%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Exmin%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20xmin%3B%0A%3CSPAN%3E%2F%2F%20store%20snapshot%20xmin%20unless%20we%20already%20have%20built%20other%20snapshots%0A%3C%2FSPAN%3E%3CSPAN%3Eif%3C%2FSPAN%3E%20(%3CSPAN%3E!%3C%2FSPAN%3EMyPgXact%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Exmin)%0A%20MyPgXact%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Exmin%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20xmin%3B%0ARecentGlobalXminHorizon%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20global_xmin%3B%0A%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20important%20observation%20about%20this%20is%20that%20the%20main%20loop%20does%20not%20just%20compute%20the%20snapshot%20contents%2C%20but%20also%20the%20%E2%80%9Cglobal%20xmin%20horizon%E2%80%9D.%20Which%20is%20not%20actually%20part%20of%20the%20snapshot%2C%20but%20can%20conveniently%20be%20computed%20at%20the%20same%20time%2C%20for%20a%20small%20amount%20of%20added%20cost.%20Or%20so%20we%20thought%E2%80%A6%3C%2FP%3E%0A%3CP%3EI%20spent%20a%20lot%20of%20time%2C%20on%20and%20off%2C%20trying%20to%20understand%20why%20iterating%20over%20a%20few%20thousand%20elements%20of%20an%20array%2C%20even%20taking%20the%20indirection%20into%20account%2C%20turns%20out%20to%20be%20so%20costly%20in%20%3CEM%3Esome%3C%2FEM%3E%20workloads.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22toc-hId-1514857040%22%20id%3D%22toc-hId-1514857036%22%3EBottleneck%201%3A%20Ping%20Pong%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20main%20problem%20turns%20out%20to%20be%3CCODE%3EMyPgXact-%26gt%3Bxmin%20%3D%20xmin%3B%3C%2FCODE%3E.%20A%20connection%E2%80%99s%20xmin%20is%20is%20set%20whenever%20a%20snapshot%20is%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dblob%3Bf%3Dsrc%2Fbackend%2Fstorage%2Fipc%2Fprocarray.c%3Bh%3D18a0f62ba67ca931babc60a3fe9a0db626e58045%3Bhb%3DREL_12_STABLE%23l1710%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ecomputed%3C%2FA%3E%20(unless%20another%20snapshot%20already%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dblob%3Bf%3Dsrc%2Fbackend%2Futils%2Ftime%2Fsnapmgr.c%3Bh%3Dd07ca1b0b242f289db940a334cb8e4f22b4d5d74%3Bhb%3DREL_12_STABLE%23l6%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eexists%3C%2FA%3E)%2C%20when%20a%20transaction%20is%20committed%20%2F%20aborted%20(%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dblob%3Bf%3Dsrc%2Fbackend%2Fstorage%2Fipc%2Fprocarray.c%3Bh%3D18a0f62ba67ca931babc60a3fe9a0db626e58045%3Bhb%3DREL_12_STABLE%23l434%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E1%3C%2FA%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dblob%3Bf%3Dsrc%2Fbackend%2Fstorage%2Fipc%2Fprocarray.c%3Bh%3D18a0f62ba67ca931babc60a3fe9a0db626e58045%3Bhb%3DREL_12_STABLE%23l456%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E2%3C%2FA%3E%20).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOn%20the%20currently%20most%20common%20multi-core%20CPU%20micro-architectures%20each%20CPU%20core%20has%20its%20own%20private%20L1%20and%20L2%20caches%20and%20all%20cores%20within%20a%20CPU%20socket%20share%20an%20L3%20cache.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EActive%20backends%20constantly%20update%3CCODE%3EMyPgXact-%26gt%3Bxmin%3C%2FCODE%3E.%20Simplifying%20a%20bit%2C%20that%20in%20turn%20requires%20that%20the%20data%20is%20in%20a%20core-local%20cache%20(in%20%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FMESI_protocol%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eexclusive%20%2F%20modified%3C%2FA%3E%20state).%20In%20contrast%20to%20that%2C%20when%20building%20a%20snapshot%2C%20a%20backend%20accesses%20all%20other%20connection%E2%80%99s%3CCODE%3EPGXACT-%26gt%3B%7Bxid%2Cxmin%7D%3C%2FCODE%3E.%20Glossing%20over%20a%20few%20details%2C%20that%2C%20in%20turn%2C%20requires%20that%20the%20cache-lines%20containing%20the%3CCODE%3EPGXACT%3C%2FCODE%3Ecannot%20be%20in%20another%20core%E2%80%99s%20private%20caches.%20%3CSTRONG%3EHead%20on%20head%20collision%20alert%3C%2FSTRONG%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETransferring%20the%20modified%20contents%20of%20a%20cache-line%20from%20a%20cache%20in%20another%20core%20to%20either%20a%20local%20cache%20or%20the%20shared%20L3%20cache%20has%20a%20fairly%20high%20latency%20cost%2C%20compared%20to%20accessing%20shared%20and%20unmodified%20data%20in%20the%20L3%20(and%20even%20more%20so%20in%20the%20local%20L1%2FL2%2C%20obviously).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20kicker%20is%20that%2C%20to%20build%20the%20snapshot%2C%3CCODE%3E-%26gt%3Bxmin%3C%2FCODE%3Edoes%20%3CSTRONG%3Enot%20actually%20need%20to%20be%20accessed%3C%2FSTRONG%3E.%20It%20is%20only%20needed%20to%20compute%3CCODE%3ERecentGlobalXminHorizon%3C%2FCODE%3E.%20However%2C%20just%20removing%20the%20read%20access%20itself%20doesn%E2%80%99t%20improve%20the%20situation%20significantly%3A%20As%3CCODE%3E-%26gt%3Bxid%3C%2FCODE%3E%2C%20which%20does%20need%20to%20be%20accessed%20to%20build%20a%20snapshot%2C%20is%20on%20the%20same%20cache%20line%20as%3CCODE%3E-%26gt%3Bxmin%3C%2FCODE%3Emodifying%3CCODE%3E-%26gt%3Bxmin%3C%2FCODE%3Ecauses%3CCODE%3E-%26gt%3Bxid%3C%2FCODE%3Eaccesses%20to%20be%20slow.%3C%2FP%3E%0A%3CH3%20id%3D%22interlude-removing-the-need-for-recentglobalxminhorizon%22%20id%3D%22toc-hId--292597423%22%20id%3D%22toc-hId--292597427%22%3E%3CBR%20%2F%3EInterlude%3A%20Removing%20the%20need%20for%3CCODE%3ERecentGlobalXminHorizon%3C%2FCODE%3E%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20reason%20that%3CCODE%3EGetSnapshotData()%3C%2FCODE%3Ealso%20re-computes%3CCODE%3ERecentGlobalXminHorizon%3C%2FCODE%3Eis%20that%20we%20use%20that%20for%20the%20cleanup%20of%20dead%20table%20and%20index%20entries%20(see%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dblob%3Bf%3Dsrc%2Fbackend%2Faccess%2Fheap%2FREADME.HOT%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EWhen%20can%2Fshould%20we%20prune%20or%20defragment%3F%3C%2FA%3E%20and%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dblob%3Bf%3Dsrc%2Fbackend%2Faccess%2Fnbtree%2FREADME%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EOn-the-Fly%20Deletion%20Of%20Index%20Tuples%3C%2FA%3E).%20The%20horizon%20is%20used%20as%20a%20threshold%20below%20which%20old%20tuple%20versions%20are%20not%20accessed%20by%20any%20connection.%20If%20older%20than%20the%20horizon%20row%20versions%2C%20as%20well%20as%20index%20entries%20pointing%20to%20them%2C%20can%20safely%20be%20deleted.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20crucial%20observation%E2%80%94after%20quite%20a%20long%20period%20of%20trying%20things%E2%80%94that%20allowed%20me%20to%20avoid%20the%20costly%20re-computation%2C%20is%20that%20we%20don%E2%80%99t%20necessarily%20need%20a%20accurate%20value%20most%20of%20the%20time.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20most%20workloads%20the%20majority%20of%20accesses%20are%20to%20%3CEM%3Elive%3C%2FEM%3E%20tuples%2C%20and%20when%20encountering%20non-live%20tuple%20versions%20they%20are%20either%20very%20old%2C%20or%20very%20new.%20With%20a%20bit%20of%20care%20we%20can%20lazily%20maintain%20a%20more%20complex%20threshold%3A%20One%20value%20that%20determines%20that%20everything%20older%20than%20it%20is%20definitely%20dead%2C%20and%20a%20second%20value%20that%20determines%20that%20everything%20above%20it%20is%20definitely%20too%20new%20to%20be%20cleaned%20up.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20encountering%20a%20tuple%20in%20between%20these%20thresholds%20we%20compute%20accurate%20values%2C%20valid%20for%20the%20current%20transaction.%20If%20we%20had%20to%20recompute%20the%20threshold%20in%20every%20short%20transaction%2C%20that%20would%20be%20more%20expensive%20than%20pre-computing%20the%20accurate%20value%20in%3CCODE%3EGetSnapshotData()%3C%2FCODE%3E%E2%80%94but%20it%E2%80%99s%20very%20hard%20to%20construct%20such%20workloads.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20main%20commit%20implementing%20this%20new%20approach%20is%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dcommit%3Bh%3Ddc7420c2c9274a283779ec19718d2d16323640c0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Edc7420c2c92%3C%2FA%3E%20%3CSTRONG%3Esnapshot%20scalability%3A%20Don%E2%80%99t%20compute%20global%20horizons%20while%20building%20snapshots%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAfter%20that%20commit%20we%20do%20not%20access%3CCODE%3E-%26gt%3Bxmin%3C%2FCODE%3Ein%3CCODE%3EGetSnapshotData()%3C%2FCODE%3Eanymore.%20To%20avoid%20the%20cache-line%20ping-pong%2C%20we%20can%20move%20it%20out%20of%20the%20data%20used%20by%3CCODE%3EGetSnapshotData()%3C%2FCODE%3E.%20That%20alone%20provides%20a%20substantial%20improvement%20in%20scalability.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20commit%20doing%20so%2C%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dcommit%3Bh%3D1f51c17c68d05c28d5b9294d8013cb9e7e653160%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E1f51c17c68d%3C%2FA%3E%20%3CSTRONG%3Esnapshot%20scalability%3A%20Move%20PGXACT-%26gt%3Bxmin%20back%20to%20PGPROC.%3C%2FSTRONG%3E%20includes%20some%20rough%20numbers%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3E%20%20%20%20For%20highly%20concurrent%2C%20snapshot%20acquisition%20heavy%2C%20workloads%20this%20change%20alone%0A%20%20%20%20can%20significantly%20increase%20scalability.%20E.g.%20plain%20pgbench%20on%20a%20smaller%202%0A%20%20%20%20socket%20machine%20gains%201.07x%20for%20read-only%20pgbench%2C%201.22x%20for%20read-only%20pgbench%0A%20%20%20%20when%20submitting%20queries%20in%20batches%20of%20100%2C%20and%202.85x%20for%20batches%20of%20100%0A%20%20%20%20'SELECT'%3B.%20%20The%20latter%20numbers%20are%20obviously%20not%20to%20be%20expected%20in%20the%0A%20%20%20%20real-world%2C%20but%20micro-benchmark%20the%20snapshot%20computation%0A%20%20%20%20scalability%20(previously%20spending%20~80%25%20of%20the%20time%20in%20GetSnapshotData()).%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22bottleneck-2-density%22%20id%3D%22toc-hId--2100051886%22%20id%3D%22toc-hId--2100051890%22%3EBottleneck%202%3A%20Density%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAbove%20I%20showed%20some%20simplified%20pseudo-code%20(%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dblob%3Bf%3Dsrc%2Fbackend%2Fstorage%2Fipc%2Fprocarray.c%3Bhb%3D5cbfce562f7cd2aab0cdc4694ce298ec3567930e%23l1577%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ereal%20code%3C%2FA%3E)%20for%20snapshot%20computations.%20The%20start%20of%20the%20pseudo%20code%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%22%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-C%22%20data-lang%3D%22C%22%3Exmin%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20global_xmin%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20inferred_maximum_possible%3B%0A%3CSPAN%3Efor%3C%2FSPAN%3E%20(i%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20%3CSPAN%3E0%3C%2FSPAN%3E%3B%20i%20%3CSPAN%3E%26lt%3B%3C%2FSPAN%3E%20%3CSPAN%3E%23%3C%2FSPAN%3Econnections%3B%20i%3CSPAN%3E%2B%2B%3C%2FSPAN%3E)%0A%7B%0A%20%20%20%20%3CSPAN%3Eint%3C%2FSPAN%3E%20procno%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20shared_memory%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Econnection_offsets%5Bi%5D%3B%0A%20%20%20%20PGXACT%20%3CSPAN%3E*%3C%2FSPAN%3Epgxact%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20shared_memory%3CSPAN%3E-%26gt%3B%3C%2FSPAN%3Eall_connections%5Bprocno%5D%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eshows%20that%20accesses%20to%3CCODE%3EPGXACT%3C%2FCODE%3Ehave%20to%20go%20through%20an%20indirection.%20That%20indirection%20allows%20to%20only%20look%20at%20the%3CCODE%3EPGXACT%3C%2FCODE%3Eof%20established%20connections%2C%20rather%20than%20also%20having%20to%20look%20at%20the%20connection%20slots%20for%20inactive%20connections.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EInstead%20of%20having%20to%20go%20through%20an%20indirection%2C%20we%20can%20instead%20make%20the%20contents%20of%3CCODE%3EPGXACT%3C%2FCODE%3Edense.%20That%20makes%20connection%20establishment%2Fdisconnections%20a%20tiny%20bit%20slower%2C%20now%20having%20to%20ensure%20not%20just%20that%20the%3CCODE%3Econnection_offsets%3C%2FCODE%3Earray%20is%20dense%2C%20but%20also%20that%20the%3CCODE%3EPGXACT%3C%2FCODE%3Econtents%20are.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20second%2C%20and%20related%2C%20observation%20is%20that%20none%20of%20the%20remaining%3CCODE%3EPGXACT%3C%2FCODE%3Emembers%20need%20to%20be%20accessed%20when%3CCODE%3E-%26gt%3Bxid%3C%2FCODE%3Eis%20not%20valid%20(%3CCODE%3E-%26gt%3Bxmin%3C%2FCODE%3E%3CA%20href%3D%22%23bottleneck-1-ping-pong%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Epreviously%3C%2FA%3E%20did%20need%20to%20be%20accessed).%20In%20many%20workloads%20most%20transactions%20do%20not%20write%2C%20and%20in%20most%20write%20heavy%20workloads%2C%20most%20transactions%20do%20not%20use%20savepoints.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%22%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-C%22%20data-lang%3D%22C%22%3E%3CSPAN%3Etypedef%3C%2FSPAN%3E%20%3CSPAN%3Estruct%3C%2FSPAN%3E%20PGXACT%0A%7B%0A%20TransactionId%20xid%3B%0A%20uint8%20%20vacuumFlags%3B%0A%20%3CSPAN%3Ebool%3C%2FSPAN%3E%20%20overflowed%3B%0A%20uint8%20%20nxids%3B%0A%7D%20PGXACT%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20a%20consequence%2C%20it%20is%20better%20not%20to%20make%20the%20entire%3CCODE%3EPGXACT%3C%2FCODE%3Earray%20dense%2C%20but%20instead%20to%20split%20its%20members%20into%20separate%20dense%20arrays.%20The%20array%20containing%20the%3CCODE%3Exids%3C%2FCODE%3Eof%20all%20established%20connections%20nearly%20always%20needs%20to%20be%20accessed%3CSUP%20id%3D%22fnref%3A6%22%3E%3CA%20class%3D%22footnote-ref%22%20role%3D%22doc-noteref%22%20href%3D%22%23fn%3A6%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E6%3C%2FA%3E%3C%2FSUP%3E.%20But%20only%20if%20the%20connection%20has%20an%20assigned%3CCODE%3Exid%3C%2FCODE%3Ethe%20other%20members%20need%20to%20be%20accessed.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBy%20having%20a%20separate%20array%20for%3CCODE%3Exids%3C%2FCODE%3Ethe%20CPU%20cache%20hit%20ratio%20can%20be%20increased%2C%20as%20most%20of%20the%20time%20the%20other%20fields%20are%20not%20accessed.%20Additionally%2C%20as%20the%20other%20fields%20change%20less%20frequently%2C%20keeping%20them%20separate%20allows%20them%20to%20be%20shared%20in%20an%20unmodified%20state%20between%20the%20cache%20domains%20(increasing%20access%20speed%20%2F%20decreasing%20bus%20traffic).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETheses%20changes%20are%20implemented%20in%20Postgres%20commits%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dcommit%3Bh%3D941697c3c1ae5d6ee153065adb96e1e63ee11224%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E941697c3c1a%3C%2FA%3E%20%3CSTRONG%3Esnapshot%20scalability%3A%20Introduce%20dense%20array%20of%20in-progress%20xids%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dcommit%3Bh%3D5788e258bb26495fab65ff3aa486268d1c50b123%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E5788e258bb2%3C%2FA%3E%20%3CSTRONG%3Esnapshot%20scalability%3A%20Move%20PGXACT-%26gt%3BvacuumFlags%20to%20ProcGlobal-%26gt%3BvacuumFlags%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dcommit%3Bh%3D73487a60fc1063ba4b5178b69aee4ee210c182c4%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E73487a60fc1%3C%2FA%3E%20%3CSTRONG%3Esnapshot%20scalability%3A%20Move%20subxact%20info%20to%20ProcGlobal%2C%20remove%20PGXACT.%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EThis%20yields%20quite%20a%20bit%20of%20benefit%2C%20as%20commented%20upon%20in%20one%20of%20the%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dcommit%3Bh%3D73487a60fc1063ba4b5178b69aee4ee210c182c4%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ecommit%20messages%3C%2FA%3E%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3E%20%20%20%20On%20a%20larger%202%20socket%20machine%20this%20and%20the%20two%20preceding%20commits%20result%0A%20%20%20%20in%20a%20~1.07x%20performance%20increase%20in%20read-only%20pgbench.%20For%20read-heavy%0A%20%20%20%20mixed%20r%2Fw%20workloads%20without%20row%20level%20contention%2C%20I%20see%20about%201.1x.%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CH3%20id%3D%22bottleneck-3-caching%22%20id%3D%22toc-hId--307702532%22%20id%3D%22toc-hId--307702536%22%3E%3CBR%20%2F%3EBottleneck%203%3A%20Caching%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEven%20with%20all%20the%20preceding%20changes%2C%20computing%20a%20snapshot%20with%20a%20lot%20of%20connections%20still%20is%20not%20cheap.%20While%20the%20changes%20improved%20the%20constant%20factor%20considerably%2C%20having%20to%20iterate%20through%20arrays%20with%20potentially%20a%20few%20thousand%20elements%20still%20is%20not%20cheap.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20that%3CCODE%3EGetSnapshotData()%3C%2FCODE%3Edoes%20not%20need%20to%20maintain%3CCODE%3ERecentGlobalXmin%3C%2FCODE%3E%3CA%20href%3D%22%23interlude-removing-the-need-for-recentglobalxminhorizon%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eanymore%3C%2FA%3E%2C%20a%20huge%20improvement%20on%20the%20table%3A%20We%20can%20avoid%20re-computing%20the%20snapshot%20if%20we%20can%20determine%20it%20has%20not%20changed.%20Previously%20that%20was%20not%20viable%2C%20as%3CCODE%3ERecentGlobalXmin%3C%2FCODE%3Echanges%20much%20more%20frequently%20than%20the%20snapshot%20contents%20themselves.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20snapshot%20only%20needs%20to%20change%20if%20a%20previously%20running%20transaction%20has%20committed%20(so%20its%20effect%20are%20visible)%3A%20Because%20all%20transactions%20bigger-or-equal%20than%3CCODE%3E-%26gt%3Bxmax%3C%2FCODE%3Eare%20treated%20as%20running%2C%20and%20because%20all%20transactions%20starting%20after%20snapshot%20has%20been%20computed%20are%20guaranteed%20to%20be%20assigned%20a%20transaction%20id%20larger%20then%3CCODE%3E-%26gt%3Bxmax%3C%2FCODE%3E%2C%20we%20need%20not%20care%20about%20newly%20started%20transactions.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETherefore%20a%20simple%20in-memory%20counter%20of%20the%20number%20of%20completed%20(i.e.%20committed%20or%20aborted)%20transactions%20can%20be%20used%20to%20invalidate%20snapshots.%20The%20%3CEM%3Ecompletion%20counter%3C%2FEM%3E%20is%20stored%20in%20the%20snapshot%2C%20and%20when%20asked%20to%20re-compute%20the%20snapshot%20contents%2C%20we%20just%20need%20to%20check%20if%20the%20snapshot%E2%80%99s%3CCODE%3EsnapXactCompletionCount%3C%2FCODE%3Eis%20the%20same%20as%20the%20current%20in-memory%20value%3CCODE%3EShmemVariableCache-%26gt%3BxactCompletionCount%3C%2FCODE%3E.%20If%20they%20are%2C%20the%20contents%20of%20the%20snapshot%20can%20be%20reused%2C%20otherwise%20the%20snapshot%20needs%20to%20be%20built%20from%20scratch.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20change%20was%20implemented%20in%20Postgres%20commit%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dcommit%3Bh%3D623a9ba79bbdd11c5eccb30b8bd5c446130e521c%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E623a9ba79bb%3C%2FA%3E%3A%20%3CSTRONG%3Esnapshot%20scalability%3A%20cache%20snapshots%20using%20a%20xact%20completion%20counter.%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20commit%20message%20again%20describes%20the%20gains%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3E%20%20%20%20On%20a%20smaller%20two%20socket%20machine%20this%20gains%20another%20~1.03x%2C%20on%20a%20larger%0A%20%20%20%20machine%20the%20effect%20is%20roughly%20double%20(earlier%20patch%20version%20tested%0A%20%20%20%20though).%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EAs%20the%20last%20sentence%20alludes%20to%2C%20currently%20we%20test%20for%20cache-ability%20holding%20a%20lock.%20It%20likely%20is%20possible%20to%20avoid%20that%2C%20but%20there%20are%20a%20few%20complexities%20that%20need%20to%20be%20addressed%3CSUP%20id%3D%22fnref%3A7%22%3E%3CA%20class%3D%22footnote-ref%22%20role%3D%22doc-noteref%22%20href%3D%22%23fn%3A7%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E7%3C%2FA%3E%3C%2FSUP%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-2050727582%22%20id%3D%22toc-hId-2050727578%22%3EConclusion%3A%20One%20bottleneck%20down%20in%20PG%2014%2C%20others%20in%20sight%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20improvements%20presented%20here%20significantly%20improve%20Postgres'%20handling%20of%20large%20numbers%20of%20connections%2C%20particularly%20when%E2%80%94as%20is%20often%20the%20case%E2%80%94a%20large%20fraction%20are%20idle.%20This%20addresses%20the%20most%20pressing%20issue%20identified%20in%20my%20previous%20post%20on%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fanalyzing-the-limits-of-connection-scalability-in-postgres%2Fba-p%2F1757266%23conclusion-start-by-improving-snapshot-scalability-in-postgres%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3EAnalyzing%20the%20Limits%20of%20Connection%20Scalability%20in%20Postgres%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20be%20clear%3A%20These%20improvements%20do%20not%20address%20all%20connection%20scalability%20issues%20in%20Postgres.%20Nor%20are%20snapshot%20computations%20eliminated%20as%20a%20scalability%20factor.%20But%20I%20do%20think%20this%20project%20has%20improved%20the%20situation%20considerably.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20read-mostly%20workloads%2C%20snapshot%20computation%20is%20nearly%20entirely%20eliminated%20as%20an%20overhead%E2%80%94and%20even%20for%20read-write%20workloads%20the%20overhead%20is%20significantly%20reduced.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOn%20a%20higher%20level%2C%20the%20changes%20outlined%20should%20allow%20applications%20to%20scale%20up%20more%20easily%20once%20using%20Postgres%2014%2C%20without%20having%20to%20worry%20about%20hitting%20Postgres%20connection%20limits%20as%20much.%20Of%20course%20it%20still%20is%20important%20to%20pay%20some%20attention%20to%20not%20use%20too%20overly%20many%20connections%E2%80%94as%20outlined%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fanalyzing-the-limits-of-connection-scalability-in-postgres%2Fba-p%2F1757266%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Ebefore%3C%2FA%3E%20there%20are%20other%20limitations%20one%20can%20hit.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22from-easy-to-hard-opportunities-for-further-improvements%22%20id%3D%22toc-hId-243273119%22%20id%3D%22toc-hId-243273115%22%3EFrom%20easy%20to%20hard%3A%20Opportunities%20for%20further%20improvements%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20are%20plenty%20additional%20snapshot%20scalability%20improvements%20that%20could%20be%20made%20on%20top%20of%20these%20changes.%20Without%20moving%20to%20an%20entirely%20different%20snapshot%20representation%2C%20even.%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CP%3EAs%20outlined%20above%2C%20the%20check%20whether%20a%20cached%20snapshot%20is%20still%20valid%20acquires%20a%20lock.%20It%20is%20very%20likely%20possible%20to%20remove%20that%20lock%20acquisition%2C%20and%20experiments%20show%20that%20to%20be%20a%20significant%20improvement.%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3ECurrently%20the%20snapshot%20caching%20is%20done%20for%20individual%20snapshot%20types%2C%20within%20each%20backend.%20It%20may%20be%20worthwhile%20to%20optimize%20it%2C%20so%20that%20each%20backend%20only%20has%20one%20cached%20snapshot.%20It%20also%20might%20be%20worthwhile%20to%20try%20to%20share%20the%20cached%20snapshot%20between%20backends%2C%20although%20the%20inter-process%20coordination%20that%20would%20require%2C%20makes%20that%20not%20too%20promising.%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3EThe%20snapshot%20computation%20is%20currently%20not%20very%20pipeline%20friendly.%20Initial%20experiments%20show%20that%20the%20computation%20could%20be%20made%20more%20efficient%20by%20re-arranging%20the%20computation%20to%20first%20assemble%20the%20set%20of%20running%20transactions%2C%20then%20check%3CCODE%3EvacuumFlags%3C%2FCODE%3Eand%20subtransaction%20counters%20in%20a%20second%20loop.%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3ELooking%20further%20into%20the%20future%2C%20it%20may%20very%20well%20be%20worthwhile%20to%20maintain%20efficient%20%E2%80%9Crunning%20transactions%20with%20xids%E2%80%9D%20data%20structure%2C%20instead%20of%20the%20current%20%E2%80%9Cxids%20of%20all%20established%20connections%E2%80%9D%20(commonly%20filled%20largely%20with%20invalid%20xids).%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CSECTION%20class%3D%22footnotes%22%20role%3D%22doc-endnotes%22%3E%3CHR%20%2F%3E%0A%3COL%3E%0A%3CLI%20id%3D%22fn%3A1%22%20role%3D%22doc-endnote%22%3E%3CP%3EPgbench%20read-only%20results%2C%20pre%2Fpost%20changes%3A%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTH%3Eclients%3C%2FTH%3E%0A%3CTH%3ETPS%20pre%3C%2FTH%3E%0A%3CTH%3ETPS%20post%3C%2FTH%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E28%2C842%3C%2FTD%3E%0A%3CTD%3E28%2C728%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E10%3C%2FTD%3E%0A%3CTD%3E236%2C287%3C%2FTD%3E%0A%3CTD%3E260%2C960%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E20%3C%2FTD%3E%0A%3CTD%3E472%2C479%3C%2FTD%3E%0A%3CTD%3E486%2C659%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E30%3C%2FTD%3E%0A%3CTD%3E584%2C984%3C%2FTD%3E%0A%3CTD%3E598%2C863%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E40%3C%2FTD%3E%0A%3CTD%3E678%2C770%3C%2FTD%3E%0A%3CTD%3E693%2C314%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E50%3C%2FTD%3E%0A%3CTD%3E788%2C529%3C%2FTD%3E%0A%3CTD%3E806%2C085%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E60%3C%2FTD%3E%0A%3CTD%3E1%2C031%2C483%3C%2FTD%3E%0A%3CTD%3E986%2C730%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E70%3C%2FTD%3E%0A%3CTD%3E1%2C254%2C570%3C%2FTD%3E%0A%3CTD%3E1%2C332%2C258%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E80%3C%2FTD%3E%0A%3CTD%3E1%2C341%2C188%3C%2FTD%3E%0A%3CTD%3E1%2C438%2C881%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E90%3C%2FTD%3E%0A%3CTD%3E1%2C496%2C374%3C%2FTD%3E%0A%3CTD%3E1%2C673%2C668%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E100%3C%2FTD%3E%0A%3CTD%3E1%2C538%2C186%3C%2FTD%3E%0A%3CTD%3E1%2C651%2C516%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E125%3C%2FTD%3E%0A%3CTD%3E1%2C504%2C833%3C%2FTD%3E%0A%3CTD%3E1%2C621%2C912%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E150%3C%2FTD%3E%0A%3CTD%3E1%2C428%2C711%3C%2FTD%3E%0A%3CTD%3E1%2C570%2C070%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E175%3C%2FTD%3E%0A%3CTD%3E1%2C433%2C643%3C%2FTD%3E%0A%3CTD%3E1%2C572%2C395%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E200%3C%2FTD%3E%0A%3CTD%3E1%2C404%2C691%3C%2FTD%3E%0A%3CTD%3E1%2C523%2C175%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E250%3C%2FTD%3E%0A%3CTD%3E1%2C368%2C605%3C%2FTD%3E%0A%3CTD%3E1%2C541%2C316%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E300%3C%2FTD%3E%0A%3CTD%3E1%2C315%2C812%3C%2FTD%3E%0A%3CTD%3E1%2C490%2C701%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E400%3C%2FTD%3E%0A%3CTD%3E1%2C305%2C039%3C%2FTD%3E%0A%3CTD%3E1%2C520%2C501%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E500%3C%2FTD%3E%0A%3CTD%3E1%2C390%2C359%3C%2FTD%3E%0A%3CTD%3E1%2C639%2C884%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E600%3C%2FTD%3E%0A%3CTD%3E1%2C364%2C976%3C%2FTD%3E%0A%3CTD%3E1%2C715%2C232%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E700%3C%2FTD%3E%0A%3CTD%3E1%2C323%2C205%3C%2FTD%3E%0A%3CTD%3E1%2C716%2C550%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E800%3C%2FTD%3E%0A%3CTD%3E1%2C362%2C618%3C%2FTD%3E%0A%3CTD%3E1%2C698%2C511%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E900%3C%2FTD%3E%0A%3CTD%3E1%2C324%2C593%3C%2FTD%3E%0A%3CTD%3E1%2C705%2C670%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E1000%3C%2FTD%3E%0A%3CTD%3E1%2C273%2C755%3C%2FTD%3E%0A%3CTD%3E1%2C722%2C917%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E1500%3C%2FTD%3E%0A%3CTD%3E1%2C246%2C604%3C%2FTD%3E%0A%3CTD%3E1%2C651%2C516%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E2000%3C%2FTD%3E%0A%3CTD%3E1%2C171%2C879%3C%2FTD%3E%0A%3CTD%3E1%2C680%2C384%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E3000%3C%2FTD%3E%0A%3CTD%3E1%2C074%2C248%3C%2FTD%3E%0A%3CTD%3E1%2C651%2C516%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E4000%3C%2FTD%3E%0A%3CTD%3E1%2C001%2C631%3C%2FTD%3E%0A%3CTD%3E1%2C683%2C714%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E5000%3C%2FTD%3E%0A%3CTD%3E732%2C530%3C%2FTD%3E%0A%3CTD%3E1%2C589%2C232%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E7500%3C%2FTD%3E%0A%3CTD%3E674%2C862%3C%2FTD%3E%0A%3CTD%3E1%2C669%2C350%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E10000%3C%2FTD%3E%0A%3CTD%3E642%2C042%3C%2FTD%3E%0A%3CTD%3E1%2C656%2C006%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12500%3C%2FTD%3E%0A%3CTD%3E541%2C565%3C%2FTD%3E%0A%3CTD%3E1%2C612%2C269%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%3CA%20class%3D%22footnote-backref%22%20role%3D%22doc-backlink%22%20href%3D%22%23fnref%3A1%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%E2%86%A9%EF%B8%8E%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22fn%3A2%22%20role%3D%22doc-endnote%22%3E%3CP%3E2x%20Xeon%20Gold%205215%2C%20192GiB%20of%20RAM%2C%20Linux%205.8.5%2C%20Debian%20Sid%20%3CA%20class%3D%22footnote-backref%22%20role%3D%22doc-backlink%22%20href%3D%22%23fnref%3A2%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%E2%86%A9%EF%B8%8E%3C%2FA%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%20id%3D%22fn%3A3%22%20role%3D%22doc-endnote%22%3E%3CP%3EIdle%20Connections%20vs%20Active%20Connections%2C%20pre%2Fpost%20changes%3A%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTH%3EIdle%20Connections%3C%2FTH%3E%0A%3CTH%3EActive%20Connections%3C%2FTH%3E%0A%3CTH%3ETPS%20pre%3C%2FTH%3E%0A%3CTH%3ETPS%20post%3C%2FTH%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E0%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E33599%3C%2FTD%3E%0A%3CTD%3E33406%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E100%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E31088%3C%2FTD%3E%0A%3CTD%3E33279%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E1000%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E29377%3C%2FTD%3E%0A%3CTD%3E33434%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E2500%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E27050%3C%2FTD%3E%0A%3CTD%3E33149%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E5000%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E21895%3C%2FTD%3E%0A%3CTD%3E33903%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E10000%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E16034%3C%2FTD%3E%0A%3CTD%3E33140%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E0%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E1042005%3C%2FTD%3E%0A%3CTD%3E1125104%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E100%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E986731%3C%2FTD%3E%0A%3CTD%3E1103584%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E1000%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E854230%3C%2FTD%3E%0A%3CTD%3E1119043%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E2500%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E716624%3C%2FTD%3E%0A%3CTD%3E1119353%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E5000%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E553657%3C%2FTD%3E%0A%3CTD%3E1119476%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E10000%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E369845%3C%2FTD%3E%0A%3CTD%3E1115740%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%3CA%20class%3D%22footnote-backref%22%20role%3D%22doc-backlink%22%20href%3D%22%23fnref%3A3%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%E2%86%A9%EF%B8%8E%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22fn%3A4%22%20role%3D%22doc-endnote%22%3E%3CP%3EMostly%20Idle%20Connections%20vs%20Active%20Connections%2C%20pre%2Fpost%20changes%3A%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTH%3EMostly%20Idle%20Connections%3C%2FTH%3E%0A%3CTH%3EActive%20Connections%3C%2FTH%3E%0A%3CTH%3ETPS%20pre%3C%2FTH%3E%0A%3CTH%3ETPS%20post%3C%2FTH%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E0%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E33837%3C%2FTD%3E%0A%3CTD%3E34095%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E100%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E30622%3C%2FTD%3E%0A%3CTD%3E31166%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E1000%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E25523%3C%2FTD%3E%0A%3CTD%3E28829%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E2500%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E19260%3C%2FTD%3E%0A%3CTD%3E24978%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E5000%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E11171%3C%2FTD%3E%0A%3CTD%3E24208%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E10000%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E6702%3C%2FTD%3E%0A%3CTD%3E29577%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E0%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E1022721%3C%2FTD%3E%0A%3CTD%3E1133153%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E100%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E980705%3C%2FTD%3E%0A%3CTD%3E1034235%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E1000%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E824668%3C%2FTD%3E%0A%3CTD%3E1115965%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E2500%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E698510%3C%2FTD%3E%0A%3CTD%3E1073280%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E5000%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E478535%3C%2FTD%3E%0A%3CTD%3E1041931%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E10000%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E276042%3C%2FTD%3E%0A%3CTD%3E953567%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%3CA%20class%3D%22footnote-backref%22%20role%3D%22doc-backlink%22%20href%3D%22%23fnref%3A4%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%E2%86%A9%EF%B8%8E%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22fn%3A5%22%20role%3D%22doc-endnote%22%3E%3CP%3ENote%20that%20commit%20order%20is%20not%20always%20the%20right%20order%20for%20some%20higher%20isolation%20levels.%20But%20for%20the%20purpose%20of%20this%20post%20that%20is%20not%20relevant.%20%3CA%20class%3D%22footnote-backref%22%20role%3D%22doc-backlink%22%20href%3D%22%23fnref%3A5%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%E2%86%A9%EF%B8%8E%3C%2FA%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%20id%3D%22fn%3A6%22%20role%3D%22doc-endnote%22%3E%3CP%3EExcept%20in%20case%20of%20the%20PGXACT%20for%20a%20backend%20running%3CCODE%3EVACUUM%3C%2FCODE%3Eor%20performing%20logical%20decoding%2C%20but%20that%20number%20usually%20will%20be%20small.%20%3CA%20class%3D%22footnote-backref%22%20role%3D%22doc-backlink%22%20href%3D%22%23fnref%3A6%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%E2%86%A9%EF%B8%8E%3C%2FA%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%20id%3D%22fn%3A7%22%20role%3D%22doc-endnote%22%3E%3CP%3EWithout%20acquiring%20the%20lock%20it%20is%20not%20easily%20possible%20to%20ensure%20that%20the%20global%20xmin%20horizon%20cannot%20temporarily%20go%20backwards.%20That%20likely%20is%20OK%2C%20but%20requires%20a%20careful%20analysis.%20%3CA%20class%3D%22footnote-backref%22%20role%3D%22doc-backlink%22%20href%3D%22%23fnref%3A7%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%E2%86%A9%EF%B8%8E%3C%2FA%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FSECTION%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1806462%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pgbench-before-and-after-performance-comparison-Postgres-snapshot-scalability-v2-999x463.jpg%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F228382iADEE1BCCEEBFC0E9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22pgbench-before-and-after-performance-comparison-Postgres-snapshot-scalability-v2-999x463.jpg%22%20alt%3D%22pgbench-before-and-after-performance-comparison-Postgres-snapshot-scalability-v2-999x463.jpg%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CDIV%3EImproving%20connection%20scalability%20in%20Postgres%2014.%20From%20the%20neat%20to%20the%20gritty%3A%20snapshot%20scalability%20edition.%3C%2FDIV%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1806462%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EConnections%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPerformance%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epostgres%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Escalability%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

I recently analyzed the limits of connection scalability, to understand the most effective way to improve Postgres' handling of large numbers of connections, and why that is important. I concluded that the most pressing issue is snapshot scalability.

 

This post details the improvements I recently contributed to Postgres 14 (to be released Q3 of 2021), significantly reducing the identified snapshot scalability bottleneck.

pgbench-before-and-after-performance-comparison-Postgres-snapshot-scalability-v2-1920x1080.jpg

As the explanation of the implementation details is fairly long, I thought it’d be more fun for of you if I start with the results of the work, instead of the technical details (I’m cheating, I know ;)).




First: Performance Improvements

 

For all of these benchmarks, I compared the Postgres development tree just before and after all the connection scalability changes have been merged. There are a few other changes interspersed, but none that are likely to affect performance in a significant way.

 

First, a before / after comparison of a read-only pgbench benchmark, on an Azure F72s_v2 VM:

 

Figure 1: Benchmark (read-only pgbench) results comparison, showing the effects of the snapshot scalability improvements.Figure 1: Benchmark (read-only pgbench) results comparison, showing the effects of the snapshot scalability improvements.

These results 1 show a significant improvement after the snapshot scalability changes I’ll be talking about in this post, with little evidence of scalability issues even at very high connection counts. The dip starting around 100 connections—for both the pre/post changes runs— appears to be caused by OS task scheduling, rather than Postgres directly.

 

Next up, a repeat of the benchmarks I used in my last post on analyzing connection scalability to identify snapshot scalability as the primary bottleneck (again executed on my workstation2).

 

Figure 2: Benchmark result comparison (1 active connection running read-only pgbench, in presence of a variable number of idle connections), showing the effects of the snapshot scalability improvements.Figure 2: Benchmark result comparison (1 active connection running read-only pgbench, in presence of a variable number of idle connections), showing the effects of the snapshot scalability improvements.

Figure 3: Benchmark result comparison (48 active connections running read-only pgbench, in presence of a variable number of idle connections), showing the effects of the snapshot scalability improvements.Figure 3: Benchmark result comparison (48 active connections running read-only pgbench, in presence of a variable number of idle connections), showing the effects of the snapshot scalability improvements.

These results (3, 4) show the extreme difference in scalability between the fixed and unfixed version of Postgres. More so than the results above, as the benchmark is chosen to highlight the snapshot scalability issue.


Unfortunately: A Minimal Transaction Visibility Primer

 

As seen in the before & after charts above, the performance / scalability effects of the changes are substantial. To, hopefully, make it a bit easier to follow along, the next section is an attempt in providing some of the necessary background.

 

Postgres implements transaction isolation, i.e. the visibility changes made concurrent with a transactions, using snapshot based Multi Version Concurrency Control (MVCC).

 

Brandur has a good post on how Postgres makes transactions atomic explaining how this works in more detail. A lot of low-level details of how this works in Postgres are explained in the relevant README inside the Postgres source code.


Multi Version Concurrency Control (MVCC)

 

In brief, both MVCC and snapshots are some of the building blocks used to implement part of concurrency control in Postgres. MVCC boils down to having the ability to have multiple row versions for the same logical row, with different versions visible to different transactions, increasing concurrency. 

E.g. imagine one query starting to scan a large table, and subsequently another query updating a row in that table. MVCC allows the update to proceed without affecting the query results by the table scan, by keeping the original row version available for the scan, and making a new row version with the updated contents. That obviously is good for concurrency.

 

Conceptually this works by each row version having a “visible since” (xmin in Postgres) and a “visible until” (xmax in Postgres) “timestamp” (not really a timestamp in Postgres, but rather a transaction identifier). That way a scan can ignore modifications that have been made after the scan started, by (a) considering row versions that have since been deleted to be visible and (b) considering row versions created after the scan started to be invisible.

Snapshots?

 

Just having two such “timestamps” associated with each row version is not enough, however. Isolation rules consider which effects by other transactions should be visible to a transaction not by the time the other transactions started, but by the time the other transaction commits. Therefore, fundamentally, a timestamp like “visible since” and “visible until” attached to row versions at the time of modification cannot alone be sufficient: The order in which transaction commit is not yet known5. That is where snapshots come into play.

 

Postgres uses snapshots to identify which which transactions were running at the time of snapshot’s creation. That allows statements (e.g. in READ COMMITTED mode) or entire transactions (e.g. in REPEATABLE READ mode) to decide which rows created by other transactions should be visible, and which not.

 

typedef struct SnapshotData
{

	TransactionId xmin;			/* all XID < xmin are visible to me */
	TransactionId xmax;			/* all XID >= xmax are invisible to me */

	/*
	 * For normal MVCC snapshot this contains the all xact IDs that are in
	 * progress, unless the snapshot was taken during recovery in which case
	 * it's empty. …
	 * note: all ids in xip[] satisfy xmin <= xip[i] < xmax
	 */
	TransactionId *xip;
	uint32		xcnt;			/* # of xact ids in xip[] */

The xip array contains all the transaction IDs (which Postgres uses instead of plain timestamps) that were running at the time the snapshot was taken. When encountering a row version with a certain xmin, it will be invisible if that transaction was still running when the snapshot was taken and conversely may be visible if xmin is a transaction that already had finished at that time. And conversely, a row version with an xmax is still visible if the associated transaction that was running at the time of the snapshot was taken, invisible otherwise.


Snapping Snapshots

 

To understand the performance problems and the improvements it is necessary to understand how snapshots were built before. The core routine for this is GetSnapshotData(), which unsurprisingly is the function we saw high up in profiles earlier.

 

Every connection to Postgres has an associated struct PGPROC and, until now, a struct PGXACT entry. These structs are pre-allocated at server based on max_connections (and max_prepared_xacts, max_autovacuum_workers, …).

 

typedef struct ProcArrayStruct
{
	int			numProcs;		/* number of valid procs entries */

	/* indexes into allPgXact[], has PROCARRAY_MAXPROCS entries */
	int			pgprocnos[FLEXIBLE_ARRAY_MEMBER];

} ProcArrayStruct;


struct PGPROC
{

}


/*
 * Prior to PostgreSQL 9.2, the fields below were stored as part of the
 * PGPROC.  However, benchmarking revealed that packing these particular
 * members into a separate array as tightly as possible sped up GetSnapshotData
 * considerably on systems with many CPU cores, by reducing the number of
 * cache lines needing to be fetched.  Thus, think very carefully before adding
 * anything else here.
 */
typedef struct PGXACT
{
	TransactionId xid;			/* id of top-level transaction currently being
								 * executed by this proc, if running and XID
								 * is assigned; else InvalidTransactionId */

	TransactionId xmin;			/* minimal running XID as it was when we were
								 * starting our xact, excluding LAZY VACUUM:
								 * vacuum must not remove tuples deleted by
								 * xid >= xmin ! */

	uint8		vacuumFlags;	/* vacuum-related flags, see above */
	bool		overflowed;

	uint8		nxids;
} PGXACT;

 

To avoid needing to grovel through all PGPROC / PGXACT entries ProcArrayStruct->pgprocnos is a sorted array of the ->maxProc established connections. Each array entry is the index into PGPROC / PGXACT.

 

To build a snapshot GetSnapshotData() iterates over all maxProc entries in pgprocnos, collecting PGXACT->xid for all connections with an assigned transaction ID.

 

There are a few aspects making this slightly more complicated than the simple loop I described:

  1. Because it was, at some point, convenient, GetSnapshotData() also computes the globally oldest PGXACT->xmin. That is, most importantly, used to remove dead tuples on access.
  2. To implement SAVEPOINT, a backend can have multiple assigned transaction IDs. A certain number of these are stored as part of PGPROC.
  3. Some backends, e.g. ones executing VACUUM, are ignored when building a snapshot, for efficiency purposes.
  4. On a replica, the snapshot computation works quite differently.


Past Optimizations

 

In 2011 GetSnapshotData() was seen as a bottleneck. At that point all the relevant data to build a snapshot was stored in PGPROC. That caused performance problems, primarily because multiple cache-lines were accessed for each established connection.

 

This was improved by splitting out the most important fields into a new data-structure PGXACT. That significantly decreases the total number of cache-lines that need to be accessed to build a snapshot. Additionally the order of accesses to PGXACT was improved to be in increasing memory order (previously it was determined by the order in which connections are established and disconnect).


Finally: Addressing Bottlenecks

 

It’s not too hard to see that the approach described above, i.e. iterating over an array containing all established connections, has a complexity of O(#connections), i.e. the snapshot computation cost increases linearly with the number of connections.

 

There are two fundamental approaches to improving scalability here: First, finding an algorithm that improves the complexity, so that each additional connection does not increase the snapshot computation costs linearly. Second, perform less work for each connection, hopefully reducing the total time taken so much that even at high connection counts the total time is still small enough to not matter much (i.e. reduce the constant factor).

 

One approach to improve the algorithmic complexity of GetSnapshotData() that has been worked on in the Postgres community for quite a few years are commit sequence number based snapshots (also called CSN based snapshots). Unfortunately implementing CSN snapshots has proven to be a very large project, with many open non-trivial problems that need to be solved. As I was looking for improvements that could be completed in a shorter time frame, I discarded pursuing that approach, and other similarly fundamental changes.

 

Back in 2015, I had previously tried to attack this problem by caching snapshots, but that turned out to not be easy either (at least not yet…).

 

Therefore I chose to first focus on improving the cost each additional connection adds. Iterating over an array of a few thousand elements and dereferencing fairly small content obviously is not free, but compared to the other work done as part of query processing, it should not be quite as prominent as in the CPU profile from the previous post:

 

Profile of one active connection running read-only pgbench concurrently with 5000 idle connections, bottleneck is clearly in `GetSnapshotData()`Profile of one active connection running read-only pgbench concurrently with 5000 idle connections, bottleneck is clearly in `GetSnapshotData()`

 

The core snapshot computation boils down to, in pseudo code, the following:

 

xmin = global_xmin = inferred_maximum_possible;
for (i = 0; i < #connections; i++)
{
    int procno = shared_memory->connection_offsets[i];
    PGXACT *pgxact = shared_memory->all_connections[procno];

    // compute global xmin minimum
	if (pgxact->xmin && pgxact->xmin < global_xmin)
	    global_xmin = pgxact->xmin;

	// nothing to do if backend has transaction id assigned
	if (!pgxact->xid)
	    continue;

	// the global xmin minimum also needs to include assigned transaction ids
	if (pxact->xid < global_xmin)
		global_xmin = pgxact->xid;

	// add the xid to the snapshot
	snapshot->xip[snapshot->xcnt++] = pgxact->xid;

	// compute minimum xid in snapshot
	if (pgxact->xid < xmin)
		xmin = pgxact->xid;

}

snapshot->xmin = xmin;
// store snapshot xmin unless we already have built other snapshots
if (!MyPgXact->xmin)
	MyPgXact->xmin = xmin;
RecentGlobalXminHorizon = global_xmin;

 

One important observation about this is that the main loop does not just compute the snapshot contents, but also the “global xmin horizon”. Which is not actually part of the snapshot, but can conveniently be computed at the same time, for a small amount of added cost. Or so we thought…

I spent a lot of time, on and off, trying to understand why iterating over a few thousand elements of an array, even taking the indirection into account, turns out to be so costly in some workloads.

 

Bottleneck 1: Ping Pong

 

The main problem turns out to be MyPgXact->xmin = xmin;. A connection’s xmin is is set whenever a snapshot is computed (unless another snapshot already exists), when a transaction is committed / aborted ( 1, 2 ).

 

On the currently most common multi-core CPU micro-architectures each CPU core has its own private L1 and L2 caches and all cores within a CPU socket share an L3 cache.

 

Active backends constantly update MyPgXact->xmin. Simplifying a bit, that in turn requires that the data is in a core-local cache (in exclusive / modified state). In contrast to that, when building a snapshot, a backend accesses all other connection’s PGXACT->{xid,xmin}. Glossing over a few details, that, in turn, requires that the cache-lines containing the PGXACT cannot be in another core’s private caches. Head on head collision alert.

 

Transferring the modified contents of a cache-line from a cache in another core to either a local cache or the shared L3 cache has a fairly high latency cost, compared to accessing shared and unmodified data in the L3 (and even more so in the local L1/L2, obviously).

 

The kicker is that, to build the snapshot, ->xmin does not actually need to be accessed. It is only needed to compute RecentGlobalXminHorizon. However, just removing the read access itself doesn’t improve the situation significantly: As ->xid, which does need to be accessed to build a snapshot, is on the same cache line as ->xmin modifying ->xmin causes ->xid accesses to be slow.


Interlude: Removing the need for RecentGlobalXminHorizon

 

The reason that GetSnapshotData() also re-computes RecentGlobalXminHorizon is that we use that for the cleanup of dead table and index entries (see When can/should we prune or defragment? and On-the-Fly Deletion Of Index Tuples). The horizon is used as a threshold below which old tuple versions are not accessed by any connection. If older than the horizon row versions, as well as index entries pointing to them, can safely be deleted.

 

The crucial observation—after quite a long period of trying things—that allowed me to avoid the costly re-computation, is that we don’t necessarily need a accurate value most of the time.

 

In most workloads the majority of accesses are to live tuples, and when encountering non-live tuple versions they are either very old, or very new. With a bit of care we can lazily maintain a more complex threshold: One value that determines that everything older than it is definitely dead, and a second value that determines that everything above it is definitely too new to be cleaned up.

 

When encountering a tuple in between these thresholds we compute accurate values, valid for the current transaction. If we had to recompute the threshold in every short transaction, that would be more expensive than pre-computing the accurate value in GetSnapshotData()—but it’s very hard to construct such workloads.

 

The main commit implementing this new approach is dc7420c2c92 snapshot scalability: Don’t compute global horizons while building snapshots

 

After that commit we do not access ->xmin in GetSnapshotData() anymore. To avoid the cache-line ping-pong, we can move it out of the data used by GetSnapshotData(). That alone provides a substantial improvement in scalability.

 

The commit doing so, 1f51c17c68d snapshot scalability: Move PGXACT->xmin back to PGPROC. includes some rough numbers:

 

    For highly concurrent, snapshot acquisition heavy, workloads this change alone
    can significantly increase scalability. E.g. plain pgbench on a smaller 2
    socket machine gains 1.07x for read-only pgbench, 1.22x for read-only pgbench
    when submitting queries in batches of 100, and 2.85x for batches of 100
    'SELECT';.  The latter numbers are obviously not to be expected in the
    real-world, but micro-benchmark the snapshot computation
    scalability (previously spending ~80% of the time in GetSnapshotData()).

 

Bottleneck 2: Density

 

Above I showed some simplified pseudo-code (real code) for snapshot computations. The start of the pseudo code:

 

xmin = global_xmin = inferred_maximum_possible;
for (i = 0; i < #connections; i++)
{
    int procno = shared_memory->connection_offsets[i];
    PGXACT *pgxact = shared_memory->all_connections[procno];

 

shows that accesses to PGXACT have to go through an indirection. That indirection allows to only look at the PGXACT of established connections, rather than also having to look at the connection slots for inactive connections.

 

Instead of having to go through an indirection, we can instead make the contents of PGXACT dense. That makes connection establishment/disconnections a tiny bit slower, now having to ensure not just that the connection_offsets array is dense, but also that the PGXACT contents are.

 

A second, and related, observation is that none of the remaining PGXACT members need to be accessed when ->xid is not valid (->xmin previously did need to be accessed). In many workloads most transactions do not write, and in most write heavy workloads, most transactions do not use savepoints.

 

typedef struct PGXACT
{
	TransactionId xid;
	uint8		vacuumFlags;
	bool		overflowed;
	uint8		nxids;
} PGXACT;

 

As a consequence, it is better not to make the entire PGXACT array dense, but instead to split its members into separate dense arrays. The array containing the xids of all established connections nearly always needs to be accessed6. But only if the connection has an assigned xid the other members need to be accessed.

 

By having a separate array for xids the CPU cache hit ratio can be increased, as most of the time the other fields are not accessed. Additionally, as the other fields change less frequently, keeping them separate allows them to be shared in an unmodified state between the cache domains (increasing access speed / decreasing bus traffic).

 

Theses changes are implemented in Postgres commits

  • 941697c3c1a snapshot scalability: Introduce dense array of in-progress xids
  • 5788e258bb2 snapshot scalability: Move PGXACT->vacuumFlags to ProcGlobal->vacuumFlags
  • 73487a60fc1 snapshot scalability: Move subxact info to ProcGlobal, remove PGXACT.

This yields quite a bit of benefit, as commented upon in one of the commit messages:

    On a larger 2 socket machine this and the two preceding commits result
    in a ~1.07x performance increase in read-only pgbench. For read-heavy
    mixed r/w workloads without row level contention, I see about 1.1x.


Bottleneck 3: Caching

 

Even with all the preceding changes, computing a snapshot with a lot of connections still is not cheap. While the changes improved the constant factor considerably, having to iterate through arrays with potentially a few thousand elements still is not cheap.

 

Now that GetSnapshotData() does not need to maintain RecentGlobalXmin anymore, a huge improvement on the table: We can avoid re-computing the snapshot if we can determine it has not changed. Previously that was not viable, as RecentGlobalXmin changes much more frequently than the snapshot contents themselves.

 

A snapshot only needs to change if a previously running transaction has committed (so its effect are visible): Because all transactions bigger-or-equal than ->xmax are treated as running, and because all transactions starting after snapshot has been computed are guaranteed to be assigned a transaction id larger then ->xmax, we need not care about newly started transactions.

 

Therefore a simple in-memory counter of the number of completed (i.e. committed or aborted) transactions can be used to invalidate snapshots. The completion counter is stored in the snapshot, and when asked to re-compute the snapshot contents, we just need to check if the snapshot’s snapXactCompletionCount is the same as the current in-memory value ShmemVariableCache->xactCompletionCount. If they are, the contents of the snapshot can be reused, otherwise the snapshot needs to be built from scratch.

 

This change was implemented in Postgres commit 623a9ba79bb: snapshot scalability: cache snapshots using a xact completion counter.

 

The commit message again describes the gains:

    On a smaller two socket machine this gains another ~1.03x, on a larger
    machine the effect is roughly double (earlier patch version tested
    though).

As the last sentence alludes to, currently we test for cache-ability holding a lock. It likely is possible to avoid that, but there are a few complexities that need to be addressed7.

 

Conclusion: One bottleneck down in PG 14, others in sight

 

The improvements presented here significantly improve Postgres' handling of large numbers of connections, particularly when—as is often the case—a large fraction are idle. This addresses the most pressing issue identified in my previous post on Analyzing the Limits of Connection Scalability in Postgres.

 

To be clear: These improvements do not address all connection scalability issues in Postgres. Nor are snapshot computations eliminated as a scalability factor. But I do think this project has improved the situation considerably.

 

For read-mostly workloads, snapshot computation is nearly entirely eliminated as an overhead—and even for read-write workloads the overhead is significantly reduced.

 

On a higher level, the changes outlined should allow applications to scale up more easily once using Postgres 14, without having to worry about hitting Postgres connection limits as much. Of course it still is important to pay some attention to not use too overly many connections—as outlined before there are other limitations one can hit.

 

From easy to hard: Opportunities for further improvements

 

There are plenty additional snapshot scalability improvements that could be made on top of these changes. Without moving to an entirely different snapshot representation, even.

  • As outlined above, the check whether a cached snapshot is still valid acquires a lock. It is very likely possible to remove that lock acquisition, and experiments show that to be a significant improvement.

  • Currently the snapshot caching is done for individual snapshot types, within each backend. It may be worthwhile to optimize it, so that each backend only has one cached snapshot. It also might be worthwhile to try to share the cached snapshot between backends, although the inter-process coordination that would require, makes that not too promising.

  • The snapshot computation is currently not very pipeline friendly. Initial experiments show that the computation could be made more efficient by re-arranging the computation to first assemble the set of running transactions, then check vacuumFlags and subtransaction counters in a second loop.

  • Looking further into the future, it may very well be worthwhile to maintain efficient “running transactions with xids” data structure, instead of the current “xids of all established connections” (commonly filled largely with invalid xids).


  1. Pgbench read-only results, pre/post changes:

    clients TPS pre TPS post
    1 28,842 28,728
    10 236,287 260,960
    20 472,479 486,659
    30 584,984 598,863
    40 678,770 693,314
    50 788,529 806,085
    60 1,031,483 986,730
    70 1,254,570 1,332,258
    80 1,341,188 1,438,881
    90 1,496,374 1,673,668
    100 1,538,186 1,651,516
    125 1,504,833 1,621,912
    150 1,428,711 1,570,070
    175 1,433,643 1,572,395
    200 1,404,691 1,523,175
    250 1,368,605 1,541,316
    300 1,315,812 1,490,701
    400 1,305,039 1,520,501
    500 1,390,359 1,639,884
    600 1,364,976 1,715,232
    700 1,323,205 1,716,550
    800 1,362,618 1,698,511
    900 1,324,593 1,705,670
    1000 1,273,755 1,722,917
    1500 1,246,604 1,651,516
    2000 1,171,879 1,680,384
    3000 1,074,248 1,651,516
    4000 1,001,631 1,683,714
    5000 732,530 1,589,232
    7500 674,862 1,669,350
    10000 642,042 1,656,006
    12500 541,565 1,612,269
    ↩︎
  2. 2x Xeon Gold 5215, 192GiB of RAM, Linux 5.8.5, Debian Sid ↩︎

  3. Idle Connections vs Active Connections, pre/post changes:

    Idle Connections Active Connections TPS pre TPS post
    0 1 33599 33406
    100 1 31088 33279
    1000 1 29377 33434
    2500 1 27050 33149
    5000 1 21895 33903
    10000 1 16034 33140
    0 48 1042005 1125104
    100 48 986731 1103584
    1000 48 854230 1119043
    2500 48 716624 1119353
    5000 48 553657 1119476
    10000 48 369845 1115740
    ↩︎
  4. Mostly Idle Connections vs Active Connections, pre/post changes:

    Mostly Idle Connections Active Connections TPS pre TPS post
    0 1 33837 34095
    100 1 30622 31166
    1000 1 25523 28829
    2500 1 19260 24978
    5000 1 11171 24208
    10000 1 6702 29577
    0 48 1022721 1133153
    100 48 980705 1034235
    1000 48 824668 1115965
    2500 48 698510 1073280
    5000 48 478535 1041931
    10000 48 276042 953567
    ↩︎
  5. Note that commit order is not always the right order for some higher isolation levels. But for the purpose of this post that is not relevant. ↩︎

  6. Except in case of the PGXACT for a backend running VACUUM or performing logical decoding, but that number usually will be small. ↩︎

  7. Without acquiring the lock it is not easily possible to ensure that the global xmin horizon cannot temporarily go backwards. That likely is OK, but requires a careful analysis. ↩︎