%3CLINGO-SUB%20id%3D%22lingo-sub-1757266%22%20slang%3D%22en-US%22%3EAnalyzing%20the%20Limits%20of%20Connection%20Scalability%20in%20Postgres%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1757266%22%20slang%3D%22en-US%22%3E%3CP%3EOne%20common%20challenge%20with%20Postgres%20for%20those%20of%20you%20who%20manage%20busy%20Postgres%20databases%2C%20and%20those%20of%20you%20who%20foresee%20being%20in%20that%20situation%2C%20is%20that%20Postgres%20does%20not%20handle%20large%20numbers%20of%20connections%20particularly%20well.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EWhile%20it%20is%20possible%20to%20have%20a%20few%20thousand%20established%20connections%20without%20running%20into%20problems%2C%20there%20are%20some%20real%20and%20hard-to-avoid%20problems.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESince%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fazure-database-for-postgresql%2Fmicrosoft-azure-welcomes-postgresql-committers%2Fba-p%2F1207612%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Ejoining%20Microsoft%3C%2FA%3E%20last%20year%20in%20the%20%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fservices%2Fpostgresql%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Database%20for%20PostgreSQL%3C%2FA%3E%20team%E2%80%94where%20I%20work%20on%20open%20source%20Postgres%E2%80%94I%20have%20spent%20a%20lot%20of%20time%20analyzing%20and%20addressing%20some%20of%20the%20issues%20with%20connection%20scalability%20in%20Postgres.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20post%20I%20will%20explain%20why%20I%20think%20it%20is%20important%20to%20improve%20Postgres'%20handling%20of%20large%20number%20of%20connections.%20Followed%20by%20an%20analysis%20of%20the%20different%20limiting%20aspects%20to%20connection%20scalability%20in%20Postgres.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20an%20upcoming%20post%20I%20will%20show%20the%20results%20of%20the%20work%20we%E2%80%99ve%20done%20to%20improve%20connection%20handling%20and%20snapshot%20scalability%20in%20Postgres%E2%80%94and%20go%20into%20detail%20about%20the%20identified%20issues%20and%20how%20we%20have%20addressed%20them%20in%20Postgres%2014.%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%23why-connection-scalability-in-postgres-is-important%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EWhy%20connection%20scalability%20in%20Postgres%20is%20important%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23surveying-connection-scalability-issues%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESurveying%20connection%20scalability%20issues%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23memory-usage%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMemory%20usage%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23constant-connection-overhead%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EConstant%20connection%20overhead%3C%2FA%3E%3COL%3E%0A%3CLI%3E%3CA%20href%3D%22%23conclusion-connection-memory-overhead-is-acceptable%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EConclusion%3A%20connection%20memory%20overhead%20is%20acceptable%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23cache-bloat%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ECache%20bloat%3C%2FA%3E%3COL%3E%0A%3CLI%3E%3CA%20href%3D%22%23problem-illustration%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EProblem%20illustration%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23conclusion-cache-bloat-is-not-the-major-issue-at-this-moment%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EConclusion%3A%20cache%20bloat%20is%20not%20the%20major%20issue%20at%20this%20moment%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23query-memory-usage%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EQuery%20memory%20usage%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23snapshot-scalability%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESnapshot%20scalability%3C%2FA%3E%3COL%3E%0A%3CLI%3E%3CA%20href%3D%22%23cause%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ECause%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23conclusion-snapshot-scalability-is-a-significant-limit%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EConclusion%3A%20Snapshot%20scalability%20is%20a%20significant%20limit%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23connection-model--context-switches%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EConnection%20model%20%26amp%3B%20context%20switches%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23conclusion-start-by-improving-snapshot-scalability-in-postgres%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EConclusion%3A%20Start%20by%20improving%20snapshot%20scalability%20in%20Postgres%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3C%2FNAV%3E%3C%2FDIV%3E%0A%3CH2%20id%3D%22why-connection-scalability-in-postgres-is-important%22%20id%3D%22toc-hId--1239863157%22%20id%3D%22toc-hId--1239863157%22%3EWhy%20connection%20scalability%20in%20Postgres%20is%20important%3C%2FH2%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%22Stylized-chart-of-Postgres-Connection-Scalability-Impact-of-Idle-Connections-1200x630.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F225060i5B43A1D4C0918895%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22Stylized-chart-of-Postgres-Connection-Scalability-Impact-of-Idle-Connections-1200x630.png%22%20alt%3D%22Stylized-chart-of-Postgres-Connection-Scalability-Impact-of-Idle-Connections-1200x630.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EIn%20some%20cases%20problems%20around%20connection%20scalability%20are%20caused%20by%20unfamiliarity%20with%20Postgres%2C%20broken%20applications%2C%20or%20other%20issues%20in%20the%20same%20vein.%20And%20as%20I%20already%20mentioned%2C%20some%20applications%20can%20have%20a%20few%20thousand%20established%20connections%20without%20running%20into%20any%20problems.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20frequent%20counter-claim%20to%20requests%20to%20improve%20Postgres'%20handling%20of%20large%20numbers%20of%20connection%20counts%20is%20that%20there%20is%20nothing%20to%20address.%20That%20the%20desire%2Fneed%20to%20handle%20large%20numbers%20of%20connection%20is%20misguided%2C%20caused%20by%20broken%20applications%20or%20similar.%20Often%20accompanied%20by%20references%20to%20the%20server%20only%20having%20a%20limited%20number%20of%20CPU%20cores.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20certainly%20are%20cases%20where%20the%20best%20approach%20is%20to%20avoid%20large%20numbers%20of%20connections%2C%20but%20there%20are%E2%80%94in%20my%20opinion%E2%80%94pretty%20clear%20reasons%20for%20needing%20larger%20number%20of%20connections%20in%20Postgres.%20Here%20are%20the%20main%20ones%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CP%3E%3CSTRONG%3ECentral%20state%20and%20spikey%20load%20require%20large%20numbers%20of%20connections%3C%2FSTRONG%3E%3A%20It%20is%20common%20for%20a%20database%20to%20be%20%3CSTRONG%3Ethe%3C%2FSTRONG%3E%20shared%20state%20for%20an%20application%20(leaving%20non-durable%20caching%20services%20aside).%20Given%20the%20cost%20of%20establishing%20a%20new%20database%20connection%20(TLS%2C%20latency%2C%20and%20Postgres%20costs%2C%20in%20that%20order)%20it%20is%20obvious%20that%20applications%20need%20to%20maintain%20pools%20of%20Postgres%20connections%20that%20are%20large%20enough%20to%20handle%20the%20inevitable%20minor%20spikes%20in%20incoming%20requests.%20Often%20there%20are%20many%20servers%20running%20%5Bweb-%5Dapplication%20code%20using%20one%20centralized%20database.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20some%20degree%20this%20issue%20can%20be%20addressed%20using%20Postgres%20connection%20poolers%20like%20%3CA%20href%3D%22https%3A%2F%2Fwww.pgbouncer.org%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EPgBouncer%3C%2FA%3E%20or%20more%20recently%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fyandex%2Fodyssey%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EOdyssey%3C%2FA%3E.%20To%20actually%20reduce%20the%20number%20of%20connections%20to%20the%20database%20server%20such%20poolers%20need%20to%20be%20used%20in%20transaction%20(or%20statement)%20%3CA%20href%3D%22https%3A%2F%2Fwww.pgbouncer.org%2Ffeatures.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Epooling%20modes%3C%2FA%3E.%20However%2C%20doing%20so%20precludes%20the%20use%20of%20many%20useful%20database%20features%20like%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fyandex%2Fodyssey%2Fissues%2F16%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eprepared%20statements%3C%2FA%3E%2C%20temporary%20tables%2C%20%E2%80%A6%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3E%3CSTRONG%3ELatency%20and%20result%20processing%20times%20lead%20to%20idle%20connections%3C%2FSTRONG%3E%3A%20Network%20latency%20and%20application%20processing%20times%20will%20often%20result%20in%20individual%20database%20connections%20being%20idle%20the%20majority%20of%20the%20time%2C%20even%20when%20the%20applications%20are%20issuing%20database%20requests%20as%20fast%20as%20they%20can.%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3ECommon%20OLTP%20database%20workloads%2C%20and%20especially%20web%20applications%2C%20are%20heavily%20biased%20towards%20reads.%20And%20with%20OLTP%20workloads%2C%20the%20majority%20of%20SQL%20queries%20are%20simple%20enough%20to%20be%20processed%20well%20below%20the%20network%20latency%20between%20application%20and%20database.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EAdditionally%20the%20application%20needs%20to%20process%20the%20results%20of%20the%20database%20queries%20it%20sent.%20That%20often%20will%20involve%20substantial%20work%20(e.g.%20template%20processing%2C%20communication%20with%20cache%20servers%2C%20%E2%80%A6).%3C%2FP%3E%0A%3CP%3E%3CBR%20%2F%3ETo%20drive%20this%20home%2C%20here%20is%20a%20simple%20experiment%20using%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2F13%2Fpgbench.htm%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Epgbench%3C%2FA%3E%20(a%20simple%20benchmarking%20program%20that%20is%20part%20of%20Postgres).%20In%20a%20memory-resident%2C%20read-only%20pgbench%20workload%20(executed%20on%20my%20workstation%20%3CSUP%20id%3D%22fnref%3A1%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%2C%2020%2F40%20CPU%20cores%2Fthreads)%20I%20am%20comparing%20the%20achievable%20throughput%20across%20increasing%20client%20counts%20between%20a%20non-delayed%20pgbench%20and%20a%20pgbench%20with%20simulated%20delays.%20For%20the%20simulated%20delays%2C%20I%20used%20a%201ms%20network%20delay%20and%20a%201ms%20processing%20delay.%20The%20non-delayed%20pgbench%20peaks%20around%2048%20clients%2C%20the%20delayed%20run%20around%203000%20connections.%20Even%20comparing%20on-machine%20TCP%20connections%20to%20a%2010GBe%20between%20two%20physically%20close%20machines%20moves%20the%20peak%20from%20around%2048%20connections%20closer%20to%20500%20connections.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3E%3CSTRONG%3EScaling%20out%20to%20allow%20for%20higher%20connection%20counts%20can%20increase%20cost%3C%2FSTRONG%3E%3A%20Even%20in%20cases%20where%20the%20application%E2%80%99s%20workload%20can%20be%20distributed%20over%20a%20number%20of%20Postgres%20instances%2C%20the%20impact%20of%20latency%20combined%20with%20low%20maximum%20connection%20limits%20will%20often%20result%20in%20low%20utilization%20of%20the%20database%20servers%2C%20while%20exerting%20pressure%20to%20increase%20the%20number%20of%20database%20servers%20to%20handle%20the%20required%20number%20of%20connections.%20That%20can%20increase%20the%20operational%20costs%20substantially.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22surveying-connection-scalability-issues%22%20id%3D%22toc-hId-1247649676%22%20id%3D%22toc-hId-1247649676%22%3ESurveying%20connection%20scalability%20issues%3C%2FH2%3E%0A%3CP%3EMy%20goal%20in%20starting%20this%20project%20was%20to%20improve%20Postgres'%20ability%20to%20handle%20substantially%20larger%20numbers%20of%20connections.%20To%20do%20that%E2%80%94to%20pick%20the%20right%20problem%20to%20solve%E2%80%94%20I%20first%20needed%20to%20understand%20which%20problems%20were%20most%20important%2C%20otherwise%20it%20would%20have%20been%20easy%20to%20end%20up%20with%20micro-optimizations%20without%20improving%20real-world%20workloads.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESo%20my%20first%20software%20engineering%20task%20was%20to%20survey%20the%20different%20aspects%20of%20connection%20scalability%20limitations%20in%20Postgres%2C%20specifically%3A%3C%2FP%3E%0A%3COL%3E%0A%3CLI%3E%3CA%20href%3D%22%23memory-usage%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EMemory%20usage%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23snapshot-scalability%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ESnapshot%20scalability%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23connection-model--context-switches%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EConnection%20model%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CP%3EBy%20the%20end%20of%20this%20deep%20dive%20into%20the%20connection%20scalability%20limitations%20in%20Postgres%2C%20I%20hope%20you%20will%20understand%20why%20I%20%3CA%20href%3D%22%23conclusion-start-by-improving-snapshot-scalability-in-postgres%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Econcluded%3C%2FA%3E%20that%20snapshot%20scalability%20should%20be%20addressed%20first.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CH2%20id%3D%22memory-usage%22%20id%3D%22toc-hId--559804787%22%20id%3D%22toc-hId--559804787%22%3EMemory%20usage%3C%2FH2%3E%0A%3CP%3EThere%20are%203%20main%20aspects%20to%20problems%20around%20memory%20usage%20of%20a%20large%20numbers%20of%20connections%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22%23constant-connection-overhead%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EConstant%20connection%20overhead%3C%2FA%3E%2C%20the%20amount%20of%20memory%20an%20established%20connection%20uses%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23cache-bloat%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3ECache%20bloat%3C%2FA%3E%2C%20the%20increase%20in%20memory%20usage%20due%20to%20large%20numbers%20of%20database%20objects%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22%23query-memory-usage%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EQuery%20memory%20usage%3C%2FA%3E%2C%20memory%20used%20by%20query%20execution%20itself%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22constant-connection-overhead%22%20id%3D%22toc-hId-1927708046%22%20id%3D%22toc-hId-1927708046%22%3EConstant%20connection%20overhead%3C%2FH2%3E%0A%3CP%3EPostgres%2C%20as%20many%20of%20you%20will%20know%2C%20uses%20a%20process-based%20connection%20model.%20When%20a%20new%20connection%20is%20established%2C%20Postgres'%20supervisor%20process%20creates%20a%20dedicated%20process%20to%20handle%20that%20connection%20going%20forward.%20The%20use%20of%20a%20%E2%80%9Cfull%20blown%20process%E2%80%9D%20over%20the%20use%20of%20of%20threads%20has%20some%20advantages%20like%20increased%20isolation%20%2F%20robustness%2C%20but%20also%20some%20disadvantages.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOne%20common%20complaint%20is%20that%20each%20connection%20uses%20too%20much%20memory.%20That%20is%2C%20at%20least%20partially%2C%20a%20common%20observation%20because%20it%20is%20surprisingly%20hard%20to%20measure%20the%20increase%20in%20memory%20usage%20by%20an%20additional%20connection.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20a%20recent%20post%20about%20measuring%20the%20%3CA%20href%3D%22https%3A%2F%2Fblog.anarazel.de%2F2020%2F10%2F07%2Fmeasuring-the-memory-overhead-of-a-postgres-connection%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ememory%20overhead%20of%20a%20Postgres%20connection%3C%2FA%3E%20I%20show%20that%20it%20is%20surprisingly%20hard%20to%20accurately%20measure%20the%20memory%20overhead.%20And%20that%20in%20many%20workloads%2C%20and%20with%20the%20right%20configuration%E2%80%94most%20importantly%2C%20using%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fcurrent%2Fruntime-config-resource.html%23GUC-SHARED-BUFFERS%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehuge_pages%3C%2FA%3E%20%E2%80%94the%20memory%20overhead%20of%20each%20connection%20is%20%3CA%20href%3D%22https%3A%2F%2Fblog.anarazel.de%2F2020%2F10%2F07%2Fmeasuring-the-memory-overhead-of-a-postgres-connection%23conclusion%2522%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ebelow%202%20MiB%3C%2FA%3E.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CH3%20id%3D%22conclusion-connection-memory-overhead-is-acceptable%22%20id%3D%22toc-hId--1676697776%22%20id%3D%22toc-hId--1676697776%22%3EConclusion%3A%20connection%20memory%20overhead%20is%20acceptable%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhen%20each%20connection%20only%20has%20an%20overhead%20of%20a%20few%20MiB%2C%20it%20is%20quite%20possible%20to%20have%20thousands%20of%20established%20connections.%20It%20would%20obviously%20be%20good%20to%20use%20less%20memory%2C%20but%20memory%20is%20not%20the%20primary%20issue%20around%20connection%20scalability.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CH2%20id%3D%22cache-bloat%22%20id%3D%22toc-hId--1687200880%22%20id%3D%22toc-hId--1687200880%22%3ECache%20bloat%3C%2FH2%3E%0A%3CP%3EAnother%20important%20aspect%20of%20memory-related%20connection%20scalability%20issues%20can%20be%20that%2C%20over%20time%2C%20the%20memory%20usage%20of%20a%20connection%20increases%2C%20due%20to%20long-lived%20resources.%20This%20particularly%20is%20an%20issue%20in%20workloads%20that%20utilize%20long-lived%20connections%20combined%20with%20schema-based%20multi-tenancy.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EUnless%20applications%20implement%20some%20form%20of%20connection%20%26lt%3B-%26gt%3B%20tenant%20association%2C%20each%20connection%20over%20time%20will%20access%20all%20relations%20for%20all%20tenants.%20That%20leads%20to%20Postgres'%20internal%20catalog%20metadata%20caches%20growing%20beyond%20a%20reasonable%20size%2C%20as%20currently%20(as%20of%20version%2013)%20Postgres%20does%20not%20prune%20its%20metadata%20caches%20of%20unchanging%20rarely-accessed%20contents.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22problem-illustration%22%20id%3D%22toc-hId--996639406%22%20id%3D%22toc-hId--996639406%22%3EProblem%20illustration%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20demonstrate%20the%20issue%20of%20cache%20bloat%2C%20I%20created%20a%20simple%20test%20bed%20with%20100k%20tables%2C%20with%20a%20few%20columns%20and%20single%20primary%20serial%20column%20index%20%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.%20Takes%20a%20while%20to%20create.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20the%20%3CA%20href%3D%22https%3A%2F%2Fgit.postgresql.org%2Fgitweb%2F%3Fp%3Dpostgresql.git%3Ba%3Dcommit%3Bh%3D3e98c0bafb28de87ae095b341687dc082371af54%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Erecently%20added%3C%2FA%3E%3CCODE%20style%3D%22color%3A%20black%3B%22%3Epg_backend_memory_contexts%3C%2FCODE%3Eview%20it%20is%20not%20too%20difficult%20to%20see%20the%20aggregated%20memory%20usage%20of%20the%20various%20caches%20(although%20it%20would%20be%20nice%20to%20see%20more%20of%20the%20different%20types%20of%20caches%20broken%20out%20into%20their%20own%20memory%20contexts).%20See%20%3CSUP%20id%3D%22fnref%3A3%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.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20a%20new%20Postgres%20connection%2C%20not%20much%20memory%20is%20used%3A%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTH%3Ename%3C%2FTH%3E%0A%3CTH%3Eparent%3C%2FTH%3E%0A%3CTH%3Esize_bytes%3C%2FTH%3E%0A%3CTH%3Esize_human%3C%2FTH%3E%0A%3CTH%3Enum_contexts%3C%2FTH%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3ECacheMemoryContext%3C%2FTD%3E%0A%3CTD%3ETopMemoryContext%3C%2FTD%3E%0A%3CTD%3E524288%3C%2FTD%3E%0A%3CTD%3E512%20kB%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3Eindex%20info%3C%2FTD%3E%0A%3CTD%3ECacheMemoryContext%3C%2FTD%3E%0A%3CTD%3E149504%3C%2FTD%3E%0A%3CTD%3E146%20kB%3C%2FTD%3E%0A%3CTD%3E80%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3Erelation%20rules%3C%2FTD%3E%0A%3CTD%3ECacheMemoryContext%3C%2FTD%3E%0A%3CTD%3E8192%3C%2FTD%3E%0A%3CTD%3E8192%20bytes%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20after%20forcing%20all%20Postgres%20tables%20we%20just%20created%20to%20be%20accessed%20%3CSUP%20id%3D%22fnref%3A4%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%2C%20this%20looks%20very%20different%3A%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTH%3Ename%3C%2FTH%3E%0A%3CTH%3Eparent%3C%2FTH%3E%0A%3CTH%3Esize_bytes%3C%2FTH%3E%0A%3CTH%3Esize_human%3C%2FTH%3E%0A%3CTH%3Enum_contexts%3C%2FTH%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3ECacheMemoryContext%3C%2FTD%3E%0A%3CTD%3ETopMemoryContext%3C%2FTD%3E%0A%3CTD%3E621805848%3C%2FTD%3E%0A%3CTD%3E593%20MB%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3Eindex%20info%3C%2FTD%3E%0A%3CTD%3ECacheMemoryContext%3C%2FTD%3E%0A%3CTD%3E102560768%3C%2FTD%3E%0A%3CTD%3E98%20MB%3C%2FTD%3E%0A%3CTD%3E100084%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3Erelation%20rules%3C%2FTD%3E%0A%3CTD%3ECacheMemoryContext%3C%2FTD%3E%0A%3CTD%3E8192%3C%2FTD%3E%0A%3CTD%3E8192%20bytes%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20the%20metadata%20cache%20for%20indexes%20is%20created%20in%20its%20own%20memory%20context%2C%20num_contexts%20for%20the%20%E2%80%9Cindex%20info%E2%80%9D%20contexts%20nicely%20shows%20that%20we%20accessed%20the%20100k%20tables%20(and%20some%20system%20internal%20ones).%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CH3%20id%3D%22conclusion-cache-bloat-is-not-the-major-issue-at-this-moment%22%20id%3D%22toc-hId-1490873427%22%20id%3D%22toc-hId-1490873427%22%3EConclusion%3A%20cache%20bloat%20is%20not%20the%20major%20issue%20at%20this%20moment%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20common%20solution%20for%20the%20cache%20bloat%20issue%20is%20to%20drop%20%E2%80%9Cold%E2%80%9D%20connections%20from%20the%20application%20connection%20pooler%20after%20a%20certain%20age.%20Many%20connection%20pooler%20libraries%20%2F%20web%20frameworks%20support%20that.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20there%20is%20a%20feasible%20workaround%2C%20and%20as%20cache%20bloat%20is%20only%20an%20issue%20for%20databases%20with%20a%20%3CEM%3Elot%3C%2FEM%3E%20of%20objects%2C%20cache%20bloat%20is%20not%20%3CEM%3Ethe%3C%2FEM%3E%20major%20issue%20at%20the%20moment%20(but%20worthy%20of%20improvement%2C%20obviously).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22query-memory-usage%22%20id%3D%22toc-hId-1480370323%22%20id%3D%22toc-hId-1480370323%22%3EQuery%20memory%20usage%3C%2FH2%3E%0A%3CP%3EThe%20third%20aspect%20is%20that%20it%20is%20hard%20to%20limit%20memory%20used%20by%20queries.%20The%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fcurrent%2Fruntime-config-resource.html%23GUC-WORK-MEM%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ework_mem%3C%2FA%3E%20setting%20does%20not%20control%20the%20memory%20used%20by%20a%20query%20as%20a%20whole%2C%20but%20only%20of%20individual%20parts%20of%20a%20query%20(e.g.%20sort%2C%20hash%20aggregation%2C%20hash%20join).%20That%20means%20that%20a%20query%20can%20end%20up%20requiring%3CCODE%20style%3D%22color%3A%20black%3B%22%3Ework_mem%3C%2FCODE%3Eseveral%20times%20over%20%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.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThat%20means%20that%20one%20has%20to%20be%20careful%20setting%3CCODE%20style%3D%22color%3A%20black%3B%22%3Ework_mem%3C%2FCODE%3Ein%20workloads%20requiring%20a%20lot%20of%20connections.%20With%20larger%3CCODE%20style%3D%22color%3A%20black%3B%22%3Ework_mem%3C%2FCODE%3Esettings%2C%20practically%20required%20for%20analytics%20workloads%2C%20one%20can%E2%80%99t%20reasonably%20use%20a%20huge%20number%20of%20concurrent%20connections%20and%20expect%20to%20never%20hit%20memory%20exhaustion%20related%20issues%20(i.e.%20errors%20or%20the%20OOM%20killer).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELuckily%20most%20workloads%20requiring%20a%20lot%20of%20connection%20don%E2%80%99t%20need%20a%20high%3CCODE%20style%3D%22color%3A%20black%3B%22%3Ework_mem%3C%2FCODE%3Esetting%2C%20and%20it%20can%20be%20set%20on%20the%20user%2C%20database%2C%20connection%2C%20and%20transaction%20level.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22snapshot-scalability%22%20id%3D%22toc-hId--327084140%22%20id%3D%22toc-hId--327084140%22%3ESnapshot%20scalability%3C%2FH2%3E%0A%3CP%3EThere%20are%20a%20lot%20of%20recommendations%20out%20there%20strongly%20recommending%20to%20not%20set%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fdocs%2Fcurrent%2Fruntime-config-connection.html%23GUC-MAX-CONNECTIONS%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Emax_connections%3C%2FA%3E%20for%20Postgres%20to%20a%20high%20value%2C%20as%20high%20values%20can%20cause%20problems.%20In%20fact%2C%20I%E2%80%99ve%20argued%20that%20myself%20many%20times.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20that%20is%20only%20half%20the%20truth.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESetting%20max_connections%20to%20a%20very%20high%20value%20alone%20only%20leads%20at%20best%20(worst%3F)%20to%20a%20very%20small%20slowdown%20in%20itself%2C%20and%20wastes%20some%20memory.%20E.g.%20on%20my%20workstation%20%3CSUP%20id%3D%22fnref%3A1%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%20there%20is%20no%20measurable%20performance%20difference%20for%20a%20read-only%20pgbench%20between%3CCODE%20style%3D%22color%3A%20black%3B%22%3Emax_connections%3D100%3C%2FCODE%3Eand%20a%20value%20as%20extreme%3CCODE%20style%3D%22color%3A%20black%3B%22%3Emax_connections%3D100000%3C%2FCODE%3E(for%20the%20same%20pgbench%20client%20count%2C%2048%20in%20this%20case).%20However%20the%20memory%20required%20for%20Postgres%20does%20increase%20measurable%20with%20such%20an%20extreme%20setting.%20With%3CCODE%20style%3D%22color%3A%20black%3B%22%3Eshared_buffers%3D16GB%3C%2FCODE%3E%20%3CCODE%20style%3D%22color%3A%20black%3B%22%3Emax_connections%3D100%3C%2FCODE%3Euses%2016804%20MiB%2C%3CCODE%20style%3D%22color%3A%20black%3B%22%3Emax_connections%3D100000%3C%2FCODE%3Euses%2021463%20MiB%20of%20shared%20memory.%20That%20is%20a%20large%20enough%20difference%20to%20potentially%20cause%20a%20slowdown%20indirectly%20(although%20most%20of%20that%20memory%20will%20never%20be%20used%2C%20therefore%20not%20allocated%20by%20the%20OS%20in%20common%20configurations).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20real%20issue%20is%20that%20currently%20Postgres%20does%20not%20scale%20well%20to%20having%20a%20large%20number%20of%20established%20connections%2C%20even%20if%20nearly%20all%20connections%20are%20idle.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20showcase%20this%2C%20I%20used%20two%20separate%20pgbench%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%20runs.%20One%20of%20them%20just%20establishes%20connections%20that%20are%20entirely%20idle%20(using%20a%20test%20file%20that%20just%20contains%3CCODE%20style%3D%22color%3A%20black%3B%22%3E%5Csleep%201s%3C%2FCODE%3E%2C%20causing%20a%20client-side%20sleep).%20Another%20to%20run%20a%20normal%20pgbench%20read-only%20workload.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20far%20from%20reproducing%20the%20worst%20possible%20version%20of%20the%20issue%2C%20as%20normally%20the%20set%20of%20idle%20connections%20varies%20over%20time%2C%20which%20makes%20this%20issue%20considerably%20worse.%20This%20version%20is%20much%20easier%20to%20reproduce%20however.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20a%20very%20useful%20scenario%20to%20test%2C%20because%20it%20allows%20us%20to%20isolate%20the%20cost%20of%20additional%20connections%20pretty%20well%20isolated.%20Especially%20when%20the%20count%20of%20active%20connections%20is%20low%2C%20the%20system%20CPU%20usage%20is%20quite%20low.%20If%20there%20is%20a%20slowdown%20when%20the%20number%20of%20idle%20connections%20increases%2C%20it%20is%20clearly%20related%20to%20the%20number%20of%20idle%20connections.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20we%20instead%20measured%20the%20throughput%20with%20a%20high%20number%20of%20active%20connections%2C%20it%E2%80%99d%20be%20harder%20to%20pinpoint%20whether%20e.g.%20the%20increase%20in%20context%20switches%20or%20lack%20of%20CPU%20cycles%20is%20to%20blame%20for%20slowdowns.%3C%2FP%3E%0A%3CFIGURE%3E%3C%2FFIGURE%3E%0A%3CFIGURE%3E%0A%3CFIGCAPTION%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22chart-performance-impact-of-idle-connections-in-Postgres-1active.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F225157iF57E3FAE756489AA%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22chart-performance-impact-of-idle-connections-in-Postgres-1active.png%22%20alt%3D%22Throughput%20of%20one%20active%20connection%20in%20presence%20of%20a%20variable%20number%20of%20idle%20connections%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EThroughput%20of%20one%20active%20connection%20in%20presence%20of%20a%20variable%20number%20of%20idle%20connections%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22chart-performance-impact-of-idle-connections-in-Postgres-48active.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F225158iB69A6DB5409E3409%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22chart-performance-impact-of-idle-connections-in-Postgres-48active.png%22%20alt%3D%22Throughput%20of%2048%20active%20connections%20in%20presence%20of%20a%20variable%20number%20of%20idle%20connections%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EThroughput%20of%2048%20active%20connections%20in%20presence%20of%20a%20variable%20number%20of%20idle%20connections%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FFIGCAPTION%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3C%2FFIGURE%3E%0A%3CP%3EThese%20results%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%20clearly%20show%20that%20the%20achievable%20throughput%20of%20active%20connections%20decreases%20significantly%20when%20the%20number%20of%20idle%20connections%20increases.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20reality%20%E2%80%9Cidle%E2%80%9D%20connections%20are%20not%20entirely%20idle%2C%20but%20send%20queries%20at%20a%20lower%20rate.%20To%20simulate%20that%20I%E2%80%99ve%20used%20the%20the%20below%20to%20simulate%20clients%20only%20occasionally%20sending%20queries%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-sql%22%20data-lang%3D%22sql%22%3E%3CSPAN%3E%5C%3C%2FSPAN%3Esleep%20%3CSPAN%3E100%3C%2FSPAN%3Ems%0A%3CSPAN%3ESELECT%3C%2FSPAN%3E%20%3CSPAN%3E1%3C%2FSPAN%3E%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CFIGURE%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22chart-performance-impact-of-mostly-idle-connections-in-Postgres-1active.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F225159iD9633BC3719A34AF%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22chart-performance-impact-of-mostly-idle-connections-in-Postgres-1active.png%22%20alt%3D%22Throughput%20of%20one%20active%20connection%20in%20presence%20of%20a%20variable%20number%20of%20mostly%20idle%20connections%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EThroughput%20of%20one%20active%20connection%20in%20presence%20of%20a%20variable%20number%20of%20mostly%20idle%20connections%3C%2FSPAN%3E%3C%2FSPAN%3E%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%22chart-performance-impact-of-mostly-idle-connections-in-Postgres-48active.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F225160iCCE8F183AF4A24B9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22chart-performance-impact-of-mostly-idle-connections-in-Postgres-48active.png%22%20alt%3D%22Throughput%20of%2048%20active%20connections%20in%20presence%20of%20a%20variable%20number%20of%20mostly%20idle%20connections%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EThroughput%20of%2048%20active%20connections%20in%20presence%20of%20a%20variable%20number%20of%20mostly%20idle%20connections%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CFIGCAPTION%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%20class%3D%22lia-align-left%22%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EThe%20results%3C%2FSPAN%3E%3CSUP%20id%3D%22fnref%3A8%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%3A8%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E8%3C%2FA%3E%3C%2FSUP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3E%20show%20that%20the%20slightly%20more%20realistic%20scenario%20causes%20active%20connections%20to%20slow%20down%20even%20worse.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3C%2FFIGCAPTION%3E%0A%3C%2FFIGURE%3E%0A%3CH3%20id%3D%22cause%22%20id%3D%22toc-hId--331686145%22%20id%3D%22toc-hId--331686145%22%3ECause%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETogether%20these%20results%20very%20clearly%20show%20that%20there%20is%20a%20significant%20issue%20handling%20large%20connection%20counts%2C%20even%20when%20CPU%2Fmemory%20are%20plentiful.%20The%20fact%20that%20a%20single%20active%20connection%20slows%20down%20by%20more%20than%202x%20due%20to%20concurrent%20idle%20connections%20points%20to%20a%20very%20clear%20issue.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20CPU%20profile%20quickly%20pinpoints%20the%20part%20of%20Postgres%20responsible%3A%3C%2FP%3E%0A%3CFIGURE%3E%0A%3CDIV%20id%3D%22tinyMceEditorandresfreund_4%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-center%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%2F225072i5636AFF3158CFCD4%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%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%20GetSnapshotData()%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%20GetSnapshotData()%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CFIGCAPTION%3E%3C%2FFIGCAPTION%3E%0A%3C%2FFIGURE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EObviously%20the%20bottleneck%20is%20entirely%20in%20the%3CCODE%20style%3D%22color%3A%20black%3B%22%3EGetSnapshotData()%3C%2FCODE%3Efunction.%20That%20function%20performs%20the%20bulk%20of%20the%20work%20necessary%20to%20provide%20readers%20with%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.%3CCODE%20style%3D%22color%3A%20black%3B%22%3EGetSnapshotData()%3C%2FCODE%3Ebuilds%20so%20called%20%E2%80%9Csnapshots%E2%80%9D%20that%20describe%20which%20effects%20of%20concurrent%20transactions%20are%20visible%20to%20a%20transaction%2C%20and%20which%20are%20not.%20These%20snapshots%20are%20built%20very%20frequently%20(at%20least%20once%20per%20transaction%2C%20%3CEM%3Every%3C%2FEM%3E%20commonly%20more%20often).%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EEven%20without%20knowing%20its%20implementation%2C%20it%20does%20make%20some%20intuitive%20sense%20(at%20least%20I%20think%20so%2C%20but%20I%20also%20know%20what%20it%20does)%20that%20such%20a%20task%20gets%20more%20expensive%20the%20more%20connections%20%2F%20transactions%20need%20to%20be%20handled.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3ETwo%20blog%20posts%20by%20Brandur%20explain%20the%20mechanics%20and%20issues%20surrounding%20this%20in%20more%20detail%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%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%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fbrandur.org%2Fpostgres-connections%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EHow%20to%20Manage%20Connections%20Efficiently%20in%20Postgres%2C%20or%20Any%20Database%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH3%20id%3D%22conclusion-snapshot-scalability-is-a-significant-limit%22%20id%3D%22toc-hId--2139140608%22%20id%3D%22toc-hId--2139140608%22%3EConclusion%3A%20Snapshot%20scalability%20is%20a%20significant%20limit%3C%2FH3%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EA%20large%20number%20of%20connections%20clearly%20reduce%20the%20efficiency%20of%20other%20connections%2C%20even%20when%20idle%20(which%20as%20explained%20above%2C%20is%20very%20common).%20Except%20for%20reducing%20the%20number%20of%20concurrent%20connections%20and%20issuing%20fewer%20queries%2C%20there%20is%20no%20real%20workaround%20for%20the%20snapshot%20scalability%20issue.%3C%2FP%3E%0A%3CHR%20%2F%3E%0A%3CH2%20id%3D%22connection-model--context-switches%22%20id%3D%22toc-hId-219289506%22%20id%3D%22toc-hId-219289506%22%3EConnection%20model%20%26amp%3B%20context%20switches%3C%2FH2%3E%0A%3CP%3EAs%20mentioned%20%3CA%20href%3D%22%23constant-connection-overhead%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eabove%3C%2FA%3E%2C%20Postgres%20uses%20a%20one-process-per-connection%20model.%20That%20works%20well%20in%20a%20lot%20of%20cases%2C%20but%20is%20a%20limiting%20factor%20for%20dealing%20with%2010s%20to%20100s%20of%20thousands%20of%20connections.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhenever%20a%20query%20is%20received%20by%20a%20backend%20process%2C%20the%20kernel%20needs%20to%20perform%20a%20context%20switch%20to%20that%20process.%20That%20is%20not%20cheap.%20But%20more%20importantly%2C%20once%20the%20result%20for%20the%20query%20has%20been%20computed%2C%20the%20backend%20will%20commonly%20be%20idle%20for%20a%20while%E2%80%94the%20query%20result%20has%20to%20traverse%20the%20network%2C%20be%20received%20and%20processed%20by%20the%20application%2C%20before%20the%20application%20sends%20a%20new%20query.%20That%20means%20on%20a%20busy%20server%20another%20process%2Fbackend%2Fconnection%20will%20need%20to%20be%20scheduled%E2%80%94another%20context%20switch%20(cross-process%20context%20switches%20are%20more%20expensive%20than%20doing%20process-kernel-same%20process%2C%20e.g.%20as%20part%20of%20a%20syscall).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENote%20that%20switching%20to%20a%20one-thread-per-connection%20model%20does%20not%20address%20this%20issue%20to%20a%20meaningful%20degree%3A%20while%20some%20of%20the%20context%20switches%20may%20get%20cheaper%2C%20context%20switches%20still%20are%20the%20major%20limit.%20There%20%3CEM%3Eare%3C%2FEM%3E%20reasons%20to%20consider%20switching%20to%20threads%2C%20but%20connection%20scalability%20itself%20is%20not%20a%20major%20one%20(without%20additional%20architectural%20changes%2C%20some%20of%20which%20may%20be%20easier%20using%20threads).%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20handle%20huge%20numbers%20of%20connections%20a%20different%20type%20of%20connection%20model%20is%20needed.%20Instead%20of%20using%20a%20process%2Fthread-per-connection%20model%2C%20a%20fixed%2Flimited%20number%20of%20processes%2Fthreads%20need%20to%20handle%20all%20connections.%20By%20waiting%20for%20incoming%20queries%20on%20many%20connections%20at%20once%20and%20then%20processing%20many%20queries%20without%20being%20interrupted%20by%20the%20OS%20CPU%20scheduler%2C%20efficiency%20can%20very%20significantly%20be%20improved.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20is%20not%20a%20brilliant%20insight%20by%20me.%20Architectures%20like%20this%20are%20in%20wide%20use%2C%20and%20have%20widely%20been%20discussed.%20See%20e.g.%20the%20%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FC10k_problem%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EC10k%20problem%3C%2FA%3E%2C%20coined%20in%201999.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBesides%20avoiding%20context%20switches%2C%20there%20are%20many%20other%20performance%20benefits%20that%20can%20be%20gained.%20E.g.%20on%20higher%20core%20count%20machines%2C%20a%20lot%20of%20performance%20can%20be%20gained%20by%20increasing%20locality%20of%20shared%20memory%2C%20e.g.%20by%20binding%20specific%20processes%2Fthreads%20and%20regions%20of%20memory%20to%20specific%20CPU%20cores.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHowever%2C%20changing%20Postgres%20to%20support%20a%20different%20kind%20of%20connection%20model%20like%20this%20is%20a%20huge%20undertaking.%20That%20does%20not%20just%20require%20carefully%20separating%20many%20dependencies%20between%20processes%20and%20connections%2C%20but%20also%20user-land%20scheduling%20between%20different%20queries%2C%20support%20for%20asynchronous%20IO%2C%20likely%20a%20different%20query%20execution%20model%20(to%20avoid%20needing%20a%20separate%20stack%20for%20each%20query)%2C%20and%20much%20more.%3C%2FP%3E%0A%3CHR%20%2F%3E%0A%3CH2%20id%3D%22conclusion-start-by-improving-snapshot-scalability-in-postgres%22%20id%3D%22toc-hId--1588164957%22%20id%3D%22toc-hId--1588164957%22%3EConclusion%3A%20Start%20by%20improving%20snapshot%20scalability%20in%20Postgres%3C%2FH2%3E%0A%3CP%3EIn%20my%20opinion%2C%20the%20memory%20usage%20issues%20are%20not%20as%20severe%20as%20the%20other%20issues%20discussed.%20Partially%20because%20the%20memory%20overhead%20of%20connections%20is%20less%20big%20than%20it%20initially%20appears%2C%20and%20partially%20because%20issues%20like%20Postgres'%20caches%20using%20too%20much%20memory%20can%20be%20worked%20around%20reasonably.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWe%20could%2C%20and%20%3CSTRONG%3Eshould%3C%2FSTRONG%3E%2C%20make%20improvements%20around%20memory%20usage%20in%20Postgres%2C%20and%20there%20are%20several%20low%20enough%20hanging%20fruits.%20But%20I%20don%E2%80%99t%20think%2C%20as%20things%20currently%20are%2C%20that%20improving%20memory%20usage%20would%2C%20on%20its%20own%2C%20change%20the%20picture%20around%20connection%20scalability%2C%20at%20least%20not%20on%20a%20fundamental%20level.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20contrast%2C%20there%20is%20no%20good%20way%20to%20work%20around%20the%20snapshot%20scalability%20issues.%20Reducing%20the%20number%20of%20established%20connections%20significantly%20is%20often%20not%20feasible%2C%20as%20explained%20above.%20There%20aren%E2%80%99t%20really%20any%20other%20workarounds.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAdditionally%2C%20as%20the%20snapshot%20scalability%20issue%20is%20very%20localized%2C%20it%20is%20quite%20feasible%20to%20tackle%20it.%20There%20are%20no%20fundamental%20paradigm%20shifts%20necessary.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELastly%2C%20there%20is%20the%20aspect%20of%20wanting%20to%20handle%20many%20tens%20of%20thousands%20of%20connections%2C%20likely%20by%20entirely%20switching%20the%20connection%20model.%20As%20outlined%2C%20that%20is%20a%20huge%20project%20%2F%20fundamental%20paradigm%20shift.%20That%20doesn%E2%80%99t%20mean%20it%20should%20not%20be%20tackled%2C%20obviously.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAddressing%20the%20snapshot%20scalability%20issue%20first%20thus%20seems%20worthwhile%2C%20promising%20significant%20benefits%20on%20its%20own.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20there%E2%80%99s%20also%20a%20more%20fundamental%20reason%20for%20tackling%20snapshot%20scalability%20first%3A%20While%20e.g.%20addressing%20some%20memory%20usage%20issues%20at%20the%20same%20time%2Cqs%20switching%20the%20connection%20model%20would%20not%20at%20all%20address%20the%20snapshot%20issue.%20We%20would%20obviously%20still%20need%20to%20provide%20isolation%20between%20the%20connections%2C%20even%20if%20a%20connection%20wouldn%E2%80%99t%20have%20a%20dedicated%20process%20anymore.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHopefully%20now%20you%20understand%20why%20I%20chose%20to%20focus%20on%20Postgres%20snapshot%20scalability%20first.%20More%20about%20that%20in%20my%20next%20blog%20post.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CSECTION%20class%3D%22footnotes%22%20role%3D%22doc-endnotes%22%3E%3CHR%20%2F%3E%3C%2FSECTION%3E%0A%3COL%3E%0A%3CLI%20id%3D%22fn%3A1%22%20role%3D%22doc-endnote%22%3E%3CP%3E2x%20xeon%20gold%205215%2C%20192GiB%20of%20RAM%2C%20kernel%205.8.5%2C%20debian%20Sid%20%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%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%20id%3D%22fn%3A2%22%20role%3D%22doc-endnote%22%3E%3CP%3Ecreating%20100k%20tables%20with%20psql%3A%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%22%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-shell%22%20data-lang%3D%22shell%22%3Epostgres%3CSPAN%3E%5B%3C%2FSPAN%3E2627319%3CSPAN%3E%5D%5B%3C%2FSPAN%3E1%3CSPAN%3E%5D%3D%3C%2FSPAN%3E%3CSPAN%3E%23%20SELECT%20format('begin%3Bcreate%20table%20foo_%251s(id%20serial%20primary%20key%2C%20data1%20int%2C%20data2%20text%2C%20data3%20json)%3Bcommit%3B'%2C%20g.i)%20FROM%20generate_series(1%2C%20100000)%20g(i)%3C%2FSPAN%3E%0A%3CSPAN%3E%5Cg%3C%2FSPAN%3Eexec%0ACOMMIT%0ACOMMIT%0A%E2%80%A6%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%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%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22fn%3A3%22%20role%3D%22doc-endnote%22%3E%3CP%3Equery%20cache%20memory%20usage%3A%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%22%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-SQL%22%20data-lang%3D%22SQL%22%3E%3CSPAN%3EWITH%3C%2FSPAN%3E%20%3CSPAN%3ERECURSIVE%3C%2FSPAN%3E%20contexts%20%3CSPAN%3EAS%3C%2FSPAN%3E%20(%0A%20%20%20%20%3CSPAN%3ESELECT%3C%2FSPAN%3E%20%3CSPAN%3E*%3C%2FSPAN%3E%20%3CSPAN%3EFROM%3C%2FSPAN%3E%20pg_backend_memory_contexts)%2C%0A%20%20%20%20caches%20%3CSPAN%3EAS%3C%2FSPAN%3E%20(%0A%20%20%20%20%20%20%20%20%3CSPAN%3ESELECT%3C%2FSPAN%3E%20%3CSPAN%3E*%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%3EFROM%3C%2FSPAN%3E%20contexts%0A%20%20%20%20%20%20%20%20%3CSPAN%3EWHERE%3C%2FSPAN%3E%20name%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20%3CSPAN%3E'CacheMemoryContext'%3C%2FSPAN%3E%0A%0A%20%20%20%20%20%20%3CSPAN%3EUNION%3C%2FSPAN%3E%20%3CSPAN%3EALL%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%3ESELECT%3C%2FSPAN%3E%20contexts.%3CSPAN%3E*%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%3EFROM%3C%2FSPAN%3E%20caches%0A%20%20%20%20%20%20%20%20%3CSPAN%3EJOIN%3C%2FSPAN%3E%20contexts%20%3CSPAN%3EON%3C%2FSPAN%3E%20(contexts.parent%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20caches.name)%0A%20%20%20%20)%0A%3CSPAN%3ESELECT%3C%2FSPAN%3E%0A%20%20%20%20name%2C%20parent%2C%0A%20%20%20%20%3CSPAN%3Esum%3C%2FSPAN%3E(total_bytes)%20size_bytes%2C%0A%20%20%20%20pg_size_pretty(%3CSPAN%3Esum%3C%2FSPAN%3E(total_bytes))%20size_human%2C%0A%20%20%20%20%3CSPAN%3Ecount%3C%2FSPAN%3E(%3CSPAN%3E*%3C%2FSPAN%3E)%20%3CSPAN%3EAS%3C%2FSPAN%3E%20num_contexts%0A%3CSPAN%3EFROM%3C%2FSPAN%3E%20caches%0A%3CSPAN%3EGROUP%3C%2FSPAN%3E%20%3CSPAN%3EBY%3C%2FSPAN%3E%20name%2C%20parent%0A%3CSPAN%3EORDER%3C%2FSPAN%3E%20%3CSPAN%3EBY%3C%2FSPAN%3E%20%3CSPAN%3ESUM%3C%2FSPAN%3E(total_bytes)%20%3CSPAN%3EDESC%3C%2FSPAN%3E%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%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%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22fn%3A4%22%20role%3D%22doc-endnote%22%3E%3CP%3EQuery%20to%20access%20all%20tables%20named%3CCODE%20style%3D%22color%3A%20black%3B%22%3Efoo*%3C%2FCODE%3E%3A%3C%2FP%3E%0A%3CDIV%20class%3D%22highlight%22%3E%0A%3CPRE%3E%3CCODE%20class%3D%22language-SQL%22%20data-lang%3D%22SQL%22%3E%3CSPAN%3EDO%3C%2FSPAN%3E%20%3CSPAN%3E%24%24%3C%2FSPAN%3E%0A%20%20%3CSPAN%3EDECLARE%3C%2FSPAN%3E%0A%20%20%20%20cnt%20int%20%3A%3CSPAN%3E%3D%3C%2FSPAN%3E%20%3CSPAN%3E0%3C%2FSPAN%3E%3B%0A%20%20%20%20v%20record%3B%0A%20%20%3CSPAN%3EBEGIN%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%3EFOR%3C%2FSPAN%3E%20v%20%3CSPAN%3EIN%3C%2FSPAN%3E%20%3CSPAN%3ESELECT%3C%2FSPAN%3E%20%3CSPAN%3E*%3C%2FSPAN%3E%20%3CSPAN%3EFROM%3C%2FSPAN%3E%20pg_class%20%3CSPAN%3EWHERE%3C%2FSPAN%3E%20relkind%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20%3CSPAN%3E'r'%3C%2FSPAN%3E%20%3CSPAN%3Eand%3C%2FSPAN%3E%20relname%20%3CSPAN%3ELIKE%3C%2FSPAN%3E%20%3CSPAN%3E'foo%25'%3C%2FSPAN%3E%20LOOP%0A%20%20%20%20%20%20%20%20%3CSPAN%3EEXECUTE%3C%2FSPAN%3E%20format(%3CSPAN%3E'SELECT%20count(*)%20FROM%20%25s'%3C%2FSPAN%3E%2C%20v.oid%3A%3Aregclass%3A%3Atext)%3B%0A%20%20%20%20%20%20%20%20cnt%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20cnt%20%3CSPAN%3E%2B%3C%2FSPAN%3E%20%3CSPAN%3E1%3C%2FSPAN%3E%3B%0A%20%20%20%20%20%20%20%20%3CSPAN%3EIF%3C%2FSPAN%3E%20cnt%20%3CSPAN%3E%25%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%20%3CSPAN%3E%3D%3C%2FSPAN%3E%20%3CSPAN%3E0%3C%2FSPAN%3E%20%3CSPAN%3ETHEN%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%3ECOMMIT%3C%2FSPAN%3E%3B%0A%20%20%20%20%20%20%20%20%3CSPAN%3EEND%3C%2FSPAN%3E%20%3CSPAN%3EIF%3C%2FSPAN%3E%3B%0A%20%20%20%20%3CSPAN%3EEND%3C%2FSPAN%3E%20LOOP%3B%0A%20%20%20%20RAISE%20NOTICE%20%3CSPAN%3E'tables%20%251'%3C%2FSPAN%3E%2C%20cnt%3B%0A%20%20%3CSPAN%3EEND%3C%2FSPAN%3E%3B%3CSPAN%3E%24%24%3C%2FSPAN%3E%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%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%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FLI%3E%0A%3CLI%20id%3D%22fn%3A5%22%20role%3D%22doc-endnote%22%3E%3CP%3EEven%20worse%2C%20there%20can%20also%20be%20several%20queries%20in%20progress%20at%20the%20same%20time%2C%20e.g.%20due%20to%20the%20use%20of%20cursors.%20It%20is%20however%20not%20common%20to%20concurrently%20use%20many%20cursors.%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%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%20id%3D%22fn%3A6%22%20role%3D%22doc-endnote%22%3E%3CP%3EThis%20is%20with%20pgbench%20modified%20to%20wait%20until%20all%20connections%20are%20established.%20Without%20that%20pgbench%20modification%2C%20sometimes%20a%20subset%20of%20clients%20may%20not%20be%20able%20to%20connect%2C%20particularly%20before%20the%20fixes%20described%20in%20this%20article.%20See%20this%20%3CA%20href%3D%22https%3A%2F%2Fwww.postgresql.org%2Fmessage-id%2F20200227180100.zyvjwzcpiokfsqm2%2540alap3.anarazel.de%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Emailing%20listpost%3C%2FA%3E%20for%20details.%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%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%20id%3D%22fn%3A7%22%20role%3D%22doc-endnote%22%3E%3CP%3EIdle%20Connections%20vs%20Active%20Connections%3A%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTH%3EPG%20Version%3C%2FTH%3E%0A%3CTH%3EIdle%20Connections%3C%2FTH%3E%0A%3CTH%3EActive%20Connections%3C%2FTH%3E%0A%3CTH%3ETPS%3C%2FTH%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E0%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E33457%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E100%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E33705%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E1000%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E30558%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E2500%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E26075%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E5000%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E23284%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E10000%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E14496%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E0%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E1032435%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E100%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E960847%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E1000%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E902109%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E2500%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E759723%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E5000%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E702680%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E10000%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E521558%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%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%2FLI%3E%0A%3CLI%20id%3D%22fn%3A8%22%20role%3D%22doc-endnote%22%3E%3CP%3EMostly%20Idle%20Connections%20vs%20Active%20Connections%3A%3C%2FP%3E%0A%3CTABLE%3E%0A%3CTHEAD%3E%0A%3CTR%3E%0A%3CTH%3EPG%20Version%3C%2FTH%3E%0A%3CTH%3ELess%20active%20Connections%3C%2FTH%3E%0A%3CTH%3EActive%20Connections%3C%2FTH%3E%0A%3CTH%3ETPS%3C%2FTH%3E%0A%3C%2FTR%3E%0A%3C%2FTHEAD%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E0%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E33773%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E100%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E29074%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E1000%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E25327%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E2500%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E19752%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E5000%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E9807%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E10000%3C%2FTD%3E%0A%3CTD%3E1%3C%2FTD%3E%0A%3CTD%3E6049%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E0%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E1040616%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E100%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E953755%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E1000%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E759366%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E2500%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E733000%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E5000%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E636057%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%3E12%3C%2FTD%3E%0A%3CTD%3E10000%3C%2FTD%3E%0A%3CTD%3E48%3C%2FTD%3E%0A%3CTD%3E416819%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%3A8%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%3C%2FOL%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1757266%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%22Stylized-chart-of-Postgres-Connection-Scalability-Impact-of-Idle-Connections-999x463.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F225059iB2D93D0285E216CC%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20title%3D%22Stylized-chart-of-Postgres-Connection-Scalability-Impact-of-Idle-Connections-999x463.png%22%20alt%3D%22Stylized-chart-of-Postgres-Connection-Scalability-Impact-of-Idle-Connections-999x463.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EAnalyzing%20the%20limitations%20of%20Postgres%20connection%20scalability%2C%20from%20memory%20usage%20to%20snapshot%20scalability%20to%20the%20connection%20model.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1757266%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

