Home
%3CLINGO-SUB%20id%3D%22lingo-sub-788005%22%20slang%3D%22en-US%22%3ESet%20up%20Pgpool-II%20Query%20Caching%20with%20Azure%20Database%20for%20PostgreSQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-788005%22%20slang%3D%22en-US%22%3E%3CDIV%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3EIn%20our%20previous%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FAzure-Database-for-PostgreSQL%2FImprove-Performance-of-Read-Intensive-Workloads-on-Azure-DB-for%2Fba-p%2F743860%22%20target%3D%22_self%22%3Eblog%20post%3C%2FA%3E%2C%20we%20showed%20how%20the%20performance%20of%20repeating%20SELECT%20queries%20can%20be%20improved%20by%20leveraging%20an%20external%20cache.%20As%20shown%20in%20the%20figure%20below%2C%20in%20our%20simple%20experiment%2C%20the%20response%20time%20of%20a%20repeating%20SELECT%20query%20%3CSTRONG%3Eimproved%20by%2086%25%3C%2FSTRONG%3Ewith%20around%2020%25%20degradation%20in%20first%20time%20execution%20of%20cacheable%20queries%2C%20which%20seems%20an%20acceptable%20trade-off%20for%20workloads%20with%20often%20repeating%20queries.%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F125594i58EC8A4402732A00%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22responsetime.png%22%20title%3D%22responsetime.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3E%26nbsp%3B%3CBR%20%2F%3EIn%20this%20blog%20post%2C%20we%20provide%20steps%20and%20scripts%20describing%20how%20to%20install%20and%20setup%20Pgpool%20on%20an%20Ubuntu%20VM%20to%20act%20as%20a%20connection%20pooler%20and%20query%20cache%20to%20an%20Azure%20Database%20for%20PostgreSQL%20server.%20Memcached%20is%20used%20as%20the%20backing%20cache%20store%20as%20shown%20below.%20At%20the%20end%20of%20the%20blog%20post%2C%20we%20share%20a%20Quickstart%20ARM%20template%20which%20allows%20you%20to%20quickly%20provision%20an%20Ubuntu%20VM%20with%20Pgpool-II%20preconfigured%20with%20an%20Azure%20Database%20for%20PostgreSQL%20server%20for%20quick%20proof-of-concept%20testing.%20%3CBR%20%2F%3E%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20500px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F125595iE42D0FF3B5AEB072%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22pgpool%22%20title%3D%22pgpool%22%20%2F%3E%3C%2FSPAN%3E%3CBR%20%2F%3E%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3EIn%20our%20Quickstart%20template%20below%2C%20Pgpool%20query%20cache%20is%20configured%20with%20default%20settings.%20One%20exception%20is%20using%20Memcached%20instead%20of%20shared%20memory%20option%20as%20the%20backing%20cache.%20You%20can%20fine-tune%20%3CA%20href%3D%22http%3A%2F%2Fwww.pgpool.net%2Fdocs%2Flatest%2Fen%2Fhtml%2Fruntime-in-memory-query-cache.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eadditional%20query%20caching%20settings%3C%2FA%3E%26nbsp%3Bbased%20on%20your%20workload%20and%20expertise.%20%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3ENote%20that%20if%20you%20don't%20require%20Pgpool's%20unique%20features%20like%20query%20caching%2C%20we%20recommend%20using%20a%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_blank%22%20rel%3D%22noopener%22%3Esimpler%20connection%20pooler%20like%26nbsp%3BPgBouncer%3C%2FA%3Ewith%20Azure%20Database%20for%20PostgreSQL.%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3EThe%20steps%20below%20represent%20an%20example%20scenario%20that%20you%20can%20modify%20to%20your%20needs%20by%20changing%20configuration%20settings%20in%20pgpool.conf%20(on%26nbsp%3BUbuntu%2C%20the%20Pgpool%20config%20file%20is%20located%20at%20%2Fetc%2Fpgpool2%2Fpgpool.conf)%2C%20followed%20by%20a%20Pgpool%20restart%20to%20pick%20them%20up%3A%26nbsp%3B%3C%2FFONT%3E%3C%2FFONT%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3Eservice%20pgpool2%20restart%3C%2FCODE%3E%3C%2FPRE%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3E%26nbsp%3B%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CH2%20id%3D%22toc-hId-1795353410%22%20id%3D%22toc-hId-1795353410%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3ESteps%20to%20manually%20setup%20Pgpool%20on%20Ubuntu%20VM%3C%2FFONT%3E%3C%2FH2%3E%0A%3CDIV%3E%0A%3CP%3ESteps%20below%20provide%20a%20setup%20example.%20The%20complete%20bash%20script%20for%20the%20steps%20below%20can%20be%20download%20from%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-postgresql%2Fblob%2Fmaster%2Farm-templates%2FExampleWithPgpoolQueryCache%2Fconfigure-pgpool.sh%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eour%20Github%20repository%3C%2FA%3E.%20(If%20you%20want%20to%20use%20the%20quick%20ARM%20template%20instead%2C%20scroll%20to%20the%20next%20section).%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CP%3EWe%20assume%20the%20existence%20of%20three%20environment%20variables%3A%3C%2FP%3E%0A%3CP%3E%3CFONT%20face%3D%22terminal%2Cmonaco%22%3ESERVERNAME%3C%2FFONT%3E%3A%20Short%20name%20of%20Azure%20PostgreSQL%20server%2C%20e.g.%20pgtestsrv%3CBR%20%2F%3E%3CFONT%20face%3D%22terminal%2Cmonaco%22%3EUSERNAME%3C%2FFONT%3E%3A%20Short%20username%20(without%20%40servername)%20of%20Azure%20PostgreSQL%20server%2C%20e.g.%20pguser%3CBR%20%2F%3E%3CFONT%20face%3D%22terminal%2Cmonaco%22%3EPASSWORD%3C%2FFONT%3E%3A%20Password%20of%20Azure%20PostgreSQL%20user%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-python%22%3E%3CCODE%3E%23%20Install%20Pgpool%20II%20and%20Memcached%0Aapt-get%20-y%20install%20pgpool2%20memcached%0A%20%0A%23%20Pgpool%20query%20cache%20requires%20a%20work%20directory%20to%20record%20table%20oids%0Amkdir%20-p%20%2Fvar%2Flog%2Fpgpool%2Foiddir%0Achown%20postgres%3Apostgres%20%2Fvar%2Flog%2Fpgpool%2Foiddir%0A%20%0A%23%20Before%20modifying%20Pgpool%20and%20Memcached%20config%20files%2C%20save%20them%20%0Acp%20%2Fetc%2Fpgpool2%2Fpgpool.conf%20%20%2Fetc%2Fpgpool2%2Fpgpool.conf.save%0Acp%20%2Fetc%2Fpgpool2%2Fpool_hba.conf%20%2Fetc%2Fpgpool2%2Fpool_hba.conf.save%0Acp%20%2Fetc%2Fmemcached.conf%20%2Fetc%2Fmemcached.conf.save%0A%20%0A%23%20Create%20self-signed%20certificate%20to%20allow%20connecting%20to%20Pgpool%20using%20SSL%0A%23%20Note%3A%20Adjust%20this%20step%20to%20meet%20your%20security%20requirements%0Aopenssl%20req%20-new%20-newkey%20rsa%3A4096%20-days%203650%20-nodes%20-x509%20-subj%20%22%2FC%3DUS%2FST%3DDenial%2FL%3DSeattle%2FO%3DDis%2FCN%3D%3CA%20href%3D%22http%3A%2F%2Fwww.pgpooltest.org%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ewww.pgpooltest.org%3C%2FA%3E%22%20-keyout%20%2Fetc%2Fpgpool2%2Fserver.key%20%20-out%20%2Fetc%2Fpgpool2%2Fserver.crt%0A%20%0A%23%20Modify%20Pgpool%20config%0A%23%20Turn%20SSL%20on%0Ased%20-i%20's%2Fssl%20%3D%20off%2Fssl%20%3D%20on%2Fg'%20%2Fetc%2Fpgpool2%2Fpgpool.conf%0A%20%0A%23%20Use%20SSL%20key%20and%20cert%20created%20above%0Ased%20-i%20%22s%2F%23ssl_key%20%3D%20'.%5C%2Fserver.key'%2Fssl_key%20%3D%20'%5C%2Fetc%5C%2Fpgpool2%5C%2Fserver.key'%2Fg%22%20%2Fetc%2Fpgpool2%2Fpgpool.conf%0Ased%20-i%20%22s%2F%23ssl_cert%20%3D%20'.%5C%2Fserver.cert'%2Fssl_cert%20%3D%20'%5C%2Fetc%5C%2Fpgpool2%5C%2Fserver.crt'%2Fg%22%20%2Fetc%2Fpgpool2%2Fpgpool.conf%0A%20%0A%23%20Accept%20all%20incoming%20connections%20%0Ased%20-i%20%22s%2Flisten_addresses%20%3D%20'localhost'%2Flisten_addresses%20%3D%20'*'%2Fg%22%20%2Fetc%2Fpgpool2%2Fpgpool.conf%0A%20%0A%23%20Set%20Azure%20PostgreSQL%20backend%0Ased%20-i%20%22s%2Fbackend_hostname0%20%3D%20'localhost'%2Fbackend_hostname0%20%3D%20'%24SERVERNAME.postgres.database.azure.com'%2Fg%22%20%2Fetc%2Fpgpool2%2Fpgpool.conf%20%0A%20%0A%23%20Use%20pool_hba.conf%20for%20client%20authentication%0Ased%20-i%20's%2Fenable_pool_hba%20%3D%20off%2Fenable_pool_hba%20%3D%20on%2Fg'%20%2Fetc%2Fpgpool2%2Fpgpool.conf%0A%20%0A%23%20Enable%20query%20cache%0Ased%20-i%20%22s%2Fmemory_cache_enabled%20%3D%20off%2Fmemory_cache_enabled%20%3D%20on%2Fg%22%20%2Fetc%2Fpgpool2%2Fpgpool.conf%20%0A%20%0A%23%20Use%20Memcached%20as%20query%20cache%20store%0Ased%20-i%20%22s%2Fmemqcache_method%20%3D%20'shmem'%2Fmemqcache_method%20%3D%20'memcached'%2Fg%22%20%2Fetc%2Fpgpool2%2Fpgpool.conf%0A%20%0A%23%20Require%20all%20clients%20connecting%20to%20Pgpool%20to%20authenticate%20with%20password%20(scram-sha-256%20in%20this%20case)%0Aecho%20%22host%20all%20all%200.0.0.0%2F0%20scram-sha-256%22%20%26gt%3B%20%2Fetc%2Fpgpool2%2Fpool_hba.conf%0A%20%0A%23%20Write%20pool_passwd%20used%20by%20Pgpool%20for%20password%20authentication%20with%20Azure%20PostgreSQL%20backend%0Aecho%20%24USERNAME%40%24SERVERNAME%3A%24PASSWORD%20%26gt%3B%20%2Fetc%2Fpgpool2%2Fpool_passwd%0Achmod%20600%20%2Fetc%2Fpgpool2%2Fpool_passwd%0Achown%20postgres%3Apostgres%20%2Fetc%2Fpgpool2%2F*%0A%20%0A%23%20Modify%20Memcached%20config%0A%23%20Allow%20Memcached%20to%20use%201024%20MB%20of%20memory%0Ased%20-i%20%22s%2Fm%2064%2Fm%201024%2Fg%22%20%2Fetc%2Fmemcached.conf%0A%20%0A%23%20Start%20Pgpool%20and%20Memcached%20services%0A%23%20Start%20Memcached%0Aservice%20memcached%20restart%0A%20%0A%23%20Set%20defaults%20for%20Pgpool%20service%0Aecho%20%22PGPOOL_CONFIG_FILE%3D%2Fetc%2Fpgpool2%2Fpgpool.conf%22%20%26gt%3B%26gt%3B%20%2Fetc%2Fdefault%2Fpgpool2%0Aecho%20%22PGPOOL_HBA_CONFIG_FILE%3D%2Fetc%2Fpgpool2%2Fpool_hba.conf%22%20%26gt%3B%26gt%3B%20%2Fetc%2Fdefault%2Fpgpool2%0Aecho%20%22PGPOOL_PCP_CONFIG_FILE%3D%2Fetc%2Fpgpool2%2Fpcp.conf%22%20%26gt%3B%26gt%3B%20%2Fetc%2Fdefault%2Fpgpool2%0Aecho%20%22PGPOOL_PID_FILE%3D%2Fvar%2Frun%2Fpostgresql%2Fpgpool.pid%22%20%26gt%3B%26gt%3B%20%2Fetc%2Fdefault%2Fpgpool2%0Aupdate-rc.d%20pgpool2%20defaults%0A%20%0A%23%20Start%20Pgpool%0Aservice%20pgpool2%20restart%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3CH2%20id%3D%22toc-hId--756803551%22%20id%3D%22toc-hId--756803551%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22toc-hId-986006784%22%20id%3D%22toc-hId-986006784%22%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3EDeploy%20Pgpool%20using%20Quickstart%20ARM%20template%3C%2FFONT%3E%3C%2FH2%3E%0A%3CDIV%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3EIf%20you%20would%20like%20to%20skip%20the%20above%20steps%20and%20want%20to%20quickly%20setup%20and%20test%20Pgpool%20with%20Azure%20DB%20for%20PostgreSQL%2C%20you%20can%20click%20on%20Deploy%20button%20below.%20It%20will%20take%20you%20to%20an%20ARM%20template%20form.%20Once%20you%20provide%20all%20the%20parameter%20values%2C%20it%20will%20provision%20an%20Ubuntu%20VM%20with%20Pgpool-II%20installed%2C%20configured%20and%20running%2C%20connected%20to%20an%20Azure%20Database%20for%20PostgreSQL%20server.%20Note%20that%20the%20template%20uses%20a%20self-signed%20certificate%20to%20enable%20SSL%20connections%20to%20Pgpool%2C%20which%20is%20something%20you%20might%20want%20to%20revisit%20for%20production%20use.%20The%20template%20is%20available%20in%20our%20%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FAzure%2Fazure-postgresql%2Ftree%2Fmaster%2Farm-templates%2FExampleWithPgpoolQueryCache%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EGitHub%20repository%3C%2FA%3E.%3CBR%20%2F%3E%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fportal.azure.com%2F%23create%2FMicrosoft.Template%2Furi%2Fhttps%253A%252F%252Fraw.githubusercontent.com%252FAzure%252Fazure-postgresql%252Fmaster%252Farm-templates%252FExampleWithPgpoolQueryCache%252Fazuredeploy.json%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20161px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F125597i409D994D9B98C0AC%2Fimage-size%2Fsmall%3Fv%3D1.0%26amp%3Bpx%3D200%22%20alt%3D%22deploybutton%22%20title%3D%22deploybutton%22%20%2F%3E%3C%2FSPAN%3E%3C%2FA%3E%3CBR%20%2F%3E%26nbsp%3B%3CBR%20%2F%3EOnce%20you%20have%20deployed%20the%20ARM%20Template%20%3CEM%3Esuccessfully%20without%20any%20errors%3C%2FEM%3E%2C%20you%20will%20be%20able%20to%20see%20the%20hostname%2C%20sshCommand%20and%20psqlCommand%20as%20part%20of%20Deployment%20Outputs.%20%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%20%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F125599iB4C23B3AE23C0974%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22outputs%22%20title%3D%22outputs%22%20%2F%3E%3C%2FSPAN%3E%3CBR%20%2F%3E%26nbsp%3B%3CBR%20%2F%3E%3CSTRONG%3Ehostname%3C%2FSTRONG%3Eis%20the%20Public%20DNS%20for%20the%20Ubuntu%20VM%20hosting%20Pgpool%20and%20Memcached%26nbsp%3B%3CBR%20%2F%3E%3CSTRONG%3EsshCommand%3C%2FSTRONG%3Eprovides%20the%20ssh%20command%20to%20connect%20to%20the%20Ubuntu%20VM%20hosting%20Pgpool%20and%20Memcached%3CBR%20%2F%3E%3CSTRONG%3EpsqlCommand%3C%2FSTRONG%3Eprovides%20the%20psql%20command%20to%20connect%20the%20PostgreSQL%20Server%20%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3Epsql%20%22host%3D%7BdnsLabelPrefix%7D.%7Bregion%7D.cloudapp.azure.com%20port%3D5432%20dbname%3D%7Byour_database%7D%20user%3D%7Byour_username%7D%22%20%3CBR%20%2F%3E%3CSTRONG%3EFor%20Example%3A%3C%2FSTRONG%3Epsql%20%22host%3Dpgpoolvm.westus2.cloudapp.azure.com%20port%3D5432%20dbname%3Dpostgres%20user%3Dpguser%40pgtestsrv%22%26nbsp%3B%20%3CBR%20%2F%3E%26nbsp%3B%3CBR%20%2F%3EHope%20this%20helps!%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3E%3CSTRONG%3EBenjamin%20Satzger%3C%2FSTRONG%3E%3CBR%20%2F%3EPrincipal%20Software%20Engineer%2C%20Microsoft%3CBR%20%2F%3E%3C%2FFONT%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CFONT%20style%3D%22background-color%3A%20%23ffffff%3B%22%3E%3CSTRONG%3EParikshit%20Savjani%3C%2FSTRONG%3E%3CBR%20%2F%3ESenior%20Program%20Manager%2C%20Microsoft%3CBR%20%2F%3E%3C%2FFONT%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-788005%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F125692iA5FC74635D7FEA0F%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22pgpool%22%20title%3D%22pgpool%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3ESteps%20to%20set%20up%20query%20caching%20and%20connection%20pooling%20using%20Pgpool-II%20and%20Memcached.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-788005%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ECaching%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Ememcached%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPerformance%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epgpool%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epostgres%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3Epostgresql%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft
In our previous blog post, we showed how the performance of repeating SELECT queries can be improved by leveraging an external cache. As shown in the figure below, in our simple experiment, the response time of a repeating SELECT query improved by 86% with around 20% degradation in first time execution of cacheable queries, which seems an acceptable trade-off for workloads with often repeating queries.
 
