• Stars
    star
    413
  • Rank 104,427 (Top 3 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated about 3 years ago

Reviews

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

Repository Details

PostgreSQL Connection Proxy by Crunchy Data (beta)

Installation Guide - Crunchy Proxy

crunchy logo

Project

crunchy-proxy is a minimal PostgreSQL-aware proxy used to handle PostgreSQL application requests. The diagram below depicts a PostgreSQL client application connecting to the crunchy-proxy, which appears to the PostgreSQL application as any other PostgreSQL database connection. The crunchy-proxy accepts the inbound client requests, and routes them to an appropriate PostgreSQL cluster member (as described in more detail below).

scaledwidth=

To the PostgreSQL server, the crunchy-proxy is transparent and appears as any other PostgreSQL client would appear. As described in more detail below, the crunchy-proxy currently provides the following capabilities:

  • ability to route messages based on the SQL command type - writes are sent to a master and reads are sent to replicas in a round-robin fashion

  • configuration via JSON file

  • PostgreSQL healthcheck

  • ability to route inbound client messages based on the health of PostgreSQL

  • REST administrative interface

  • ability to publish healthcheck events to consumers outside of the proxy

  • ability to load balance SQL statements across multiple replica backends

  • connection pooling

  • support for certificate and SSL based authentication

As described under "Approach" below, implementation of the crunchy-proxy requires that (1) a user has access to their application’s SQL and (2) a user has the ability to add basic annotations to their SQL statements.

Approach

One of the key features of the crunchy-proxy is the ability to route different SQL statements to a PostgreSQL cluster members based on certain pre-defined rules. This capability enables end-user applications to see a PostgreSQL cluster as a single connection. In order to support this capability, it is necessary that crunchy-proxy have knowledge of the SQL statements that a PostgreSQL application sends to PostgreSQL.

One approach to addressing the requirement that a proxy be "SQL aware" (e.g. pgpool) is to first parse each SQL statement looking for grammar that would imply a write or update and to then send those SQL statements to the cluster master. SQL statements that are determined to be read-only in nature are deemed safe to send to the cluster replica members. Remember, in PostgreSQL cluster replica members are only able to process SQL that is read-only in nature.

crunchy-proxy takes a different approach to SQL routing. Instead of attempting to parse the entire SQL grammer of each SQL statement, it looks for a simple annotation, supplied by the end-user within their SQL statement, to determine the routing destination.

For example, the following SQL statement is annotated to indicate it is a read-only SQL statement:

/* read */ select now();

Examples of a write SQL statement include the following:

create table boofar (id int);
drop table boofar (id int);

If a SQL statement does not include an annotation, the the statement is deemed a write and thus sent to the master cluster member.

By parsing only the annotation, crunchy-proxy simplifies the complexity associated with determining whether a SQL statement is a write or read and thus to which member (master or replica) of a PostgreSQL cluser to send a SQL statement.

In taking this approach, crunchy-proxy has assumed (1) a user has access to their application’s SQL and (2) a user has the ability to add the annotation in their SQL statements. If they do, then they can use the crunchy-proxy for SQL routing.

Of course these assumptions introduce certain limitations on the crunchy-proxy. Nonetheless, it was determined that these assumptions will not be unduly limiting in the usability of the crunchy-proxy and that the resulting limiations are justified by the benefits of (1) reduction in complexity associated with SQL parsing implementation, (2) increase in proxy throughput and (3) improved routing accuracy of the SQL parsing.

PostgreSQL Wire Protocol

crunchy-proxy operates at the PostgreSQL wire protocol (network) layer to understand PostgreSQL client authentication requests and SQL statements passed by a client to a PostgreSQL backend.

As crunchy-proxy uses annotations to route messages to the backend, the proxy primarily examines SQL statements for proxy-specific annotations and does very little processing of the messages sent between a client and an actual backend.

Its important to note that the proxy does not implement all features of libpq or provide an application interface similar to a JDBC driver or other language driver.

The following resources are useful in understanding the PostgreSQL wire protocol:

In the future, by working at the wire protocol level, crunchy-proxy can implement a variety of features important for high speed proxy handling and for supporting PostgreSQL features.

Execution

The proxy is a golang binary, you execute it as follows:

$> crunchy-proxy start --config=config.yaml

To run the proxy at different logging output levels:

$> crunchy-proxy start --config=config.yaml --log-level=<level>

Where <level> is one of the following:

  • debug

  • info

  • error

  • fatal

Detailed documentation including configuration file format and developer information is found in the User Guide

For Docker users, you can run the proxy using the run-docker.sh script.

Feedback

If you find a bug, or want to provide feedback on the design and features feel free to create a github issue.

Copyright © 2017 Crunchy Data Solutions, Inc.

CRUNCHY DATA SOLUTIONS, INC. PROVIDES THIS GUIDE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF NON INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.

Crunchy, Crunchy Data Solutions, Inc. and the Crunchy Hippo Logo are trademarks of Crunchy Data Solutions, Inc.

More Repositories

1

postgres-operator

Production PostgreSQL for Kubernetes, from high availability Postgres clusters to full-scale database-as-a-service.
Go
3,884
star
2

crunchy-containers

Containers for Managing PostgreSQL on Kubernetes by Crunchy Data
Shell
1,011
star
3

pg_tileserv

A very thin PostGIS-only tile server in Go. Takes in HTTP tile requests, executes SQL, returns MVT tiles.
Go
858
star
4

pgmonitor

PostgreSQL Monitoring, Metrics Collection and Alerting Resources from Crunchy Data
PLpgSQL
568
star
5

pg_featureserv

Lightweight RESTful Geospatial Feature Server for PostGIS in Go
Go
451
star
6

pg_eventserv

Small golang server to push PgSQL listen/notify events into websockets
Go
200
star
7

postgres-operator-examples

Examples for deploying applications with PGO, the Postgres Operator from Crunchy Data
Handlebars
184
star
8

pgCompare

pgCompare – a straightforward utility crafted to simplify the data comparison process, providing a robust solution for comparing data across various database platforms.
Java
79
star
9

postgresql-prometheus-adapter

Remote storage adapter enabling Prometheus to use PostgreSQL as a long-term store for time-series metrics.
Go
75
star
10

Postgres-AI-Tutorial

Python
58
star
11

pgnodemx

A PostgreSQL extension that provides SQL functions to allow capture of node OS metrics via SQL queries.
C
47
star
12

pgstigcheck-inspec

PostgreSQL STIG Compliance Testing Solution using InSpec
Ruby
41
star
13

crunchy-postgresql-manager

PostgreSQL-as-a-Service Management Utility. Superseded by Crunchy Container Suite: https://github.com/CrunchyData/crunchy-containers
HTML
36
star
14

pgbouncer_fdw

PostgreSQL Foreign Data Wrapper to Connect to pgbouncer
PLpgSQL
32
star
15

crunchy_check_access

Functions and views to facilitate PostgreSQL object access inspection
PLpgSQL
27
star
16

bridge-cli

CLI for Crunchy Bridge
Crystal
23
star
17

postgres-operator-client

Go
14
star
18

pgo-osb

Open Service Broker Implementation Based on the Crunchy PostgreSQL Operator
Go
13
star
19

crunchy-demo-data

Demo Data that is used in Crunchy Data Demos and Workshop that can be freely redistributed
HTML
13
star
20

crunchy-katacoda

Shell
10
star
21

openshift-postgres-cartridge

OpenShift V2 PG cartridge
Shell
8
star
22

backup-scripts

High Performance PostgreSQL Tools
Shell
8
star
23

crunchy-watch

A PostgreSQL Automated Failover Container
Go
8
star
24

pgSimload

pgSimload, a versatile CLI tool to create activity on PostgreSQL server(s) and/or test HA in Crunchy Postgres or Crunchy Postgres for Kubernetes
Go
7
star
25

terraform-provider-crunchybridge

Go
6
star
26

exporter2perfdata

Go
5
star
27

pgseccomp

C
5
star
28

pg_goggles

pg_goggles provides better scaled and summarized views into PostgreSQL's cryptic internal counters.
4
star
29

openshift-postgres-rls-cartridge

Shell
3
star
30

nixpkgs

Nix
3
star
31

crunchy-bridge-operator

Go
3
star
32

crunchy-hugo-theme

Theme used for Crunchy Data generated documentation
JavaScript
3
star
33

skybridge2

Go
2
star
34

postgres-ci-cd-demo

Python
2
star
35

crunchy-bridge-for-analytics-examples

1
star
36

os-pg-testing

Shell
1
star
37

pgmonitor-extension

PLpgSQL
1
star
38

postgres-realtime-demo

Python
1
star
39

doc-content-guide

1
star
40

spatial-suite-rest-demo

Python
1
star
41

crystal-keyring

Keyring implementation for Crystal
Crystal
1
star
42

crunchy-docs

TypeScript
1
star
43

crunchy-postgresql-manager-openshift

CPM Openshift version
HTML
1
star
44

homebrew-brew

homebrew tap
Ruby
1
star
45

spatial-suite-demo-webapp

JavaScript
1
star