Home
%3CLINGO-SUB%20id%3D%22lingo-sub-790883%22%20slang%3D%22en-US%22%3EConnection%20handling%20best%20practice%20with%20PostgreSQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-790883%22%20slang%3D%22en-US%22%3E%3CP%3EManaging%20connections%20in%26nbsp%3BMicrosoft%26nbsp%3BAzure%20Database%20for%20PostgreSQL%20is%20a%20topic%20that%20seems%20to%20come%20up%26nbsp%3Bseveral%20times%20in%20conversations%20with%20our%20customers.%26nbsp%3BThe%20connections%20in%26nbsp%3BPostgres%26nbsp%3Baren%E2%80%99t%20free%26nbsp%3Beach%26nbsp%3Bconnection%2C%26nbsp%3Bwhether%20idle%20or%20active%2C%26nbsp%3Bconsumes%20a%20certain%20overhead%20of%20memory%26nbsp%3B(10MB%20per%20connection).%26nbsp%3BIdle%26nbsp%3Bis%26nbsp%3Bsomething%26nbsp%3Bthat%26nbsp%3Bgrabs%26nbsp%3Bconnection%26nbsp%3Bfrom%20your%20application%20and%20holds%26nbsp%3Bit.%26nbsp%3BApplication%20connection%20poolers%26nbsp%3Boften%26nbsp%3Balso%20consume%20one%20or%20more%20idle%26nbsp%3Bconnections.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%26nbsp%3BAzure%20Database%20for%20PostgreSQL%2C%26nbsp%3Byou%20can%20use%26nbsp%3Bvarious%20ways%2C%26nbsp%3Bfor%20example%20using%20Postgres%20metadata%20and%26nbsp%3BAzure%20Monitor%2C%26nbsp%3Bto%26nbsp%3Bbetter%20track%20what%20is%20going%20through%20your%20database%26nbsp%3Band%26nbsp%3Btake%20proactive%20steps%26nbsp%3Baccordingly.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId-1796046535%22%20id%3D%22toc-hId-1796046535%22%20id%3D%22toc-hId-1796046535%22%3E%3CSPAN%20data-ccp-props%3D%22%7B%26quot%3B201341983%26quot%3B%3A0%2C%26quot%3B335559739%26quot%3B%3A160%2C%26quot%3B335559740%26quot%3B%3A264%7D%22%3E%3CSPAN%20class%3D%22TextRun%20SCXW261283631%20BCX0%22%20data-contrast%3D%22none%22%3E%3CSPAN%20class%3D%22NormalTextRun%20SCXW261283631%20BCX0%22%3EUsing%20Postgres%20metadata%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3EUnder%26nbsp%3Bthe%20covers%2C%26nbsp%3BPostgres%26nbsp%3Bhas%20a%20lot%20of%20metadata%20about%20both%20historical%20and%20current%20activity%20against%20a%20system.%20Within%20Postgres%2C%26nbsp%3Byou%20can%20run%20the%20following%20query%26nbsp%3Bto%20get%20a%26nbsp%3Bfew%20results%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESELECT%20count(*)%2Cstate%20FROM%20pg_stat_activity%20GROUP%20BY%202%3B%0A%20count%20%7C%20%20%20%20%20%20%20%20%20%20%20%20state%0A-------%2B------------------------------%0A%20%20%20%20%207%20%7C%20active%0A%20%20%20%2032%20%7C%20idle%0A%20%20%20%2022%20%7C%20idle%20in%20transaction%0A%20%20%20%20%204%20%7C%20idle%20in%20transaction(aborted)%0A(4%20rows)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEach%20of%20the%20above%20output%20is%20useful%20in%20determining%20what%20you%20should%20do%20to%20better%20manage%20your%20connection%20count.%20All%20these%20numbers%20can%20be%20useful%20to%20record%20every%20say%2030%20seconds%20and%20chart%20on%20your%20own%20internal%20monitoring.%26nbsp%3B%3C%2FP%3E%0A%3CP%3ELet's%20break%20down%20each%3A%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%20data-leveltext%3D%22%EF%82%B7%22%20data-font%3D%22Symbol%22%20data-listid%3D%2213%22%20aria-setsize%3D%22-1%22%20data-aria-posinset%3D%221%22%20data-aria-level%3D%221%22%3E%3CSTRONG%3Eactive%26nbsp%3B%3C%2FSTRONG%3E%E2%80%93%26nbsp%3BIdentifies%26nbsp%3Bcurrently%20running%20queries%3B%20in%20a%20sense%2C%26nbsp%3Bthis%20is%20truly%20how%20many%20connections%20you%20may%20require%20at%20a%20time%26nbsp%3B%3C%2FLI%3E%0A%3CLI%20data-leveltext%3D%22%EF%82%B7%22%20data-font%3D%22Symbol%22%20data-listid%3D%2213%22%20aria-setsize%3D%22-1%22%20data-aria-posinset%3D%222%22%20data-aria-level%3D%221%22%3E%3CSTRONG%3Eidle%26nbsp%3B%3C%2FSTRONG%3E%E2%80%93%26nbsp%3BIdentifies%26nbsp%3Bconnections%26nbsp%3Bopened%26nbsp%3Bto%20the%20DB%20(most%20frameworks%20do%20this%20and%20maintain%20a%20pool%20of%26nbsp%3Bconnections)%2C%26nbsp%3Bthat%20are%20not%20in%20use.%26nbsp%3BThis%20is%20the%20one%20area%26nbsp%3Bin%20which%26nbsp%3Ba%20connection%20pooler%26nbsp%3Bsuch%20as%26nbsp%3BPgBouncer%26nbsp%3Bcan%26nbsp%3Bhelp%26nbsp%3Bmost.%26nbsp%3B%3C%2FLI%3E%0A%3CLI%20data-leveltext%3D%22%EF%82%B7%22%20data-font%3D%22Symbol%22%20data-listid%3D%2213%22%20aria-setsize%3D%22-1%22%20data-aria-posinset%3D%223%22%20data-aria-level%3D%221%22%3E%3CSTRONG%3Eidle%20in%20transaction%3C%2FSTRONG%3E%26nbsp%3B%E2%80%93%26nbsp%3BIdentifies%20connections%26nbsp%3Bagainst%20which%26nbsp%3Byour%20app%20has%20run%20a%20BEGIN%20but%20it%26nbsp%3Bis%26nbsp%3Bnow%20waiting%20somewhere%20in%20a%20transaction%20and%20not%20doing%20work.%26nbsp%3B%3C%2FLI%3E%0A%3CLI%20data-leveltext%3D%22%EF%82%B7%22%20data-font%3D%22Symbol%22%20data-listid%3D%2213%22%20aria-setsize%3D%22-1%22%20data-aria-posinset%3D%224%22%20data-aria-level%3D%221%22%3E%3CSTRONG%3Eidle%20in%20transaction%20(aborted)%3C%2FSTRONG%3E%26nbsp%3B%E2%80%93%26nbsp%3BIdentifies%20connections%20that%26nbsp%3Bwere%20idle%20in%26nbsp%3Bthe%26nbsp%3Btransaction%26nbsp%3Bthat%20have%20since%20been%20aborted.%26nbsp%3B%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EYou%20should%20monitor%26nbsp%3B%3CSTRONG%3Eidle%26nbsp%3B%3C%2FSTRONG%3Econnections%2C%26nbsp%3Band%26nbsp%3Bif%20you%20see%20a%20high%26nbsp%3Bcount%26nbsp%3Bhere%20it%E2%80%99s%20worth%20investing%20in%20setting%20up%20a%26nbsp%3BPgBouncer.%26nbsp%3BYou%20can%20read%20more%20about%20setting%20up%20PgBouncer%20in%20azure%20database%20for%20PostgreSQL%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FAzure-Database-for-PostgreSQL%2FSteps-to-install-and-setup-PgBouncer-connection-pooling-proxy%2Fba-p%2F730555%22%20target%3D%22_self%22%3Ehere%3C%2FA%3E.%26nbsp%3BConnections%20that%20are%26nbsp%3Bidle%20in%20transaction%26nbsp%3Bare%26nbsp%3Ba%20bit%20more%20interesting.%26nbsp%3BFor%20these%2C%26nbsp%3Bwhat%20you%20likely%20want%20to%20do%20when%20first%20investigating%26nbsp%3Bis%26nbsp%3Bto%26nbsp%3Bdetermine%20the%20age%20of%20the%20connections.%26nbsp%3BYou%20can%20do%20this%20by%20querying%26nbsp%3Bpg_stat_activity%26nbsp%3Band%20filtering%20for%26nbsp%3Bconnections%20that%20are%26nbsp%3Bidle%20in%20transaction%2C%26nbsp%3Band%26nbsp%3Bthen%26nbsp%3Bchecking%26nbsp%3Bthe%20age%20of%20those%26nbsp%3Bqueries.%26nbsp%3BFor%26nbsp%3Bones%26nbsp%3Bthat%20have%20been%20running%20too%20long%2C%26nbsp%3Byou%20may%20want%20to%26nbsp%3Bend%20them%26nbsp%3Bmanually.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20find%20that%20you%20have%20some%20stale%20transactions%20hanging%20around%20for%20days%2C%20hours%2C%20or%20even%20just%20a%20few%20minutes%2C%26nbsp%3Byou%20may%20want%20to%20set%20a%20default%20to%26nbsp%3Bend%26nbsp%3Bthose%20transactions.%26nbsp%3BTo%20help%20with%20this%2C%26nbsp%3BPostgres%26nbsp%3Bhas%20a%20nice%20feature%20of%20a%26nbsp%3B%3CSTRONG%3Estatement_timeout%3C%2FSTRONG%3E.%20A%20statement%20timeout%20will%20automatically%26nbsp%3Bend%26nbsp%3Bqueries%20that%20run%20longer%20than%20the%20allotted%20time.%20You%20can%20set%20this%20at%20both%20a%20global%20level%20and%20for%20a%20specific%20session.%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20do%20this%20at%20the%20database%20level%2C%26nbsp%3Brun%20the%20following%20command%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Ealter%20database%20dbnamehere%20set%20statement_timeout%20%3D%2060000%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%26nbsp%3Bcommand%26nbsp%3Babove%26nbsp%3Bsets%26nbsp%3Bthe%26nbsp%3Bstatement_timeout%26nbsp%3Bto%20%3CSTRONG%3E60%20s%3C%2FSTRONG%3E.%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20do%20so%20during%20a%20given%20session%2C%26nbsp%3Bsimply%20run%26nbsp%3Bthe%20following%20command%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eset%20statement_timeout%20%3D%206000000%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20idle%20in%20transaction%26nbsp%3Bconnections%26nbsp%3Bthat%20have%20been%20running%20too%20long%2C%26nbsp%3Busing%26nbsp%3BPostgres%209.6%26nbsp%3Bor%20higher%2C%20you%20can%26nbsp%3Btake%20advantage%20of%26nbsp%3Ba%26nbsp%3Bdedicated%26nbsp%3Bsetting%26nbsp%3B%3CSTRONG%3E(idle_in_transaction_session_timeout%3C%2FSTRONG%3E)%2C%20which%26nbsp%3Byou%26nbsp%3Bconfigure%20in%26nbsp%3Ba%20similar%20fashion.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EConfiguring%26nbsp%3Bboth%26nbsp%3Bthe%26nbsp%3B%3CSTRONG%3Estatement_timeout%3C%2FSTRONG%3E%26nbsp%3Band%26nbsp%3B%3CSTRONG%3Eidle_in_transaction_session_timeout%3C%2FSTRONG%3E%26nbsp%3Bsettings%26nbsp%3Bwill%20help%20with%20cancelling%20long%20running%20queries%20and%20transactions.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20aria-level%3D%221%22%20id%3D%22toc-hId--756110426%22%20id%3D%22toc-hId--756110426%22%20id%3D%22toc-hId--756110426%22%3E%3CSPAN%20data-contrast%3D%22none%22%3ETracking%20and%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22none%22%3Em%3C%2FSPAN%3E%3CSPAN%20data-contrast%3D%22none%22%3Eanaging%20through%20Azure%20Monitor%3C%2FSPAN%3E%3CSPAN%20data-ccp-props%3D%22%7B%26quot%3B201341983%26quot%3B%3A0%2C%26quot%3B335559738%26quot%3B%3A240%2C%26quot%3B335559739%26quot%3B%3A0%2C%26quot%3B335559740%26quot%3B%3A259%7D%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CP%3EYou%20can%20receive%20an%20alert%20based%20on%20monitoring%20metrics%20for%20your%20Azure%20Database%20for%20PostgreSQL%26nbsp%3Bserver.%20The%20alert%20triggers%20when%20the%20value%20of%20a%20specified%20metric%26nbsp%3Bexceeds%26nbsp%3Ba%20threshold%20you%20assign.%20The%20alert%20triggers%20both%20when%20the%20condition%26nbsp%3Bfirst%26nbsp%3Bexists%26nbsp%3Band%20then%20afterwards%2C%26nbsp%3Bwhen%20the%26nbsp%3Bcondition%26nbsp%3Bhas%20abated.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20configure%20an%20alert%26nbsp%3Bto%20trigger%20and%20perform%26nbsp%3Bthe%20following%20actions%3A%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%20data-leveltext%3D%22%EF%82%B7%22%20data-font%3D%22Symbol%22%20data-listid%3D%2214%22%20aria-setsize%3D%22-1%22%20data-aria-posinset%3D%225%22%20data-aria-level%3D%221%22%3ESend%20email%20notifications%20to%20the%20service%20administrator%20and%20co-administrators.%26nbsp%3B%3C%2FLI%3E%0A%3CLI%20data-leveltext%3D%22%EF%82%B7%22%20data-font%3D%22Symbol%22%20data-listid%3D%2214%22%20aria-setsize%3D%22-1%22%20data-aria-posinset%3D%226%22%20data-aria-level%3D%221%22%3ESend%26nbsp%3Bemail%26nbsp%3Bto%20additional%20email%26nbsp%3Baddresses%26nbsp%3Byou%20specify.%26nbsp%3B%3C%2FLI%3E%0A%3CLI%20data-leveltext%3D%22%EF%82%B7%22%20data-font%3D%22Symbol%22%20data-listid%3D%2214%22%20aria-setsize%3D%22-1%22%20data-aria-posinset%3D%227%22%20data-aria-level%3D%221%22%3ECall%20a%20webhook.%26nbsp%3B%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EYou%20can%20configure%20and%20get%20information%20about%20alert%20rules%20using%3A%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%20data-leveltext%3D%22%EF%82%B7%22%20data-font%3D%22Symbol%22%20data-listid%3D%2214%22%20aria-setsize%3D%22-1%22%20data-aria-posinset%3D%228%22%20data-aria-level%3D%221%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-monitor%2Fplatform%2Falerts-metric%23create-with-azure-portal%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%20data-contrast%3D%22none%22%3EAzure%20portal%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%20data-ccp-props%3D%22%7B%26quot%3B134233279%26quot%3B%3Atrue%2C%26quot%3B201341983%26quot%3B%3A0%2C%26quot%3B335559739%26quot%3B%3A160%2C%26quot%3B335559740%26quot%3B%3A264%7D%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%20data-leveltext%3D%22%EF%82%B7%22%20data-font%3D%22Symbol%22%20data-listid%3D%2214%22%20aria-setsize%3D%22-1%22%20data-aria-posinset%3D%229%22%20data-aria-level%3D%221%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fazure-monitor%2Fplatform%2Falerts-metric%23with-azure-cli%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%20data-contrast%3D%22none%22%3EAzure%20CLI%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%20data-ccp-props%3D%22%7B%26quot%3B134233279%26quot%3B%3Atrue%2C%26quot%3B201341983%26quot%3B%3A0%2C%26quot%3B335559739%26quot%3B%3A160%2C%26quot%3B335559740%26quot%3B%3A264%7D%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%20data-leveltext%3D%22%EF%82%B7%22%20data-font%3D%22Symbol%22%20data-listid%3D%2214%22%20aria-setsize%3D%22-1%22%20data-aria-posinset%3D%2210%22%20data-aria-level%3D%221%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Frest%2Fapi%2Fmonitor%2Fmetricalerts%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%20data-contrast%3D%22none%22%3EAzure%20Monitor%20REST%20API%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%20data-ccp-props%3D%22%7B%26quot%3B134233279%26quot%3B%3Atrue%2C%26quot%3B201341983%26quot%3B%3A0%2C%26quot%3B335559739%26quot%3B%3A160%2C%26quot%3B335559740%26quot%3B%3A264%7D%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EFor%20more%20detail%20on%26nbsp%3Bcreating%26nbsp%3Balerts%20on%20metrics%26nbsp%3Bin%20Azure%20Database%20for%20PostgreSQL%2C%20see%20the%20article%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fazure%2Fpostgresql%2Fhowto-alert-on-metric%22%20target%3D%22_self%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%3Ehere%3C%2FSPAN%3E%3C%2FA%3E%3CSPAN%20data-contrast%3D%22auto%22%3E.%3C%2FSPAN%3E%3CSPAN%20data-ccp-props%3D%22%7B%26quot%3B201341983%26quot%3B%3A0%2C%26quot%3B335559739%26quot%3B%3A160%2C%26quot%3B335559740%26quot%3B%3A264%7D%22%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ECombining%26nbsp%3BPostgres%26nbsp%3Bmetadata%20checks%26nbsp%3Bwith%20Azure%26nbsp%3BMonitor%20alerts%26nbsp%3Bshould%20lead%20to%20a%20much%20healthier%20performing%20database%20and%26nbsp%3Bwould%20protect%20you%20from%20unwanted%20surprises.%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20you%20have%20trouble%20setting%20up%20any%20extension%20supported%20by%20Azure%20Database%20for%20PostgreSQL%2C%20please%20let%20us%20know%20by%20contacting%20us%20at%26nbsp%3BAskAzureDBforPostgreSQL%40service.microsoft.com!%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThank%20you!%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECraig%20Kerstiens%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPrincipal%20PM%20Manager%20%E2%80%93%20Microsoft%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAmol%20Bhatnagar%26nbsp%3B%3C%2FP%3E%0A%3CP%3EProgram%20Manager%20-%20Microsoft%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-790883%22%20slang%3D%22en-US%22%3E%3CP%3ELearn%20how%20to%20track%20and%20manage%20connections%20in%20PostgreSQL%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-790883%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EConnection%20handling%20best%20practice%20with%20PostgreSQL%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-997964%22%20slang%3D%22en-US%22%3ERe%3A%20Connection%20handling%20best%20practice%20with%20PostgreSQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-997964%22%20slang%3D%22en-US%22%3E%3CP%3EGood%20overview%20on%20connection%20handling%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20I%20connect%20to%20Azure%20PostGre%20SQL%20using%20URI%20from%20external%20source%3F%20I%20am%20using%20MLFLow%20for%20ML%20model%20management%20and%20want%20to%20use%20Azure%20PostGre%20SQL%20for%20metrics%20and%20artefact%20storage.%20MLFLow%20supports%20URI%20storage%20with%20Azure%20Blob%2C%20but%20no%20mention%20of%20Azure%20PostGre%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