responsetime.png
 
In this blog post, we provide steps and scripts describing how to install and setup Pgpool on an Ubuntu VM to act as a connection pooler and query cache to an Azure Database for PostgreSQL server. Memcached is used as the backing cache store as shown below. At the end of the blog post, we share a Quickstart ARM template which allows you to quickly provision an Ubuntu VM with Pgpool-II preconfigured with an Azure Database for PostgreSQL server for quick proof-of-concept testing.
 pgpool
In our Quickstart template below, Pgpool query cache is configured with default settings. One exception is using Memcached instead of shared memory option as the backing cache. You can fine-tune additional query caching settings based on your workload and expertise.
 
Note that if you don't require Pgpool's unique features like query caching, we recommend using a simpler connection pooler like PgBouncer with Azure Database for PostgreSQL.
 
The steps below represent an example scenario that you can modify to your needs by changing configuration settings in pgpool.conf (on Ubuntu, the Pgpool config file is located at /etc/pgpool2/pgpool.conf), followed by a Pgpool restart to pick them up: 
service pgpool2 restart
 
 

Steps to manually setup Pgpool on Ubuntu VM

Steps below provide a setup example. The complete bash script for the steps below can be download from our Github repository. (If you want to use the quick ARM template instead, scroll to the next section).

We assume the existence of three environment variables:

