PostgreSQL Server
Programming
Second Edition
Extend PostgreSQL using PostgreSQL server
programming to create, test, debug, and optimize
a range of user-defined functions in your favorite
programming language
Usama Dar
Hannu Krosing
Jim Mlodgenski
Kirk Roybal
BIRMINGHAM - MUMBAI
PostgreSQL Server Programming
Second Edition
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the authors, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
First published: June 2013
Second edition: February 2015
Production reference: 1180215
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-058-1
www.packtpub.com
Credits
Authors
Usama Dar
Hannu Krosing
Jim Mlodgenski
Kirk Roybal
Reviewers
Stephen Frost
Rick van Hattem
Vibhor Kumar
Jeff Lawson
Mariano Reingart
Julien Tachoires
Commissioning Editor
Usha Iyer
Acquisition Editors
Antony Lowe
Meeta Rajani
Sam Wood
Content Development Editor
Adrian Raposo
Technical Editors
Mrunmayee Patil
Chinmay Puranik
Copy Editors
Dipti Kapadia
Aarti Saldanha
Project Coordinator
Kinjal Bari
Proofreaders
Maria Gould
Linda Morris
Indexer
Monica Ajmera Mehta
Production Coordinator
Nitesh Thakur
Cover Work
Nitesh Thakur
Vibhor Kumar is a principal system architect at EnterpriseDB who specializes in
assisting Fortune 100 companies to deploy, manage, and optimize Postgres databases.
He joined EnterpriseDB in 2008 to work with Postgres after several years of working
with Oracle systems. He has worked in team leadership roles at IBM Global Services
and BMC Software as well as an Oracle database administrator at CMC Ltd. for
several years. He has developed expertise in Oracle, DB2, and MongoDB and holds
certifications in them. He has experience working with MS SQL Server, MySQL,
and data warehousing. He holds a bachelor's degree in computer science from the
University of Lucknow and a master's degree in computer science from the Army
Institute of Management, Kolkata. He is a certified PostgreSQL trainer and holds a
professional certification in Postgres Plus Advanced Server from EnterpriseDB.
Jeff Lawson has been a fan and user of PostgreSQL since the time he discovered
it in 2001. Over the years, he has also developed and deployed applications for
IBM DB2, Oracle, MySQL, Microsoft SQL Server, Sybase, and others, but he always
prefers PostgreSQL for its balance of features and openness. Much of his experience
involves developing for Internet-facing websites/projects that require highly scalable
databases with high availability or with provisions for disaster recovery.
He currently works as the director of software development for FlightAware, which
is an airplane-tracking website that uses PostgreSQL and other open source software
to store and analyze the positions of the thousands of flights that are operated
worldwide every day. He has extensive experience in software architecture, data
security, and network protocol design from the software engineering positions he
has held at Univa / United Devices, Microsoft, NASA's Jet Propulsion Laboratory,
and WolfeTech. He is a founder of distributed.net, which pioneered distributed
computing in the 1990s, and he continues to serve as the chief of operations and
as a member of the board there. He earned a BSc degree in computer science from
Harvey Mudd College.
He is fond of cattle, holds an FAA private pilot certificate, and owns an airplane
based in Houston, Texas.
Mariano Reingart lives in Buenos Aires, Argentina, and is a specialist in
the software development of applications and libraries (web services, PDF,
GUI, replication, and so on) with more than 10 years of experience. Currently,
he is the PostgreSQL regional contact for Argentina and a Python Software
Foundation member.
He is a major contributor to the web2py Python web framework, and now he's
working on the wxWidgets multiplatform GUI toolkit (specifically in the Qt port
and Android mobile areas). Also, he has contributed to more than a dozen open
source projects, including an interface for Free Electronic Invoice web services
(PyAfipWs) and Pythonic replication for PostgreSQL (PyReplica).
He has a bachelor's degree in computer systems analysis, and currently, he's a
master's candidate for the MSc in free software degree at the Open University
of Catalonia.
He works on his own funded entrepreneurial venture formed by an open group
of independent professionals, dedicated to software development, training, and
technical support, focusing on open source tools (GNU/Linux, Python, PostgreSQL,
and web2py/wxPython).
He has worked for local Python-based companies in large business applications
(ERP, SCM, and CRM) and mission critical systems (election counting, electronic
voting, 911 emergency events support). He has contributed to books such as
web2py Enterprise Web Framework, Third Edition, and web2py Application Development
Cookbook, Packt Publishing, and several Spanish translations of the PostgreSQL
official documentation.
His full resume is available at http://reingart.blogspot.com/p/resume.html.
Julien Tachoires is a PostgreSQL specialist, who works as consultant for the
French PostgreSQL company Dalibo. He is the main developer of pg_activity,
a top-end software dedicated to follow the PostgreSQL incoming traffic in real
time, which is written in Python.
I want to thank my employer Dalibo; my wife, Camille; and my
son, Arthur.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit
www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF
and ePub files available? You can upgrade to the eBook version at www.PacktPub.com
and as a print book customer, you are entitled to a discount on the eBook copy. Get in
touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles,
sign up for a range of free newsletters and receive exclusive discounts and offers
on Packt books and eBooks.
TM
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital
book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
• Fully searchable across every book published by Packt
• Copy and paste, print, and bookmark content
• On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access
PacktLib today and view 9 entirely free books. Simply use your login credentials for
immediate access.

