Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
PostgreSQL Server Programming
PostgreSQL Server Programming

PostgreSQL Server Programming: 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.

eBook
€22.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

PostgreSQL Server Programming

Chapter 2. Server Programming Environment

You have had a chance to get acquainted with the general idea of using PostgreSQL, but now we are going to answer the question of why anyone would choose PostgreSQL as a development platform. As much as I like to believe that it's an easy decision for everyone, it's not.

For starters, let's get rid of the optimistic idea that anyone chooses a database platform for technical reasons. Sure, we would all like to think that we are objective, and we base our decisions on a preponderance of the technical evidence. This preponderance of evidence then indicates which features are available and relevant to our application. We would then proceed to make a weighted choice in favor of the most advantageous platform, and use the balance of the evidence to create workarounds and alternatives where our choice falls short. The fact is, we don't really understand all of the requirements of the application until we are halfway through the development cycle. Here are...

Cost of acquisition


One of biggest factors in deciding what technology is used in the application stack is the cost of acquisition. I've seen many application architectures drawn on a whiteboard where the technical team was embarrassed to show me, but they justified the design by trying to keep software licensing costs down. When it comes to the database environment, the usual suspects are Oracle, SQL Server, MySQL, and PostgreSQL. Oracle, the dominant player in the database space, is also the most costly. At the low end, Oracle does have reasonably priced offering and even a free express edition, but they are limited. Most people have needs beyond the low priced offerings and fall into the enterprise sales machine of Oracle. This usually results in a high price quote that makes your CFO fall out of his chair and you're back to designing your solution to keep your licensing costs down.

Then comes Microsoft SQL Server. This is your first reasonably viable option. The pricing is listed on the...

Availability of developers


This has been one of the most hilariously fun parts of my development life. I recently recommended using PostgreSQL for a reporting system in a local company. The company in question wanted to know if they chose PostgreSQL, would anyone on staff be able to maintain it. So I began to interview the developers to find out about their experience with PostgreSQL.

Me: Do you have any experience with PostgreSQL?

Developer 1: Yes, I used it at the last job for a product fulfillment project, but I don't think very many people have that experience. We should probably stick with MySQL.

Me: Do you have any experience with PostgreSQL?

Developer 2: Yes, I used it at the last job for a reporting project, but I don't think very many people have that experience. We should probably stick with MySQL.

After interviewing all seven developers that were influential on the project, I found that the only person without hands-on experience with PostgreSQL was the project manager. Since the project...

Licensing


About two months after Oracle bought MySQL, they announced a plan that divided the development into two camps. There would now be a MySQL community edition and a professional version. The community edition would no longer gain any new features, and the professional version would become a commercial product.

There was a vast and thunderous sucking sound in the open source community, as they thrashed wildly about to find a new platform for Free and Open Source (FOSS) development.

Oracle immediately (in about 2 weeks) countermanded the order, and declared that things would stay as they were for the indefinite future. Those with short memories, forgiving hearts, or who just weren't paying attention went on about their business. Many other open source projects either switched to PostgreSQL or suddenly grew PostgreSQL database support.

Today we have MySQL and MySQL Enterprise Edition. If you want "backup, high availability, enterprise scalability, and the MySQL Enterprise monitor", you...

Predictability


This section could just as well have been titled "standards compliance," but I decided against it because the benefits of standards compliance in corporate projects are not obvious. The limitations of the common databases are well documented, and I will show you a few websites in a moment where you can make a comparison of who has the most "unintended behavior". I encourage you to read the material while thinking to yourself about the question, "Which method of feature development is most likely to make my application break in the future?"

http://www.sql-info.de/postgresql/postgres-gotchas.html

http://www.sql-info.de/mysql/gotchas.html

Spoiler alert: Stricter adherence to standards comes at the cost of not allowing ambiguous behavior. Not allowing ambiguous behavior makes the developer's life more difficult. Making the developer's life more difficult ensures that the interpretation of the commands that the developer gives will not change later, breaking the application.

Just how...

Community


Oracle and SQL Server don't have a community. Please understand when I say that, I mean that the chance that you will get to talk to a developer of the core database is about the same as your chance of winning the lottery. By the time you do, it's probably because you found a bug so heinous that it couldn't be ignored, and the only person who can understand your report is the guy that wrote the code in question. They have paid technical support, and that support has proven in my experience to be generally competent, but not stellar. I have had to work around the problem that I originally requested help with about 40 percent of the time.

Contrast that to MySQL and PostgreSQL, where just about anybody can speak to just about anybody else all day long. Many of the core developers of both the platforms can be found on IRC, met at conventions, contacted for contract development work, and for the most part, bribed remarkably easily with beer (hint, hint, wink, wink, nudge, nudge).

