Sikau

PostgreSQL · Day 6 · about 40 min

Project: App event log

JSONB, enums, time and upserts together.

What happened, and when

Apps log events: sign-ups, orders, errors. Build an event log and a daily summary using this unit's PostgreSQL features.

Design the event table

Event logs grow quickly, so plan for size from the start: index by kind and time, keep payloads small, and archive or delete events older than you need for reports.

An event log is mostly written and rarely changed. Each event has a kind from a fixed list, a precise moment, and a payload whose shape depends on the kind: a sign-up has a user id, an order has a total, an error has a message. That mix — fixed columns plus a flexible document — is exactly where enums and JSONB shine together.

  • kind event_kind NOT NULL — enum: signup, order, error.
  • at timestamptz NOT NULL DEFAULT now() — when it happened.
  • data jsonb NOT NULL DEFAULT '{}' — the details for that kind.
  • tags text[] — optional labels such as 'mobile' or 'beta'.

Index what dashboards filter on: (kind, at) for "orders this week", and GIN on data if you search inside payloads.

The rest of this lesson

  • A summary table kept by upsertsexplain
  • Reading payloads in reportsexplain
  • Feature for each needmatch
  • Rerunning the summarypredict
  • Event logchallenge
  • Badge quizquiz
  • Unit 1 complete!recap

Continue this lesson free — run real code in your browser

Continue with Google

See all 16 lessons of PostgreSQL