SERVERNAME: Short name of Azure PostgreSQL server, e.g. pgtestsrv
USERNAME: Short username (without @servername) of Azure PostgreSQL server, e.g. pguser
PASSWORD: Password of Azure PostgreSQL user

# Install Pgpool II and Memcached
apt-get -y install pgpool2 memcached
 
# Pgpool query cache requires a work directory to record table oids
mkdir -p /var/log/pgpool/oiddir
chown postgres:postgres /var/log/pgpool/oiddir
 
# Before modifying Pgpool and Memcached config files, save them 
cp /etc/pgpool2/pgpool.conf  /etc/pgpool2/pgpool.conf.save
cp /etc/pgpool2/pool_hba.conf /etc/pgpool2/pool_hba.conf.save
cp /etc/memcached.conf /etc/memcached.conf.save
 
# Create self-signed certificate to allow connecting to Pgpool using SSL
# Note: Adjust this step to meet your security requirements
openssl req -new -newkey rsa:4096 -days 3650 -nodes -x509 -subj "/C=US/ST=Denial/L=Seattle/O=Dis/CN=www.pgpooltest.org" -keyout /etc/pgpool2/server.key  -out /etc/pgpool2/server.crt
 
# Modify Pgpool config
# Turn SSL on
sed -i 's/ssl = off/ssl = on/g' /etc/pgpool2/pgpool.conf
 
