• Stars
    star
    335
  • Rank 121,344 (Top 3 %)
  • Language
    Shell
  • License
    MIT License
  • Created about 11 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Run pgbouncer in a dyno along with your application

Heroku buildpack: pgbouncer

This is a Heroku buildpack that allows one to run pgbouncer in a dyno alongside application code. It is meant to be used in conjunction with other buildpacks.

The primary use of this buildpack is to allow for transaction pooling of PostgreSQL database connections among multiple workers in a dyno. For example, 10 unicorn workers would be able to share a single database connection, avoiding connection limits and Out Of Memory errors on the Postgres server.

FAQ

  • Q: Why should I use transaction pooling?

  • A: You have many workers per dyno that hold open idle Postgres connections and you want to reduce the number of unused connections. This is a slightly more complete answer from stackoverflow

  • Q: Why shouldn't I use transaction pooling?

  • A: If you need to use named prepared statements, advisory locks, listen/notify, or other features that operate on a session level. Please refer to PGBouncer's feature matrix for all transaction pooling caveats.

Disable Prepared Statements

With Rails 4.1, you can disable prepared statements by appending ?prepared_statements=false to the database's URI. Set the PGBOUNCER_PREPARED_STATEMENTS config var to false for the buildpack to do that for you.

Rails versions 4.0.0 - 4.0.3, reportedly can't disable prepared statements at all. Make sure your framework is up to date before troubleshooting prepared statements failures.

Rails 3.2 - 4.0 also requires an initializer to properly cast the prepared_statements configuration string as a boolean. This initializer is adapted from this commit. In file config/initializers/database_connection.rb insert the following:

require "active_record/connection_adapters/postgresql_adapter"

class ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
  alias initialize_without_config_boolean_coercion initialize
  def initialize(connection, logger, connection_parameters, config)
    if config[:prepared_statements] == 'false'
      config = config.merge(prepared_statements: false)
    end
    initialize_without_config_boolean_coercion(connection, logger, connection_parameters, config)
  end
end

Usage

Example usage:

$ ls -a
Gemfile  Gemfile.lock  Procfile  config/  config.ru

$ heroku buildpacks:add heroku/pgbouncer
Buildpack added. Next release on pgbouncer-test-app will use heroku/pgbouncer.
Run `git push heroku main` to create a new release using this buildpack.

$ heroku buildpacks:add heroku/ruby
Buildpack added. Next release on pgbouncer-test-app will use:
  1. https://github.com/heroku/heroku-buildpack-pgbouncer
  2. https://github.com/heroku/heroku-buildpack-ruby
Run `git push heroku main` to create a new release using these buildpacks.

$ cat Procfile
web:    bin/start-pgbouncer bundle exec unicorn -p $PORT -c ./config/unicorn.rb -E $RACK_ENV
worker: bundle exec rake worker

$ git push heroku main
...
-----> Multipack app detected
-----> Fetching custom git buildpack... done
-----> pgbouncer app detected
       Using pgbouncer version: 1.7-heroku
-----> Fetching and vendoring pgbouncer into slug
-----> Moving the configuration generation script into app/bin
-----> Moving the start-pgbouncer script into app/bin
-----> pgbouncer done
-----> Fetching custom git buildpack... done
...

The buildpack will install and configure pgbouncer to connect to DATABASE_URL over a TLS connection, where available. Prepend bin/start-pgbouncer to any process in the Procfile to run pgbouncer alongside that process.

PgBouncer Version

  • Heroku-18: v1.17.0
  • Heroku-20: v1.17.0
  • Heroku-22: v1.17.0

Multiple Databases

It is possible to connect to multiple databases through pgbouncer by setting PGBOUNCER_URLS to a list of config vars. Example:

$ heroku config:add PGBOUNCER_URLS="DATABASE_URL HEROKU_POSTGRESQL_ROSE_URL"
$ heroku run bash

~ $ env | grep 'HEROKU_POSTGRESQL_ROSE_URL\|DATABASE_URL'
HEROKU_POSTGRESQL_ROSE_URL=postgres://u9dih9htu2t3ll:[email protected]:5482/db6h3bkfuk5430
DATABASE_URL=postgres://uf2782hv7b3uqe:[email protected]:5622/deamhhcj6q0d31

~ $ bin/start-pgbouncer env # filtered for brevity
HEROKU_POSTGRESQL_ROSE_URL=postgres://u9dih9htu2t3ll:[email protected]:6000/db2
DATABASE_URL=postgres://uf2782hv7b3uqe:[email protected]:6000/db1