One common challenge with Postgres for those of you who manage busy Postgres databases, and those of you who foresee being in that situation, is that Postgres does not handle large numbers of connections particularly well.

While it is possible to have a few thousand established connections without running into problems, there are some real and hard-to-avoid problems.

 

Since joining Microsoft last year in the Azure Database for PostgreSQL team—where I work on open source Postgres—I have spent a lot of time analyzing and addressing some of the issues with connection scalability in Postgres.

 

In this post I will explain why I think it is important to improve Postgres' handling of large number of connections. Followed by an analysis of the different limiting aspects to connection scalability in Postgres.

 

In an upcoming post I will show the results of the work we’ve done to improve connection handling and snapshot scalability in Postgres—and go into detail about the identified issues and how we have addressed them in Postgres 14.


Why connection scalability in Postgres is important

 

Stylized-chart-of-Postgres-Connection-Scalability-Impact-of-Idle-Connections-1200x630.png

In some cases problems around connection scalability are caused by unfamiliarity with Postgres, broken applications, or other issues in the same vein. And as I already mentioned, some applications can have a few thousand established connections without running into any problems.

 

A frequent counter-claim to requests to improve Postgres' handling of large numbers of connection counts is that there is nothing to address. That the desire/need to handle large numbers of connection is misguided, caused by broken applications or similar. Often accompanied by references to the server only having a limited number of CPU cores.

 

