Free Sample
+ Collection

PostgreSQL Server Programming

Hannu Krosing, Jim Mlodgenski, Kirk Roybal

Take your skills with PostgreSQL to a whole new level with this fascinating guide to server programming. A step by step approach with illuminating examples will educate you in the full range of possibilities.
RRP $29.99
RRP $49.99
Print + eBook

Want this title & more?

$12.99 p/month

Subscribe to PacktLib

Enjoy full and instant access to over 2000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.

Book Details

ISBN 139781849516983
Paperback264 pages

About This Book

  • Understand the extension framework of PostgreSQL, and leverage it in ways that you haven't even invented yet
  • Write functions, create your own data types, all in your favourite programming language
  • Step-by-step tutorial with plenty of tips and tricks to kick-start server programming

Who This Book Is For

"PostgreSQL Server Programming" is for moderate to advanced PostgreSQL database professionals. To get the best understanding of this book, you should have general experience in writing SQL, a basic idea of query tuning, and some coding experience in a language of your choice.

Table of Contents

Chapter 1: What Is a PostgreSQL Server?
Why program in the server?
About this book's code examples
Moving beyond simple functions
Managing related data with triggers
Auditing changes
Data cleaning
Custom sort orders
Programming best practices
On caching
Wrap up – why program in the server?
Chapter 2: Server Programming Environment
Cost of acquisition
Availability of developers
Procedural languages
Chapter 3: Your First PL/pgSQL Function
Why PL/pgSQL?
Structure of a PL/pgSQL function
Conditional expressions
Returning a record
Acting on function results
Chapter 4: Returning Structured Data
Sets and arrays
Returning sets
Using a set-returning function
Functions based on views
OUT parameters and records
Summary of RETURN SETOF variants
Returning cursors
Other ways to work with structured data
Chapter 5: PL/pgSQL Trigger Functions
Creating the trigger function
Simple "Hey, I'm called" trigger
The audit trigger
Disallowing DELETE
Disallowing TRUNCATE
Modifying the NEW record
Immutable fields trigger
Controlling when a trigger is called
Variables passed to the PL/pgSQL TRIGGER function
Chapter 6: Debugging PL/pgSQL
''Manual'' debugging with RAISE NOTICE
Visual debugging
Chapter 7: Using Unrestricted Languages
Are untrusted languages inferior to trusted ones?
Will untrusted languages corrupt the database?
Why untrusted?
Quick introduction to PL/Python
Thinking out of the "SQL database server" box
Chapter 8: Writing Advanced Functions in C
Simplest C function – return (a + b)
Adding functionality to add(int, int)
Basic guidelines for writing C code
Error reporting from C functions
Running queries and calling PostgreSQL functions
Handling records as arguments or returned values
Fast capturing of database changes
Doing something at commit/rollback
Synchronizing between backends
Additional resources for C
Chapter 9: Scaling Your Database with PL/Proxy
Simple single-server chat
Dealing with success – splitting tables over multiple databases
Chapter 10: Publishing Your Code as PostgreSQL Extensions
When to create an extension
Unpackaged extensions
Extension versions
The .control file
Building an extension
Installing an extension
Publishing your extension
Installing an extension from PGXN

What You Will Learn

  • Write functions in the built-in PL/PgSQL language or your language of choice
  • Extract data from foreign data sources
  • Add operators, data types, and other custom elements
  • Debug and code efficiently
  • Decide what machine resources your process will use
  • Create your own data types, operators, functions, aggregates, and even your own language
  • Fully integrate the database layer into your development

In Detail

Learn how to work with PostgreSQL as if you spent the last decade working on it. PostgreSQL is capable of providing you with all of the options that you have in your favourite development language and then extending that right on to the database server. With this knowledge in hand, you will be able to respond to the current demand for advanced PostgreSQL skills in a lucrative and booming market.

"PostgreSQL Server Programming" will show you that PostgreSQL is so much more than a database server. In fact, it could even be seen as an application development framework, with the added bonuses of transaction support, massive data storage, journaling, recovery and a host of other features that the PostgreSQL engine provides.

This book will take you from learning the basic parts of a PostgreSQL function, then writing them in languages other than the built-in PL/PgSQL. You will see how to create libraries of useful code, group them into even more useful components, and distribute them to the community. You will see how to extract data from a multitude of foreign data sources, and then extend PostgreSQL to do it natively. And you can do all of this in a nifty debugging interface that will allow you to do it efficiently and with reliability.


Read More