Managing connections in Microsoft Azure Database for PostgreSQL is a topic that seems to come up several times in conversations with our customers. The connections in Postgres aren’t free each connection, whether idle or active, consumes a certain overhead of memory (10MB per connection). Idle is something that grabs connection from your application and holds it. Application connection poolers often also consume one or more idle connections. 

In Azure Database for PostgreSQL, you can use various ways, for example using Postgres metadata and Azure Monitor, to better track what is going through your database and take proactive steps accordingly. 

 

Using Postgres metadata

Under the covers, Postgres has a lot of metadata about both historical and current activity against a system. Within Postgres, you can run the following query to get a few results: 

 

 

SELECT count(*),state FROM pg_stat_activity GROUP BY 2;
 count |            state
-------+------------------------------
     7 | active
    32 | idle
    22 | idle in transaction
     4 | idle in transaction(aborted)
(4 rows)

 

 

Each of the above output is useful in determining what you should do to better manage your connection count. All these numbers can be useful to record every say 30 seconds and chart on your own internal monitoring. 

Let's break down each: 

  • active – Identifies currently running queries; in a sense, this is truly how many connections you may require at a time 
  • idle – Identifies connections opened to the DB (most frameworks do this and maintain a pool of connections), that are not in use. This is the one area in which a connection pooler such as PgBouncer can help most. 
  • idle in transaction – Identifies connections against which your app has run a BEGIN but it is now waiting somewhere in a transaction and not doing work. 
  • idle in transaction (aborted) – Identifies connections that were idle in the transaction that have since been aborted. 

