Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Dimitri Fontaine

Rating No ratings yet

This book is intended for developers working on applications that use a database server. The book specifically addresses the PostgreSQL RDBMS: it actually is the world’s most advanced Open Source database, just like it says in the tagline on the official website. By the end of this book you’ll know why, and you’ll agree! I wanted to write this book after having worked with many customers who were making use of only a fraction of what SQL and PostgreSQL are capable of delivering. In most cases, developers I met with didn’t know what’s possible to achieve in SQL. As soon as they realized — or more exactly, as soon as they were shown what’s possible to achieve —, replacing hundreds of lines of application code with a small and efficient SQL query, then in some cases they would nonetheless not know how to integrate a raw SQL query in their code base. Integrating a SQL query and thinking about SQL as code means using the same advanced tooling that we use when using other programming languages: versioning, automated testing, code reviewing, and deployment. Really, this is more about the developer’s workflow than the SQL code itself… In this book, you will learn best practices that help with integrating SQL into your own workflow, and through the many examples provided, you’ll see all the reasons why you might be interested in doing more in SQL. Primarily, it means writing fewer lines of code. As Dijkstra said, we should count lines of code as lines spent, so by learning how to use SQL you will be able to spend less to write the same application!

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A practical, developer-focused guide to PostgreSQL that shows how to push business logic into SQL, treat queries as first-class code, and dramatically reduce application code by mastering the full power of the world's most advanced open-source database. Ideal for application developers, backend engineers, and data-savvy programmers who want to move beyond basic CRUD operations. 【Book Arc】 - **Opening (~0%–6%)**: The book opens with a manifesto for SQL-centric development, arguing that most developers use only a fraction of PostgreSQL's capabilities. It sets up the core philosophy: treat SQL as code with versioning, testing, and deployment, and demonstrates early on how replacing hundreds of lines of application code with a single efficient query is both possible and desirable. - **Early (~6%–19%)**: Introduces the fundamentals of writing SQL queries as an application developer, covering psql basics, query structure, and the concept of business logic embedded in SQL. This section includes practical examples like calendar generation with `generate_series`, JSON querying with `jsonb`, and the first look at stored procedures as a data access API. - **Early (~19%–28%)**: Dives into the developer workflow around SQL: using psql as a powerful REPL, implementing regression testing with pgTap, and establishing a comprehensive indexing strategy. This is where the book shifts from "writing queries" to "engineering with SQL," covering the six main index types (B-tree, Hash, GiST, SP-GiST, GIN, BRIN) and when to use each. - **Middle (~28%–38%)**: Explores schemaless design with JSONB and hstore, then systematically breaks down the SELECT statement into its core components. The focus here is on PostgreSQL's rich processing functions (like `extract()`, `to_char()`, and `CASE`) that let you do data transformation directly in the database rather than in application code. - **Middle (~38%–47%)**: Covers advanced query patterns: ORDER BY with complex expressions, GROUP BY with CUBE for multi-dimensional aggregation, UNION/EXCEPT for set operations, and a deep dive into window functions. This section demonstrates the analytical power that becomes available when you stop treating SQL as a simple data-fetching language. - **Late (~47%+)**: The book concludes with an in-depth look at understanding relations and joins from a mathematical perspective, setting up the reader for advanced topics like query optimization and extension development that are covered in the final chapters. 【Key Takeaways】 - **SQL is a programming language, not a query tool** (Early): The book's central thesis is that SQL deserves the same engineering rigor as any other language—versioning, testing, code review, and deployment. This mindset shift alone can transform how you build database-backed applications. - **Business logic belongs in the database** (Early): Every clause in a query (SELECT, FROM, WHERE, ORDER BY) encodes business decisions. By moving this logic into SQL, you reduce application code, improve correctness, and eliminate the "race conditions and Heisenbugs" that come from implementing relational checks in application code. - **psql is the professional's REPL** (Early): The command-line tool psql is not a fallback for those without a GUI—it's the preferred interface for experts. Its support for variables, decorated literals, and scripting makes it ideal for both exploration and production work. - **Testing SQL is non-negotiable** (Early): pgTap provides a framework for writing unit tests directly in SQL, covering both result sets and schema integrity. This makes regression testing for database code as straightforward as it is for application code. - **Indexing is a strategy, not an afterthought** (Early): PostgreSQL offers six index types (B-tree, Hash, GiST, SP-GiST, GIN, BRIN), each designed for specific use cases. Understanding when to use each—not just defaulting to B-tree—is critical for query performance. - **Window functions changed SQL forever** (Middle): The ability to process peer rows through a frame—seeing multiple values at once to compute a single output—is described as a watershed moment in SQL history. Mastering window functions unlocks analytical queries that would otherwise require complex application code. - **PostgreSQL's extensibility is its superpower** (Early): The ability to add new data types, operators, and indexing support from "userland code" means you can enhance SQL itself. The JSONB `@>` operator with GIN index support is a prime example of this extensibility in action. - **Set operations are underused tools** (Middle): UNION, EXCEPT, and their variants allow for elegant solutions to complex data problems, like finding drivers who scored points in one race but not the next. Parenthesizing branches improves readability and clarifies where ORDER BY applies. 【Reading Tips】 - **Skim the preface and introduction** (~0%–6%) if you're already convinced SQL is powerful; the real value starts with the practical query examples in Part 3. - **Deep-read the indexing chapter** (Early ~28%)—this is where performance knowledge lives, and it's worth understanding each index type even if you don't memorize all the details. - **Pay special attention to the window functions section** (Middle ~47%)—this is described as a "before and after" moment in SQL capability, and the frame concept is the key to unlocking it. - **Work through the examples in psql yourself**—the book is built around hands-on learning, and the REPL is the intended environment for experimentation. - **Treat the stored procedures section as a bridge** (Early ~19%)—it connects simple queries to the more advanced data access patterns that follow, so don't skip it even if you're tempted to jump ahead. 【Coverage Limits】 The excerpts cover roughly the first half of the book (through ~47%), focusing on query writing, testing, indexing, and window functions. Later chapters on extensions, advanced relations, and performance tuning are mentioned in the table of contents but not covered in detail in this guide.
Page 9
. . . 358 42.4 A Primer on Authoring PostgreSQL Extensions . . . . . . . . 359 42.5 A Short List of Noteworthy Extensions . . . . . . . . . . . . . 359 43 Au...
View in text
Excerpt 2
order by clause implements the idea that we want to display the track names in the order they appear on the disk. Not only that, it also incorporates the spe...
View in text
Excerpt 3
The GiST project studies the engineering andmathematics be- hind content-based indexing for massive amounts of complex content. Its implementation in Postgre...
View in text
Excerpt 4
-04-16): 1 ( 2 select driverid, 3 format('%s %s', 4 drivers.forename, 5 drivers.surname) 6 as name 7 8 from results 9 join drivers using(driverid) 10 11 wher...
View in text
Excerpt 5
ces are based on bigint arithmetic, so the range cannot ex- ceed the range of an eight-byte integer (-9223372036854775808 to 9223372036854775807). So if you...
View in text
Excerpt 6
ontent Chapter 28 Normalization | 229 8. Rule of Robustness Robustness the child of transparency and simplicity. 9. Rule of Representation Fold knowledge int...
View in text
Excerpt 7
tions. The next chapter — Data Manipulation and Concurrency Control — dives into this topic. To summarize, denormalization techniques are meant to optimize a...
View in text
Excerpt 8
pleMVCC system and will simply remove the data les on disk. Note that the truncate command is still MVCC compliant: 1 select count(*) from foo; 2 3 begin; 4...
View in text
Tags
AI categories
DatabaseSQLBackend
Publish Year: 2021
Language: English
File Format: PDF
File Size: 1.7 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…