@@ -27,9 +27,47 @@ function hasVpc(
2727}
2828
2929/**
30- * An RDS instance with pgSTAC installed. This is a wrapper around the
31- * `rds.DatabaseInstance` higher-level construct making use
32- * of the BootstrapPgStac construct.
30+ * An RDS instance with pgSTAC installed and PgBouncer connection pooling.
31+ *
32+ * This construct creates an optimized pgSTAC database setup that includes:
33+ * - RDS PostgreSQL instance with pgSTAC extension
34+ * - PgBouncer connection pooler (enabled by default)
35+ * - Automated health monitoring system
36+ * - Optimized database parameters for the selected instance type
37+ *
38+ * ## Connection Pooling with PgBouncer
39+ *
40+ * By default, this construct deploys PgBouncer as a connection pooler running on
41+ * a dedicated EC2 instance. PgBouncer provides several benefits:
42+ *
43+ * - **Connection Management**: Pools and reuses database connections to reduce overhead
44+ * - **Performance**: Optimizes connection handling for high-traffic applications
45+ * - **Scalability**: Allows more concurrent connections than the RDS instance alone
46+ * - **Health Monitoring**: Includes comprehensive health checks to ensure availability
47+ *
48+ * ### PgBouncer Configuration
49+ * - Pool mode: Transaction-level pooling (default)
50+ * - Maximum client connections: 1000
51+ * - Default pool size: 20 connections per database/user combination
52+ * - Instance type: t3.micro EC2 instance
53+ *
54+ * ### Health Check System
55+ * The construct includes an automated health check system that validates:
56+ * - PgBouncer service is running and listening on port 5432
57+ * - Connection tests to ensure accessibility
58+ * - Cloud-init setup completion before validation
59+ * - Detailed diagnostics for troubleshooting
60+ *
61+ * ### Connection Details
62+ * When PgBouncer is enabled, applications connect through the PgBouncer instance
63+ * rather than directly to RDS. The `pgstacSecret` contains connection information
64+ * pointing to PgBouncer, and the `connectionTarget` property refers to the
65+ * PgBouncer EC2 instance.
66+ *
67+ * To disable PgBouncer and connect directly to RDS, set `addPgbouncer: false`.
68+ *
69+ * This is a wrapper around the `rds.DatabaseInstance` higher-level construct
70+ * making use of the BootstrapPgStac construct.
3371 */
3472export class PgStacDatabase extends Construct {
3573 db : rds . DatabaseInstance ;
@@ -40,6 +78,7 @@ export class PgStacDatabase extends Construct {
4078 public readonly connectionTarget : rds . IDatabaseInstance | ec2 . Instance ;
4179 public readonly securityGroup ?: ec2 . SecurityGroup ;
4280 public readonly secretBootstrapper ?: CustomResource ;
81+ public readonly pgbouncerHealthCheck ?: CustomResource ;
4382
4483 constructor ( scope : Construct , id : string , props : PgStacDatabaseProps ) {
4584 super ( scope , id ) ;
@@ -186,6 +225,7 @@ export class PgStacDatabase extends Construct {
186225 this . connectionTarget = this . _pgBouncerServer . instance ;
187226 this . securityGroup = this . _pgBouncerServer . securityGroup ;
188227 this . secretBootstrapper = this . _pgBouncerServer . secretUpdateComplete ;
228+ this . pgbouncerHealthCheck = this . _pgBouncerServer . healthCheck ;
189229 } else {
190230 this . connectionTarget = this . db ;
191231 }
0 commit comments