There certainly are cases where the best approach is to avoid large numbers of connections, but there are—in my opinion—pretty clear reasons for needing larger number of connections in Postgres. Here are the main ones:

 

  • Central state and spikey load require large numbers of connections: It is common for a database to be the shared state for an application (leaving non-durable caching services aside). Given the cost of establishing a new database connection (TLS, latency, and Postgres costs, in that order) it is obvious that applications need to maintain pools of Postgres connections that are large enough to handle the inevitable minor spikes in incoming requests. Often there are many servers running [web-]application code using one centralized database.

     

    To some degree this issue can be addressed using Postgres connection poolers like PgBouncer or more recently Odyssey. To actually reduce the number of connections to the database server such poolers need to be used in transaction (or statement) pooling modes. However, doing so precludes the use of many useful database features like prepared statements, temporary tables, …

     

  • Latency and result processing times lead to idle connections: Network latency and application processing times will often result in individual database connections being idle the majority of the time, even when the applications are issuing database requests as fast as they can.


    Common OLTP database workloads, and especially web applications, are heavily biased towards reads. And with OLTP workloads, the majority of SQL queries are simple enough to be processed well below the network latency between application and database.

    Additionally the application needs to process the results of the database queries it sent. That often will involve substantial work (e.g. template processing, communication with cache servers, …).


    To drive this home, here is a simple experiment using pgbench (a simple benchmarking program that is part of Postgres). In a memory-resident, read-only pgbench workload (executed on my workstation 1, 20/40 CPU cores/threads) I am comparing the achievable throughput across increasing client counts between a non-delayed pgbench and a pgbench with simulated delays. For the simulated delays, I used a 1ms network delay and a 1ms processing delay. The non-delayed pgbench peaks around 48 clients, the delayed run around 3000 connections. Even comparing on-machine TCP connections to a 10GBe between two physically close machines moves the peak from around 48 connections closer to 500 connections.

  • Scaling out to allow for higher connection counts can increase cost: Even in cases where the application’s workload can be distributed over a number of Postgres instances, the impact of latency combined with low maximum connection limits will often result in low utilization of the database servers, while exerting pressure to increase the number of database servers to handle the required number of connections. That can increase the operational costs substantially.