You should monitor idle connections, and if you see a high count here it’s worth investing in setting up a PgBouncer. You can read more about setting up PgBouncer in azure database for PostgreSQL here. Connections that are idle in transaction are a bit more interesting. For these, what you likely want to do when first investigating is to determine the age of the connections. You can do this by querying pg_stat_activity and filtering for connections that are idle in transaction, and then checking the age of those queries. For ones that have been running too long, you may want to end them manually. 

If you find that you have some stale transactions hanging around for days, hours, or even just a few minutes, you may want to set a default to end those transactions. To help with this, Postgres has a nice feature of a statement_timeout. A statement timeout will automatically end queries that run longer than the allotted time. You can set this at both a global level and for a specific session. 

To do this at the database level, run the following command: 

 

 

alter database dbnamehere set statement_timeout = 60000;

 

 

The command above sets the statement_timeout to 60 s

To do so during a given session, simply run the following command:

 

 

set statement_timeout = 6000000;

 

 

For idle in transaction connections that have been running too long, using Postgres 9.6 or higher, you can take advantage of a dedicated setting (idle_in_transaction_session_timeout), which you configure in a similar fashion. 

Configuring both the statement_timeout and idle_in_transaction_session_timeout settings will help with cancelling long running queries and transactions.

 

Tracking and managing through Azure Monitor 

