PostgreSQL 18 中文参考文档(未翻译部分保留英文)🌐 切换到英文原版 →

Part II. SQL 语言

本篇阐述 SQL 语言在 PostgreSQL 中的使用实践。我们从 SQL 的基本语法讲起,依次介绍如何创建数据表、如何插入数据以及如何进行数据查询。中间章节系统梳理了可用于各类 SQL 命令的内置数据类型与函数库。最后,深入剖析了数据库性能调优的若干关键维度。

内容编排旨在使初学者能够自始至终循序渐进地阅读,无需频繁跨章节前置查阅即可建立完整的技术理解。各个章节彼此具备独立完整性,进阶读者完全可以根据实际需要单独查阅特定章节。全篇内容以专题形式展开叙述;如需查询特定命令的完整语法规范,建议查阅 Part VI。

读者应已掌握如何连接到 PostgreSQL 数据库并执行基本的 SQL 命令。尚不熟悉这些基础操作的读者,建议先阅读 Part I。SQL 命令通常通过 PostgreSQL 官方交互式终端 psql 输入执行,其他具备类似功能的数据库客户端亦可通用。

Table of Contents

4. SQL 语法
4.1. 词法结构
4.2. 值表达式
4.3. 调用函数
5. 数据定义
5.1. 表基础
5.2. 默认值
5.3. 标识列
5.4. 生成列
5.5. 约束
5.6. 系统列
5.7. 修改表结构
5.8. 权限
5.9. 行安全性策略
5.10. 模式
5.11. 继承
5.12. 表分区
5.13. 外部数据
5.14. 其他数据库对象
5.15. 依赖关系跟踪
6. 数据操作
6.1. 插入数据
6.2. 更新数据
6.3. 删除数据
6.4. 从修改的行中返回数据
7. 查询
7.1. 概述
7.2. 表表达式
7.3. 选择列表
7.4. 组合查询
7.5. 排序行
7.6. LIMIT 与 OFFSET
7.7. VALUES 列表
7.8. WITH 查询(公用表表达式)
8. 数据类型
8.1. 数值类型
8.2. 货币类型
8.3. 字符类型
8.4. 二进制数据类型
8.5. 日期/时间类型
8.6. 布尔类型
8.7. 枚举类型
8.8. 几何类型
8.9. 网络地址类型
8.10. 位串类型
8.11. 文本搜索类型
8.12. UUID 类型
8.13. XML 类型
8.14. 域类型
8.15. 对象标识符类型
8.16. pg_lsn 类型
8.17. 伪类型
9. Functions and Operators
9.1. Logical Operators
9.2. Comparison Functions and Operators
9.3. Mathematical Functions and Operators
9.4. String Functions and Operators
9.5. Binary String Functions and Operators
9.6. Bit String Functions and Operators
9.7. Pattern Matching
9.8. Data Type Formatting Functions
9.9. Date/Time Functions and Operators
9.10. Enum Support Functions
9.11. Geometric Functions and Operators
9.12. Network Address Functions and Operators
9.13. Text Search Functions and Operators
9.14. UUID Functions
9.15. XML Functions
9.16. JSON Functions and Operators
9.17. Sequence Manipulation Functions
9.18. Conditional Expressions
9.19. Array Functions and Operators
9.20. Range/Multirange Functions and Operators
9.21. Aggregate Functions
9.22. Window Functions
9.23. Merge Support Functions
9.24. Subquery Expressions
9.25. Row and Array Comparisons
9.26. Set Returning Functions
9.27. System Information Functions and Operators
9.28. System Administration Functions
9.29. Trigger Functions
9.30. Event Trigger Functions
9.31. Statistics Information Functions
10. Type Conversion
10.1. Overview
10.2. Operators
10.3. Functions
10.4. Value Storage
10.5. UNION, CASE, and Related Constructs
10.6. SELECT Output Columns
11. Indexes
11.1. Introduction
11.2. Index Types
11.3. Multicolumn Indexes
11.4. Indexes and ORDER BY
11.5. Combining Multiple Indexes
11.6. Unique Indexes
11.7. Indexes on Expressions
11.8. Partial Indexes
11.9. Index-Only Scans and Covering Indexes
11.10. Operator Classes and Operator Families
11.11. Indexes and Collations
11.12. Examining Index Usage
12. Full Text Search
12.1. Introduction
12.2. Tables and Indexes
12.3. Controlling Text Search
12.4. Additional Features
12.5. Parsers
12.6. Dictionaries
12.7. Configuration Example
12.8. Testing and Debugging Text Search
12.9. Preferred Index Types for Text Search
12.10. psql Support
12.11. Limitations
13. Concurrency Control
13.1. Introduction
13.2. Transaction Isolation
13.3. Explicit Locking
13.4. Data Consistency Checks at the Application Level
13.5. Serialization Failure Handling
13.6. Caveats
13.7. Locking and Indexes
14. Performance Tips
14.1. Using EXPLAIN
14.2. Statistics Used by the Planner
14.3. Controlling the Planner with Explicit JOIN Clauses
14.4. Populating a Database
14.5. Non-Durable Settings
15. 并行查询
15.1. 并行查询的工作原理
15.2. 何时可以使用并行查询?
15.3. 并行执行计划
15.4. 并行安全性