Surveying connection scalability issues

My goal in starting this project was to improve Postgres' ability to handle substantially larger numbers of connections. To do that—to pick the right problem to solve— I first needed to understand which problems were most important, otherwise it would have been easy to end up with micro-optimizations without improving real-world workloads.

 

So my first software engineering task was to survey the different aspects of connection scalability limitations in Postgres, specifically:

  1. Memory usage
  2. Snapshot scalability
  3. Connection model

By the end of this deep dive into the connection scalability limitations in Postgres, I hope you will understand why I concluded that snapshot scalability should be addressed first.

Memory usage

There are 3 main aspects to problems around memory usage of a large numbers of connections:

 

Constant connection overhead

Postgres, as many of you will know, uses a process-based connection model. When a new connection is established, Postgres' supervisor process creates a dedicated process to handle that connection going forward. The use of a “full blown process” over the use of of threads has some advantages like increased isolation / robustness, but also some disadvantages.

 

One common complaint is that each connection uses too much memory. That is, at least partially, a common observation because it is surprisingly hard to measure the increase in memory usage by an additional connection.

 

In a recent post about measuring the memory overhead of a Postgres connection I show that it is surprisingly hard to accurately measure the memory overhead. And that in many workloads, and with the right configuration—most importantly, using huge_pages —the memory overhead of each connection is below 2 MiB.

