229
0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011 0012 0013 0014 0015 0016 0017 0018 0019 0020 0021 0022 0023 0024 0025 0026 0027 0028 0029 0030 0031 0032 0033 0034 0035 0036 0037 0038 0039 0040 0041 0042 0043 0044 0045 0046 0047 0048 0049 0050 0051 0052 0053 0054 0055 0056 0057 0058 0059 0060 0061 0062 0063 0064 0065 0066 PostgreSQL: Introduction and Concepts Bruce Momjian June 17, 2000

PostgreSQL: Introduction and Concepts - database.sarang.net€¦ · A database novice should read the entire book, while skimming over the later chapters. The complex ... mailing

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

  • 000100020003000400050006000700080009001000110012001300140015001600170018001900200021002200230024002500260027002800290030003100320033003400350036003700380039004000410042004300440045004600470048004900500051005200530054005500560057005800590060006100620063006400650066

    PostgreSQL:Introduction

    andConcepts

    Bruce Momjian

    June 17, 2000

  • 006700680069007000710072007300740075007600770078007900800081008200830084008500860087008800890090009100920093009400950096009700980099010001010102010301040105010601070108010901100111011201130114011501160117011801190120012101220123012401250126012701280129013001310132

    ii

    WHERE NULL CREATE UNION AS DISTINCT

    INDEX TRIGGER GRANT ROLLBACK DEFAULT SUM

    INTO ALTER COMMIT SELECT REVOKE CASE

    TABLE FROM INSERT OPERATOR SET UPDATE

    FUNCTION EXCEPT DELETE VALUES ORDER BY COUNT

    BEGIN WORK LIKE IN VIEW HAVING EXISTS

  • 013301340135013601370138013901400141014201430144014501460147014801490150015101520153015401550156015701580159016001610162016301640165016601670168016901700171017201730174017501760177017801790180018101820183018401850186018701880189019001910192019301940195019601970198

    Note to Reviewers

    The material on these pages is a work in progress, titled, PostgreSQL: Introduction and Concepts, to bepublished in 2000, ©Addison–Wesley. Posted with permission of the publisher. All rights reserved.

    I have completed my first draft. The appendix needs a little more work.I am interested in any comments you may have, including typographic errors, places with not enough

    detail or too much detail, missing topics, extraneous topics, confusing sentences, poor word choice, etc. ThePDF version has numbers appearing in the margins to allow you to easily refer to specific lines in the book.People reading the web version may refer to specific URL’S. Please mention the date of June 17, 2000 whenreferring to this document. You may contact me at mailto:[email protected].

    A current copy may be retrieved from http://www.postgresql.org/docs/awbook.html. Also, it is availablefrom the POSTGRESQL FAQ’s and Documentation page, http://www.postgresql.org/docs. It is updatedautomatically every night. This book is set in Bitstream Century Old Style, 11 point.

    Keep in mind that this is to be printed as a book. In the PDF version, diagrams may not appear on thesame pages that refer to them. They will appear on the facing page when printed in book format.

    iii

    mailto:[email protected]://www.postgresql.org/docs/awbook.htmlhttp://www.postgresql.org/docs

  • 019902000201020202030204020502060207020802090210021102120213021402150216021702180219022002210222022302240225022602270228022902300231023202330234023502360237023802390240024102420243024402450246024702480249025002510252025302540255025602570258025902600261026202630264

    iv NOTE TO REVIEWERS

  • 026502660267026802690270027102720273027402750276027702780279028002810282028302840285028602870288028902900291029202930294029502960297029802990300030103020303030403050306030703080309031003110312031303140315031603170318031903200321032203230324032503260327032803290330

    Foreword

    Most research projects never leave the academic environment. Occasionally, exceptional ones survivethe transition from the university to the real world and go on to become a phenomenon. POSTGRESQLis one of those projects. Its popularity and success are a testament to the dedication and hard work ofthe POSTGRESQL global development team. Developing an advanced database system is no small feat.Maintaining and enhancing an inherited code base is even more challenging. The POSTGRESQL team has notonly managed to improve the quality and usability of the system, but also expand its use among the Internetuser community. This book is a major milestone in the history of the project.

    POSTGRES95, later renamed POSTGRESQL, started out as a small project to overhaul POSTGRES. POSTGRESis a novel and feature-rich database system created by the students and staff at the UNIVERSITY OF CALIFORNIAAT BERKELEY. Our goal was to keep the powerful and useful features while trimming down the bloat causedby much experimentation and research. We had a lot of fun reworking the internals. At the time, we hadno idea where we were going with the project. The POSTGRES95 exercise was not research, but simplya bit of engineering housecleaning. By the spring of 1995, it occurred to us that there was a need for anopen-source SQL-based multi-user database in the Internet user community. Our first release was met withgreat enthusiasm. We are very pleased to see the project continuing.

    Obtaining information about a complex system like POSTGRESQL is a great barrier to its adoption. Thisbook fills a critical gap in the documentation of the project and provides an excellent overview of the system.It covers a wide range of topics from the basics to the more advanced and unique features of POSTGRESQL.

    In writing this book, Bruce Momjian has drawn on his experience in helping beginners with POSTGRESQL.The text is easy to understand and full of practical tips. Momjian captures database concepts using simpleand easy to understand language. He also presents numerous real life examples throughout the book. Hedoes an outstanding job and covers many advanced POSTGRESQL topics. Enjoy reading the book and havefun exploring POSTGRESQL! It is our hope this book will not only teach you about using PostgreSQL but alsoinspire you to delve into its innards and contribute to the ongoing POSTGRESQL development effort.

    JOLLY CHEN and ANDREW YU, co-authors of POSTGRES95

    v

  • 033103320333033403350336033703380339034003410342034303440345034603470348034903500351035203530354035503560357035803590360036103620363036403650366036703680369037003710372037303740375037603770378037903800381038203830384038503860387038803890390039103920393039403950396

    vi FOREWORD

  • 039703980399040004010402040304040405040604070408040904100411041204130414041504160417041804190420042104220423042404250426042704280429043004310432043304340435043604370438043904400441044204430444044504460447044804490450045104520453045404550456045704580459046004610462

    Preface

    This book is about POSTGRESQL, the most advanced open source database. From its origin in academia,POSTGRESQL has moved to the Internet with explosive growth. It is hard to believe the advances during thepast four years under the guidance of a team of world-wide Internet developers. This book is a testament totheir vision, and to the success POSTGRESQL has become.

    The book is designed to lead the reader from their first database query through the complex queriesneeded to solve real-world problems. No knowledge of database theory or practice is required. Basicknowledge of operating system capabilities is expected, such as the ability to type at an operating systemprompt.

    The book begins with a short history of POSTGRESQL. It leads the reader through their first query, andteaches the most important database commands. Common problems are covered early, like placing quotesinside quoted strings. This should prevent users from getting stuck with queries that fail. I have seen manybug reports in the past few years, and try to cover the common pitfalls.

    With a firm foundation established, additional commands are introduced. Finally, specialty chaptersoutline complex topics like multi-user control and performance. While coverage of these complex topics isnot exhaustive, I try to show common real-world problems and their solutions.

    At each step, the purpose of each command is clearly illustrated. I want readers to understand more thanquery syntax. I want them to know why each command is valuable, so they will use the proper commands intheir real-world database applications.

    A database novice should read the entire book, while skimming over the later chapters. The complexnature of database systems should not prevent readers from getting started. Test databases are a safe wayto try queries. As readers gain experience, later chapters will begin to make sense. Experienced databaseusers can skip the chapters on basic SQL functionality. The cross-referencing of sections should allow you toquickly move from general to more specific information.

    Much information has been moved out of the main body of the book into appendices. Appendix Ashows how to find additional information about POSTGRESQL. Appendix B has information about installingPOSTGRESQL. Appendix C lists the features of POSTGRESQL not found in other database systems. Appendix Dcontains a copy of the POSTGRESQL reference manual which should be consulted anytime you are havingtrouble with query syntax. Also, I should mention the excellent documentation that is part of POSTGRESQL.The documentation covers many complex topics. It includes much POSTGRESQL-specific functionality thatcannot be covered in a book of this length. I refer to sections of the documentation in this text whereappropriate.

    The website for this book is located at http://www.postgresql.org/docs/awbook.html.

    vii

    http://www.postgresql.org/docs/awbook.html

  • 046304640465046604670468046904700471047204730474047504760477047804790480048104820483048404850486048704880489049004910492049304940495049604970498049905000501050205030504050505060507050805090510051105120513051405150516051705180519052005210522052305240525052605270528

    viii PREFACE

  • 052905300531053205330534053505360537053805390540054105420543054405450546054705480549055005510552055305540555055605570558055905600561056205630564056505660567056805690570057105720573057405750576057705780579058005810582058305840585058605870588058905900591059205930594

    Acknowledgements

    Update this page with current information before publication.

    POSTGRESQL and this book would not be possible without the talented and hard-working members of thePOSTGRESQL Global Development Team. They took source code that could have become just another aban-doned project, and turned it into the open source alternative to commercial database systems. POSTGRESQLis a shining example of Internet community development.

    Steering

    • FOURNIER, MARC G. in Wolfville, Nova Scotia, Canada coordinates the whole effort and provides theserver and administers our primary web site, mailing lists, ftp site, and source code repository.

    • LANE, TOM in Pittsburgh, Pennsylvania, USA Often seen working on planner/optimizer, but has leftfingerprints in many places. Generally more interested in bugfixes and performance improvementsthan adding features.

    • LOCKHART, THOMAS G. in Pasadena, California, USA works on documentation, data types, particularlydate/time and geometric objects, and on SQL standards compatibility.

    • MIKHEEV, VADIM B. in San Francisco, California, USA does large projects, like vacuum, subselects,triggers, and multi-version concurrency control(MVCC).

    • MOMJIAN, BRUCE in Philadelphia, Pennsylvania, USA maintains FAQ and TODO lists, code cleanup, patchapplication, training materials, and some coding.

    • WIECK, JAN near Hamburg, Germany overhauled the query rewrite rule system, wrote our procedurallanguages PL/PGSQL and PL/TCL and added the NUMERIC type.

    Major Developers

    • CAIN, D’ARCY J.M. in Toronto, Ontario, Canada worked on the TCL interface, PyGreSQL, and the INETtype.

    • DAL ZOTTO, MASSIMO near Trento, Italy has done locking code and other improvements.

    • ELPHICK, OLIVER in Newport, Isle of Wight, UK maintains the POSTGRESQL package for DebianGNU/Linux.

    • HORAK, DANIEL near Pilzen, Czech Republic did the WinNT port of PostgreSQL (using the Cygwinenvironment).

    • INOUE, HIROSHI in Fukui, Japan improved btree index access.

    ix

  • 059505960597059805990600060106020603060406050606060706080609061006110612061306140615061606170618061906200621062206230624062506260627062806290630063106320633063406350636063706380639064006410642064306440645064606470648064906500651065206530654065506560657065806590660

    x ACKNOWLEDGEMENTS

    • ISHII, TATSUO in Zushi, Kanagawa, Japan handles multi-byte foreign language support and portingissues.

    • MARTIN, DR. ANDREW C.R. in London, England has done the ECPG interface and helped in the Linuxand Irix FAQs including some patches to the POSTGRESQL code.

    • MERGL, EDMUND in Stuttgart, Germany created and maintains pgsql_perl5. He also created DBD-Pgwhich is available via CPAN.

    • MESKES, MICHAEL in Dusseldorf, Germany handles multi-byte foreign language support, and maintainsecpg.

    • MOUNT, PETER in Maidstone, Kent, United Kingdom has done the Java JDBC Interface.

    • NIKOLAIDIS, BYRON in Baltimore, Maryland, USA rewrote and maintains the ODBC interface for Windows.

    • OWEN, LAMAR in Pisgah Forest, North Carolina, USA RPM package maintainer.

    • TEODORESCU, CONSTANTIN in Braila, Romania has done the PgAccess DB Interface.

    • THYNI, GÖRAN in Kiruna, Sweden has worked on the unix socket code.

    Non-code contributors

    • BARTUNOV, OLEG in Moscow, Russia introduced the locale support.

    • VIELHABER, VINCE near Detroit, Michigan, USA maintains our website.

    All developers listed in alphabetical order.

  • 066106620663066406650666066706680669067006710672067306740675067606770678067906800681068206830684068506860687068806890690069106920693069406950696069706980699070007010702070307040705070607070708070907100711071207130714071507160717071807190720072107220723072407250726

    Contents

    Note to Reviewers iii

    Foreword v

    Preface vii

    Acknowledgements ix

    1 History of POSTGRESQL 11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 UNIVERSITY OF CALIFORNIA AT BERKELEY . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.3 Development Leaves BERKELEY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.4 POSTGRESQL Global Development Team . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.5 Open Source Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

    2 Issuing Database Commands 52.1 Starting a Database Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52.2 Controlling a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.3 Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.4 Exiting a Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

    3 Basic SQL Commands 93.1 Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.2 Creating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.3 Adding Data with INSERT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.4 Viewing Data with SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.5 Selecting Specific Rows with WHERE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.6 Removing Data with DELETE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153.7 Modifying Data with UPDATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153.8 Sorting Data with ORDER BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153.9 Destroying Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

    4 Customizing Queries 194.1 Data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194.2 Quotes Inside Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194.3 Using NULL Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

    xi

  • 072707280729073007310732073307340735073607370738073907400741074207430744074507460747074807490750075107520753075407550756075707580759076007610762076307640765076607670768076907700771077207730774077507760777077807790780078107820783078407850786078707880789079007910792

    xii CONTENTS

    4.4 Controlling DEFAULT Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234.5 Column Labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244.6 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244.7 AND/OR Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254.8 Range of Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284.9 LIKE Comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284.10 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294.11 CASE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334.12 Distinct Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344.13 Functions and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344.14 SET, SHOW, and RESET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384.15 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

    5 SQL Aggregates 415.1 Aggregates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415.2 Using GROUP BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445.3 Using HAVING . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445.4 Query Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

    6 Joining Tables 476.1 Table and Column References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476.2 Joined Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476.3 Creating Joined Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 496.4 Performing Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516.5 Three and Four Table Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536.6 Additional Join Possibilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556.7 Choosing a Join Key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566.8 One-to-Many Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576.9 Unjoined Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596.10 Table Aliases and Self-Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596.11 Non-Equijoins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606.12 Ordering Multiple Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606.13 Primary and Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 626.14 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62

    7 Numbering Rows 657.1 Object Identification Numbers (OIDs) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 657.2 Object Identification Number Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667.3 Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677.4 Creating Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677.5 Using Sequences to Number Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 697.6 Serial Column Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707.7 Manually Numbering Rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71

  • 079307940795079607970798079908000801080208030804080508060807080808090810081108120813081408150816081708180819082008210822082308240825082608270828082908300831083208330834083508360837083808390840084108420843084408450846084708480849085008510852085308540855085608570858

    CONTENTS xiii

    8 Combining SELECTs 738.1 UNION, EXCEPT, INTERSECT Clauses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738.2 Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 768.3 Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838.4 Subqueries in Non-SELECT Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 838.5 UPDATE with FROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858.6 Inserting Data Using SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858.7 Creating Tables Using SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 868.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87

    9 Data Types 899.1 Purpose of Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899.2 Installed Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899.3 Type Conversion using CAST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939.4 Support Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939.5 Support Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939.6 Support Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969.7 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969.8 Large Objects(BLOBS) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 989.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

    10 Transactions and Locks 9910.1 Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9910.2 Multi-Statement Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9910.3 Visibility of Committed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10110.4 Read Committed and Serializable Isolation Levels . . . . . . . . . . . . . . . . . . . . . . . . 10210.5 Locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10310.6 Deadlocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10510.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

    11 Performance 10711.1 Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10711.2 Unique Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10811.3 Cluster . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10811.4 Vacuum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10911.5 Vacuum Analyze . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10911.6 EXPLAIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10911.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

    12 Controlling Results 11312.1 LIMIT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11312.2 Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11412.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

    13 Table Management 11713.1 Temporary Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11713.2 ALTER TABLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11713.3 GRANT and REVOKE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11913.4 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

  • 085908600861086208630864086508660867086808690870087108720873087408750876087708780879088008810882088308840885088608870888088908900891089208930894089508960897089808990900090109020903090409050906090709080909091009110912091309140915091609170918091909200921092209230924

    xiv CONTENTS

    13.5 Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12213.6 Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12313.7 LISTEN and NOTIFY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12613.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

    14 Constraints 12914.1 NOT NULL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12914.2 UNIQUE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13014.3 PRIMARY KEY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13114.4 FOREIGN KEY/REFERENCES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13114.5 CHECK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13914.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

    15 Importing and Exporting Data 14115.1 Using COPY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14115.2 COPY File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14115.3 DELIMITERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14315.4 COPY without files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14315.5 Backslashes and NULLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14415.6 COPY Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14515.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

    16 Database Query Tools 14716.1 PSQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14716.2 PGACCESS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15216.3 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

    17 Programming Interfaces 15517.1 C Language Interface (LIBPQ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15617.2 Pgeasy(LIBPGEASY) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15817.3 Embedded C (ECPG) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15817.4 C++ (LIBPQ++) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15817.5 Compiling Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15817.6 Assignment to Program Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16217.7 ODBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16217.8 JAVA (JDBC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16317.9 Scripting Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16317.10PERL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16317.11TCL/TK (PGTCLSH/PGTKSH) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16317.12PYTHON (PYGRESQL) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16317.13PHP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16617.14Installing Scripting Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16817.15Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168

    18 Functions and Triggers 16918.1 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16918.2 SQL Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16918.3 PL/PGSQL Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17418.4 Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

  • 092509260927092809290930093109320933093409350936093709380939094009410942094309440945094609470948094909500951095209530954095509560957095809590960096109620963096409650966096709680969097009710972097309740975097609770978097909800981098209830984098509860987098809890990

    CONTENTS xv

    18.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180

    19 Extending POSTGRESQL Using C 18319.1 Writing C code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18319.2 Compile the C code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18419.3 Register the New Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18419.4 Optionally Create Operators, Types, and Aggregates . . . . . . . . . . . . . . . . . . . . . . 18519.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

    20 Administration 18720.1 Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18720.2 Creating Users . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18720.3 Creating Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18920.4 Access Configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18920.5 Backup and Restore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19120.6 Server Startup and Shutdown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19220.7 Monitoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19220.8 Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19320.9 System Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19420.10Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19420.11Upgrading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19420.12Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

    A Additional Resources 197A.1 Frequently Asked Questions (FAQ’S) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197A.2 Mailing List Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197A.3 Supplied Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197A.4 Commercial Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197A.5 Modifying the Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197

    B Installation 199

    C PostgreSQL Non-Standard Features by Chapter 201

    D Reference Manual 203

    Bibliography 205

    Index 205

  • 099109920993099409950996099709980999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056

    xvi CONTENTS

  • 105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122

    List of Figures

    2.1 psql session startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.2 My first SQL query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62.3 Multi-line query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.4 Backslash-p demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

    3.1 Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.2 Create table friend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.3 Example of backslash-d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113.4 INSERT into friend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.5 Additional friend INSERTs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123.6 My first SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133.7 My first WHERE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143.8 More complex WHERE clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143.9 A single cell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143.10 A block of cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143.11 Comparing string fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153.12 DELETE example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.13 My first UPDATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163.14 Use of ORDER BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173.15 Reverse ORDER BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173.16 Use of ORDER BY and WHERE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

    4.1 Example of common data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204.2 Insertion of specific columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214.3 NULL handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224.4 Comparison of NULL fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224.5 NULLs and blank strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234.6 Using DEFAULTs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244.7 Controlling column labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244.8 Computation using a column label . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254.9 Comment styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254.10 New friends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264.11 WHERE test for Sandy Gleason . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264.12 Friends in New Jersey and Pennsylvania . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274.13 Mixing ANDs and ORs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274.14 Properly mixing ANDs and ORs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274.15 Selecting a range of values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284.16 Firstname begins with D. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294.17 Regular expression sample queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

    xvii

  • 112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188

    xviii LIST OF FIGURES

    4.18 Complex regular expression queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324.19 CASE example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334.20 Complex CASE example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344.21 DISTINCT prevents duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354.22 Function examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364.23 Operator examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 374.24 SHOW and RESET examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

    5.1 Aggregate examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425.2 Aggregates and NULLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435.3 Aggregate with GROUP BY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445.4 GROUP BY on two columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455.5 HAVING usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

    6.1 Qualified column names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486.2 Joining tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486.3 Creation of company tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506.4 Insertion into company tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 516.5 Finding customer name using two queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526.6 Finding customer name using one query . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 526.7 Finding order number for customer name . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536.8 Three-table join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 536.9 Four-table join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 546.10 Employees who have taken orders for customers. . . . . . . . . . . . . . . . . . . . . . . . . 546.11 Joining customer and employee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556.12 Joining part and employee . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 556.13 Statename table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 566.14 Using a customer code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576.15 One-to-many join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 586.16 Unjoined tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596.17 Using table aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 596.18 Examples of self-joins using table aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 606.19 Non-equijoins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616.20 New salesorder table for multiple parts per order . . . . . . . . . . . . . . . . . . . . . . . . 616.21 Orderpart table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 616.22 Queries involving orderpart table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

    7.1 OID test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667.2 Columns with OIDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 667.3 Examples of sequence function use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 687.4 Numbering customer rows using a sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . 697.5 Customer table using SERIAL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

    8.1 Combining two columns with UNION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738.2 Combining two tables with UNION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 748.3 UNION with duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758.4 UNION ALL with duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 758.5 EXCEPT restricts output from the first SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . 758.6 INTERSECT returns only duplicated rows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

  • 118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254

    LIST OF FIGURES xix

    8.7 Friends not in Dick Gleason’s state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 778.8 Subqueries can replace some joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 788.9 Correlated subquery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 798.10 Employees who took orders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808.11 Customers who have no orders . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 818.12 IN query rewritten using ANY and EXISTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 828.13 NOT IN query rewritten using ALL and EXISTS . . . . . . . . . . . . . . . . . . . . . . . . . . 838.14 Simulating outer joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848.15 Subqueries with UPDATE and DELETE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 848.16 UPDATE the order_date . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858.17 Using SELECT with INSERT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 858.18 Table creation with SELECT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86

    9.1 Example of a function call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 939.2 Error generated by undefined function/type combination. . . . . . . . . . . . . . . . . . . . . 959.3 Error generated by undefined operator/type combination . . . . . . . . . . . . . . . . . . . . 969.4 Creation of array columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 969.5 Using arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 979.6 Using large images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98

    10.1 INSERT with no explicit transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9910.2 INSERT with explicit transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10010.3 Two INSERTs in a single transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10010.4 Multi-statement transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10010.5 Transaction rollback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10110.6 Read-committed isolation level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10210.7 Serializable isolation level . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10310.8 SELECT with no locking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10410.9 SELECT…FOR UPDATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

    11.1 Example of CREATE INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10711.2 Example of a unique index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10811.3 Using EXPLAIN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10911.4 More complex EXPLAIN examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11011.5 EXPLAIN example using joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

    12.1 Examples of LIMIT and LIMIT/OFFSET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11312.2 Cursor usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

    13.1 Temporary table auto-destruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11813.2 Example of temporary table use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11813.3 ALTER TABLE examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11913.4 Examples of the GRANT command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12013.5 Creation of inherited tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12013.6 Accessing inherited tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12113.7 Inheritance in layers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12113.8 Examples of views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12213.9 Rule that prevents INSERT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12313.10Rules to log table changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124

  • 125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320

    xx LIST OF FIGURES

    13.11Use of rule to log table changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12513.12Views ignore table modifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12513.13Rules to handle view modifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12613.14Rules handle view modifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127

    14.1 NOT NULL constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12914.2 NOT NULL with DEFAULT constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13014.3 Unique column constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13014.4 Multi-column unique constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13114.5 Creation of PRIMARY KEY column . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13114.6 Example of a multi-column primary key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13214.7 Foreign key creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13214.8 Foreign key constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13314.9 Creation of company tables using primary and foreign keys . . . . . . . . . . . . . . . . . . . 13314.10Customer table with foreign key actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13414.11Foreign key actions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13514.12Example of a multi-column foreign key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13614.13MATCH FULL foreign key . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13714.14DEFERRABLE foreign key constraint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13814.15CHECK constraints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

    15.1 Example of COPY…TO and COPY…FROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14215.2 Example of COPY…FROM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14215.3 Example of COPY…TO…USING DELIMITERS . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14315.4 Example of COPY…FROM…USING DELIMITERS . . . . . . . . . . . . . . . . . . . . . . . . . . 14315.5 COPY using stdin and stdout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14415.6 COPY backslash handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

    16.1 Example of \pset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14916.2 psql variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15016.3 Pgaccess opening window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15316.4 Pgaccess table window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154

    17.1 Sample application being run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15517.2 Statename table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15617.3 Libpq data flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15617.4 Libpq sample program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15717.5 libpgeasy sample program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15917.6 Ecpg sample program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16017.7 Libpq++ sample program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16117.8 JAVA sample program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16417.9 PERL sample program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16517.10TCL sample program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16517.11PYTHON sample program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16617.12PHP sample program — Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16617.13PHP sample program – Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

    18.1 SQL ftoc function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17018.2 SQL tax function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171

  • 132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386

    LIST OF FIGURES xxi

    18.3 Recreation of the part table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17118.4 SQL shipping function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17218.5 SQL function getstatename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17318.6 Getting state name using join and function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17318.7 PL/PGSQL version of getstatename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17418.8 PL/PGSQL spread function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17518.9 PL/PGSQL getstatecode function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17618.10Calls to getstatecode function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17718.11PL/PGSQL change_statename function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17818.12Example of change_statename() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17918.13Trigger creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181

    19.1 C ctof function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18419.2 Create function ctof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18419.3 Calling function ctof . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185

    20.1 Examples of user administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18820.2 Examples of database creation and removal . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18920.3 Making a new copy of database test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19120.4 Postmaster and postgres processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192

  • 138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452

    xxii LIST OF FIGURES

  • 145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518

    List of Tables

    3.1 Table friend . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

    4.1 Common data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194.2 Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274.3 LIKE comparison . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294.4 Regular expression operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294.5 Regular expression special characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304.6 Regular expression examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304.7 SET options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384.8 DATESTYLE output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

    5.1 Aggregates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41

    7.1 Sequence number access functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

    9.1 POSTGRESQL data types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 909.2 Geometric types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 929.3 Common functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949.4 Common operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 959.5 Common variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96

    10.1 Visibility of single-query transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10110.2 Visibility using multi-query transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10210.3 Waiting for a lock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10410.4 Deadlock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

    13.1 Temporary table isolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

    15.1 Backslashes understood by COPY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145

    16.1 psql query buffer commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14716.2 psql general commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14816.3 psql \pset options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14816.4 psql output format shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14916.5 psql predefined variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15116.6 psql listing commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15216.7 psql large object commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15216.8 psql command-line arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153

    17.1 Interface summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

    xxiii

  • 151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584

    xxiv LIST OF TABLES

    20.1 Commonly used system tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

  • 158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650

    Chapter 1

    History of POSTGRESQL

    1.1 Introduction

    POSTGRESQL is the most advanced open source database server. In this chapter, you will learn aboutdatabases, open source software, and the history of POSTGRESQL.

    There are three basic office productivity applications: word processors, spreadsheets, and databases. Wordprocessors produce text documents critical to any business. Spreadsheets are used for financial calculationsand analysis. Databases are used primarily for data storage and retrieval. You can use a word processor ora spreadsheet to store small amounts of data. However, with large volumes of data or data that must beretrieved and updated frequently, databases are the best choice. Databases allow orderly data storage, rapiddata retrieval, and complex data analysis, as you will see in the coming chapters.

    1.2 UNIVERSITY OF CALIFORNIA AT BERKELEY

    POSTGRESQL’S ancestor was INGRES, developed at the UNIVERSITY OF CALIFORNIA AT BERKELEY (1977–1985).The INGRES code was taken and enhanced by RELATIONAL TECHNOLOGIES/INGRES CORPORATION1, whichproduced one of the first commercially successful relational database servers. Also at Berkeley, MICHAELSTONEBRAKER led a team to develop an object-relational database server called POSTGRES (1986–1994). ThePOSTGRES code was taken by ILLUSTRA2 and developed into a commercial product. Two Berkeley graduatestudents, JOLLY CHEN and ANDREW YU, added SQL capabilities to POSTGRES, and called it POSTGRES95(1994–1995). They left Berkeley, but Chen continued maintaining POSTGRES95, which had an active mailinglist.

    1.3 Development Leaves BERKELEY

    In the summer of 1996, it became clear that the demand for an open source SQL database server was great,and a team was formed to continue development. MARC G. FOURNIER, Toronto, Canada, offered to host themailing list, and provide a server to host the source tree. One thousand mailing list subscribers were movedto the new list. A server was configured, giving a few people login accounts to apply patches to the sourcecode using cvs.3.

    1Ingres Corp. was later purchased by Computer Associates.2Illustra was later purchased by Informix and integrated into Informix’s Universal Server.3cvs sychronizes access by developers to shared program files.

    1

  • 165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716

    2 CHAPTER 1. HISTORY OF POSTGRESQL

    JOLLY CHEN had stated, "This project needs a few people with lots of time, not many people with a littletime." With 250,000 lines of C4 code, we understood what he meant. In the early days, there were four peopleheavily involved, MARC FOURNIER in Canada, THOMAS LOCKHART in Pasadena, California, VADIM MIKHEEVin Krasnoyarsk, Russia, and me in Philadelphia, Pennsylvania. We all had full-time jobs, so we did this in ourspare time. It certainly was a challenge.

    Our first goal was to scour the old mailing list, evaluating patches that had been posted to fix variousproblems. The system was quite fragile then, and not easily understood. During the first six months ofdevelopment, there was fear that a single patch would break the system, and we would be unable to correctthe problem. Many bug reports had us scratching our heads, trying to figure out not only what was wrong,but how the system even performed many functions.

    We inherited a huge installed base. A typical bug report was, "When I do this, it crashes the database."We had a whole list of them. It became clear that some organization was needed. Most bug reports requiredsignificant research to fix, and many were duplicates, so our TODO list reported every buggy SQL query. Ithelped us identify our bugs, and made users aware of them too, cutting down on duplicate bug reports.

    We had many eager developers, but the learning curve in understanding how the back-end worked wassignificant. Many developers got involved in the edges of the source code, like language interfaces or databasetools, where things were easier to understand. Other developers focused on specific problem queries, tryingto locate the source of the bug. It was amazing to see that many bugs were fixed with just one line of Ccode. POSTGRES had evolved in an academic environment, and had not been exposed to the full spectrum ofreal-world queries. During that period, there was talk of adding features, but the instability of the systemmade bug fixing our major focus.

    1.4 POSTGRESQL Global Development Team

    In late 1996, we changed the name from POSTGRES95 to POSTGRESQL. It is a mouthful, but honors theBerkeley name and SQL capabilities. We started distributing the source code using remote cvs, whichallowed people to keep up-to-date copies of the development tree without downloading an entire set of filesevery day.

    Releases occurred every 3–5 months. This consisted of 2–3 months of development, one month of betatesting, a major release, and a few weeks to issue sub-releases to correct serious bugs. We were nevertempted to follow a more aggressive schedule with more releases. A database server is not like a wordprocessor or a game, where you can easily restart it if there is a problem. Databases are multi-user, and lockuser data inside the database, so we must make our software as reliable as possible.

    Development of source code of this scale and complexity is not for the novice. We initially had trouble get-ting developers interested in a project with such a steep learning curve. However, our civilized atmosphere,and our improved reliability and performance, finally helped attract the experienced talent we needed.

    Getting our developers the knowledge they needed to assist with POSTGRESQL was clearly a priority.We had a TODO list that outlined what needed to be done, but with 250,000 lines of code, taking on any TODOitem was a major project. We realized developer education would pay major benefits in helping people getstarted. We wrote a detailed flowchart of the back-end modules.5 We wrote a developers’ FAQ6, to describesome of the common questions of POSTGRESQL developers. With this, developers became more productiveat fixing bugs and adding features.

    The source code we inherited from Berkeley was very modular. However, most Berkeley coders usedPOSTGRESQL as a test bed for research projects. Improving existing code was not a priority. Their coding

    4C is a popular computer language first developed in the 1970’s.5All the files mentioned in this chapter are available as part of the POSTGRESQL distribution, or at http://www.postgresql.org/docs.6Frequently Asked Questions

    http://www.postgresql.org/docs

  • 171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782

    1.5. OPEN SOURCE SOFTWARE 3

    styles were also quite varied.We wrote a tool to reformat the entire source tree in a consistent manner. We wrote a script to find

    functions that could be marked as static7, or unused functions that could be removed completely. These arerun just before each release. A release checklist reminds us of the items to be changed for each release.

    As we gained knowledge of the code, we were able to perform more complicated fixes and featureadditions. We redesigned poorly structured code. We moved into a mode where each release had major newfeatures, instead of just bug fixes. We improved SQL conformance, added sub-selects, improved locking, andadded missing SQL functionality. A company formed to offer telephone support.

    The Usenet discussion group archives started touting us. In the previous year, we searched for POST-GRESQL, and found many people were recommending other databases, even though we were addressing userconcerns as rapidly as possible. One year later, many people were recommending us to users who neededtransaction support, complex queries, commercial-grade SQL support, complex data types, and reliability.This clearly portrayed our strengths. Other databases were recommended when speed was the overridingconcern. REDHAT’S shipment of POSTGRESQL as part of their LINUX8 distribution quickly expanded our userbase.

    Every release is now a major improvement over the last. Our global development team now has mastery ofthe source code we inherited from Berkeley. Finally, every module is understood by at least one developmentteam member. We are now easily adding major features, thanks to the increasing size and experience of ourworld-wide development team.

    1.5 Open Source Software

    POSTGRESQL is open source software. The term open source software often confuses people. With commercialsoftware, a company hires programmers, develops a product, and sells it to users. With Internet communi-cation, there are new possibilities. In open source software, there is no company. Capable programmers withinterest and some free time get together via the Internet and exchange ideas. Someone writes a programand puts it in a place everyone can access. Other programmers join and make changes. When the programis sufficiently functional, they advertise the program’s availability to other Internet users. Users find bugsor missing features and report them back to the developers, who enhance the program.

    It sounds like an unworkable cycle, but in fact it has several advantages:

    • A company structure is not required, so there is no overhead and no economic restrictions.

    • Program development is not limited to a hired programming staff, but taps the capabilities and experi-ence of a large pool of Internet programmers.

    • User feedback is facilitated, allowing program testing by a large number of users in a short period oftime.

    • Program enhancements can be rapidly distributed to users.

    1.6 Summary

    This chapter has explored the long history of POSTGRESQL, starting with its roots in university research.POSTGRESQL would not have grown to the success it is today without the Internet. The ability to commu-nicate with people around the world has allowed a community of unpaid developers to enhance and support

    7A static function is a function that is used by only one program file.8Linux is a popular UNIX-like, open source operating system.

  • 178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848

    4 CHAPTER 1. HISTORY OF POSTGRESQL

    software that rivals commercial database offerings. By allowing everyone to see the source code and con-tribute, POSTGRESQL continues to improve every day. The remainder of this book shows how to use thisamazing piece of software.

  • 184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914

    Chapter 2

    Issuing Database Commands

    At this point, the book assumes you have:

    • POSTGRESQL installed

    • POSTGRESQL server running

    • You are a configured POSTGRESQL user

    • You have created a database called test.

    If not, please see appendix B.In this chapter, you will learn how to connect to the database server, and issue simple commands to the

    POSTGRESQL server.

    2.1 Starting a Database Session

    POSTGRESQL uses a client/server model of communication. That means that a POSTGRESQL server continuallyruns, waiting for client requests. The server processes the request and returns the result to the client.

    Choosing an Interface

    Because the POSTGRESQL server runs as an independent process on the computer, there is no way for a userto interact with it directly. Instead, there are client applications designed specifically for user interaction.This chapter shows you how to interact with POSTGRESQL using the psql interface. Additional interfacesare covered in Chapter 17.

    Choosing a Database

    Each POSTGRESQL server controls access to a number of databases. Databases are storage areas used bythe server to partition information. For example, a typical installation may have a production database, usedto keep all information about a company. They may also have a training database, used for training andtesting purposes. They may have private databases, used by individuals to store personal information. Forthis exercise, we will assume you have created an empty database called test. If this is not the case, seesection B.

    5

  • 191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980

    6 CHAPTER 2. ISSUING DATABASE COMMANDS

    Starting a Session

    To start a psql session and connect to the test database, type psql test at the command prompt. Your outputshould look similar to figure 2.1. Remember, the operating system command prompt is case-sensitive, soyou must type this in all lowercase.1

    $ psql testWelcome to psql, the PostgreSQL interactive terminal.

    Type: \copyright for distribution terms\h for help with SQL commands\? for help on internal slash commands\g or terminate with semicolon to execute query\q to quit

    test=>

    Figure 2.1: psql session startup

    2.2 Controlling a Session

    Congratulations. You have successfully connected to the POSTGRESQL server. You can now issue commands,and receive replies from the server. Let’s try one. Type SELECT CURRENT_USER; and press Enter (see figure 2.2).If you make a mistake, just press backspace and retype. This should show your login name underneath the

    test=> SELECT CURRENT_USER;getpgusername---------------postgres(1 row)

    test=>

    Figure 2.2: My first SQL query

    dashed line. In the example, the login name postgres is shown. The word getpgusername is a column label.The server is also reporting that it has returned one row of data. The line test=> tells you that the server isdone and is waiting for your next database query.

    Let’s try another one. At the test=> prompt, type SELECT CURRENT_TIMESTAMP; and press Enter. It shouldshow the current date and time. Each time you execute the query, the server will report the current time toyou.

    Typing in the Query Buffer

    Typing in the query buffer is similar to typing at an operating system command prompt. However, at anoperating system command prompt, Enter completes each command. In psql, commands are completed only

    1A few operating systems are case-insensitive.

  • 198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046

    2.3. GETTING HELP 7

    when you enter a semicolon (;) or backslash-g (\g). Here’s a good example. Let’s do SELECT 1 + 3; but ina different way. See figure 2.3.2 Notice the query is spread over three lines. Notice the prompt changed

    test=> SELECTtest-> 1 + 3test-> ;?column?----------

    4(1 row)

    test=>

    Figure 2.3: Multi-line query

    from => on the first line to -> on the second line to indicate the query was being continued. The semicolontold psql to send the query to the server. We could easily have replaced the semicolon with backslash-g. Ido not recommend you type queries as ugly as this one, but longer queries will benefit from the ability tospread them over multiple lines. You might notice the query is in uppercase. Unless you are typing a stringin quotes, the POSTGRESQL server does not care whether words are uppercase or lowercase. For stylisticreasons, I recommend you enter words special to POSTGRESQL in uppercase.

    Try some queries on your own involving arithmetic. Each computation must start with the word SELECT,then your computation, and finally a semicolon or backslash-g to finish. For example, SELECT 4 * 10; wouldreturn 40. Addition is performed using plus (+), subtraction using minus (-), multiplication using asterisk(*), and division using forward slash (/).

    If you have readline3 installed, psql will even allow you to use your arrow keys. Your left and right arrowkeys allow you to move around, and the up and down arrows retrieve previously typed queries.

    Displaying the Query Buffer

    You can continue typing indefinitely, until you use a semicolon or backslash-g. Everything you type willbe buffered by psql until you are ready to send the query. If you use backslash-p (\p), you see everythingaccumulated in the query buffer. In figure 2.4, three lines of text are accumulated and displayed by the userusing backslash-p. After display, we use backslash-g to execute the query which returns the value 21. Thiscomes in handy with long queries.

    Erasing the Query Buffer

    If you do not like what you have typed, use backslash-r (\r) to reset or erase the buffer.

    2.3 Getting Help

    You might ask, “Are these backslash commands documented anywhere?” If you look at figure 2.1, you willsee the answer is printed every time psql starts. Backslash-? (\?) prints all valid backslash commands.Backslash-h displays help for SQL commands. SQL commands are covered in the next chapter.

    2Don’t be concerned about ?column?. We will cover that in section 4.7.3Readline is an open-source library that allows powerful command-line editing.

  • 204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112

    8 CHAPTER 2. ISSUING DATABASE COMMANDS

    test=> SELECTtest-> 2 * 10 + 1test-> \pSELECT2 * 10 + 1test-> \g?column?----------

    21(1 row)

    test=>

    Figure 2.4: Backslash-p demo

    2.4 Exiting a Session

    This chapter would not be complete without showing you how to exit psql. Use backslash-q (\q) to quit thesession. Backslash-q exits psql. Backslash g (go), p (print), r (reset), and q (quit) should be all you need for awhile.

    2.5 Summary

    This chapter has shown how to use the most important features of psql. This knowledge will allow you totry all the examples in this book. However, psql has many features that can assist you. Section 16.1 coverspsql in detail. You may want to review that chapter while reading through the book.

  • 211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178

    Chapter 3

    Basic SQL Commands

    SQL stands for Structured Query Language. It is the most common way of communicating with databaseservers, and is supported by almost all database systems. In this chapter, you will learn about relationaldatabase systems and how to issue the most important SQL commands.

    3.1 Relational Databases

    As I mentioned in section 1.1, the purpose of a database is rapid data storage and retrieval. Today, mostdatabase systems are relational databases. While the term relational database has a mathematical foundation,in practice it means that all data stored in the database is arranged in a uniform structure.

    In figure 3.1, you see the database server with access to three databases, test, demo, and finance. You

    Database Server

    ��������������������������������������������������������������������������������������������������������

    ��������������������������������������������������������������������������������������������������������

    ���������������������������������������������������������������������������������������������������������������������������������������

    ���������������������������������������������������������������������������������������������������������������������

    ��������������������������������������������������������������������������������������������������������

    ����������������������������������������������������������������������������������������

    Database Test�����������������������������������

    �����������������������������������

    Database Demo

    ������������

    ��

    ��

    ��

    ��

    �����������������������������������

    �����������������������������������

    Database Finance

    Figure 3.1: Databases

    could issue the command psql finance and be connected to the finance database. You have already dealt withthis in chapter 2. Using psql, you chose to connect to database test with the command psql test. To see alist of databases available at your site, type psql -l. The first column lists the database names. However,you may not have permission to connect to them.

    You might ask, “What are those black rectangles in the databases?” Those are tables. Tables are thefoundation of a relational database management system (RDBMS). As I mentioned earlier, databases store data.

    9

  • 217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244

    10 CHAPTER 3. BASIC SQL COMMANDS

    Those tables are where data is stored in a database. Each table has a name defined by the person who createdit.

    Let’s look at a single table called friend in table 3.1. You can easily see how tables are used to store data.

    FirstName LastName City State AgeMike Nichols Tampa FL 19Cindy Anderson Denver CO 23Sam Jackson Allentown PA 22

    Table 3.1: Table friend

    Each friend is listed as a separate row in the table. The table records five pieces of information about eachfriend, firstname, lastname, city, state, and age.1

    Each friend is on a separate row. Each column contains the same type of information. This is the type ofstructure that makes relational databases successful. Relational databases allow you to select certain rowsof data, certain columns of data, or certain cells. You could select the entire row for Mike, the entire columnfor City, or a specific cell like Denver. There are synonyms for the terms table, row, and column. Table is moreformally referred to as a relation or class, row as record or tuple, and column as field or attribute.

    3.2 Creating Tables

    Let’s create our own table and call it friend. The psql statement to create the table is shown in figure 3.2.You do not have to type it exactly like that. You could have used all lowercase, or you could have written it

    test=> CREATE TABLE friend (test(> firstname CHAR(15),test(> lastname CHAR(20),test(> city CHAR(15),test(> state CHAR(2),test(> age INTEGERtest(> );CREATE

    Figure 3.2: Create table friend

    in one long line, and it would have worked just the same.Let’s look at it from the top down. The words CREATE TABLE have special meaning to the database

    server. They indicate that the next request from the user is to create a table. You will find most SQLrequests can be quickly identified by the first few words. The rest of the request has a specific format thatis understood by the database server. While capitalization and spacing are optional, the format for a querymust be followed exactly. Otherwise, the database server will issue an error such as parser: parse errorat or near "pencil", meaning the database server got confused near the word pencil. In such a case, themanual page for the command should be consulted and the query reissued in the proper format. A copy ofthe POSTGRESQL manual pages appear in appendix D.

    The CREATE TABLE command follows a specific format. First, the two words CREATE TABLE, then the tablename, then an open parenthesis, then a list of column names and their types, followed by a close parenthesis.

    1In a real-world database, the person’s birth date would be stored and not the person’s age. Age has to be updated every timethe person has a birthday. A person’s age can be computed when needed from a birth date field.

  • 224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310

    3.3. ADDING DATA WITH INSERT 11

    The important part of this query is between the parentheses. You will notice there are five lines there. Thefirst line, firstname CHAR(15), represents the first column of the table to create. The word firstname is thename of the first column, and the text CHAR(15) indicates the column type and length. The CHAR(15) meansthe first column of every row holds up to 15 characters. The second column is called lastname and holdsup to 20 characters. Columns of type char hold characters of a specified length. User-supplied characterstrings2 that do not fill the entire length of the field are right-padded with blanks. Columns city and state aresimilar. The final column, age, is different. It is not a CHAR() column. It is an INTEGER column. It holds wholenumbers, not characters. Even if there were 5,000 friends in the table, you can be certain that there are nonames appearing in the age column, only whole numbers. It is this structure that helps databases to be fastand reliable.

    POSTGRESQL supports more column types than just char() and integer. However, in this chapter we willuse only these two. Sections 4.1 and 9.2 cover column types in more detail.

    Create some tables yourself now. Only use letters for your table and column names. Do not use anynumbers, punctuation, or spaces at this time.

    The \d command allows you to see information about a specific table, or a list of all table names in thecurrent database. To see information about a specific table, type \d followed by the name of the table. Forexample, to see the column names and types of your new friend table in psql, type \d friend. Figure 3.3shows this. If you use \d with no table name after it, you will see a list of all table names in the database.

    test=> \d friendTable "friend"

    Attribute | Type | Modifier-----------+----------+----------firstname | char(15) |lastname | char(20) |city | char(15) |state | char(2) |age | integer |

    Figure 3.3: Example of backslash-d

    3.3 Adding Data with INSERT

    Let’s continue toward the goal of making a table exactly like the friend table in table 3.1. We have the tablecreated, but there is no data/friends in it. You add data into a table with the INSERT command. Just as CREATETABLE has a specific format that must be followed, INSERT has a specific format too. You can see the formatin figure 3.4. First, you must use single quotes around the character strings. Double quotes will not work.Spacing and capitalization are optional, except inside the single quotes. Inside them, the text is