# Use SSL key and cert created above
sed -i "s/#ssl_key = '.\/server.key'/ssl_key = '\/etc\/pgpool2\/server.key'/g" /etc/pgpool2/pgpool.conf
sed -i "s/#ssl_cert = '.\/server.cert'/ssl_cert = '\/etc\/pgpool2\/server.crt'/g" /etc/pgpool2/pgpool.conf
 
# Accept all incoming connections 
sed -i "s/listen_addresses = 'localhost'/listen_addresses = '*'/g" /etc/pgpool2/pgpool.conf
 
# Set Azure PostgreSQL backend
sed -i "s/backend_hostname0 = 'localhost'/backend_hostname0 = '$SERVERNAME.postgres.database.azure.com'/g" /etc/pgpool2/pgpool.conf 
 
# Use pool_hba.conf for client authentication
sed -i 's/enable_pool_hba = off/enable_pool_hba = on/g' /etc/pgpool2/pgpool.conf
 
# Enable query cache
sed -i "s/memory_cache_enabled = off/memory_cache_enabled = on/g" /etc/pgpool2/pgpool.conf 
 
# Use Memcached as query cache store
sed -i "s/memqcache_method = 'shmem'/memqcache_method = 'memcached'/g" /etc/pgpool2/pgpool.conf
 