Conclusion: connection memory overhead is acceptable

 

When each connection only has an overhead of a few MiB, it is quite possible to have thousands of established connections. It would obviously be good to use less memory, but memory is not the primary issue around connection scalability.

Cache bloat

Another important aspect of memory-related connection scalability issues can be that, over time, the memory usage of a connection increases, due to long-lived resources. This particularly is an issue in workloads that utilize long-lived connections combined with schema-based multi-tenancy.

 

Unless applications implement some form of connection <-> tenant association, each connection over time will access all relations for all tenants. That leads to Postgres' internal catalog metadata caches growing beyond a reasonable size, as currently (as of version 13) Postgres does not prune its metadata caches of unchanging rarely-accessed contents.

 

Problem illustration

 

To demonstrate the issue of cache bloat, I created a simple test bed with 100k tables, with a few columns and single primary serial column index 2. Takes a while to create.

 

With the recently added pg_backend_memory_contexts view it is not too difficult to see the aggregated memory usage of the various caches (although it would be nice to see more of the different types of caches broken out into their own memory contexts). See 3.

 

In a new Postgres connection, not much memory is used:

name parent size_bytes size_human num_contexts
CacheMemoryContext TopMemoryContext 524288 512 kB 1
index info CacheMemoryContext 149504 146 kB 80
relation rules CacheMemoryContext 8192 8192 bytes 1

 