⚠️ A referenced configuration variable in PGBOUNCER_URLS must not be empty, and must be a valid PostgreSQL connection string.

Follower Replica Databases

As of v0.3.2 of this buildpack, it is possible to use pgbouncer to connect to multiple databases that share a database name, such as a leader and follower. To use, add the follower's config var to PGBOUNCER_URLS as detailed in the Multiple Databases section.

If you are using Octopus Replication to send reads to a replica, make sure to include the color url of your leader in the SLAVE_DISABLED_FOLLOWERS blacklist. Otherwise, Octopus will attempt to use your leader as a read-only replica, potentially doubling your connection count.

Tweak settings

Some settings are configurable through app config vars at runtime. Refer to the appropriate documentation for pgbouncer configurations to see what settings are right for you.

  • PGBOUNCER_POOL_MODE Default is transaction
  • PGBOUNCER_MAX_CLIENT_CONN Default is 100
  • PGBOUNCER_DEFAULT_POOL_SIZE Default is 1
  • PGBOUNCER_MIN_POOL_SIZE Default is 0
  • PGBOUNCER_RESERVE_POOL_SIZE Default is 1
  • PGBOUNCER_RESERVE_POOL_TIMEOUT Default is 5.0 seconds
  • PGBOUNCER_SERVER_LIFETIME Default is 3600.0 seconds
  • PGBOUNCER_SERVER_IDLE_TIMEOUT Default is 600.0 seconds
  • PGBOUNCER_URLS should contain all config variables that will be overridden to connect to pgbouncer. For example, set this to AMAZON_RDS_URL to send RDS connections through pgbouncer. The default is DATABASE_URL.
  • PGBOUNCER_LOG_CONNECTIONS Default is 1. If your app does not use persistent database connections, this may be noisy and should be set to 0.
  • PGBOUNCER_LOG_DISCONNECTIONS Default is 1. If your app does not use persistent database connections, this may be noisy and should be set to 0.
  • PGBOUNCER_LOG_POOLER_ERRORS Default is 1
  • PGBOUNCER_STATS_PERIOD Default is 60
  • PGBOUNCER_SERVER_RESET_QUERY Default is empty when pool mode is transaction, and "DISCARD ALL;" when session.
  • PGBOUNCER_IGNORE_STARTUP_PARAMETERS Adds parameters to ignore when pgbouncer is starting. Some postgres libraries, like Go's pq, append this parameter, making it impossible to use this buildpack. Default is empty and the most common ignored parameter is extra_float_digits. Multiple parameters can be seperated via commas. Example: PGBOUNCER_IGNORE_STARTUP_PARAMETERS="extra_float_digits, some_other_param"
  • PGBOUNCER_QUERY_WAIT_TIMEOUT Default is 120 seconds, helps when the server is down or the database rejects connections for any reason. If this is disabled, clients will be queued infinitely.

For more info, see CONTRIBUTING.md

Using the edge version of the buildpack

