+ All Categories
Home > Documents > The PostgreSQL Global Development Group · 2019-09-26 · PostgreSQL 9.5.19 Documentation The...

The PostgreSQL Global Development Group · 2019-09-26 · PostgreSQL 9.5.19 Documentation The...

Date post: 21-Jun-2020
Category:
Upload: others
View: 31 times
Download: 1 times
Share this document with a friend
2529
PostgreSQL 9.5.19 Documentation The PostgreSQL Global Development Group
Transcript
  • PostgreSQL 9.5.19 Documentation

    The PostgreSQL Global Development Group

  • PostgreSQL 9.5.19 Documentationby The PostgreSQL Global Development GroupCopyright © 1996-2019 The PostgreSQL Global Development Group

    Legal Notice

    PostgreSQL is Copyright © 1996-2019 by the PostgreSQL Global Development Group.

    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 writtenagreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in allcopies.

    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 SOFTWAREAND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OFSUCH 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 PROVIDEMAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

  • Table of ContentsPreface .............................................................................................................................................xliii

    1. What is PostgreSQL? ..........................................................................................................xliii2. A Brief History of PostgreSQL...........................................................................................xliv

    2.1. The Berkeley POSTGRES Project .........................................................................xliv2.2. Postgres95...............................................................................................................xliv2.3. PostgreSQL..............................................................................................................xlv

    3. Conventions..........................................................................................................................xlv4. Further Information.............................................................................................................xlvi5. Bug Reporting Guidelines...................................................................................................xlvi

    5.1. Identifying Bugs ....................................................................................................xlvii5.2. What to Report ......................................................................................................xlvii5.3. Where to Report Bugs ............................................................................................xlix

    I. Tutorial ............................................................................................................................................. 11. Getting Started ........................................................................................................................ 1

    1.1. Installation .................................................................................................................. 11.2. Architectural Fundamentals........................................................................................ 11.3. Creating a Database.................................................................................................... 21.4. Accessing a Database ................................................................................................. 3

    2. The SQL Language ................................................................................................................. 52.1. Introduction ................................................................................................................ 52.2. Concepts ..................................................................................................................... 52.3. Creating a New Table ................................................................................................. 52.4. Populating a Table With Rows ................................................................................... 62.5. Querying a Table ........................................................................................................ 72.6. Joins Between Tables.................................................................................................. 92.7. Aggregate Functions................................................................................................. 112.8. Updates ..................................................................................................................... 122.9. Deletions................................................................................................................... 13

    3. Advanced Features ................................................................................................................ 143.1. Introduction .............................................................................................................. 143.2. Views ........................................................................................................................ 143.3. Foreign Keys............................................................................................................. 143.4. Transactions.............................................................................................................. 153.5. Window Functions.................................................................................................... 173.6. Inheritance ................................................................................................................ 203.7. Conclusion................................................................................................................ 21

    II. The SQL Language...................................................................................................................... 224. SQL Syntax ........................................................................................................................... 24

    4.1. Lexical Structure....................................................................................................... 244.1.1. Identifiers and Key Words............................................................................ 244.1.2. Constants...................................................................................................... 26

    4.1.2.1. String Constants .............................................................................. 264.1.2.2. String Constants with C-style Escapes............................................ 264.1.2.3. String Constants with Unicode Escapes.......................................... 284.1.2.4. Dollar-quoted String Constants ....................................................... 284.1.2.5. Bit-string Constants......................................................................... 294.1.2.6. Numeric Constants .......................................................................... 294.1.2.7. Constants of Other Types ................................................................ 30

    iii

  • 4.1.3. Operators...................................................................................................... 314.1.4. Special Characters........................................................................................ 314.1.5. Comments .................................................................................................... 324.1.6. Operator Precedence .................................................................................... 32

    4.2. Value Expressions..................................................................................................... 334.2.1. Column References...................................................................................... 344.2.2. Positional Parameters................................................................................... 344.2.3. Subscripts..................................................................................................... 354.2.4. Field Selection ............................................................................................. 354.2.5. Operator Invocations.................................................................................... 364.2.6. Function Calls .............................................................................................. 364.2.7. Aggregate Expressions................................................................................. 374.2.8. Window Function Calls................................................................................ 394.2.9. Type Casts .................................................................................................... 414.2.10. Collation Expressions ................................................................................ 414.2.11. Scalar Subqueries....................................................................................... 424.2.12. Array Constructors..................................................................................... 424.2.13. Row Constructors....................................................................................... 444.2.14. Expression Evaluation Rules ..................................................................... 45

    4.3. Calling Functions...................................................................................................... 474.3.1. Using Positional Notation ............................................................................ 474.3.2. Using Named Notation ................................................................................ 484.3.3. Using Mixed Notation.................................................................................. 48

    5. Data Definition ...................................................................................................................... 505.1. Table Basics.............................................................................................................. 505.2. Default Values .......................................................................................................... 515.3. Constraints ................................................................................................................ 52

    5.3.1. Check Constraints ........................................................................................ 525.3.2. Not-Null Constraints.................................................................................... 545.3.3. Unique Constraints....................................................................................... 555.3.4. Primary Keys................................................................................................ 565.3.5. Foreign Keys ................................................................................................ 565.3.6. Exclusion Constraints .................................................................................. 59

    5.4. System Columns....................................................................................................... 595.5. Modifying Tables...................................................................................................... 61

    5.5.1. Adding a Column......................................................................................... 615.5.2. Removing a Column .................................................................................... 625.5.3. Adding a Constraint ..................................................................................... 625.5.4. Removing a Constraint ................................................................................ 625.5.5. Changing a Column’s Default Value............................................................ 635.5.6. Changing a Column’s Data Type ................................................................. 635.5.7. Renaming a Column .................................................................................... 635.5.8. Renaming a Table ........................................................................................ 63

    5.6. Privileges .................................................................................................................. 645.7. Row Security Policies............................................................................................... 645.8. Schemas.................................................................................................................... 69

    5.8.1. Creating a Schema ....................................................................................... 705.8.2. The Public Schema ...................................................................................... 715.8.3. The Schema Search Path.............................................................................. 715.8.4. Schemas and Privileges................................................................................ 735.8.5. The System Catalog Schema ....................................................................... 735.8.6. Usage Patterns.............................................................................................. 73

    iv

  • 5.8.7. Portability..................................................................................................... 745.9. Inheritance ................................................................................................................ 74

    5.9.1. Caveats ......................................................................................................... 775.10. Partitioning ............................................................................................................. 78

    5.10.1. Overview.................................................................................................... 785.10.2. Implementing Partitioning ......................................................................... 795.10.3. Managing Partitions ................................................................................... 825.10.4. Partitioning and Constraint Exclusion ....................................................... 825.10.5. Alternative Partitioning Methods............................................................... 845.10.6. Caveats ....................................................................................................... 84

    5.11. Foreign Data ........................................................................................................... 855.12. Other Database Objects .......................................................................................... 865.13. Dependency Tracking............................................................................................. 86

    6. Data Manipulation................................................................................................................. 886.1. Inserting Data ........................................................................................................... 886.2. Updating Data........................................................................................................... 896.3. Deleting Data............................................................................................................ 906.4. Returning Data From Modified Rows ...................................................................... 90

    7. Queries .................................................................................................................................. 927.1. Overview .................................................................................................................. 927.2. Table Expressions ..................................................................................................... 92

    7.2.1. The FROM Clause.......................................................................................... 937.2.1.1. Joined Tables ................................................................................... 937.2.1.2. Table and Column Aliases............................................................... 977.2.1.3. Subqueries ....................................................................................... 987.2.1.4. Table Functions ............................................................................... 987.2.1.5. LATERAL Subqueries ....................................................................... 99

    7.2.2. The WHERE Clause...................................................................................... 1017.2.3. The GROUP BY and HAVING Clauses......................................................... 1017.2.4. GROUPING SETS, CUBE, and ROLLUP ....................................................... 1047.2.5. Window Function Processing .................................................................... 106

    7.3. Select Lists.............................................................................................................. 1067.3.1. Select-List Items ........................................................................................ 1077.3.2. Column Labels ........................................................................................... 1077.3.3. DISTINCT .................................................................................................. 108

    7.4. Combining Queries................................................................................................. 1087.5. Sorting Rows .......................................................................................................... 1097.6. LIMIT and OFFSET................................................................................................. 1107.7. VALUES Lists .......................................................................................................... 1107.8. WITH Queries (Common Table Expressions) ......................................................... 111

    7.8.1. SELECT in WITH......................................................................................... 1117.8.2. Data-Modifying Statements in WITH ......................................................... 115

    8. Data Types........................................................................................................................... 1178.1. Numeric Types........................................................................................................ 118

    8.1.1. Integer Types.............................................................................................. 1198.1.2. Arbitrary Precision Numbers ..................................................................... 1198.1.3. Floating-Point Types .................................................................................. 1218.1.4. Serial Types................................................................................................ 122

    8.2. Monetary Types ...................................................................................................... 1238.3. Character Types ...................................................................................................... 1238.4. Binary Data Types .................................................................................................. 125

    8.4.1. bytea Hex Format..................................................................................... 126

    v

  • 8.4.2. bytea Escape Format................................................................................ 1268.5. Date/Time Types..................................................................................................... 128

    8.5.1. Date/Time Input ......................................................................................... 1298.5.1.1. Dates.............................................................................................. 1308.5.1.2. Times ............................................................................................. 1308.5.1.3. Time Stamps.................................................................................. 1318.5.1.4. Special Values ............................................................................... 132

    8.5.2. Date/Time Output ...................................................................................... 1338.5.3. Time Zones ................................................................................................ 1348.5.4. Interval Input.............................................................................................. 1358.5.5. Interval Output ........................................................................................... 137

    8.6. Boolean Type.......................................................................................................... 1388.7. Enumerated Types .................................................................................................. 139

    8.7.1. Declaration of Enumerated Types.............................................................. 1398.7.2. Ordering ..................................................................................................... 1408.7.3. Type Safety ................................................................................................ 1408.7.4. Implementation Details.............................................................................. 141

    8.8. Geometric Types..................................................................................................... 1418.8.1. Points ......................................................................................................... 1428.8.2. Lines........................................................................................................... 1428.8.3. Line Segments............................................................................................ 1428.8.4. Boxes.......................................................................................................... 1438.8.5. Paths........................................................................................................... 1438.8.6. Polygons..................................................................................................... 1438.8.7. Circles ........................................................................................................ 143

    8.9. Network Address Types.......................................................................................... 1448.9.1. inet ........................................................................................................... 1448.9.2. cidr ........................................................................................................... 1448.9.3. inet vs. cidr............................................................................................ 1458.9.4. macaddr .................................................................................................... 145

    8.10. Bit String Types .................................................................................................... 1468.11. Text Search Types................................................................................................. 146

    8.11.1. tsvector ................................................................................................ 1478.11.2. tsquery .................................................................................................. 148

    8.12. UUID Type ........................................................................................................... 1498.13. XML Type ............................................................................................................ 150

    8.13.1. Creating XML Values .............................................................................. 1508.13.2. Encoding Handling .................................................................................. 1518.13.3. Accessing XML Values............................................................................ 151

    8.14. JSON Types .......................................................................................................... 1528.14.1. JSON Input and Output Syntax................................................................ 1538.14.2. Designing JSON documents effectively .................................................. 1548.14.3. jsonb Containment and Existence.......................................................... 1558.14.4. jsonb Indexing........................................................................................ 156

    8.15. Arrays ................................................................................................................... 1588.15.1. Declaration of Array Types...................................................................... 1588.15.2. Array Value Input..................................................................................... 1598.15.3. Accessing Arrays ..................................................................................... 1618.15.4. Modifying Arrays..................................................................................... 1628.15.5. Searching in Arrays.................................................................................. 1658.15.6. Array Input and Output Syntax................................................................ 166

    8.16. Composite Types .................................................................................................. 167

    vi

  • 8.16.1. Declaration of Composite Types.............................................................. 1688.16.2. Constructing Composite Values............................................................... 1698.16.3. Accessing Composite Types .................................................................... 1698.16.4. Modifying Composite Types.................................................................... 1708.16.5. Using Composite Types in Queries.......................................................... 1708.16.6. Composite Type Input and Output Syntax............................................... 173

    8.17. Range Types ......................................................................................................... 1748.17.1. Built-in Range Types ............................................................................... 1748.17.2. Examples.................................................................................................. 1748.17.3. Inclusive and Exclusive Bounds .............................................................. 1758.17.4. Infinite (Unbounded) Ranges................................................................... 1758.17.5. Range Input/Output.................................................................................. 1758.17.6. Constructing Ranges ................................................................................ 1768.17.7. Discrete Range Types .............................................................................. 1778.17.8. Defining New Range Types ..................................................................... 1778.17.9. Indexing ................................................................................................... 1788.17.10. Constraints on Ranges............................................................................ 178

    8.18. Object Identifier Types ......................................................................................... 1798.19. pg_lsn Type........................................................................................................... 1818.20. Pseudo-Types........................................................................................................ 181

    9. Functions and Operators ..................................................................................................... 1849.1. Logical Operators ................................................................................................... 1849.2. Comparison Operators............................................................................................ 1849.3. Mathematical Functions and Operators.................................................................. 1869.4. String Functions and Operators .............................................................................. 190

    9.4.1. format....................................................................................................... 2039.5. Binary String Functions and Operators .................................................................. 2059.6. Bit String Functions and Operators ........................................................................ 2079.7. Pattern Matching .................................................................................................... 208

    9.7.1. LIKE ........................................................................................................... 2099.7.2. SIMILAR TO Regular Expressions ............................................................ 2109.7.3. POSIX Regular Expressions ...................................................................... 211

    9.7.3.1. Regular Expression Details ........................................................... 2149.7.3.2. Bracket Expressions ...................................................................... 2169.7.3.3. Regular Expression Escapes.......................................................... 2179.7.3.4. Regular Expression Metasyntax.................................................... 2199.7.3.5. Regular Expression Matching Rules ............................................. 2219.7.3.6. Limits and Compatibility .............................................................. 2229.7.3.7. Basic Regular Expressions ............................................................ 223

    9.8. Data Type Formatting Functions ............................................................................ 2239.9. Date/Time Functions and Operators....................................................................... 230

    9.9.1. EXTRACT, date_part ............................................................................... 2369.9.2. date_trunc .............................................................................................. 2399.9.3. AT TIME ZONE.......................................................................................... 2409.9.4. Current Date/Time ..................................................................................... 2419.9.5. Delaying Execution.................................................................................... 243

    9.10. Enum Support Functions ...................................................................................... 2439.11. Geometric Functions and Operators ..................................................................... 2449.12. Network Address Functions and Operators.......................................................... 2489.13. Text Search Functions and Operators ................................................................... 2519.14. XML Functions .................................................................................................... 254

    9.14.1. Producing XML Content.......................................................................... 255

    vii

  • 9.14.1.1. xmlcomment ............................................................................... 2559.14.1.2. xmlconcat ................................................................................. 2559.14.1.3. xmlelement ............................................................................... 2569.14.1.4. xmlforest ................................................................................. 2579.14.1.5. xmlpi .......................................................................................... 2589.14.1.6. xmlroot...................................................................................... 2589.14.1.7. xmlagg ........................................................................................ 258

    9.14.2. XML Predicates ....................................................................................... 2599.14.2.1. IS DOCUMENT ............................................................................. 2599.14.2.2. IS NOT DOCUMENT..................................................................... 2599.14.2.3. XMLEXISTS ................................................................................. 2599.14.2.4. xml_is_well_formed .............................................................. 260

    9.14.3. Processing XML ...................................................................................... 2619.14.4. Mapping Tables to XML.......................................................................... 262

    9.15. JSON Functions and Operators ............................................................................ 2659.16. Sequence Manipulation Functions ....................................................................... 2749.17. Conditional Expressions....................................................................................... 276

    9.17.1. CASE ......................................................................................................... 2769.17.2. COALESCE ................................................................................................ 2789.17.3. NULLIF..................................................................................................... 2789.17.4. GREATEST and LEAST.............................................................................. 279

    9.18. Array Functions and Operators ............................................................................ 2799.19. Range Functions and Operators............................................................................ 2829.20. Aggregate Functions............................................................................................. 2859.21. Window Functions................................................................................................ 2919.22. Subquery Expressions .......................................................................................... 293

    9.22.1. EXISTS..................................................................................................... 2939.22.2. IN ............................................................................................................. 2949.22.3. NOT IN..................................................................................................... 2949.22.4. ANY/SOME ................................................................................................. 2959.22.5. ALL ........................................................................................................... 2959.22.6. Single-row Comparison ........................................................................... 296

    9.23. Row and Array Comparisons ............................................................................... 2969.23.1. IN ............................................................................................................. 2969.23.2. NOT IN..................................................................................................... 2979.23.3. ANY/SOME (array) ..................................................................................... 2979.23.4. ALL (array) ............................................................................................... 2979.23.5. Row Constructor Comparison.................................................................. 2989.23.6. Composite Type Comparison................................................................... 299

    9.24. Set Returning Functions ....................................................................................... 2999.25. System Information Functions ............................................................................. 3029.26. System Administration Functions ........................................................................ 315

    9.26.1. Configuration Settings Functions............................................................. 3159.26.2. Server Signaling Functions ...................................................................... 3169.26.3. Backup Control Functions ....................................................................... 3169.26.4. Recovery Control Functions .................................................................... 3189.26.5. Snapshot Synchronization Functions....................................................... 3209.26.6. Replication Functions .............................................................................. 3219.26.7. Database Object Management Functions................................................. 3249.26.8. Index Maintenance Functions .................................................................. 3269.26.9. Generic File Access Functions................................................................. 3279.26.10. Advisory Lock Functions....................................................................... 328

    viii

  • 9.27. Trigger Functions ................................................................................................. 3309.28. Event Trigger Functions ....................................................................................... 330

    9.28.1. Capturing Changes at Command End...................................................... 3309.28.2. Processing Objects Dropped by a DDL Command ................................. 3319.28.3. Handling a Table Rewrite Event .............................................................. 333

    10. Type Conversion................................................................................................................ 33410.1. Overview .............................................................................................................. 33410.2. Operators .............................................................................................................. 33510.3. Functions .............................................................................................................. 33910.4. Value Storage........................................................................................................ 34310.5. UNION, CASE, and Related Constructs.................................................................. 344

    11. Indexes .............................................................................................................................. 34611.1. Introduction .......................................................................................................... 34611.2. Index Types........................................................................................................... 34711.3. Multicolumn Indexes............................................................................................ 34911.4. Indexes and ORDER BY......................................................................................... 35011.5. Combining Multiple Indexes ................................................................................ 35111.6. Unique Indexes ..................................................................................................... 35211.7. Indexes on Expressions ........................................................................................ 35211.8. Partial Indexes ...................................................................................................... 35311.9. Operator Classes and Operator Families .............................................................. 35511.10. Indexes and Collations........................................................................................ 35711.11. Examining Index Usage...................................................................................... 357

    12. Full Text Search ................................................................................................................ 35912.1. Introduction .......................................................................................................... 359

    12.1.1. What Is a Document?............................................................................... 36012.1.2. Basic Text Matching ................................................................................ 36012.1.3. Configurations.......................................................................................... 361

    12.2. Tables and Indexes................................................................................................ 36212.2.1. Searching a Table ..................................................................................... 36212.2.2. Creating Indexes ...................................................................................... 363

    12.3. Controlling Text Search........................................................................................ 36412.3.1. Parsing Documents .................................................................................. 36412.3.2. Parsing Queries ........................................................................................ 36512.3.3. Ranking Search Results ........................................................................... 36712.3.4. Highlighting Results ................................................................................ 369

    12.4. Additional Features .............................................................................................. 37012.4.1. Manipulating Documents......................................................................... 37012.4.2. Manipulating Queries............................................................................... 371

    12.4.2.1. Query Rewriting .......................................................................... 37212.4.3. Triggers for Automatic Updates .............................................................. 37312.4.4. Gathering Document Statistics ................................................................ 374

    12.5. Parsers................................................................................................................... 37512.6. Dictionaries........................................................................................................... 377

    12.6.1. Stop Words............................................................................................... 37812.6.2. Simple Dictionary .................................................................................... 37912.6.3. Synonym Dictionary ................................................................................ 38012.6.4. Thesaurus Dictionary ............................................................................... 381

    12.6.4.1. Thesaurus Configuration ............................................................. 38212.6.4.2. Thesaurus Example ..................................................................... 383

    12.6.5. Ispell Dictionary....................................................................................... 38412.6.6. Snowball Dictionary ................................................................................ 385

    ix

  • 12.7. Configuration Example......................................................................................... 38512.8. Testing and Debugging Text Search ..................................................................... 387

    12.8.1. Configuration Testing............................................................................... 38712.8.2. Parser Testing........................................................................................... 38912.8.3. Dictionary Testing.................................................................................... 390

    12.9. GIN and GiST Index Types .................................................................................. 39112.10. psql Support........................................................................................................ 39212.11. Limitations.......................................................................................................... 39412.12. Migration from Pre-8.3 Text Search................................................................... 395

    13. Concurrency Control ......................................................................................................... 39613.1. Introduction .......................................................................................................... 39613.2. Transaction Isolation ............................................................................................ 396

    13.2.1. Read Committed Isolation Level ............................................................. 39713.2.2. Repeatable Read Isolation Level.............................................................. 39913.2.3. Serializable Isolation Level...................................................................... 400

    13.3. Explicit Locking ................................................................................................... 40213.3.1. Table-level Locks ..................................................................................... 40213.3.2. Row-level Locks ...................................................................................... 40513.3.3. Page-level Locks ...................................................................................... 40613.3.4. Deadlocks................................................................................................. 40613.3.5. Advisory Locks........................................................................................ 407

    13.4. Data Consistency Checks at the Application Level.............................................. 40813.4.1. Enforcing Consistency With Serializable Transactions ........................... 40813.4.2. Enforcing Consistency With Explicit Blocking Locks ............................ 409

    13.5. Caveats.................................................................................................................. 41013.6. Locking and Indexes............................................................................................. 410

    14. Performance Tips .............................................................................................................. 41214.1. Using EXPLAIN .................................................................................................... 412

    14.1.1. EXPLAIN Basics ....................................................................................... 41214.1.2. EXPLAIN ANALYZE ................................................................................. 41814.1.3. Caveats ..................................................................................................... 421

    14.2. Statistics Used by the Planner .............................................................................. 42214.3. Controlling the Planner with Explicit JOIN Clauses............................................ 42414.4. Populating a Database .......................................................................................... 426

    14.4.1. Disable Autocommit ................................................................................ 42614.4.2. Use COPY.................................................................................................. 42614.4.3. Remove Indexes ....................................................................................... 42614.4.4. Remove Foreign Key Constraints ............................................................ 42714.4.5. Increase maintenance_work_mem ........................................................ 42714.4.6. Increase max_wal_size ......................................................................... 42714.4.7. Disable WAL Archival and Streaming Replication ................................. 42714.4.8. Run ANALYZE Afterwards........................................................................ 42814.4.9. Some Notes About pg_dump................................................................... 428

    14.5. Non-Durable Settings ........................................................................................... 429

    III. Server Administration ............................................................................................................. 43015. Installation from Source Code .......................................................................................... 432

    15.1. Short Version ........................................................................................................ 43215.2. Requirements........................................................................................................ 43215.3. Getting The Source............................................................................................... 43415.4. Installation Procedure........................................................................................... 43415.5. Post-Installation Setup.......................................................................................... 444

    x

  • 15.5.1. Shared Libraries ....................................................................................... 44415.5.2. Environment Variables ............................................................................. 445

    15.6. Supported Platforms ............................................................................................. 44615.7. Platform-specific Notes ........................................................................................ 446

    15.7.1. AIX .......................................................................................................... 44615.7.1.1. GCC Issues.................................................................................. 44715.7.1.2. Unix-Domain Sockets Broken..................................................... 44715.7.1.3. Internet Address Issues................................................................ 44715.7.1.4. Memory Management ................................................................. 448

    References and Resources ................................................................ 44915.7.2. Cygwin..................................................................................................... 44915.7.3. HP-UX ..................................................................................................... 45015.7.4. macOS...................................................................................................... 45115.7.5. MinGW/Native Windows ........................................................................ 451

    15.7.5.1. Collecting Crash Dumps on Windows ........................................ 45215.7.6. SCO OpenServer and SCO UnixWare..................................................... 452

    15.7.6.1. Skunkware ................................................................................... 45215.7.6.2. GNU Make .................................................................................. 45215.7.6.3. Readline....................................................................................... 45315.7.6.4. Using the UDK on OpenServer................................................... 45315.7.6.5. Reading the PostgreSQL Man Pages........................................... 45315.7.6.6. C99 Issues with the 7.1.1b Feature Supplement ......................... 45315.7.6.7. Threading on UnixWare .............................................................. 454

    15.7.7. Solaris ...................................................................................................... 45415.7.7.1. Required Tools ............................................................................ 45415.7.7.2. Problems with OpenSSL ............................................................. 45415.7.7.3. configure Complains About a Failed Test Program .................... 45415.7.7.4. 64-bit Build Sometimes Crashes ................................................. 45415.7.7.5. Compiling for Optimal Performance........................................... 45515.7.7.6. Using DTrace for Tracing PostgreSQL....................................... 455

    16. Installation from Source Code on Windows ..................................................................... 45616.1. Building with Visual C++ or the Microsoft Windows SDK................................. 456

    16.1.1. Requirements ........................................................................................... 45716.1.2. Special Considerations for 64-bit Windows ............................................ 45916.1.3. Building ................................................................................................... 45916.1.4. Cleaning and Installing ............................................................................ 45916.1.5. Running the Regression Tests .................................................................. 46016.1.6. Building the Documentation .................................................................... 460

    16.2. Building libpq with Visual C++ or Borland C++................................................. 46116.2.1. Generated Files ........................................................................................ 461

    17. Server Setup and Operation .............................................................................................. 46317.1. The PostgreSQL User Account ............................................................................ 46317.2. Creating a Database Cluster ................................................................................. 463

    17.2.1. Use of Secondary File Systems................................................................ 46417.2.2. Use of Network File Systems .................................................................. 464

    17.3. Starting the Database Server................................................................................. 46517.3.1. Server Start-up Failures ........................................................................... 46617.3.2. Client Connection Problems .................................................................... 467

    17.4. Managing Kernel Resources................................................................................. 46817.4.1. Shared Memory and Semaphores ............................................................ 46817.4.2. systemd RemoveIPC................................................................................ 47317.4.3. Resource Limits ....................................................................................... 474

    xi

  • 17.4.4. Linux Memory Overcommit .................................................................... 47517.4.5. Linux Huge Pages .................................................................................... 476

    17.5. Shutting Down the Server..................................................................................... 47717.6. Upgrading a PostgreSQL Cluster ......................................................................... 478

    17.6.1. Upgrading Data via pg_dumpall.............................................................. 47917.6.2. Upgrading Data via pg_upgrade .............................................................. 48017.6.3. Upgrading Data via Replication............................................................... 480

    17.7. Preventing Server Spoofing .................................................................................. 48017.8. Encryption Options............................................................................................... 48117.9. Secure TCP/IP Connections with SSL ................................................................. 482

    17.9.1. Using Client Certificates .......................................................................... 48317.9.2. SSL Server File Usage ............................................................................. 48317.9.3. Creating Certificates................................................................................. 484

    17.10. Secure TCP/IP Connections with SSH Tunnels ................................................. 48517.11. Registering Event Log on Windows................................................................... 486

    18. Server Configuration ......................................................................................................... 48818.1. Setting Parameters ................................................................................................ 488

    18.1.1. Parameter Names and Values................................................................... 48818.1.2. Parameter Interaction via the Configuration File..................................... 48818.1.3. Parameter Interaction via SQL................................................................. 48918.1.4. Parameter Interaction via the Shell .......................................................... 49018.1.5. Managing Configuration File Contents.................................................... 490

    18.2. File Locations ....................................................................................................... 49118.3. Connections and Authentication........................................................................... 492

    18.3.1. Connection Settings ................................................................................. 49218.3.2. Security and Authentication..................................................................... 495

    18.4. Resource Consumption......................................................................................... 49718.4.1. Memory.................................................................................................... 49718.4.2. Disk .......................................................................................................... 49918.4.3. Kernel Resource Usage............................................................................ 49918.4.4. Cost-based Vacuum Delay ....................................................................... 50018.4.5. Background Writer................................................................................... 50118.4.6. Asynchronous Behavior........................................................................... 501

    18.5. Write Ahead Log .................................................................................................. 50218.5.1. Settings..................................................................................................... 50218.5.2. Checkpoints.............................................................................................. 50618.5.3. Archiving ................................................................................................. 507

    18.6. Replication............................................................................................................ 50818.6.1. Sending Server(s)..................................................................................... 50818.6.2. Master Server ........................................................................................... 50918.6.3. Standby Servers ....................................................................................... 509

    18.7. Query Planning..................................................................................................... 51118.7.1. Planner Method Configuration................................................................. 51118.7.2. Planner Cost Constants ............................................................................ 51218.7.3. Genetic Query Optimizer......................................................................... 51318.7.4. Other Planner Options.............................................................................. 514

    18.8. Error Reporting and Logging ............................................................................... 51618.8.1. Where To Log .......................................................................................... 51618.8.2. When To Log ........................................................................................... 51818.8.3. What To Log ............................................................................................ 52018.8.4. Using CSV-Format Log Output ............................................................... 52318.8.5. Process Title............................................................................................. 525

    xii

  • 18.9. Run-time Statistics................................................................................................ 52518.9.1. Query and Index Statistics Collector ....................................................... 52518.9.2. Statistics Monitoring................................................................................ 526

    18.10. Automatic Vacuuming ........................................................................................ 52618.11. Client Connection Defaults ................................................................................ 528

    18.11.1. Statement Behavior ................................................................................ 52818.11.2. Locale and Formatting ........................................................................... 53218.11.3. Shared Library Preloading ..................................................................... 53418.11.4. Other Defaults ........................................................................................ 535

    18.12. Lock Management .............................................................................................. 53618.13. Version and Platform Compatibility................................................................... 537

    18.13.1. Previous PostgreSQL Versions .............................................................. 53718.13.2. Platform and Client Compatibility......................................................... 539

    18.14. Error Handling.................................................................................................... 53918.15. Preset Options..................................................................................................... 54018.16. Customized Options ........................................................................................... 54118.17. Developer Options .............................................................................................. 54118.18. Short Options...................................................................................................... 544

    19. Client Authentication ........................................................................................................ 54619.1. The pg_hba.conf File ........................................................................................ 54619.2. User Name Maps .................................................................................................. 55219.3. Authentication Methods ....................................................................................... 553

    19.3.1. Trust Authentication ................................................................................ 55419.3.2. Password Authentication ......................................................................... 55419.3.3. GSSAPI Authentication ........................................................................... 55419.3.4. SSPI Authentication................................................................................. 55619.3.5. Ident Authentication................................................................................. 55619.3.6. Peer Authentication.................................................................................. 55719.3.7. LDAP Authentication .............................................................................. 55819.3.8. RADIUS Authentication.......................................................................... 55919.3.9. Certificate Authentication ........................................................................ 56019.3.10. PAM Authentication .............................................................................. 561

    19.4. Authentication Problems ...................................................................................... 56120. Database Roles .................................................................................................................. 563

    20.1. Database Roles ..................................................................................................... 56320.2. Role Attributes...................................................................................................... 56420.3. Role Membership ................................................................................................. 56520.4. Dropping Roles..................................................................................................... 56620.5. Function Security.................................................................................................. 567

    21. Managing Databases ......................................................................................................... 56821.1. Overview .............................................................................................................. 56821.2. Creating a Database.............................................................................................. 56821.3. Template Databases .............................................................................................. 56921.4. Database Configuration ........................................................................................ 57021.5. Destroying a Database .......................................................................................... 57121.6. Tablespaces........................................................................................................... 571

    22. Localization....................................................................................................................... 57422.1. Locale Support...................................................................................................... 574

    22.1.1. Overview.................................................................................................. 57422.1.2. Behavior ................................................................................................... 57522.1.3. Problems .................................................................................................. 576

    22.2. Collation Support.................................................................................................. 576

    xiii

  • 22.2.1. Concepts................................................................................................... 57622.2.2. Managing Collations................................................................................ 578

    22.3. Character Set Support........................................................................................... 57922.3.1. Supported Character Sets......................................................................... 57922.3.2. Setting the Character Set.......................................................................... 58222.3.3. Automatic Character Set Conversion Between Server and Client........... 58322.3.4. Further Reading ....................................................................................... 585

    23. Routine Database Maintenance Tasks............................................................................... 58623.1. Routine Vacuuming .............................................................................................. 586

    23.1.1. Vacuuming Basics.................................................................................... 58623.1.2. Recovering Disk Space ............................................................................ 58723.1.3. Updating Planner Statistics ...................................................................... 58823.1.4. Updating The Visibility Map ................................................................... 58923.1.5. Preventing Transaction ID Wraparound Failures..................................... 589

    23.1.5.1. Multixacts and Wraparound ........................................................ 59223.1.6. The Autovacuum Daemon ....................................................................... 592

    23.2. Routine Reindexing .............................................................................................. 59323.3. Log File Maintenance........................................................................................... 594

    24. Backup and Restore .......................................................................................................... 59624.1. SQL Dump............................................................................................................ 596

    24.1.1. Restoring the Dump ................................................................................. 59624.1.2. Using pg_dumpall.................................................................................... 59724.1.3. Handling Large Databases ....................................................................... 598

    24.2. File System Level Backup.................................................................................... 59924.3. Continuous Archiving and Point-in-Time Recovery (PITR)................................ 600

    24.3.1. Setting Up WAL Archiving...................................................................... 60124.3.2. Making a Base Backup ............................................................................ 60324.3.3. Making a Base Backup Using the Low Level API .................................. 60424.3.4. Recovering Using a Continuous Archive Backup ................................... 60524.3.5. Timelines.................................................................................................. 60724.3.6. Tips and Examples ................................................................................... 608

    24.3.6.1. Standalone Hot Backups ............................................................. 60824.3.6.2. Compressed Archive Logs .......................................................... 60924.3.6.3. archive_command Scripts ........................................................ 609

    24.3.7. Caveats ..................................................................................................... 60925. High Availability, Load Balancing, and Replication......................................................... 611

    25.1. Comparison of Different Solutions....................................................................... 61125.2. Log-Shipping Standby Servers............................................................................. 614

    25.2.1. Planning ................................................................................................... 61525.2.2. Standby Server Operation ........................................................................ 61525.2.3. Preparing the Master for Standby Servers ............................................... 61625.2.4. Setting Up a Standby Server .................................................................... 61625.2.5. Streaming Replication.............................................................................. 617

    25.2.5.1. Authentication ............................................................................. 61825.2.5.2. Monitoring................................................................................... 618

    25.2.6. Replication Slots ...................................................................................... 61925.2.6.1. Querying and manipulating replication slots .............................. 61925.2.6.2. Configuration Example ............................................................... 619

    25.2.7. Cascading Replication ............................................................................. 62025.2.8. Synchronous Replication ......................................................................... 620

    25.2.8.1. Basic Configuration..................................................................... 62025.2.8.2. Planning for Performance............................................................ 621

    xiv

  • 25.2.8.3. Planning for High Availability .................................................... 62225.2.9. Continuous archiving in standby ............................................................. 622

    25.3. Failover ................................................................................................................. 62325.4. Alternative Method for Log Shipping .................................................................. 623

    25.4.1. Implementation ........................................................................................ 62425.4.2. Record-based Log Shipping..................................................................... 625

    25.5. Hot Standby .......................................................................................................... 62525.5.1. User’s Overview....................................................................................... 62525.5.2. Handling Query Conflicts ........................................................................ 62725.5.3. Administrator’s Overview........................................................................ 62925.5.4. Hot Standby Parameter Reference ........................................................... 63225.5.5. Caveats ..................................................................................................... 632

    26. Recovery Configuration .................................................................................................... 63426.1. Archive Recovery Settings ................................................................................... 63426.2. Recovery Target Settings ...................................................................................... 63526.3. Standby Server Settings........................................................................................ 636

    27. Monitoring Database Activity........................................................................................... 63827.1. Standard Unix Tools ............................................................................................. 63827.2. The Statistics Collector......................................................................................... 639

    27.2.1. Statistics Collection Configuration .......................................................... 63927.2.2. Viewing Statistics..................................................................................... 64027.2.3. Statistics Functions .................................................................................. 654

    27.3. Viewing Locks...................................................................................................... 65627.4. Dynamic Tracing .................................................................................................. 656

    27.4.1. Compiling for Dynamic Tracing.............................................................. 65627.4.2. Built-in Probes ......................................................................................... 65627.4.3. Using Probes ............................................................................................ 66527.4.4. Defining New Probes ............................................................................... 666

    28. Monitoring Disk Usage..................................................................................................... 66828.1. Determining Disk Usage ...................................................................................... 66828.2. Disk Full Failure................................................................................................... 669

    29. Reliability and the Write-Ahead Log................................................................................ 67029.1. Reliability ............................................................................................................. 67029.2. Write-Ahead Logging (WAL) .............................................................................. 67229.3. Asynchronous Commit......................................................................................... 67229.4. WAL Configuration .............................................................................................. 67429.5. WAL Internals ...................................................................................................... 677

    30. Regression Tests................................................................................................................ 67830.1. Running the Tests ................................................................................................. 678

    30.1.1. Running the Tests Against a Temporary Installation ............................... 67830.1.2. Running the Tests Against an Existing Installation ................................. 67830.1.3. Additional Test Suites .............................................................................. 67930.1.4. Locale and Encoding................................................................................ 67930.1.5. Extra Tests................................................................................................ 68030.1.6. Testing Hot Standby................................................................................. 680

    30.2. Test Evaluation ..................................................................................................... 68130.2.1. Error Message Differences....................................................................... 68130.2.2. Locale Differences ................................................................................... 68130.2.3. Date and Time Differences ...................................................................... 68230.2.4. Floating-Point Differences....................................................................... 68230.2.5. Row Ordering Differences ....................................................................... 68230.2.6. Insufficient Stack Depth........................................................................... 683

    xv

  • 30.2.7. The “random” Test ................................................................................... 68330.2.8. Configuration Parameters......................................................................... 683

    30.3. Variant Comparison Files ..................................................................................... 68330.4. TAP Tests.............................................................................................................. 68430.5. Test Coverage Examination.................................................................................. 685

    IV. Client Interfaces ....................................................................................................................... 68631. libpq - C Library ............................................................................................................... 688

    31.1. Database Connection Control Functions .............................................................. 68831.1.1. Connection Strings................................................................................... 694

    31.1.1.1. Keyword/Value Connection Strings ............................................ 69431.1.1.2. Connection URIs ......................................................................... 695

    31.1.2. Parameter Key Words .............................................................................. 69531.2. Connection Status Functions ................................................................................ 69931.3. Command Execution Functions ........................................................................... 704

    31.3.1. Main Functions ........................................................................................ 70431.3.2. Retrieving Query Result Information ...................................................... 71131.3.3. Retrieving Other Result Information ....................................................... 71531.3.4. Escaping Strings for Inclusion in SQL Commands ................................. 715

    31.4. Asynchronous Command Processing ................................................................... 71831.5. Retrieving Query Results Row-By-Row .............................................................. 72231.6. Canceling Queries in Progress.............................................................................. 72331.7. The Fast-Path Interface......................................................................................... 72431.8. Asynchronous Notification................................................................................... 72531.9. Functions Associated with the COPY Command .................................................. 726

    31.9.1. Functions for Sending COPY Data............................................................ 72631.9.2. Functions for Receiving COPY Data......................................................... 72731.9.3. Obsolete Functions for COPY ................................................................... 728

    31.10. Control Functions ............................................................................................... 73031.11. Miscellaneous Functions .................................................................................... 73131.12. Notice Processing ............................................................................................... 73331.13. Event System ...................................................................................................... 734

    31.13.1. Event Types............................................................................................ 73431.13.2. Event Callback Procedure...................................................................... 73631.13.3. Event Support Functions........................................................................ 73731.13.4. Event Example ....................................................................................... 738

    31.14. Environment Variables ....................................................................................... 74031.15. The Password File .............................................................................................. 74231.16. The Connection Service File .............................................................................. 74231.17. LDAP Lookup of Connection Parameters .......................................................... 74331.18. SSL Support........................................................................................................ 744

    31.18.1. Client Verification of Server Certificates ............................................... 74431.18.2. Client Certificates................................................................................... 74531.18.3. Protection Provided in Different Modes ................................................ 74531.18.4. SSL Client File Usage............................................................................ 74731.18.5. SSL Library Initialization ...................................................................... 747

    31.19. Behavior in Threaded Programs ......................................................................... 74831.20. Building libpq Programs..................................................................................... 74931.21. Example Programs.............................................................................................. 750

    32. Large Objects .................................................................................................................... 76032.1. Introduction .......................................................................................................... 76032.2. Implementation Features ...................................................................................... 760

    xvi

  • 32.3. Client Interfaces.................................................................................................... 76032.3.1. Creating a Large Object ........................................................................... 76132.3.2. Importing a Large Object......................................................................... 76132.3.3. Exporting a Large Object......................................................................... 76232.3.4. Opening an Existing Large Object........................................................... 76232.3.5. Writing Data to a Large Object................................................................ 76232.3.6. Reading Data from a Large Object .......................................................... 76332.3.7. Seeking in a Large Object........................................................................ 76332.3.8. Obtaining the Seek Position of a Large Object........................................ 76332.3.9. Truncating a Large Object ....................................................................... 76432.3.10. Closing a Large Object Descriptor ........................................................ 76432.3.11. Removing a Large Object ...................................................................... 764

    32.4. Server-side Functions ........................................................................................... 76532.5. Example Program ................................................................................................. 766

    33. ECPG - Embedded SQL in C............................................................................................ 77233.1. The Concept.......................................................................................................... 77233.2. Managing Database Connections ......................................................................... 772

    33.2.1. Connecting to the Database Server .......................................................... 77233.2.2. Choosing a Connection ............................................................................ 77433.2.3. Closing a Connection............................................................................... 775

    33.3. Running SQL Commands..................................................................................... 77533.3.1. Executing SQL Statements ...................................................................... 77533.3.2. Using Cursors........................................................................................... 77633.3.3. Managing Transactions ............................................................................ 77733.3.4. Prepared Statements................................................................................. 777

    33.4. Using Host Variables ............................................................................................ 77833.4.1. Overview.................................................................................................. 77833.4.2. Declare Sections....................................................................................... 77833.4.3. Retrieving Query Results ......................................................................... 77933.4.4. Type Mapping .......................................................................................... 780

    33.4.4.1. Handling Character Strings ......................................................... 78133.4.4.2. Accessing Special Data Types..................................................... 781

    33.4.4.2.1. timestamp, d


Recommended