But after forcing all Postgres tables we just created to be accessed 4, this looks very different:

name parent size_bytes size_human num_contexts
CacheMemoryContext TopMemoryContext 621805848 593 MB 1
index info CacheMemoryContext 102560768 98 MB 100084
relation rules CacheMemoryContext 8192 8192 bytes 1

 

As the metadata cache for indexes is created in its own memory context, num_contexts for the “index info” contexts nicely shows that we accessed the 100k tables (and some system internal ones).

Conclusion: cache bloat is not the major issue at this moment

 

A common solution for the cache bloat issue is to drop “old” connections from the application connection pooler after a certain age. Many connection pooler libraries / web frameworks support that.

 

As there is a feasible workaround, and as cache bloat is only an issue for databases with a lot of objects, cache bloat is not the major issue at the moment (but worthy of improvement, obviously).

 

Query memory usage

The third aspect is that it is hard to limit memory used by queries. The work_mem setting does not control the memory used by a query as a whole, but only of individual parts of a query (e.g. sort, hash aggregation, hash join). That means that a query can end up requiring work_mem several times over 5.

 

That means that one has to be careful setting work_mem in workloads requiring a lot of connections. With larger work_mem settings, practically required for analytics workloads, one can’t reasonably use a huge number of concurrent connections and expect to never hit memory exhaustion related issues (i.e. errors or the OOM killer).

 

