Skip to content

Commit 11eb89e

Browse files
authored
feat(stac-db): Provide sensible defaults for pgSTAC db parameters (#1)
1 parent f51f9c1 commit 11eb89e

File tree

2 files changed

+626
-3
lines changed

2 files changed

+626
-3
lines changed

lib/database/index.ts

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
import { Construct } from "constructs";
77
import { BootstrapPgStac, BootstrapPgStacProps } from "../bootstrapper";
88

9+
const instanceSizes: Record<string, number> = require("./instance-memory.json");
10+
911
/**
1012
* An RDS instance with pgSTAC installed. This is a wrapper around the
1113
* `rds.DatabaseInstance` higher-level construct making use
@@ -18,12 +20,22 @@ export class PgStacDatabase extends Construct {
1820
constructor(scope: Construct, id: string, props: PgStacDatabaseProps) {
1921
super(scope, id);
2022

23+
const defaultParameters = this.getParameters(
24+
props.instanceType?.toString() || "m5.large",
25+
props.parameters
26+
);
2127
const parameterGroup = new rds.ParameterGroup(this, "parameterGroup", {
2228
engine: props.engine,
2329
parameters: {
24-
max_locks_per_transaction: "1024",
25-
work_mem: "64000",
26-
temp_buffers: "32000",
30+
shared_buffers: defaultParameters.sharedBuffers,
31+
effective_cache_size: defaultParameters.effectiveCacheSize,
32+
work_mem: defaultParameters.workMem,
33+
maintenance_work_mem: defaultParameters.maintenanceWorkMem,
34+
max_locks_per_transaction: defaultParameters.maxLocksPerTransaction,
35+
temp_buffers: defaultParameters.tempBuffers,
36+
seq_page_cost: defaultParameters.seqPageCost,
37+
random_page_cost: defaultParameters.randomPageCost,
38+
...props.parameters,
2739
},
2840
});
2941

@@ -45,6 +57,45 @@ export class PgStacDatabase extends Construct {
4557

4658
this.pgstacSecret = bootstrap.secret;
4759
}
60+
61+
public getParameters(
62+
instanceType: string,
63+
parameters: PgStacDatabaseProps["parameters"]
64+
): DatabaseParameters {
65+
// https://github.com/aws/aws-cli/issues/1279#issuecomment-909318236
66+
const memory_in_kb = instanceSizes[instanceType] * 1024;
67+
68+
// It's only necessary to consider passed in parameters for any value that used to
69+
// derive subsequent values. Values that don't have dependencies will be overriden
70+
// when we unpack the passed-in user parameters
71+
const maxConnections = parameters?.maxConnections
72+
? Number.parseInt(parameters.maxConnections)
73+
: // https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_Limits.html#RDS_Limits.MaxConnections
74+
Math.min(Math.round((memory_in_kb * 1024) / 9531392), 5000);
75+
const sharedBuffers = parameters?.sharedBufers
76+
? Number.parseInt(parameters.sharedBufers)
77+
: Math.round(0.25 * memory_in_kb);
78+
79+
const effectiveCacheSize = Math.round(0.75 * memory_in_kb);
80+
const workMem = Math.floor(sharedBuffers / maxConnections);
81+
const maintenanceWorkMem = Math.round(0.25 * sharedBuffers);
82+
83+
const tempBuffers = 128 * 1024;
84+
const seqPageCost = 1;
85+
const randomPageCost = 1.1;
86+
87+
return {
88+
maxConnections: `${maxConnections}`,
89+
sharedBuffers: `${sharedBuffers}`,
90+
effectiveCacheSize: `${effectiveCacheSize}`,
91+
workMem: `${workMem}`,
92+
maintenanceWorkMem: `${maintenanceWorkMem}`,
93+
maxLocksPerTransaction: "1024",
94+
tempBuffers: `${tempBuffers}`,
95+
seqPageCost: `${seqPageCost}`,
96+
randomPageCost: `${randomPageCost}`,
97+
};
98+
}
4899
}
49100

50101
export interface PgStacDatabaseProps extends rds.DatabaseInstanceProps {
@@ -53,3 +104,50 @@ export interface PgStacDatabaseProps extends rds.DatabaseInstanceProps {
53104
readonly pgstacUsername?: BootstrapPgStacProps["pgstacUsername"];
54105
readonly secretsPrefix?: BootstrapPgStacProps["secretsPrefix"];
55106
}
107+
108+
export interface DatabaseParameters {
109+
/**
110+
* @default - LEAST({DBInstanceClassMemory/9531392}, 5000)
111+
*/
112+
readonly maxConnections: string;
113+
114+
/**
115+
* @default - 25% of instance memory
116+
*/
117+
readonly sharedBuffers: string;
118+
119+
/**
120+
* @default - 75% of instance memory
121+
*/
122+
readonly effectiveCacheSize: string;
123+
124+
/**
125+
* @default - shared buffers divided by max connections
126+
*/
127+
readonly workMem: string;
128+
129+
/**
130+
* @default - 25% of shared buffers
131+
*/
132+
readonly maintenanceWorkMem: string;
133+
134+
/**
135+
* @default 1024
136+
*/
137+
readonly maxLocksPerTransaction: string;
138+
139+
/**
140+
* @default 131172 (128 * 1024)
141+
*/
142+
readonly tempBuffers: string;
143+
144+
/**
145+
* @default 1
146+
*/
147+
readonly seqPageCost: string;
148+
149+
/**
150+
* @default 1.1
151+
*/
152+
readonly randomPageCost: string;
153+
}

0 commit comments

Comments
 (0)