Skip to content

PoC: database with PostgreSQL

Checks whether PostgreSQL fits the AREA data: users, workflows with a variable number of nodes, deduplicated hook events and stored OAuth tokens. It serves the persistence selection document. This PoC and pocs/database-mongodb/ reach the same target, so the two can be compared.

  • Author: ClĂ©ment
  • Date: 2026-11-30
  • Timebox: 45 minutes
  • Why this timebox: the same for both database PoCs; long enough to start a database in Docker and run a short script, short enough to leave time for the selection document before the Plan defense.
  • Time actually spent: about 15 minutes
  • Stack: PostgreSQL 16 (postgres:16-alpine in Docker Compose), Python 3 with psycopg 3, Fernet (cryptography)

Target

The same for both database PoCs.

  • The database starts with docker compose up, its data kept in a named volume: compose.yaml is written and passes docker compose config, but was not started with Docker yet (see Evidence)
  • The connection string is read from the DATABASE_URL environment variable, not a literal in the source
  • A schema for users, workflows, workflow nodes and processed hook events (schema.sql), plus OAuth tokens
  • A script saves one user and one workflow with a trigger, a condition and 4 reactions, then reads the nodes back in order; nothing in the model limits the number of reactions to 4
  • Saving the same hook event twice (same subscription, same external id) is rejected by the database
  • After a restart of the server, the data is still there (server stopped and started on the same data directory)
  • An OAuth token is stored encrypted, not in clear text (bonus)

Where I was when the time ran out

Everything is reached on a PostgreSQL 16 server. The only part left is running the same thing through docker compose up and down (see Evidence for why).

What took the most time

Nothing on PostgreSQL itself. The time went into the setup around it: the Docker image could not be pulled on the machine used, so the checks ran on a local PostgreSQL 16 server instead, and the area role and database had to be created by hand there (Compose does it from the environment).

What it would take to go further

  • Run docker compose up -d, the script, docker compose down, up -d, the script again, to tick the first target.
  • Migrations: schema.sql only runs when the volume is created; a real server needs a migration tool (Prisma or TypeORM migrations on NestJS, Alembic on FastAPI).
  • Keep the encryption key out of the database and its backups, and plan its rotation.
  • Measure the hook dedup under load: two workers inserting the same event at the same time.

Running the PoC

cd pocs/database-postgresql
cp .env.example .env
# Fill OAUTH_ENCRYPTION_KEY with the output of the command written in .env.example
set -a; . ./.env; set +a
docker compose up -d --wait          # creates the schema on the first start of the volume
python -m venv .venv && . .venv/bin/activate
pip install -r requirements.txt
python demo.py
docker compose down                  # without -v: the volume is kept
docker compose up -d --wait
python demo.py                       # "Workflows already in the database" is now 1

docker compose down -v deletes the volume, only use it to start from an empty database.

Evidence

Checks run on PostgreSQL 16.15 (the image in compose.yaml is the same major version) with the schema applied from schema.sql:

===== RUN 1
Workflows already in the database: 0
Saved user 1, workflow 1; token stored as ciphertext (gAAAAABrDb5C8eDG...)
Workflow 1, nodes in order:
  0: trigger   {'event': 'issue_opened', 'service': 'github'}
  1: condition {'field': 'issue.labels', 'value': 'bug', 'operator': 'contains'}
  2: reaction  {'action': 'post_message', 'service': 'discord'}
  3: reaction  {'action': 'send', 'service': 'email'}
  4: reaction  {'action': 'post_message', 'service': 'slack'}
  5: reaction  {'action': 'create_page', 'service': 'notion'}
  -> 1 trigger, 1 condition, 4 reactions
Duplicate hook event rejected: uq_subscription_external_event
Same event with ON CONFLICT DO NOTHING: skipped
===== server stopped, then started on the same data directory
===== RUN 2
Workflows already in the database: 1
...
  • Duplicate inserted by hand with psql: ERROR: duplicate key value violates unique constraint "uq_subscription_external_event".
  • In the oauth_tokens table the token column holds only the Fernet ciphertext (gAAAAABrDb5C...); the script also checks it decrypts back.
  • Without DATABASE_URL the script stops with DATABASE_URL must be set.
  • docker compose config --quiet passes. docker compose up was not run: pulling postgres:16-alpine from Docker Hub was blocked on the machine used.

Impressions

  • Liked: the database does the safety work. The unique constraint rejects a duplicate hook event, and INSERT ... ON CONFLICT DO NOTHING lets a hook worker skip it without handling an exception, which is exactly the "at least once plus idempotency" target of the hooks question. JSONB stores each node's settings freely while the workflow structure (order, kinds, foreign keys) stays strict.
  • Got in the way: nothing in PostgreSQL itself; only the environment (image pull, creating the role by hand).
  • For AREA: our data is relational (a user has services, tokens and workflows; a workflow has ordered nodes; deleting a user must delete the rest), and ON DELETE CASCADE, transactions and constraints cover it directly. Compared with MariaDB it brings JSONB with indexes, RETURNING and ON CONFLICT; compared with MongoDB, the dedup and the links between users and workflows are enforced by the database instead of the code.