They...

Procedural languages


SQL Server allows you to create DLLs in any language that produces CLR. These DLLs must be loaded into the server at boot time. To create a procedure at run time and have it be immediately available, the only choice is the built in SQL dialect, Transact SQL (TSQL).

MySQL has a feature called plugins. One of the legal plugin types is a procedural language. Several languages have been tooled to work with MySQL via the plugin system, including most of the popular ones such as PHP and Python. These functions cannot be used for stored procedures or triggers, but they can be invoked from the common SQL statements. For the rest, you are stuck with the built-in SQL.

PostgreSQL has full support for additional procedural languages, which can be used to create any legal entity in the database that can be created with PL/pgSQL. The language can be added (or removed) from a running version of PostgreSQL and any function defined using that language may also be created or dropped while...

Summary


Now you know a few things about the PostgreSQL environment, as well as some things that will help you in the unforeseeable future. PostgreSQL is built to handle your needs, but more importantly, it is built not to change underneath you in the future.

We touched a little on the environment and called out some of the more important things to keep in mind while programming on the server in PostgreSQL. Don't worry too much if you don't remember all of it. It is fine to go on to the next chapter, where we actually start making some useful functions. Then come back and review this chapter when you have a clearer understanding of the features available to the function writer.

Left arrow icon Right arrow icon

Key benefits

  • 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

Description

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.

Who is this book 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.

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

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 25, 2013
Length: 264 pages
Edition : 1st
Language : English
ISBN-13 : 9781849516983
Category :
Tools :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : Jun 25, 2013
Length: 264 pages
Edition : 1st
Language : English
ISBN-13 : 9781849516983
Category :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 83.98
PostgreSQL Server Programming
€41.99
PostgreSQL 9.0 High Performance
€41.99
Total 83.98 Stars icon

Table of Contents

10 Chapters
What Is a PostgreSQL Server? Chevron down icon Chevron up icon
Server Programming Environment Chevron down icon Chevron up icon
Your First PL/pgSQL Function Chevron down icon Chevron up icon
Returning Structured Data Chevron down icon Chevron up icon
PL/pgSQL Trigger Functions Chevron down icon Chevron up icon
Debugging PL/pgSQL Chevron down icon Chevron up icon
Using Unrestricted Languages Chevron down icon Chevron up icon
Writing Advanced Functions in C Chevron down icon Chevron up icon
Scaling Your Database with PL/Proxy Chevron down icon Chevron up icon
Publishing Your Code as PostgreSQL Extensions Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Full star icon Half star icon 4.7
(9 Ratings)
5 star 88.9%
4 star 0%
3 star 0%
2 star 11.1%
1 star 0%
Filter icon Filter
Top Reviews

Filter reviews by