Luckily most workloads requiring a lot of connection don’t need a high work_mem setting, and it can be set on the user, database, connection, and transaction level.

 

Snapshot scalability

There are a lot of recommendations out there strongly recommending to not set max_connections for Postgres to a high value, as high values can cause problems. In fact, I’ve argued that myself many times.

 

But that is only half the truth.

 

Setting max_connections to a very high value alone only leads at best (worst?) to a very small slowdown in itself, and wastes some memory. E.g. on my workstation 1 there is no measurable performance difference for a read-only pgbench between max_connections=100 and a value as extreme max_connections=100000 (for the same pgbench client count, 48 in this case). However the memory required for Postgres does increase measurable with such an extreme setting. With shared_buffers=16GB max_connections=100 uses 16804 MiB, max_connections=100000 uses 21463 MiB of shared memory. That is a large enough difference to potentially cause a slowdown indirectly (although most of that memory will never be used, therefore not allocated by the OS in common configurations).

 

The real issue is that currently Postgres does not scale well to having a large number of established connections, even if nearly all connections are idle.

 

To showcase this, I used two separate pgbench6 runs. One of them just establishes connections that are entirely idle (using a test file that just contains \sleep 1s, causing a client-side sleep). Another to run a normal pgbench read-only workload.

 

This is far from reproducing the worst possible version of the issue, as normally the set of idle connections varies over time, which makes this issue considerably worse. This version is much easier to reproduce however.

 

