PostgreSQL handles transactional state: 25 tables, ~600K trades, lead pipeline, engagement metrics, and campaign state machines.
HOW WE USE IT
PostgreSQL runs on port 5432 on the Apex server as the transactional database for anything that requires ACID guarantees: the trading brain (orders, positions, audit trail), the lead pipeline state machine, engagement metrics for lead scoring, and campaign state for the outbound email system.
The trading brain database has 25 tables and approximately 600K historical trades. It stores the authoritative position record that Apex references before sizing any new order. PostgreSQL is the right choice here, not ClickHouse, because the access pattern is point lookups: "what is the current position in AAPL?" requires a single index scan, not an analytical range query. ClickHouse's columnar storage makes it slower for point lookups by design.
The lead pipeline uses a state machine implemented as a PostgreSQL enum column with CHECK constraints that enforce valid transitions. A lead moves through: raw, enriched, verified, scored, ready, in-sequence, replied, booked, closed. Triggers on the transition column write audit rows to a leads_audit table. This is the same pattern used by the variant_stats table in the A/B testing system, where PostgreSQL tracks which email subject line variant each lead received.
Example workflow: implementing a state machine for a client's order management system. 1. Define a PostgreSQL ENUM type (order_status) with all valid states: pending, confirmed, in_fulfillment, shipped, delivered, refunded. 2. Add a CHECK constraint on the transition column that enforces valid moves using a lookup table of allowed (from, to) pairs stored in a small PostgreSQL table. 3. Create a BEFORE UPDATE trigger that rejects any transition not in the allowed-moves table, raising a descriptive exception. 4. Add an order_audit table with columns (order_id, from_status, to_status, changed_at, changed_by). The trigger writes a row here on every valid transition. 5. Expose the state machine transitions via a Next.js Server Action that wraps the UPDATE in a transaction. Any constraint violation rolls back cleanly. 6. Index the status column for the common query pattern: SELECT * FROM orders WHERE status = 'in_fulfillment' ORDER BY created_at.
The engagement_metrics table feeds the lead scoring V2 model. It holds open, click, reply, and unsubscribe events from Instantly webhook deliveries, keyed by lead email. The lead scoring training script joins this table against ClickHouse behavioral signals and SEC IAPD firmographic data to build the training set.
A REST proxy runs on port 3001 in front of PostgreSQL for any service that cannot use a native PostgreSQL driver. This is primarily used by the Next.js admin UI, which hits the proxy rather than maintaining a direct database connection pool.
Production numbers
25
Tables in brain DB
~600K
Historical trades
9
Lead pipeline states
3001
REST proxy port
We upgraded from a static 6-factor lead score to a three-tier behavioral composite integrating email engagement, AUM, and headcount, projecting 5 to 10% conversion uplift.
3,889 Tier A leads (V1)
Read case study →
AI / Machine LearningWe replaced fixed 50/50 email A/B splits with Thompson sampling over Beta distributions, cutting sends to convergence from never-observed to 147 median and raising open rate from 13.7% to 19.2%.
19.2% Open rate (Thompson vs 13.7% fixed)
Read case study →
PlatformsWe built a 90-inbox Google Workspace cold email system using Maildoso + Smartlead warmup, capable of 3,600 sends per day at 92 to 95% inbox placement for $369/month.
90 GWS inboxes
Read case study →
PlatformsWe built a campaign orchestration system managing 4-touch email, 3-touch LinkedIn, 2-touch Twitter, and social listening in a single PostgreSQL state machine, targeting 20-minute daily operator review.
1,000+ Daily touch capacity
Read case study →
AI / Machine LearningWe rebuilt the backtesting engine with point-in-time cursors and separate ingestion timestamps, collapsing the backtest-to-live delta from 37 percentage points to 1.4 points.
37→1.4pp Backtest-to-live delta (biased → clean)
Read case study →
Start a project
Most projects ship in under two weeks. Start with a free 30-minute discovery call.
Start a project →