Inside databases Greenplum
Greenplum
A massively parallel database made of PostgreSQL instances: one master that plans, and segments that hold the data and do the work.
My work
A Greenplum 6 cluster: PgBouncer, a standby master, resource groups, expansion with gpexpand, a standby cluster, vacuum on a schedule from CI. Rolled out by Ansible, backed up with WAL-G; tuning queries and I/O took load off the cluster.
How it is built
The cluster is a generalised layout from the Greenplum 6 documentation; Ansible and the MinIO storage beside it are the tools of this story, not parts of the product; PgBouncer comes with Greenplum and stands in front of it. Two segment hosts with two primaries each are an example: the numbers of a real site are not shown. Version 6 calls the first host the master; from version 7 it is the coordinator.
After the mirrors have taken over
the route after the failure unavailable takes over
- Segments 0 and 1 are marked down, and their mirrors on host 2 act as primaries. Users may not notice; a transaction that was in progress is rolled back.
- Host 2 now runs four primaries instead of two.
- None of the four segments has a live mirror now. Each primary tracks its changes in logs that are applied to its mirror when the mirror is recovered.
- Once the host is repaired, gprecoverseg brings its instances back, by default copying only the changes made while they were down (a full copy with -F), and they synchronise. Returning every segment to its preferred role is a separate step after that: gprecoverseg -r. 6
7 13
Components
Press a component to read what it does.
Client SQL
A client program such as psql, or an application through JDBC, ODBC or libpq: as with a typical PostgreSQL database. Here it connects to PgBouncer, in front of the master, the entry point of the system.
Ansible control node
The machine the playbooks are run from. It keeps the inventory: the list of hosts and of the groups they belong to. Ansible itself is normally not installed on the hosts it manages.
PgBouncer connection pool
A connection pool in front of the master. Clients connect to it, and it keeps a pool of connections to the master for each database and user. It is included in the Greenplum installation.
Master catalog only
The entry point for clients. Holds the global catalog and no user data, plans queries and hands the work to the segments.
Standby WAL replica
A copy of the master kept current by transaction-log replication. An administrator activates it; it does not take over by itself.
Primaries segments 0, 1
Primary segments of the first host: independent PostgreSQL instances, each holding a portion of the data and doing most of the query work.
Mirrors of 2 and 3
Mirrors that the first host keeps for the primaries of the second one.
Primaries segments 2, 3
Primary segments of the second host.
Mirrors of 0 and 1
Mirrors that the second host keeps for the primaries of the first. A mirror is always on another host than its primary.
MinIO S3 storage
S3-compatible object storage outside the cluster. The base backups and the WAL archive of the master and of every primary segment land here.
-
Provisioning
Every host is prepared as the installation guide asks. Then gpinitsystem, run on the master host, creates the master and the primary segments and, if its configuration says so, their mirrors and the standby.
My part I automated the deployment with Ansible: a Greenplum 6 cluster is rolled out by a playbook, not by hand. For the same cluster I automated LDAP authentication: users were put into LDAP groups by department, and the TLS certificates were issued by the admins. I expanded a cluster with gpexpand.
In detail
Before there is a cluster, every host is prepared as the installation guide requires: kernel settings, limits, XFS mounts, the gpadmin user, the package. Then gpinitsystem is run on the master host: it creates the master and the primaries and, if its configuration says so, their mirrors and the standby. Later gpexpand grows the system in two phases: it initialises the new segment instances, then redistributes the table data. Queries can access all segments before that is done; a table being redistributed is locked for reads and writes until it is. 1
2 3 11 12 18 19 Where it breaks gpinitsystem can fail half way and leave a partly created cluster behind. For that case it writes a backout script: the cause is corrected, the script is run, and only then is the initialisation tried again.
-
Connection
A client connects to PgBouncer, not to the master. PgBouncer keeps a pool of connections for each database and user, and either reuses one of them for the client or opens a new one.
My part PgBouncer in front of the master, for the many connections of BI and reports.
In detail
The PgBouncer connection pooler, from the PostgreSQL community, is included in the Greenplum installation. It maintains a pool of connections for each database and user combination, and either creates a new database connection for a client or reuses an existing one for the same user and database; when the client disconnects, the connection goes back to the pool. The client connects to the host and port on which PgBouncer is running rather than to the master’s. 15
Where it breaks By default PgBouncer pools sessions: a server connection is assigned to a client for the whole time it stays connected. Under transaction pooling it is assigned only during a transaction, and under statement pooling multi-statement transactions are disallowed, as they would break.
-
The query plan
The master parses and optimises the query and sends the plan to the segments: to all of them or, when the distribution key allows, to one.
In detail
A client sends its SQL to the master. The master parses and optimises the query and sends the plan to the segments: to all of them or, when the distribution key allows, to one. It holds the catalog and no user data. 4
5 16 Where it breaks The master is one host: if it fails, the system shuts down. Its standby follows it by WAL replication, and an administrator activates it with gpactivatestandby: it does not take over by itself.
-
Execution
Each segment works on its own portion of the data, all of them at once under a parallel plan. Rows that have to meet on another segment travel there over the interconnect.
My part I tuned queries and I/O across the segments, which took load off the cluster. Resource groups set CPU and memory limits for different kinds of load. Exporters of my own show queries, locks and resource use in Grafana.
In detail
Every segment is a PostgreSQL instance with its own portion of the data, and most of a query’s work is done there: under a parallel plan, on all of them at once. Rows that have to meet on another segment travel there over the interconnect; in the plan such a move is called a motion. Resource groups set and enforce CPU, memory and concurrent transaction limits for the roles assigned to them; resource queues are on by default, and to use resource groups instead gp_resource_manager is set to group. 4
5 17 Where it breaks The system is as fast as its slowest segment host. Data and work are meant to be spread evenly, so that all segments finish together: a query that runs on all of them has to wait for the last one.
-
Mirrors and failure
Every primary has a mirror on another host, and the master a standby that follows its log. When a primary stops answering, the master’s probe marks it down and hands its role to the mirror.
My part A standby master, and the switch to it when the master fails.
In detail
Every primary has a mirror on another host. A probe on the master (FTS) checks the primaries; when one stops answering, it makes sure the mirror is up, marks the primary down and lets the mirror act in its place. A transaction caught in the middle is rolled back, and the client should be ready to send it again. 4
6 8 13 16 Where it breaks The host that took the work over runs more primaries than on a good day. That is why a segment’s memory limit (gp_vmem_protect_limit, under resource queues) is sized for the largest number of primaries a host can be running once mirrors are activated after a failure, not for a healthy day.
-
Backup
WAL-G takes a base backup of the master and of every primary segment; each of them archives its WAL to S3-compatible storage. A restore replays the log up to a restore point common to the whole cluster.
My part I set up the backups of the cluster with WAL-G to MinIO, with retention and cleanup policies.
In detail
WAL-G, which works with Greenplum from 6.19 on, takes a base backup of the master and of every primary segment, and each of them archives its completed WAL files to S3-compatible storage. To restore is to fetch the base backups and replay the log up to a restore point: a mark common to the whole cluster, which the master and every segment have to reach. 9
10 Where it breaks Replaying needs a continuous sequence of archived WAL from the start of the base backup, on the master and on every segment. A missing file stops the replay there, unless the file is found again or recovery starts over from a later base backup that has a complete run of WAL up to the restore point.
-
Clean-up
Updates and deletes leave dead rows behind: bloat, in user tables and in the system catalog. In heap tables routine VACUUM makes their space reusable, and ANALYZE updates the statistics.
My part Vacuum runs on a schedule, started from CI.
In detail
Bloat is disk space that was used by a table or index, is no longer needed, and has not been returned to the operating system; it is created when tables and indexes see deletes and updates. In heap tables, VACUUM marks the dead rows as free space for later inserts and removes their index entries, but does not return the space to the operating system; an append-optimized table it compacts by rewriting it without the dead rows, once the percentage of dead rows exceeds gp_appendonly_compaction_threshold, 10% by default. Every table in every database must be vacuumed at least once per two billion transactions: otherwise the system can experience transaction ID wraparound, where past transactions appear to be in the future. The best practices: VACUUM after large UPDATE and DELETE operations, VACUUM the system catalog tables nightly and at least weekly (they are scanned often, and bloated they slow the whole system), and ANALYZE after INSERT, UPDATE and DELETE operations that significantly change the data: updated statistics let the planner make optimal plans. 20
21 22 23 Where it breaks VACUUM FULL requires an ACCESS EXCLUSIVE lock, which precludes any other concurrent operation on the table: the usual goal of routine vacuuming is to run standard VACUUMs often enough never to need it.
Deep dives
- Deep dive: Deploying Greenplum 6 with Ansible
A repeatable Greenplum 6 rollout with Ansible: idempotent host preparation, a guarded one-time initialisation, mirrors on other hosts and a verification play that can fail.
- Deep dive: Greenplum backups to S3
WAL-G backups of a Greenplum cluster to MinIO: base backups, continuous WAL archiving, restore points, retention as a dry run, object lock and restore tests.
Sources
- Greenplum 6: Configuring Your Systems
- Greenplum 6: Initializing a Greenplum Database System
- Greenplum 6: gpinitsystem
- Greenplum 6: About the Greenplum Architecture
- Greenplum 6: About Greenplum Query Processing
- Greenplum 6: How Greenplum Database Detects a Failed Segment
- Greenplum 6: gprecoverseg
- Greenplum 6: System Configuration best practices
- WAL-G: Greenplum
- PostgreSQL: Continuous Archiving and Point-in-Time Recovery
- Ansible: Ansible concepts
- Greenplum 6: Installing the Greenplum Database Software
- Greenplum 6: Overview of Segment Mirroring
- Greenplum 7: About the Greenplum Architecture
- Greenplum 6: Using the PgBouncer Connection Pooler
- Greenplum 6: Overview of Master Mirroring
- Greenplum 6: Using Resource Groups
- Greenplum 6: gpexpand
- Greenplum 6: Redistributing Tables
- Greenplum 6: Managing Bloat in a Database
- Greenplum 6: Routine System Maintenance Tasks
- Greenplum 6: Best Practices Summary
- Greenplum 6: Updating Statistics with ANALYZE
Next engine
ClickHouse
A column-oriented database: the values of each column are stored one after another, and a query reads from disk only the columns it needs. Its MergeTree tables keep data in sorted parts.