Donut Shop Database: PostgreSQL Design for a Growing Business
What this is
A complete PostgreSQL database that runs a donut shop's daily work: stock, suppliers, staff, customers, orders and payments. It has 3 schemas, 25 tables, 32 procedures and 9 triggers.
Why PostgreSQL
I chose PostgreSQL because it is reliable and scalable. If the business gets bigger, with more customers, orders and staff, the database keeps working smoothly and doesn't need to be rebuilt.
How it is organized
The database is split into 3 sections (schemas), so everything has a clear place:
Inventory: donuts, ingredients, suppliers, purchases, stock levels, discounts, waste, and price history.
HR: employee roles, shifts, salaries, and payment history.
Sales: customers, orders, payments, and canceled items.
All the tables are connected, so the data stays consistent. There are also 4 ready-made reports (views), such as daily sales, sales by donut, and canceled items.
Procedures: ready-made, safe operations
Every change to the data goes through one of 32 stored procedures, such as record_items, record_payment and cancel_sale. Each procedure checks the data first and gives a clear error message if something is wrong, for example a negative price, a donut that doesn't exist, or an order bigger than the stock.
I built it this way for speed and safety. Running operations directly from Python is slower, so my Python functions call these procedures instead. The work then runs inside the database in a single call, and the application never writes to the tables directly. In my testing, this was really fast.
Triggers: the automatic watchmen
9 triggers react the moment data changes, with no extra code needed:
A new order item is added: the donut stock goes down automatically and the change is logged.
A donut or ingredient price changes: the old and new prices are saved in a history table.
An employee's salary or shift changes: the change is saved in a history table.
Stock gets low: a warning is created for donuts at 10 or fewer and for ingredients below their minimum level.
New ingredients are purchased: the stock level is updated automatically.
Built with data analysis in mind
Customers and employees are never deleted, only marked inactive, so history is kept. Together with the price, salary, waste and inventory logs, this gives clean data for reports, forecasting and dashboards.
This hero helps a RevOps buyer understand the offer in 3 seconds.
→ Headline: the outcome they want
→ Subline: the pipeline number mismatch they hate
→ One CTA
→ Client logos right under the button
→ Visual: forecast, pipeline, win rate
Autonomous backend that turns local merchants into bespoke landing pages with almost no human in the loop. Celery tasks discover businesses, an LLM designs each page, then two independent AI gates - an HTTP health oracle and a 5-axis design critic - validate it. Rejections are mined into lessons and re-injected, so the system self-heals and regenerates a better layout automatically. Qualified pages wait behind a signed preview link and a human approval gate; nothing ships without consent, payment and a global kill switch
Case Study — The $800M Trade That Happened Before the News
The event
In 2026, oil futures activity jumped into the hundreds of millions of dollars in under a minute — before a major geopolitical announcement hit the wire. The BBC investigated. The CFTC opened a probe. No one was accused of wrongdoing, but the pattern was clear: either someone knew, or a machine reacted faster than any human could verify【4†L28-L33】.
The problem underneath
This is not a story about one bad actor. It is a story about a market where algorithms read headlines in milliseconds, detect keywords like "Navy," "escorted," "Hormuz," and fire sell orders before anyone has checked whether the headline is real, relevant, or already priced in. The lag between communication and repricing no longer exists.
The gap in every fast bot
Speed is not the edge. Verification is. A bot that trades in 3 milliseconds on a tweet it has not classified is not fast — it is reckless. It will fire on olive oil, on pipeline protests, on sarcasm. It will trade stale news because it acted on the first sighting instead of the newest. It will bleed.
What a reasoning gate changes
Before a single dollar moves, the system returns four fields: IsOil, Impact, Strength, Confidence. A trade only executes if confidence passes a hard threshold. Everything else is discarded. The gate costs ~300ms. On a 20-second cycle, that is 2% of the window — noise. In exchange, it removes the class of trades that destroyed accounts in the $800M case: premature, unverified, headline-driven execution.
The fallback
A reasoning gate is a single point of failure. Three consecutive API failures trip a circuit breaker. The system falls back to a deterministic keyword engine. It gets dumber for one cycle. It does not go dark. It does not crash. It degrades — gracefully.
The honest gap
No local/hosted two-tier setup yet. A small local model for the first pass, escalating only when impact is ambiguous, is the next optimization. Auditability was chosen over raw speed for v1. Every trade has a single reasoning trace you can read line by line.
The point
In a market where machines react in milliseconds to unverified headlines, the edge is not reacting first. It is deciding not to react at all. The $800M trade happened before the news. The next one will happen before the verification — unless the system has a gate.