Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 8

PostgreSQL vs. NoSQL: An ADR for Relational Database Selection

Recommended resources

Welcome to the final lesson of our first module on PostgreSQL. In our last session, you established a crucial professional habit: maintaining an engineering journal. You learned that this practice is not about simple note-taking, but about structured reflection—forming hypotheses, recording observations, and analyzing outcomes to accelerate your learning.

This lesson introduces a complementary practice: the Architecture Decision Record (ADR). While your journal is the detailed, often private, log of your day-to-day discovery process, an ADR is a concise, formal document that communicates a significant, long-lasting architectural choice to your team and your future self. Today, you will write the first ADR for your capstone project, fulfilling the final learning outcome for this module and solidifying all the concepts we have covered.

1. Why Document Decisions?

Imagine joining a project and finding a critical component built with an unusual technology. You ask "Why did we use this?" and no one remembers. The original developers have left, and the remaining team can only guess at the context. This loss of knowledge leads to wasted effort, as teams may endlessly re-litigate old decisions or, worse, rip out a well-chosen component because they don't understand its original purpose.

ADRs are the solution. They are the "why" log for your project's architecture.

Architecture Decision Records (ADR) as a LOG that answers "WHY?"

To understand the problem that ADRs solve, watch the first few minutes of the video "Architecture Decision Records (ADR) as a LOG that answers 'WHY?'" from the CodeOpinion channel.

Pay close attention to the explanation of institutional knowledge loss from the initial problem and how that motivates the need for a record. Then, watch the introduction to Michael Nygard's template, which covers the core components of an ADR: Title, Context, Decision, Status, and Consequences.

The key takeaway is that an ADR captures the context surrounding a decision, not just the decision itself. This context is what allows future engineers to evaluate whether the choice is still valid as requirements and technologies evolve.


2. The Structure of an Architecture Decision Record

To be effective, ADRs must be lightweight and consistent. A popular format is the Markdown Architectural Decision Record (MADR).

Solution-Architecture-Practice/docs/frameworks/adr-framework.md at main · JCallico/Solution-Architecture-Practice · GitHub

This document from a solution architecture practice on GitHub provides an excellent framework for using ADRs. It gives a clear, practical template and guidelines for writing effective records.

First, read the introductory sections to understand what ADRs are and their benefits. Then, carefully study the ADR template provided. Finally, and most importantly, internalize the "Do's and Don'ts". Notice the emphasis on using data and benchmarks over vague claims—a principle central to your goal of engineering mastery.

A strong ADR is not an essay. It's a structured, objective summary that presents:

  • Context and Problem Statement: What problem are we trying to solve?
  • Decision Drivers: What are the key requirements or constraints influencing this decision? (e.g., performance needs, data structure, team skills).
  • Considered Options: What are the viable alternatives?
  • Decision Outcome: What did we decide, and why? This includes the rationale and a clear-eyed view of both positive and negative consequences (trade-offs).

3. Building the Argument: PostgreSQL vs. MongoDB

Now, let's apply this framework to the first major architectural decision for your Narrative Intelligence Engine: choosing a primary database. The two main contenders are PostgreSQL (a relational/SQL database) and MongoDB (a document/NoSQL database).

To make a defensible choice, we must analyze the project's requirements against the strengths and weaknesses of each option.

The Case for a Relational Model (PostgreSQL)

Your project's data—books, chapters, paragraphs, characters, events—is inherently relational. A book has chapters; a chapter contains paragraphs; an event involves characters at a specific time. This structured, interconnected data is the natural domain of a relational database. Your long-term goal of building a knowledge graph and performing temporal reasoning only amplifies the need for a system that excels at managing complex relationships.

Let's look at some data-driven arguments.

MongoDB vs. PostgreSQL: Performance & Functionality

The video "MongoDB vs. PostgreSQL" provides a clear visual explanation of the core difference between the two models.

Watch the section from "Relational vs Document Database". It uses a simple e-commerce example to demonstrate data normalization and how relational databases use joins to connect related but separate tables. This directly maps to how you would store books and chapters separately.

But what about flexibility? What if you extract entities from a book and they have different attributes? This is a classic argument for a document database like MongoDB.

MongoDB vs. PostgreSQL: Performance & Functionality

Continuing with the same video, let's see how PostgreSQL addresses this.

Watch the segment on JSON vs JSONB. It explains that PostgreSQL has a binary JSON type (JSONB) that is efficient, indexable, and designed to store flexible, semi-structured data.

This is a critical point: by using PostgreSQL with its powerful JSONB capabilities, you get the best of both worlds—the integrity and query power of a relational model for your core structured data, and the flexibility of a document store for your semi-structured metadata, all within a single system.

The Performance Trade-off

The ultimate goal of your platform is analysis: finding contradictions, tracking relationships, and reasoning over narrative. These are complex, analytical queries that often involve joining data from multiple tables. How do our options compare here?

Comparative Read Performance Analysis of PostgreSQL and ... - MDPI

This research paper provides a direct, data-backed comparison of PostgreSQL and MongoDB for workloads involving complex queries, which is highly relevant to your project.

Start with the abstract to get the main finding: PostgreSQL is significantly faster for analytical queries. Then, review the decision framework in Table 7. Walk through each row of that table and think about how it applies to the Narrative Intelligence Engine. For example, is your primary workload "Simple document retrieval" or "Complex analytical queries"? Is your data structure "Highly relational" or does it have a "Frequently evolving schema"?