# Require all clients connecting to Pgpool to authenticate with password (scram-sha-256 in this case)
echo "host all all 0.0.0.0/0 scram-sha-256" > /etc/pgpool2/pool_hba.conf
 
# Write pool_passwd used by Pgpool for password authentication with Azure PostgreSQL backend
echo $USERNAME@$SERVERNAME:$PASSWORD > /etc/pgpool2/pool_passwd
chmod 600 /etc/pgpool2/pool_passwd
chown postgres:postgres /etc/pgpool2/*
 
# Modify Memcached config
# Allow Memcached to use 1024 MB of memory
sed -i "s/m 64/m 1024/g" /etc/memcached.conf
 
# Start Pgpool and Memcached services
# Start Memcached
service memcached restart
 
# Set defaults for Pgpool service
echo "PGPOOL_CONFIG_FILE=/etc/pgpool2/pgpool.conf" >> /etc/default/pgpool2
echo "PGPOOL_HBA_CONFIG_FILE=/etc/pgpool2/pool_hba.conf" >> /etc/default/pgpool2
echo "PGPOOL_PCP_CONFIG_FILE=/etc/pgpool2/pcp.conf" >> /etc/default/pgpool2
echo "PGPOOL_PID_FILE=/var/run/postgresql/pgpool.pid" >> /etc/default/pgpool2
update-rc.d pgpool2 defaults
 
# Start Pgpool
service pgpool2 restart

 

Deploy Pgpool using Quickstart ARM template

If you would like to skip the above steps and want to quickly setup and test Pgpool with Azure DB for PostgreSQL, you can click on Deploy button below. It will take you to an ARM template form. Once you provide all the parameter values, it will provision an Ubuntu VM with Pgpool-II installed, configured and running, connected to an Azure Database for PostgreSQL server. Note that the template uses a self-signed certificate to enable SSL connections to Pgpool, which is something you might want to revisit for production use. The template is available in our GitHub repository.
 
 deploybutton
 
Once you have deployed the ARM Template successfully without any errors, you will be able to see the hostname, sshCommand and psqlCommand as part of Deployment Outputs.  
  outputs
 
hostname is the Public DNS for the Ubuntu VM hosting Pgpool and Memcached 
sshCommand provides the ssh command to connect to the Ubuntu VM hosting Pgpool and Memcached
psqlCommand provides the psql command to connect the PostgreSQL Server   
psql "host={dnsLabelPrefix}.{region}.cloudapp.azure.com port=5432 dbname={your_database} user={your_username}"
For Example: psql "host=pgpoolvm.westus2.cloudapp.azure.com port=5432 dbname=postgres user=pguser@pgtestsrv" 
 
Hope this helps!
 
Benjamin Satzger
Principal Software Engineer, Microsoft
 
Parikshit Savjani
Senior Program Manager, Microsoft