The heroku/pgbouncer buildpack points to the latest stable version of the buildpack published in the Buildpack Registry. To use the latest version of the buildpack (the code in this repository, run the following command:

$ heroku buildpacks:add https://github.com/heroku/heroku-buildpack-pgbouncer

Notes

Currently, the connection string parsing requires the connection string to be in a specific format:

postgres://<user>:<pass>@<host>:<port>/<database>

This corresponds to the regular expression ^postgres(?:ql)?:\/\/([^:]*):([^@]*)@(.*?):(.*?)\/(.*?)$. All components must be present in order for the buildpack to correctly parse the connection string.

More Repositories

1

react-refetch

A simple, declarative, and composable way to fetch data for React components
JavaScript
3,439
star
2

legacy-cli

Heroku CLI
Ruby
1,370
star
3

heroku-pg-extras

A heroku plugin for awesome pg:* commands that are also great and fun and super.
JavaScript
1,306
star
4

heroku-buildpack-nodejs

The official Heroku buildpack for Node.js apps.
Shell
1,265
star
5

node-js-getting-started

Getting Started with Node on Heroku
EJS
1,054
star
6

logplex

[DEPRECATED] Heroku log router
Erlang
984
star
7

heroku-buildpack-python

The official Heroku buildpack for Python apps
Ruby
953
star
8

heroku-django-template

A Django 2.0 base template featuring all recommended best practices for deployment on Heroku and local development.
Python
901
star
9

node-js-sample

This repository is deprecated. Head over to https://github.com/heroku/node-js-getting-started
JavaScript
847
star
10

cli

Heroku CLI
JavaScript
847
star
11

rails_12factor

Ruby
845
star
12

python-getting-started

Getting Started with Python on Heroku.
Python
818
star
13

heroku-buildpack-php

The official PHP buildpack for Heroku.
Shell
799
star
14

heroku-buildpack-go

Heroku Go Buildpack
Shell
790
star
15

heroku-buildpack-ruby

Heroku's Ruby Buildpack
Ruby
778
star
16

hk

DEPRECATED: see
Go
709
star
17

heroku-buildpack-static

[DEPRECATED] Heroku buildpack for handling static sites and single page web apps
Ruby
681
star
18

heroku-repo

Plugin for heroku CLI that can manipulate the repo
JavaScript
680
star
19

vegur

Vegur: HTTP Proxy Library
Erlang
620
star
20

heroku-accounts

Helps use multiple accounts on Heroku.
JavaScript
548
star
21

django-heroku

[DEPRECATED] Do not use! See https://github.com/heroku/django-heroku/issues/56
Python
465
star
22

devcenter-embedded-tomcat

Java
330
star
23

webapp-runner

Lightweight Application Launcher. Launch your webapp in the most popular open source web container available with a single command.
Java
319
star
24

docker-registry-client

A Go API client for the v2 Docker Registry API
Go
287
star
25

heroku-buildpack-google-chrome

Run (headless) Google Chrome on Heroku
Shell
283
star
26

stack-images

Recipies for building Heroku's stack images
Shell
264
star
27

java-getting-started

Getting Started with Java on Heroku
HTML
248
star
28

identity

[DEPRECATED] Login and OAuth management service for Heroku
CSS
247
star
29

go-getting-started

Getting Started with Go on Heroku https://devcenter.heroku.com/articles/getting-started-with-go
Dockerfile
246
star
30

heroku-buildpack-nginx

Run NGINX in a Heroku app
Shell
242
star
31

heroku-buildpack-apt

Buildpack that installs APT based dependencies
Shell
239
star
32

log-shuttle

HTTP log transport.
Go
236
star
33

terrier

Terrier is a Image and Container analysis tool that can be used to scan Images and Containers to identify and verify the presence of specific files according to their hashes.
Go
226
star
34

umpire

HTTP metrics monitoring endpoint
Ruby
221
star
35

platform-api

Ruby HTTP client for the Heroku API
Ruby
211
star
36

starboard

onboarding, offboarding, or crossboarding made easy
SCSS
204
star
37

salesforce-bulk

Python interface to the Salesforce.com Bulk API
Python
203
star
38

php-getting-started

Getting Started with PHP on Heroku
Twig
200
star
39

heroku-container-tools

DEPRECATED Heroku Toolbelt plugin to help configure, test and release apps to Heroku using local containers.
JavaScript
195
star
40

heroku-buildpack-scala

Heroku buildpack: Scala
Shell
190
star
41

node-heroku-client

A wrapper around the Heroku API for Node.js
JavaScript
188
star
42

roadmap

This is the public roadmap for Salesforce Heroku services.
187
star
43

vulcan

A build server in the cloud.
Ruby
172
star
44

pg_lock

Use Postgres advisory lock to isolate code execution across machines
Ruby
168
star
45

awsdetailedbilling

A toolkit for importing AWS detailed billing reports into Redshift
JavaScript
167
star
46

heroku-buildpack-java

A Heroku buildpack for Java apps.
Shell
167
star
47

pulse

DEPRECATED: Real-time Heroku operations dashboard
Clojure
161
star
48

heroku.rb

DEPRECATED! Official Heroku Ruby Legacy API wrapper
Ruby
161
star
49

heroku-buildpack-multi

[DEPRECATED] Please use https://devcenter.heroku.com/articles/using-multiple-buildpacks-for-an-app instead
Shell
157
star
50

erlang-in-anger

A little guide about how to be the Erlang medic in a time of war. It is first and foremost a collection of tips and tricks to help understand where failures come from, and a dictionary of different code snippets and practices that helped developers debug production systems that were built in Erlang.
TeX
157
star
51

plexy

A toolkit for building excellent APIs with Elixir
Elixir
154
star
52

heroku-buildpack-multi-procfile

Everyone gets a Procfile!
Shell
150
star
53

log2viz

DEFUNCT: Realtime analysis of your Heroku app logs.
Ruby
145
star
54

heroku.py

DEPRECATED! Heroku API wrapper for Python.
Python
142
star
55

facebook-template-nodejs

JavaScript
136
star
56

ruby-getting-started

Getting Started with Ruby on Heroku
Ruby
120
star
57

heroku-buildpack-chromedriver

Installs chromedriver in a Heroku slug
Shell
117
star
58

heroku-buildpack-clojure

Heroku's buildpack for Clojure applications.
Shell
115
star
59

mobile-template1

JavaScript
115
star
60

instruments

Collecting metrics over discrete time intervals
Go
112
star
61

heroku-sbt-plugin

An sbt plugin for deploying Heroku Scala applications
Scala
111
star
62

heroku-buildpack-erlang

Erlang buildpack
Shell
107
star
63

cli-engine

TypeScript
97
star
64

semver.io

*DEPRECATED* The semver.io instance has now been sunset: https://github.com/heroku/semver.io/issues/74
CoffeeScript
96
star
65

facebook-template-php

example facebook app for heroku
PHP
96
star
66

terraform-provider-heroku

Terraform Heroku provider
Go
95
star
67

dotnet-buildpack

ASP.NET 5 Buildpack
Shell
92
star
68

kensa

A tool to help Heroku add-on providers integrate their services with Heroku
Ruby
92
star
69

netrc

Reads and writes netrc files.
Ruby
89
star
70

hstore_example

Ruby
89
star
71

alpinehelloworld

An Alpine-based Docker example
Python
85
star
72

heroku-kong

πŸ’ Kong API gateway as a Heroku app
Lua
84
star
73

heroku-buildpack-hello

Shell
82
star
74

heroku-releases-retry

CLI plugin to allow retrying the latest release-phase command
JavaScript
79
star
75

faceplate

A Node.js wrapper for Facebook authentication and API
JavaScript
76
star
76

rails_stdout_logging

Logs to stdout so you don't have to
Ruby
76
star
77

shaas

Shell as a Service: API to inspect and execute scripts in a server's environment via HTTP and WebSockets
Go
75
star
78

devcenter-spring-mvc-hibernate

AspectJ
75
star
79

heroku-buildpack-core-data

A Heroku Buildpack that generates a REST webservice from a Core Data model
Shell
74
star
80

heroku-buildpack-emberjs

**This buildpack is deprecated!** Please use the official Node.js buildpack combined with the static or nginx buildpack instead.
Ruby
72
star
81

facebook-template-python

Python
69
star
82

devcenter-java

Java
62
star
83

heroku-buildpack-c

C Language Pack
Shell
62
star
84

nibs

JavaScript
61
star
85

heroku-buildpack-gradle

This is a Heroku buildpack for Gradle apps. It uses Gradle to build your application and OpenJDK to run it.
Shell
61
star
86

heroku-buildpack-ember-cli

A Heroku buildpack for ember-cli apps; powers dashboard.heroku.com
Shell
60
star
87

heroku-guardian

Easy to use CLI security checks for the Heroku platform. Validate baseline security configurations for your own Heroku deployments.
Python
59
star
88

list-of-ingredients

An example of using Create React App with Rails 5 API and ActiveAdmin on Heroku
Ruby
56
star
89

heroku-fork

Heroku CLI plugin to fork an existing app into a new app
JavaScript
55
star
90

salesforce-buildpack

Heroku Buildpack for Salesforce
Shell
53
star
91

ruby-rails-sample

Ruby
52
star
92

facebook-template-ruby

CSS
52
star
93

heroku-jupyterlab

An example of running JupyterLab on Heroku, with Amazon S3.
Python
52
star
94

heroku-maven-plugin

This plugin is used to deploy Java applications directly to Heroku without pushing to a Git repository.
Java
51
star
95

cnb-builder-images

Recipes for building Heroku's Cloud Native Buildpacks builder images
Shell
51
star
96

stillir

Cache environment variables as Erlang app variables
Erlang
51
star
97

heroku-gradle-plugin

A Gradle plugin for deploying JAR and WAR files to Heroku.
Java
49
star
98

rails_serve_static_assets

Ruby
49
star
99

x

A set of packages for reuse within Heroku Go applications
Go
49
star
100

template-java-spring-hibernate

Java
48
star