The evidence from this paper is compelling. For a system that will rely on "Complex analytical queries," "Multi-table joins," and where "Strong consistency is critical," PostgreSQL is the more suitable choice. MongoDB's strengths in schema flexibility and horizontal scaling are less critical for the MVP and can be addressed by PostgreSQL's JSONB features and vertical scaling, respectively.


4. Your First Architecture Decision Record

You now have the structure, the context, and the evidence to write your first ADR.

Action Item:

  1. In your project repository, create a directory structure: docs/decisions/.
  2. Inside this directory, create a new file named ADR-0001-select-postgresql-as-primary-database.md.
  3. Use the template below, which synthesizes our analysis, as the content for your file. Read through it carefully to ensure you understand how each part of the argument connects to the resources we've reviewed. This is the standard you'll aim for in future ADRs.




# ADR-0001: Select PostgreSQL as Primary Database

**Status:** Accepted

**Date:** YYYY-MM-DD




## Context and Problem Statement

The Narrative Intelligence Engine requires a persistent data store for its core entities (books, chapters, chunks), extracted information (characters, locations, events), and vector embeddings. The initial version of the platform will function as a RAG system, but the architecture must support future evolution towards a knowledge graph, temporal reasoning, and complex analytical queries. We need to choose a primary database technology that can serve the MVP's needs while providing a robust foundation for these future capabilities.




## Decision Drivers

*   **Data Structure:** The core narrative data is highly structured and relational (books contain chapters, which contain chunks).
*   **Query Complexity:** The system's value derives from its ability to run complex analytical queries (e.g., "Find all characters present in scenes where character X was also present," "Identify timeline inconsistencies across chapters"). This requires powerful join capabilities.
*   **Data Integrity:** Ensuring consistency (ACID compliance) is critical for reliably detecting contradictions and maintaining narrative coherence.
*   **Extensibility:** The database must support vector storage and search (`pgvector`) for the RAG functionality and provide a pathway for graph-like queries.
*   **Operational Simplicity:** For the initial versions, a single, versatile database system is preferable to a complex polyglot persistence architecture.




## Considered Options

1.  **PostgreSQL:** A mature, open-source object-relational database system known for its reliability, feature robustness, and extensibility.
2.  **MongoDB:** A popular, open-source document-oriented NoSQL database known for its schema flexibility and horizontal scalability.




## Decision Outcome

**Chosen option:** PostgreSQL.




### Rationale

PostgreSQL is selected because its core strengths align directly with the primary requirements of the Narrative Intelligence Engine, both immediate and long-term.

1.  **Superior for Relational and Analytical Workloads:** The project's data model is fundamentally relational. PostgreSQL's mature query planner and execution engine are optimized for the complex joins and aggregations needed for narrative analysis. As demonstrated by comparative studies (U., J.; S., V.; A., S. 2025), PostgreSQL consistently outperforms MongoDB by a significant margin (1.6x-15.1x) on complex analytical queries.
2.  **ACID Compliance:** Strong transactional guarantees are essential for the integrity of the narrative graph. This is a native strength of PostgreSQL.
3.  **Sufficient Flexibility with JSONB:** While MongoDB's schema flexibility is appealing, PostgreSQL's JSONB data type provides a powerful and indexable solution for storing semi-structured data (e.g., extracted entity attributes) without sacrificing the integrity of the core relational model. This gives us the "best of both worlds."
4.  **Unified Data Platform:** PostgreSQL, with the `pgvector` extension, can serve as a single store for relational data, semi-structured JSON, and vector embeddings. This simplifies the initial architecture, reduces operational overhead, and avoids data synchronization issues between different database systems.




### Positive Consequences

*   A single, robust data store for the entire application stack simplifies development, deployment, and maintenance.
*   The architecture is primed for future complex analytical features.
*   Leverages the vast ecosystem and proven reliability of PostgreSQL.
*   Strong data consistency from the start.




### Negative Consequences

*   Requires upfront schema design. Changes to the core schema will require formal migrations, which are less flexible than MongoDB's on-the-fly schema evolution.
*   While PostgreSQL can scale vertically to handle very large loads, horizontal scaling (sharding) is generally more complex to implement than in MongoDB. This is a potential concern for massive scale in the distant future but not a limitation for the foreseeable project scope.



Conclusion

Congratulations on completing the first module! You have not only learned the fundamentals of SQL and PostgreSQL but have also established two of the most important habits of a professional engineer: keeping a reflective implementation journal and authoring data-driven architecture decision records. This ADR serves as the capstone for this module, bringing together your technical knowledge and your commitment to engineering rigor.

Key Takeaways:

  • An Architecture Decision Record (ADR) formally documents a significant architectural choice, its context, and its consequences.
  • ADRs prevent knowledge loss and provide a rationale for future development and maintenance.
  • A good ADR is data-driven, objective, and clearly articulates the trade-offs between considered options.
  • PostgreSQL is the optimal choice for the Narrative Intelligence Engine due to its strengths in handling relational data, complex analytical queries, and its ability to manage semi-structured and vector data through extensions.

With your data layer now designed and defended, you are ready to build the application that will sit on top of it. In the next module, "API Development with FastAPI," you will learn how to create a high-performance, asynchronous web API in Python. This will be the service that receives user queries, interacts with your PostgreSQL database, and eventually orchestrates the entire RAG pipeline.