PostgreSQL 16 Cookbook, Second Edition (Peter G) (Z-Library)
SQL
No Description
268
Views
0
Downloads
0.00
Total Donations
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.
Page
1
(This page has no text content)
Page
2
PostgreSQL 16 Cookbook Second Edition Solve challenges across scalability, performance optimization, essential commands, cloud provisioning, backup, and recovery Peter G
Page
3
Preface Offering a detailed practical look at PostgreSQL 16's new features, "PostgreSQL 16 Cookbook, Second Edition" equips database administrators and developers to take advantage of the most recent developments. Building on the foundation of version 15, this second edition with version 16 walks you through the enhancements that make PostgreSQL a game changer in the database world. This edition provides in-depth coverage of enhanced logical replication, which now includes the ability to replicate from standby servers. We provide detailed instructions for setting up these advanced replication configurations, allowing you to better distribute workloads and improve data availability. The optimization of concurrent bulk loading capabilities for faster data ingestion is another noteworthy addition. Another standout feature of PostgreSQL 16 is the expanded SQL/JSON syntax, which gives developers more control over JSON data management. Our book contains practical solutions and examples for using the new JSON functions and operators, which make it easier to store, query, and manipulate JSON data in your applications. We walk you through the process of
Page
4
configuring refined user roles and permissions, ensuring effective access control in complex environments. Additionally, the book teaches new monitoring capabilities introduced with the pg_stat_io view, which provide insights into I/O operations to help optimize performance. The book goes on to implement performance enhancements such as SIMD acceleration for processing ASCII and JSON strings, as well as the new load balancing feature, load_balance_hosts, which distributes traffic efficiently among multiple servers. The goal of this book is to provide you with the knowledge you need to successfully manage, optimize, and troubleshoot database environments by providing a deep-dive understanding of how to implement and benefit from PostgreSQL 16's latest features. In this book you will learn how to: Boost data availability and workload distribution using advanced logical replication techniques. Apply the SIMD acceleration to expedite the processing of ASCII and JSON strings. Make use of improved SQL/JSON syntax to manage complicated JSON data operations.
Page
5
Enhance efficiency and decrease query times by optimizing query performance with parallel execution. Utilize pg_stat_io for troubleshooting and monitoring I/O operations. Utilize Rust libraries like pgx and rust-postgres for easy integration with PostgreSQL. Distribute workload among numerous PostgreSQL instances by configuring load_balance_hosts. Simplify user role configurations and security with refined privilege management. Utilize pgBackRest and Barman to implement strong backup strategies. Optimize database performance using concurrent bulk loading.
Page
6
Prologue It is with great pleasure that I, as the author of "PostgreSQL 16 Cookbook, Second Edition," join you on such a journey of addressing PostgreSQL flaws. PostgreSQL has long been regarded as a pillar of the open-source database community, known for its robustness, scalability, and extensibility. My goals in revising this book for a second time are to make it more up-to-date with the features and improvements in PostgreSQL 16 and to fix any mistakes or missing information from the original so that it is useful for database administration novices and experts alike. With great effort, I have integrated the new features and enhancements brought in by PostgreSQL 16, such as improved logical replication, which permits replication from standby servers. I explore these new possibilities for data availability and workload distribution in detail, giving you examples and solutions to help you implement them in your own environments. One of the most notable features of PostgreSQL 16 is the addition of SIMD acceleration, which significantly improves the performance of string processing tasks. I've dedicated an entire chapter to SIMD acceleration, demonstrating how to use this feature to optimize database operations,
Page
7
especially when dealing with large amounts of JSON and ASCII data. Through practical examples, I demonstrate the performance gains possible with SIMD, allowing you to maximize the potential of your hardware. I've expanded the security and access control sections to include enhancements, as well as tips for configuring user roles and privileges to effectively protect your data. This edition also covers advanced authentication methods like LDAP and SSL, giving you the knowledge you need to protect your databases from modern threats. Along with these updates, I've fixed the problems with the previous edition by breaking down tricky concepts and giving more thorough explanations where they were lacking. For example, the backup and recovery sections have been thoroughly revised to include new tools such as pgBackRest and Barman, which provide powerful solutions for point-in-time recovery and continuous archiving. I've included practical examples to help you set up reliable backup strategies, ensuring that you're prepared for any situation. I've also worked on integrating PostgreSQL with modern programming languages, particularly Rust. Rust's growing popularity in the systems programming community makes it an excellent choice for developing high-performance database applications. I've included examples from popular Rust libraries such as pgx and rust-postgres that
Page
8
show how you can use Rust's safety and concurrency features to create efficient and dependable database applications. My goal throughout this book is to give you a clear and practical understanding of PostgreSQL 16, allowing you to face the challenges of database management with confidence. Whether you're upgrading from PostgreSQL 15 or starting from scratch with version 16, this cookbook provides a wealth of information and insights to help you succeed. I've worked hard to make this second edition an indispensable resource for anyone working with PostgreSQL by filling in the gaps and inaccuracies of the previous edition and incorporating the most recent developments. Thank you for joining me on this journey, and I hope you find this edition useful and inspiring. Copyright © 2024 by GitforGits
Page
9
All rights reserved. This book is protected under copyright laws and no part of it may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage and retrieval system, without the prior written permission of the publisher. Any unauthorized reproduction, distribution, or transmission of this work may result in civil and criminal penalties and will be dealt with in the respective jurisdiction at anywhere in India, in accordance with the applicable copyright laws. Published by: GitforGits Publisher: Sonal Dhandre www.gitforgits.com support@gitforgits.com Printed in India First Printing: August 2024 Cover Design by: Kitten Publishing
Page
10
For permission to use material from this book, please contact GitforGits at support@gitforgits.com.
Page
11
Content Preface GitforGits Acknowledgement Chapter 1: Preparing PostgreSQL 16 Introduction Recipe #1: Simplify PostgreSQL Architecture Core Components Auxiliary Processes Data Storage Objects Query Execution Process Practical Tips Recipe #2: Installing PostgreSQL 16.1 from Binaries Step-by-step Installation Post-Installation Configuration Verification and Testing
Page
12
Recipe #3: Installing PostgreSQL 16.1 from Source Code Step-by-step Installation Compile PostgreSQL Post-Installation Configuration Verification and Testing Recipe #4: Parsing Database Startup Logs Parsing PostgreSQL Logs Sample Program: Troubleshooting a Startup Error Recipe #5: Discovering PostgreSQL Database Structural Objects Key Structural Objects Tables Indexes Sequences Views Stored Procedures Discovering Structural Objects Sample Program: Analyzing Database Structure List All Tables Describe a Specific Table Identify Indexes Analyze Views Explore Stored Procedures
Page
13
Recipe #6: Understanding Use of Key Configuration Parameters Key Configuration Parameters shared_buffers work_mem maintenance_work_mem effective_cache_size max_connections checkpoint_timeout autovacuum wal_buffers Summary Chapter 2: Performing Basic PostgreSQL Operations Introduction Recipe #1: Exploring AdventureWorks Database Production Schema Sales Schema Purchasing Schema HumanResources Schema Person Schema dbo Schema
Page
14
Recipe #2: Selecting Right Database Schema Understanding PostgreSQL Schema Types Public Schema Private Schema Creating Schemas Moving Objects Between Schemas Benefits of using Schemas Recipe #3: Selecting Indexing Techniques Indexing Techniques in PostgreSQL B-tree Indexing Hash Indexing GiST Indexing (Generalized Search Tree) SP-GiST Indexing (Space-Partitioned Generalized Search Tree) GIN Indexing (Generalized Inverted Index) BRIN Indexing (Block Range INdex) Optimizing Database Optimize Sales Orders by Date Improve Full-Text Search on Product Descriptions Enhance Query Performance for Customer Lookups Recipe #4: Preparing Database Log Directory Setting up the Log Directory Using ‘logging_collector’
Page
15
Recipe #5: Using PostgreSQL TOAST Using TOAST Viewing TOASTed Data Recipe #6: Creating and Administering PostgreSQL Temporary Tables Creating Temporary Tables Sample Program: Performing Data Analysis Recipe #7: Using SELECT in WITH Queries Understanding CTEs Using Multiple CTEs Sample Program: Analyzing Sales Performance Recipe #8: Running Recursive Queries Understanding Recursive Queries Recursive Queries for Organizational Charts Sample Program: Exploring Product Categories Summary Chapter 3: PostgreSQL Cloud Provisioning Introduction
Page
16
Recipe #1: Create PostgreSQL AWS Instance and Manage Database Connection Configure RDS Instance Connect to RDS Instance using pgAdmin Launch and Connect EC2 Instance Recipe #2: Native Backup/Restore with AWS EC2 Instance Backup the Database Restore the Database from Backup Recipe #3: Natively Backup/Restore with AWS RDS Instance Create Manual Snapshot Restore from Snapshot and Verify Resoration Recipe #4: Manage Connection to Database on AWS Modify Security Group Rules Use PgBouncer Recipe #5: Perform Replication of Database on AWS Create a Read Replica Monitor Replication Performance
Page
17
Recipe #6: Run PostgreSQL Bi-directional Replication using pglogical Install pglogical Extension Configure Replication Nodes Create Replication Subscriptions Summary Chapter 4: Database Migration to Cloud and PostgreSQL Introduction Recipe #1: Migrating from On-Premise to AWS EC2/RDS Instance Create Database Backup Transfer Backup to AWS Environment Verify Migration Recipe #2: Utilizing AWS Data Migration Service (DMS) Setup AWS Environment Create Endpoints in AWS DMS Replication Instance Create Database Migration Task Recipe #3: Migrating Database from EC2 to RDS Instance Create Backup of Database
Page
18
Restore Database Monitor and Optimize Recipe #4: Preparing Pgloader to Use with Database Install Pgloader Create Pgloader Command File Recipe #5: Migrating from MySQL to PostgreSQL Create Pgloader Command File Run the Migration Recipe #6: Setting up Foreign Data Wrapper (FDW) Install MySQL FDW Create FDW Table Summary Chapter 5: WAL, AutoVacuum & ArchiveLog Introduction Recipe #1: WAL Compression Option for Space Management Enable WALCompression Tune WAL Compression
Page
19
Recipe #2: Configure WAL Performance Parameters Adjust WAL Performance Parameters Monitor WAL Performance Evaluate Impact and Fine-Tune Settings Recipe #3: Administer Continuous Archiving Test Archiving Setup Manage Archive Retention and Cleanup Prepare Recovery Environment Recipe #4: Using Remote WAL Archive Options Setup SSH Keys for Secure Transfer: Enable Archive Mode: Archived WAL Files on Remote Server Recipe #5: Exploring Vacuum Process Perform a Basic VACUUM Execute VACUUM FULL Automate VACUUM Handle Large Tables Optimize and Run VACUUM in Parallel Recipe #6: Debug PostgreSQL Autovacuum Check Autovacuum Settings
Page
20
Review Autovacuum Logs and Manually Trigger Adjust Autovacuum Settings Adjust Cost-Based Vacuum Parameters Summary Chapter 6: Partitioning and Sharding Strategies Introduction Recipe #1: Setup Partitioning Define Partitions Monitor Partition Usage and Performance Recipe #2: Vertical & Horizontal Partitioning Implement Vertical Partitioning Implement Horizontal Partitioning Recipe #3: Perform Attaching, Detaching, and Dropping Partitions Attach a New Partition Detach an Existing Partition Drop a Detached Partition Recipe #4: Tables Partitioning using Table Inheritance
The above is a preview of the first 20 pages. Register to read the complete e-book.
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
AI guide
# PostgreSQL 16 Cookbook, Second Edition — Reading Guide
## 【One-Line Pitch】
A hands-on, recipe-driven guide for database professionals who want to master PostgreSQL 16 through practical, real-world scenarios—covering everything from basic administration to advanced AWS cloud deployment, performance tuning, and high-availability clustering.
## 【Book Arc】
- **Opening (~0%–13%)**: Introduces PostgreSQL 16 fundamentals, including installation, configuration, and security hardening. Covers initial setup on AWS (RDS and EC2 instances), SSL configuration troubleshooting, and basic database exploration using the AdventureWorks sample database.
- **Early (~13%–33%)**: Dives into database structure management—schemas, indexing strategies (B-tree, GIN), TOAST data handling, and advanced query techniques using CTEs and recursive queries. Introduces cloud backup/restore workflows with AWS S3 and PgBouncer connection pooling.
- **Middle (~33%–53%)**: Focuses on migration and performance optimization. Covers migrating from on-premise to AWS using DMS and pgloader (including MySQL-to-PostgreSQL conversions), WAL compression tuning, remote WAL archiving, and VACUUM strategies for maintaining database health.
- **Middle (~53%–67%)**: Explores partitioning and sharding strategies for managing massive datasets. Includes range partitioning, automatic partition creation, dynamic partition management (attach/detach/drop), and shard mapping for horizontal scaling.
- **Late (~67%–end)**: Covers high availability and replication cluster management using repmgr, streaming replication verification, and upgrade procedures for replication clusters. Concludes with connection pooling configuration and production-readiness considerations.
## 【Key Takeaways】
- **AWS deployment is treated as a first-class citizen** (Opening): The book walks through creating RDS instances, connecting via pgAdmin, and launching EC2 instances—making cloud deployment approachable for PostgreSQL beginners and intermediates alike.
- **Security configuration requires attention to file permissions** (Early): SSL private key permission errors are common; fixing ownership (`chown`) and permissions (`chmod 600`) resolves most connection issues. This practical troubleshooting saves hours of debugging.
- **Indexing strategy depends on query patterns** (Early): B-tree indexes suit customer ID lookups, while GIN indexes with `to_tsvector` optimize full-text search on product descriptions. Matching index types to query workloads is essential for performance.
- **TOAST manages large data transparently** (Early): PostgreSQL automatically splits large attributes into separate TOAST tables, minimizing performance impact. Understanding this mechanism helps when storing blobs or large text fields.
- **CTEs enable complex, readable queries** (Early): Chained and recursive CTEs handle hierarchical data (like employee-manager relationships) elegantly, making complex business logic more maintainable than nested subqueries.
- **WAL compression and archiving improve write performance** (Middle): Enabling `wal_compression` reduces disk I/O, while remote WAL archiving via rsync ensures recoverability. These configurations matter for write-heavy workloads.
- **VACUUM FULL requires careful scheduling** (Middle): While it reclaims space by rewriting tables, it locks them exclusively—use sparingly during maintenance windows. Regular VACUUM prevents bloat without disruption.
- **Partitioning and sharding scale data management** (Middle–Late): Range partitioning by date enables efficient data lifecycle management, while shard maps distribute data across nodes. Both approaches require planning around query patterns.
## 【Reading Tips】
- **Skim the AWS-specific recipes** (~0%–13%) if you're not using AWS—the core PostgreSQL concepts still apply, but cloud-specific steps (RDS, EC2, S3) can be skipped or revisited when needed.
- **Deep-read the indexing and query optimization sections** (Early ~20%–27%): These recipes demonstrate practical performance techniques using the AdventureWorks database, which you can download and experiment with directly.
- **Pay special attention to partitioning and sharding chapters** (Middle ~53%–67%): These are the most advanced topics and require careful study of the code examples to understand partition management and shard mapping.
- **Use the repmgr and replication sections** (Late ~67%+) as a reference when setting up production clusters—the configuration examples are directly applicable to real deployments.
- **Focus on the "why" behind each recipe**: The book explains not just commands but the reasoning behind configuration choices (e.g., why `VACUUM FULL` locks tables, why WAL compression helps). Understanding these principles helps you adapt solutions to your own environment.
## 【Coverage Limits】
Excerpts do not cover the book's early chapters on basic SQL operations, PL/pgSQL stored procedures, or the final chapters on advanced connection management in full detail. The guide focuses on the AWS deployment, performance tuning, partitioning, and replication content that appears in the sampled material.
##
Passage locations
Excerpt 1
g Queries using Shell Script Create Executable Shell Script Integrate Script into Automation Workflow Chapter 1: Preparing PostgreSQL 16 Enhance securi...
View in text
Excerpt 2
CT order_id, customer_id, total_due FROM sales.orders WHERE order_date = '2023-03-22' ) SELECT COUNT(order_id) AS total_orders, SUM(total_due) AS...
View in text
Excerpt 3
r is running and accessible with the necessary credentials. Create an Amazon RDS for PostgreSQL instance to host the database, as explained in Chapter 3. If...
View in text
Excerpt 4
oning Automatic partitioning simplifies the management of large tables by automatically creating and managing partitions based on predefined rules. This re...
View in text
Recommended for You
{{#thumbnailUrl}}
{{/thumbnailUrl}}
{{^thumbnailUrl}}
{{/thumbnailUrl}}
Loading recommended books...
Failed to load, please try again later
Tip the Site
Scan the WeChat Pay or Alipay code to tip. No login required.
WeChat Pay
Alipay