You can receive an alert based on monitoring metrics for your Azure Database for PostgreSQL server. The alert triggers when the value of a specified metric exceeds a threshold you assign. The alert triggers both when the condition first exists and then afterwards, when the condition has abated. 

You can configure an alert to trigger and perform the following actions: 

  • Send email notifications to the service administrator and co-administrators. 
  • Send email to additional email addresses you specify. 
  • Call a webhook. 

You can configure and get information about alert rules using: 

For more detail on creating alerts on metrics in Azure Database for PostgreSQL, see the article here. 

Combining Postgres metadata checks with Azure Monitor alerts should lead to a much healthier performing database and would protect you from unwanted surprises. 

If you have trouble setting up any extension supported by Azure Database for PostgreSQL, please let us know by contacting us at AskAzureDBforPostgreSQL@service.microsoft.com! 

 

Thank you! 

Craig Kerstiens 

Principal PM Manager – Microsoft 

 

Amol Bhatnagar 

Program Manager - Microsoft 

 

1 Comment
Occasional Visitor

Good overview on connection handling

 

Can I connect to Azure PostGre SQL using URI from external source? I am using MLFLow for ML model management and want to use Azure PostGre SQL for metrics and artefact storage. MLFLow supports URI storage with Azure Blob, but no mention of Azure PostGre