0581OS_FM_Final_NT

  • 1.
    PostgreSQL Server Programming Second Edition ExtendPostgreSQL using PostgreSQL server programming to create, test, debug, and optimize a range of user-defined functions in your favorite programming language Usama Dar Hannu Krosing Jim Mlodgenski Kirk Roybal BIRMINGHAM - MUMBAI
  • 2.
    PostgreSQL Server Programming SecondEdition Copyright © 2015 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: June 2013 Second edition: February 2015 Production reference: 1180215 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78398-058-1 www.packtpub.com
  • 3.
    Credits Authors Usama Dar Hannu Krosing JimMlodgenski Kirk Roybal Reviewers Stephen Frost Rick van Hattem Vibhor Kumar Jeff Lawson Mariano Reingart Julien Tachoires Commissioning Editor Usha Iyer Acquisition Editors Antony Lowe Meeta Rajani Sam Wood Content Development Editor Adrian Raposo Technical Editors Mrunmayee Patil Chinmay Puranik Copy Editors Dipti Kapadia Aarti Saldanha Project Coordinator Kinjal Bari Proofreaders Maria Gould Linda Morris Indexer Monica Ajmera Mehta Production Coordinator Nitesh Thakur Cover Work Nitesh Thakur
  • 4.
    Vibhor Kumar isa principal system architect at EnterpriseDB who specializes in assisting Fortune 100 companies to deploy, manage, and optimize Postgres databases. He joined EnterpriseDB in 2008 to work with Postgres after several years of working with Oracle systems. He has worked in team leadership roles at IBM Global Services and BMC Software as well as an Oracle database administrator at CMC Ltd. for several years. He has developed expertise in Oracle, DB2, and MongoDB and holds certifications in them. He has experience working with MS SQL Server, MySQL, and data warehousing. He holds a bachelor's degree in computer science from the University of Lucknow and a master's degree in computer science from the Army Institute of Management, Kolkata. He is a certified PostgreSQL trainer and holds a professional certification in Postgres Plus Advanced Server from EnterpriseDB. Jeff Lawson has been a fan and user of PostgreSQL since the time he discovered it in 2001. Over the years, he has also developed and deployed applications for IBM DB2, Oracle, MySQL, Microsoft SQL Server, Sybase, and others, but he always prefers PostgreSQL for its balance of features and openness. Much of his experience involves developing for Internet-facing websites/projects that require highly scalable databases with high availability or with provisions for disaster recovery. He currently works as the director of software development for FlightAware, which is an airplane-tracking website that uses PostgreSQL and other open source software to store and analyze the positions of the thousands of flights that are operated worldwide every day. He has extensive experience in software architecture, data security, and network protocol design from the software engineering positions he has held at Univa / United Devices, Microsoft, NASA's Jet Propulsion Laboratory, and WolfeTech. He is a founder of distributed.net, which pioneered distributed computing in the 1990s, and he continues to serve as the chief of operations and as a member of the board there. He earned a BSc degree in computer science from Harvey Mudd College. He is fond of cattle, holds an FAA private pilot certificate, and owns an airplane based in Houston, Texas.
  • 5.
    Mariano Reingart livesin Buenos Aires, Argentina, and is a specialist in the software development of applications and libraries (web services, PDF, GUI, replication, and so on) with more than 10 years of experience. Currently, he is the PostgreSQL regional contact for Argentina and a Python Software Foundation member. He is a major contributor to the web2py Python web framework, and now he's working on the wxWidgets multiplatform GUI toolkit (specifically in the Qt port and Android mobile areas). Also, he has contributed to more than a dozen open source projects, including an interface for Free Electronic Invoice web services (PyAfipWs) and Pythonic replication for PostgreSQL (PyReplica). He has a bachelor's degree in computer systems analysis, and currently, he's a master's candidate for the MSc in free software degree at the Open University of Catalonia. He works on his own funded entrepreneurial venture formed by an open group of independent professionals, dedicated to software development, training, and technical support, focusing on open source tools (GNU/Linux, Python, PostgreSQL, and web2py/wxPython). He has worked for local Python-based companies in large business applications (ERP, SCM, and CRM) and mission critical systems (election counting, electronic voting, 911 emergency events support). He has contributed to books such as web2py Enterprise Web Framework, Third Edition, and web2py Application Development Cookbook, Packt Publishing, and several Spanish translations of the PostgreSQL official documentation. His full resume is available at http://reingart.blogspot.com/p/resume.html. Julien Tachoires is a PostgreSQL specialist, who works as consultant for the French PostgreSQL company Dalibo. He is the main developer of pg_activity, a top-end software dedicated to follow the PostgreSQL incoming traffic in real time, which is written in Python. I want to thank my employer Dalibo; my wife, Camille; and my son, Arthur.
  • 6.
    www.PacktPub.com Support files, eBooks,discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view 9 entirely free books. Simply use your login credentials for immediate access.