David Lee Dec 27, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is helpful for anyone that wants to learn PostgreSQL.
Amazon Verified review Amazon
Shaun Thomas Dec 12, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
There comes a time in every DBA's life, that he needs to add functionality to his database software. To most DBAs, and indeed for most databases, this amounts to writing a few stored procedures or triggers. In extremely advanced cases, the database may provide an API for direct C-language calls. PostgreSQL however, has gone above and beyond this for several years, and have continuously made the process easier with each iteration.So once again, I'm glad to review a book by three authors in the industry who either work directly on PostgreSQL internals, or use it extensively enough to contribute vastly important functionality. Hannu Krosing, Jim Mlodgenski, and Kirk Roybal collaborated to produce PostgreSQL Server Programming, a necessary and refreshing addition to the PostgreSQL compendium. I don't know who contributed each individual chapter, but I can make a fairly educated guess that anything PL/Proxy related came from Mr. Krosing, its original designer.As usual for a book of this type, things start off with relative simplicity. The authors make a very important note I try to convey to staff developers regularly: let the database do its job. The database is there to juggle data, handle set theory, and otherwise reduce traffic to and from the application to a minimum. This saves both network bandwidth and processing time on the front end, which can be combined with caching to service vastly larger infrastructures than otherwise possible.Beyond this, are the basics. Using stored procedures, taking advantage of triggers, writing functions that can return sets. The gamut of examples runs from data auditing and logging, to integrity control and a certain extent of business logic. One or more of the authors suggests that functions are the proper interface to the database, to reduce overhead, and provide an abstract API that can change without directly altering the application code. It is, after all, the common denominator in any library or tool dealing with the data. While I personally don't agree with this type of approach, the logical reasoning is sound, and can help simplify and prevent many future headaches.But then? Then comes the real nitty-gritty. PostgreSQL allows interfacing with the database through several languages, including Python, Perl, and even TCL/TK, and the authors want you to know it. Databases like Oracle have long allowed C-level calls to the database, and this has often included Java in later incarnations. PostgreSQL though, is the only RDBMS that acts almost like its own middle layer. It's a junction that allows JSON (a Javascript encapsulation) accessed via Python, to be filtered by a TCL trigger, on a table that matched data through an index produced by a Perl function. The authors provide Python and C examples for much of this scenario, including the JSON!And that's where this book really shines: examples. There's Python, C, PLPGSQL, triggers, procedures, compiled code, variants of several difficult techniques, and more. In the C case, things start with a simple "Hello World" type you might see in a beginning programming class, and the author steps through increasingly complex examples. Eventually, the C code is returning sets of sets of data per call, as if simulating several table rows.In the more concrete, the authors provide copious links to external documentation and Wiki pages for those who want to explore this territory in more depth. Beyond that, they want readers to know about major sources of contributed code and extensions, all to make the database more useful, and perhaps entice the reader join in the fun. Everything from installing, to details necessary for writing extensions is covered, so that is well within the realm of possibility!I already mentioned that at least one of the authors encourages functional database access instead of direct SQL. Well, there's more than the obvious reasons for this: PL/Proxy is a procedural language that uses functions to facilitate database sharding for horizontal scalability. Originally designed for Skype, PL/Proxy has been used by many other projects. While it might not apply to everyone, sharding is a very real technique with non-trivial implementation details that have stymied the efforts of many development teams.I actually would have liked a more extensive chapter or two regarding PL/Proxy. While several examples of functional access are outlined for a chat server, none of these functions are later modified in a way that would obviously leverage PL/Proxy. Further, Krosing doesn't address how sequences should be distributed so data on all the various servers get non-conflicting surrogate keys. It would have been nice to see an end-to-end implementation.All in all, anyone serious about their PostgreSQL database should take a look. Getting a server up and running is only half the story; making the database an integral component of an application instead of a mere junk drawer provides more functionality with fewer resources. It's good to see a book that not only emphasizes this, but conveys the knowledge in order to accomplish such a feat. Hannu, Jim, and Kirk have all done the community a great service. I hope to see revisions of this in the future as PostgreSQL matures.
Amazon Verified review Amazon
Pierre Jul 25, 2013
Full star icon Full star icon Full star icon Full star icon Full star icon 5
At long last, a PostgreSQL book. Why saying that with all the existing PostgreSQL books ? It's just that most of these books cover usual topics, that are not PostgreSQL specific. Replication, basic administration... Here, we are going straigh to some of the best features of PostgreSQL : its high extensibility with server-side programming.The book begins with explanations about why using server-side programming, and then a tutorial to guide you through PL/pgSQL.Warning : that tutorial, as the whole book, assumes you have previous PostgreSQL knowledge.Afterwards, you'll go through the triggers world, learn some stuff about debugging in case you are not a perfect developper, and then you'll travel to more exotic server programming choices. The language used for all examples is PL/python, but it works the same with Perl/Lua... And next language is C, to write extra low level extensions, accessing the whole server internals.And finally, you'll learn how to contribute to the extension network and use your functions to spread accross servers using PL/Proxy.It's really a great book, I was looking forward reading it, and it met my expectations. I needed a book to push me forward in my server side programming, giving general guidance and suggestions. It's not a SQL reference, it would require many many books while still being far from exhaustive. But it's a deep Server Programming introduction. And it is good. The introduction comes also handy to teach your collegues about that.
Amazon Verified review Amazon
Brian Wells Oct 05, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Not something you'd want to pay top dollar for, as it does not cover newer developments, but for a newcomer it's a great intro to everything up through 2015, IMHO. Look for a used copy under $10 and you'll be pleased with the purchase. (I doubt the 2nd edition merits the prices I am seeing, but the 1st edition can be found cheaply)
Amazon Verified review Amazon
B. Walter Finn Jan 25, 2014
Full star icon Full star icon Full star icon Full star icon Full star icon 5
There seems to be more code than explanation... that's perfectly fine. I read "MySQL Stored Procedure Programming" by Guy Harrison as well as a few Python books before this one, so I was comfortable with the material and was able to study the code and make some sense of it. An essential reference for anyone who prefers a tutorial style over cryptic reference documentation. Has examples that can be adapted and applied. I would recommend to anyone before starting their next project. It's always good to know what options you have available to meet business requirements, and not all are at the application layer.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.