Postgresql Programmer's Guide
Total Page:16
File Type:pdf, Size:1020Kb
PostgreSQL Programmer’s Guide The PostgreSQL Development Team Edited by Thomas Lockhart PostgreSQL Programmer’s Guide by The PostgreSQL Development Team Edited by Thomas Lockhart PostgreSQL is Copyright © 1996-9 by the Postgres Global Development Group. Table of Contents Summary......................................................................................................................................i 1. Introduction ............................................................................................................................1 Resources............................................................................................................................1 Terminology........................................................................................................................2 Notation ..............................................................................................................................3 Y2K Statement....................................................................................................................3 Copyrights and Trademarks................................................................................................4 2. Architecture ............................................................................................................................5 Postgres Architectural Concepts .........................................................................................5 3. Extending SQL: An Overview...............................................................................................7 How Extensibility Works....................................................................................................7 The Postgres Type System..................................................................................................7 About the Postgres System Catalogs ..................................................................................8 4. Extending SQL: Functions ..................................................................................................11 Query Language (SQL) Functions....................................................................................11 SQL Functions on Base Types ................................................................................11 SQL Functions on Composite Types.......................................................................12 Programming Language Functions ...................................................................................13 Programming Language Functions on Base Types .................................................13 Programming Language Functions on Composite Types........................................15 Caveats ....................................................................................................................16 5. Extending SQL: Types.........................................................................................................18 User-Defined Types..........................................................................................................18 Functions Needed for a User-Defined Type............................................................18 Large Objects ..........................................................................................................19 6. Extending SQL: Operators..................................................................................................20 Operator Optimization Information ..................................................................................21 COMMUTATOR ....................................................................................................21 NEGATOR..............................................................................................................22 RESTRICT..............................................................................................................22 JOIN .......................................................................................................................23 HASHES .................................................................................................................23 SORT1 and SORT2.................................................................................................24 7. Extending SQL: Aggregates ................................................................................................26 8. The Postgres Rule System....................................................................................................28 What is a Querytree?.........................................................................................................28 The Parts of a Querytree..........................................................................................28 Views and the Rule System ..............................................................................................30 Implementation of Views in Postgres......................................................................30 How SELECT Rules Work .....................................................................................30 View Rules in Non-SELECT Statements................................................................35 The Power of Views in Postgres .............................................................................36 i Benefits..........................................................................................................36 Concerns ........................................................................................................36 Implementation Side Effects ...................................................................................37 Rules on INSERT, UPDATE and DELETE.....................................................................38 Differences to View Rules ......................................................................................38 How These Rules Work ..........................................................................................38 A First Rule Step by Step ..............................................................................39 Cooperation with Views..........................................................................................42 Rules and Permissions ......................................................................................................48 Rules versus Triggers........................................................................................................49 9. Interfacing Extensions To Indices.......................................................................................52 10. GiST Indices........................................................................................................................59 11. Procedural Languages........................................................................................................61 Installing Procedural Languages.......................................................................................61 PL/pgSQL .........................................................................................................................62 Overview .................................................................................................................62 Description ..............................................................................................................63 Structure of PL/pgSQL ..................................................................................63 Comments......................................................................................................63 Declarations ...................................................................................................63 Data Types.....................................................................................................64 Expressions....................................................................................................65 Statements......................................................................................................66 Trigger Procedures.........................................................................................68 Exceptions......................................................................................................69 Examples.................................................................................................................69 Some Simple PL/pgSQL Functions ...............................................................70 PL/pgSQL Function on Composite Type.......................................................70 PL/pgSQL Trigger Procedure ........................................................................70 PL/Tcl ...............................................................................................................................71 Overview .................................................................................................................71 Description ..............................................................................................................71 Postgres Functions and Tcl Procedure Names...............................................71 Defining Functions in PL/Tcl ........................................................................71 Global Data in PL/Tcl....................................................................................72 Trigger Procedures in PL/Tcl.........................................................................72 Database Access from PL/Tcl........................................................................74