PostgreSQL 9.0.1 Documentation
The PostgreSQL Global Development Group
PostgreSQL 9.0.1 Documentation
by The PostgreSQL Global Development Group
Copyright © 1996-2010 The PostgreSQL Global Development Group
Legal Notice
PostgreSQL is Copyright © 1996-2010 by the PostgreSQL Global Development Group and is distributed under the terms of the license of the
University of California below.
Postgres95 is Copyright © 1994-5 by the Regents of the University of California.
Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written
agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all
copies.
IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE
AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PRO-
VIDED HEREUNDER IS ON AN “AS-IS” BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE
MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
Table of Contents
Preface .............................................................................................................................................xlix
1. What is PostgreSQL? ..........................................................................................................xlix
2. A Brief History of PostgreSQL................................................................................................ l
2.1. The Berkeley POSTGRES Project .............................................................................. l
2.2. Postgres95.................................................................................................................... l
2.3. PostgreSQL.................................................................................................................li
3. Conventions.............................................................................................................................li
4. Further Information................................................................................................................lii
5. Bug Reporting Guidelines......................................................................................................lii
5.1. Identifying Bugs .......................................................................................................liii
5.2. What to report...........................................................................................................liii
5.3. Where to report bugs ................................................................................................. lv
I. Tutorial ............................................................................................................................................. 1
1. Getting Started ........................................................................................................................ 1
1.1. Installation .................................................................................................................. 1
1.2. Architectural Fundamentals........................................................................................ 1
1.3. Creating a Database.................................................................................................... 2
1.4. Accessing a Database ................................................................................................. 3
2. The SQL Language ................................................................................................................. 5
2.1. Introduction ................................................................................................................ 5
2.2. Concepts ..................................................................................................................... 5
2.3. Creating a New Table ................................................................................................. 5
2.4. Populating a Table With Rows ................................................................................... 6
2.5. Querying a Table ........................................................................................................ 7
2.6. Joins Between Tables.................................................................................................. 9
2.7. Aggregate Functions................................................................................................. 11
2.8. Updates ..................................................................................................................... 12
2.9. Deletions................................................................................................................... 13
3. Advanced Features ................................................................................................................ 14
3.1. Introduction .............................................................................................................. 14
3.2. Views ........................................................................................................................ 14
3.3. Foreign Keys............................................................................................................. 14
3.4. Transactions.............................................................................................................. 15
3.5. Window Functions.................................................................................................... 17
3.6. Inheritance ................................................................................................................ 20
3.7. Conclusion................................................................................................................ 21
II. The SQL Language...................................................................................................................... 22
4. SQL Syntax ........................................................................................................................... 24
4.1. Lexical Structure....................................................................................................... 24
4.1.1. Identifiers and Key Words............................................................................ 24
4.1.2. Constants...................................................................................................... 26
4.1.2.1. String Constants .............................................................................. 26
4.1.2.2. String Constants with C-Style Escapes ........................................... 26
4.1.2.3. String Constants with Unicode Escapes.......................................... 28
4.1.2.4. Dollar-Quoted String Constants ...................................................... 29
4.1.2.5. Bit-String Constants ........................................................................ 29
4.1.2.6. Numeric Constants .......................................................................... 30
4.1.2.7. Constants of Other Types ................................................................ 30
iii
4.1.3. Operators...................................................................................................... 31
4.1.4. Special Characters........................................................................................ 32
4.1.5. Comments .................................................................................................... 32
4.1.6. Lexical Precedence ...................................................................................... 33
4.2. Value Expressions..................................................................................................... 34
4.2.1. Column References...................................................................................... 35
4.2.2. Positional Parameters................................................................................... 35
4.2.3. Subscripts..................................................................................................... 35
4.2.4. Field Selection ............................................................................................. 36
4.2.5. Operator Invocations.................................................................................... 36
4.2.6. Function Calls .............................................................................................. 36
4.2.7. Aggregate Expressions................................................................................. 37
4.2.8. Window Function Calls................................................................................ 38
4.2.9. Type Casts .................................................................................................... 39
4.2.10. Scalar Subqueries....................................................................................... 40
4.2.11. Array Constructors..................................................................................... 41
4.2.12. Row Constructors....................................................................................... 42
4.2.13. Expression Evaluation Rules ..................................................................... 43
4.3. Calling Functions...................................................................................................... 44
4.3.1. Using positional notation ............................................................................. 45
4.3.2. Using named notation .................................................................................. 45
4.3.3. Using mixed notation................................................................................... 46
5. Data Definition ...................................................................................................................... 47
5.1. Table Basics.............................................................................................................. 47
5.2. Default Values .......................................................................................................... 48
5.3. Constraints ................................................................................................................ 49
5.3.1. Check Constraints ........................................................................................ 49
5.3.2. Not-Null Constraints.................................................................................... 51
5.3.3. Unique Constraints....................................................................................... 52
5.3.4. Primary Keys................................................................................................ 52
5.3.5. Foreign Keys ................................................................................................ 53
5.3.6. Exclusion constraints ................................................................................... 56
5.4. System Columns....................................................................................................... 56
5.5. Modifying Tables...................................................................................................... 57
5.5.1. Adding a Column......................................................................................... 58
5.5.2. Removing a Column .................................................................................... 58
5.5.3. Adding a Constraint ..................................................................................... 59
5.5.4. Removing a Constraint ................................................................................ 59
5.5.5. Changing a Column’s Default Value............................................................ 59
5.5.6. Changing a Column’s Data Type ................................................................. 60
5.5.7. Renaming a Column .................................................................................... 60
5.5.8. Renaming a Table ........................................................................................ 60
5.6. Privileges .................................................................................................................. 60
5.7. Schemas.................................................................................................................... 61
5.7.1. Creating a Schema ....................................................................................... 62
5.7.2. The Public Schema ...................................................................................... 63
5.7.3. The Schema Search Path.............................................................................. 63
5.7.4. Schemas and Privileges................................................................................ 64
5.7.5. The System Catalog Schema ....................................................................... 64
5.7.6. Usage Patterns.............................................................................................. 65
5.7.7. Portability..................................................................................................... 65
5.8. Inheritance ................................................................................................................ 65
iv
5.8.1. Caveats ......................................................................................................... 68
5.9. Partitioning ............................................................................................................... 69
5.9.1. Overview...................................................................................................... 69
5.9.2. Implementing Partitioning ........................................................................... 70
5.9.3. Managing Partitions ..................................................................................... 72
5.9.4. Partitioning and Constraint Exclusion ......................................................... 73
5.9.5. Alternative Partitioning Methods................................................................. 74
5.9.6. Caveats ......................................................................................................... 75
5.10. Other Database Objects .......................................................................................... 76
5.11. Dependency Tracking............................................................................................. 76
6. Data Manipulation................................................................................................................. 78
6.1. Inserting Data ........................................................................................................... 78
6.2. Updating Data........................................................................................................... 79
6.3. Deleting Data............................................................................................................ 80
7. Queries .................................................................................................................................. 81
7.1. Overview .................................................................................................................. 81
7.2. Table Expressions ..................................................................................................... 81
7.2.1. The FROM Clause.......................................................................................... 82
7.2.1.1. Joined Tables ................................................................................... 82
7.2.1.2. Table and Column Aliases............................................................... 85
7.2.1.3. Subqueries ....................................................................................... 86
7.2.1.4. Table Functions ............................................................................... 86
7.2.2. The WHERE Clause........................................................................................ 87
7.2.3. The GROUP BY and HAVING Clauses........................................................... 88
7.2.4. Window Function Processing ...................................................................... 90
7.3. Select Lists................................................................................................................ 91
7.3.1. Select-List Items .......................................................................................... 91
7.3.2. Column Labels ............................................................................................. 92
7.3.3. DISTINCT .................................................................................................... 92
7.4. Combining Queries................................................................................................... 93
7.5. Sorting Rows ............................................................................................................ 93
7.6. LIMIT and OFFSET................................................................................................... 94
7.7. VALUES Lists ............................................................................................................ 95
7.8. WITH Queries ............................................................................................................ 96
8. Data Types........................................................................................................................... 100
8.1. Numeric Types........................................................................................................ 101
8.1.1. Integer Types.............................................................................................. 102
8.1.2. Arbitrary Precision Numbers ..................................................................... 102
8.1.3. Floating-Point Types .................................................................................. 103
8.1.4. Serial Types................................................................................................ 104
8.2. Monetary Types ...................................................................................................... 105
8.3. Character Types ...................................................................................................... 106
8.4. Binary Data Types .................................................................................................. 108
8.4.1. bytea hex format ...................................................................................... 108
8.4.2. bytea escape format ................................................................................. 109
8.5. Date/Time Types..................................................................................................... 110
8.5.1. Date/Time Input ......................................................................................... 112
8.5.1.1. Dates.............................................................................................. 112
8.5.1.2. Times ............................................................................................. 113
8.5.1.3. Time Stamps.................................................................................. 114
8.5.1.4. Special Values ............................................................................... 115
8.5.2. Date/Time Output ...........................................