---
id: catalog-database
name: Catalog Database
version: 1.0.0
summary: Authoritative PostgreSQL system of record owned by the Catalog bounded context for books, authors, categories, publishers, and vector-indexing coordination.
owners:
  - catalog-experience-team
container_type: database
badges:
  - content: Database
    backgroundColor: blue
    textColor: blue
technology: azure-postgresql-flexible-server
authoritative: true
access_mode: readWrite
purpose: System of record for Catalog domain entities and durable coordination of asynchronous vector indexing.
classification: internal
retention: indefinite
residency: eastasia
styles:
  icon: /icons/database/postgresql.svg
---

## Purpose & Ownership Boundary

Catalog Database (`catalogdb`) is the authoritative relational system of record for the Catalog bounded context, owned by [[service|catalog-api]]. PostgreSQL was chosen for strict relational integrity across product taxonomy, transactional outbox messaging, and trigger-driven change capture for asynchronous search indexing by [[service|catalog-ingestion]].

## Data Store Profile

| Property                            | Value                                                                                                |
| :---------------------------------- | :--------------------------------------------------------------------------------------------------- |
| **Bounded Context / Owner**         | Catalog — [[team\|catalog-experience-team]]                                                          |
| **Engine / Technology**             | PostgreSQL 18 (Azure Database for PostgreSQL Flexible Server)                                        |
| **Store Type**                      | Database                                                                                             |
| **Primary Writer(s)**               | [[service\|catalog-api]] (domain writes), [[service\|catalog-ingestion]] (queue acknowledgment only) |
| **Primary Reader(s)**               | [[service\|catalog-api]], [[service\|catalog-ingestion]]                                             |
| **Authoritative / Source of Truth** | Yes                                                                                                  |
| **Data Classification**             | Internal                                                                                             |
| **Retention / TTL Policy**          | Indefinite (soft-delete flag on books; transactional outbox pruned after dispatch)                   |

## Logical Data Layout & Key Patterns

All identifiers use `snake_case`, primary keys default to time-ordered `uuidv7()`, and audit timestamps (`created_at`, `last_modified_at`) are database-generated.

| Table / Schema           | Owned Entity                         | Partition / Primary Key  | Purpose & Concurrency                                                                                                                                                                                                   |
| :----------------------- | :----------------------------------- | :----------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `books`                  | [[entity\|book]]                     | `id` (`uuidv7()`)        | Canonical book catalog, pricing, availability status, soft-delete flag (`is_deleted`), blob URN (`image`), and derived rating projection (`average_rating`, `total_reviews`); optimistic concurrency via `row_version`. |
| `authors`                | [[entity\|author]]                   | `id` (`uuidv7()`)        | Author directory; deletion restricted while referenced in `book_authors`.                                                                                                                                               |
| `categories`             | [[entity\|category]]                 | `id` (`uuidv7()`)        | Product category taxonomy with unique `name`; deleting a category sets `books.category_id` to `NULL`.                                                                                                                   |
| `publishers`             | [[entity\|publisher]]                | `id` (`uuidv7()`)        | Publisher directory with unique `name`; deleting a publisher sets `books.publisher_id` to `NULL`.                                                                                                                       |
| `book_authors`           | [[entity\|book]], [[entity\|author]] | (`book_id`, `author_id`) | Many-to-many join table; deleting a book cascades to its author links.                                                                                                                                                  |
| `pending_vector_updates` | —                                    | `book_id` (`uuid`)       | Coalescing change-capture queue with `revision` UUID and `updated_at`; repeated edits update the row in place.                                                                                                          |
| `wolverine` schema       | —                                    | Envelope ID              | Transactional outbox and inbox tables sharing the domain write transaction; partitions Kafka events by `BookId` and `FeedbackId`.                                                                                       |

## Consistency, Isolation & Security Guardrails

- **Transactional outbox:** Domain writes and outbound integration events commit in a single PostgreSQL transaction via the `wolverine` outbox schema.
- **Trigger-driven search sync:** Five database triggers (`books`, `book_authors`, `authors`, `categories`, `publishers`) enqueue affected `book_id` rows into `pending_vector_updates`. [[service|catalog-ingestion]] acquires PostgreSQL advisory lock `740031001`, processes up to `200` rows per batch, and deletes only rows whose `book_id` and `revision` still match.
- **Soft-delete & URN invariants:** Books use `is_deleted` soft deletion rather than physical row removal. The `books.image` column stores permanent blob URNs pointing to [[container|catalog-blob]], never raw bytes or expiring SAS URLs.
- **Derived rating projections:** `average_rating` and `total_reviews` on `books` are eventually consistent projections updated from [[event|FeedbackCreatedIntegrationEvent]] and [[event|FeedbackDeletedIntegrationEvent]]; [[container|rating-database]] remains authoritative for individual reviews.

:::warning
**Database-per-Service Isolation:** Only [[service|catalog-api]] may mutate catalog domain tables, and [[service|catalog-ingestion]] is restricted to reading catalog state and acknowledging `pending_vector_updates`. External services (such as [[service|basket-api]], [[service|order-api]], or [[service|rating-api]]) must never connect to `catalogdb` directly—all cross-context reads flow through [[service|catalog-api]] HTTP/gRPC contracts or Kafka events.
:::

## Operational & Resilience Notes

<AccordionGroup>
	<Accordion title="Hosting, backup retention, and environment parity">
		- **Local vs. Cloud:** Local development runs a single persistent PostgreSQL container with
		automatic dev seeding; cloud environments run Azure Database for PostgreSQL Flexible Server on
		the shared `postgres` instance alongside `orderingdb` and `schedulerdb`. - **Scaling & HA:**
		Development uses `Standard_B1ms` (single zone, 7-day backup, 32 GB); Staging uses
		`Standard_D2ds_v5` (zone-redundant HA, PgBouncer, 14-day backup, 128 GB); Production uses
		`Standard_D4ds_v5` (zone-redundant HA, PgBouncer, 35-day backup, 256 GB). - **Upgrades &
		monitoring:** Apply schema changes exclusively through versioned migrations (never by replaying
		static SQL snapshots), and monitor `pending_vector_updates` row count and oldest `updated_at` to
		detect ingestion lag.
	</Accordion>
</AccordionGroup>

## Architecture Visualizations & Schema

<NodeGraph />

<MermaidFileLoader file="mermaids/catalog-database-diagram-1.mmd" />

<AccordionGroup>
	<Accordion title="Catalog Database DDL">
		<Schema file="schema.sql" lang="sql" title="Catalog Database DDL" />
	</Accordion>
</AccordionGroup>

## Related Documentation

- [[service|catalog-api]] — Owning service endpoints, gRPC contracts, and transactional outbox.
- [[service|catalog-ingestion]] — Worker draining `pending_vector_updates` into [[container|catalog-vector]].
- [[container|catalog-vector]] — Derived Qdrant hybrid search index fed from this database.
- [[container|catalog-blob]] — Binary cover media store referenced by `books.image` URNs.
- [[adr|adr-001-postgresql-system-of-record]] — Decision establishing PostgreSQL as Catalog's system of record.
- [[adr|adr-003-database-queue-for-vector-indexing]] — Decision for trigger-based vector update coalescing.
- [[doc|operational/005-backup-restore-and-dr]] — Platform backup, restore, and disaster recovery runbook.