This is a very useful scenario to test, because it allows us to isolate the cost of additional connections pretty well isolated. Especially when the count of active connections is low, the system CPU usage is quite low. If there is a slowdown when the number of idle connections increases, it is clearly related to the number of idle connections.

 

If we instead measured the throughput with a high number of active connections, it’d be harder to pinpoint whether e.g. the increase in context switches or lack of CPU cycles is to blame for slowdowns.

 

Throughput of one active connection in presence of a variable number of idle connectionsThroughput of one active connection in presence of a variable number of idle connections

 

Throughput of 48 active connections in presence of a variable number of idle connectionsThroughput of 48 active connections in presence of a variable number of idle connections

 

 

These results7 clearly show that the achievable throughput of active connections decreases significantly when the number of idle connections increases.

 

In reality “idle” connections are not entirely idle, but send queries at a lower rate. To simulate that I’ve used the the below to simulate clients only occasionally sending queries:

 

\sleep 100ms
SELECT 1;

 

Throughput of one active connection in presence of a variable number of mostly idle connectionsThroughput of one active connection in presence of a variable number of mostly idle connections

 

Throughput of 48 active connections in presence of a variable number of mostly idle connectionsThroughput of 48 active connections in presence of a variable number of mostly idle connections

 

The results8 show that the slightly more realistic scenario causes active connections to slow down even worse.

Cause

 

Together these results very clearly show that there is a significant issue handling large connection counts, even when CPU/memory are plentiful. The fact that a single active connection slows down by more than 2x due to concurrent idle connections points to a very clear issue.

 

A CPU profile quickly pinpoints the part of Postgres responsible:

 

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()

 

Obviously the bottleneck is entirely in the GetSnapshotData() function. That function performs the bulk of the work necessary to provide readers with transaction isolation. GetSnapshotData() builds so called “snapshots” that describe which effects of concurrent transactions are visible to a transaction, and which are not. These snapshots are built very frequently (at least once per transaction, very commonly more often).

Even without knowing its implementation, it does make some intuitive sense (at least I think so, but I also know what it does) that such a task gets more expensive the more connections / transactions need to be handled.

Two blog posts by Brandur explain the mechanics and issues surrounding this in more detail:

Conclusion: Snapshot scalability is a significant limit

 

A large number of connections clearly reduce the efficiency of other connections, even when idle (which as explained above, is very common). Except for reducing the number of concurrent connections and issuing fewer queries, there is no real workaround for the snapshot scalability issue.


Connection model & context switches

As mentioned above, Postgres uses a one-process-per-connection model. That works well in a lot of cases, but is a limiting factor for dealing with 10s to 100s of thousands of connections.

 

Whenever a query is received by a backend process, the kernel needs to perform a context switch to that process. That is not cheap. But more importantly, once the result for the query has been computed, the backend will commonly be idle for a while—the query result has to traverse the network, be received and processed by the application, before the application sends a new query. That means on a busy server another process/backend/connection will need to be scheduled—another context switch (cross-process context switches are more expensive than doing process-kernel-same process, e.g. as part of a syscall).

 

Note that switching to a one-thread-per-connection model does not address this issue to a meaningful degree: while some of the context switches may get cheaper, context switches still are the major limit. There are reasons to consider switching to threads, but connection scalability itself is not a major one (without additional architectural changes, some of which may be easier using threads).

 

To handle huge numbers of connections a different type of connection model is needed. Instead of using a process/thread-per-connection model, a fixed/limited number of processes/threads need to handle all connections. By waiting for incoming queries on many connections at once and then processing many queries without being interrupted by the OS CPU scheduler, efficiency can very significantly be improved.

 

This is not a brilliant insight by me. Architectures like this are in wide use, and have widely been discussed. See e.g. the C10k problem, coined in 1999.

 

Besides avoiding context switches, there are many other performance benefits that can be gained. E.g. on higher core count machines, a lot of performance can be gained by increasing locality of shared memory, e.g. by binding specific processes/threads and regions of memory to specific CPU cores.

 

However, changing Postgres to support a different kind of connection model like this is a huge undertaking. That does not just require carefully separating many dependencies between processes and connections, but also user-land scheduling between different queries, support for asynchronous IO, likely a different query execution model (to avoid needing a separate stack for each query), and much more.


Conclusion: Start by improving snapshot scalability in Postgres

In my opinion, the memory usage issues are not as severe as the other issues discussed. Partially because the memory overhead of connections is less big than it initially appears, and partially because issues like Postgres' caches using too much memory can be worked around reasonably.

 

We could, and should, make improvements around memory usage in Postgres, and there are several low enough hanging fruits. But I don’t think, as things currently are, that improving memory usage would, on its own, change the picture around connection scalability, at least not on a fundamental level.

 

In contrast, there is no good way to work around the snapshot scalability issues. Reducing the number of established connections significantly is often not feasible, as explained above. There aren’t really any other workarounds.

 

Additionally, as the snapshot scalability issue is very localized, it is quite feasible to tackle it. There are no fundamental paradigm shifts necessary.

 

Lastly, there is the aspect of wanting to handle many tens of thousands of connections, likely by entirely switching the connection model. As outlined, that is a huge project / fundamental paradigm shift. That doesn’t mean it should not be tackled, obviously.

 

Addressing the snapshot scalability issue first thus seems worthwhile, promising significant benefits on its own.

 

But there’s also a more fundamental reason for tackling snapshot scalability first: While e.g. addressing some memory usage issues at the same time,qs switching the connection model would not at all address the snapshot issue. We would obviously still need to provide isolation between the connections, even if a connection wouldn’t have a dedicated process anymore.

 

Hopefully now you understand why I chose to focus on Postgres snapshot scalability first. More about that in my next blog post on improving Postgres connection scalability.


  1. 2x xeon gold 5215, 192GiB of RAM, kernel 5.8.5, debian Sid ↩︎

  2. creating 100k tables with psql:

    postgres[2627319][1]=# SELECT format('begin;create table foo_%1s(id serial primary key, data1 int, data2 text, data3 json);commit;', g.i) FROM generate_series(1, 100000) g(i)
    \gexec
    COMMIT
    COMMIT
    …
    
    ↩︎

  3. query cache memory usage:

    WITH RECURSIVE contexts AS (
        SELECT * FROM pg_backend_memory_contexts),
        caches AS (
            SELECT *
            FROM contexts
            WHERE name = 'CacheMemoryContext'
    
          UNION ALL
            SELECT contexts.*
            FROM caches
            JOIN contexts ON (contexts.parent = caches.name)
        )
    SELECT
        name, parent,
        sum(total_bytes) size_bytes,
        pg_size_pretty(sum(total_bytes)) size_human,
        count(*) AS num_contexts
    FROM caches
    GROUP BY name, parent
    ORDER BY SUM(total_bytes) DESC;
    
    ↩︎

  4. Query to access all tables named foo*:

    DO $$
      DECLARE
        cnt int := 0;
        v record;
      BEGIN
        FOR v IN SELECT * FROM pg_class WHERE relkind = 'r' and relname LIKE 'foo%' LOOP
            EXECUTE format('SELECT count(*) FROM %s', v.oid::regclass::text);
            cnt = cnt + 1;
            IF cnt % 100 = 0 THEN
                COMMIT;
            END IF;
        END LOOP;
        RAISE NOTICE 'tables %1', cnt;
      END;$$;
    
    ↩︎

  5. Even worse, there can also be several queries in progress at the same time, e.g. due to the use of cursors. It is however not common to concurrently use many cursors. ↩︎

  6. This is with pgbench modified to wait until all connections are established. Without that pgbench modification, sometimes a subset of clients may not be able to connect, particularly before the fixes described in this article. See this mailing listpost for details. ↩︎

  7. Idle Connections vs Active Connections:

    PG Version Idle Connections Active Connections TPS
    12 0 1 33457
    12 100 1 33705
    12 1000 1 30558
    12 2500 1 26075
    12 5000 1 23284
    12 10000 1 14496
    12 0 48 1032435
    12 100 48 960847
    12 1000 48 902109
    12 2500 48 759723
    12 5000 48 702680
    12 10000 48 521558
    ↩︎
  8. Mostly Idle Connections vs Active Connections:

    PG Version Less active Connections Active Connections TPS
    12 0 1 33773
    12 100 1 29074
    12 1000 1 25327
    12 2500 1 19752
    12 5000 1 9807
    12 10000 1 6049
    12 0 48 1040616
    12 100 48 953755
    12 1000 48 759366
    12 2500 48 733000
    12 5000 48 636057
    12 10000 48 416819
    ↩︎