Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
PostgreSQL Server Programming - Second Edition

You're reading from   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

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher
ISBN-13 9781783980581
Length 320 pages
Edition 2nd Edition
Arrow right icon
Authors (3):
Arrow left icon
 Dar Dar
Author Profile Icon Dar
Dar
 Krosing Krosing
Author Profile Icon Krosing
Krosing
Jim Mlodgenski Jim Mlodgenski
Author Profile Icon Jim Mlodgenski
Jim Mlodgenski
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. What Is a PostgreSQL Server? 2. Server Programming Environments FREE CHAPTER 3. Your First PL/pgSQL Function 4. Returning Structured Data 5. PL/pgSQL Trigger Functions 6. PostgreSQL Event Triggers 7. Debugging PL/pgSQL 8. Using Unrestricted Languages 9. Writing Advanced Functions in C 10. Scaling Your Database with PL/Proxy 11. PL/Perl – Perl Procedural Language 12. PL/Tcl – Tcl Procedural Language 13. Publishing Your Code as PostgreSQL Extensions 14. PostgreSQL as an Extensible RDBMS Index

Wrapping up – why program in the server?

The main advantages of doing most data manipulation code on the server-side are stated in the following sections.

Performance

Doing the computation near the data is almost always a performance win, as the latencies to get the data are minimal. In a typical data-intensive computation, most of the time is spent in getting the data. Therefore, making data access inside the computation faster is the best way to make the whole thing fast. On my laptop, it takes 2.2 ms to query one random row from a 1,000,000-row database into the client, but it takes only 0.12 ms to get the data inside the database. This is 20 times faster and inside the same machine over Unix sockets. The difference can be bigger if there is a network connection between the client and the server.

A small real-word story:

A friend of mine was called to help a large company (I'm sure all of you know it, but I can't tell you which one) in order to make its e-mail sending application faster. They had implemented their e-mail generation system with all the latest Java EE technologies: first, getting the data from the database, passing the data around between services, and serializing and deserializing it several times before finally doing XSLT transformation on the data to produce the e-mail text. The end result being that it produced only a few hundred e-mails per second, and they were falling behind with their responses.

When he rewrote the process to use a PL/Perl function inside the database to format the data and the query returned already fully-formatted e-mails, it suddenly started spewing out tens of thousands of e-mails per second and they had to add a second copy of the sent mail to actually be able to send them out.

Ease of maintenance

If all the data manipulation code is in a database, either as database functions or views, the actual upgrade process becomes very easy. All that is needed is to run a DDL script that redefines the functions; all the clients automatically use the new code with no downtime and no complicated coordination between several frontend systems and teams.

Improved productivity

Server-side functions are perhaps the best way to achieve code reuse. Any client application written in any language or framework can make use of the server-side functions, ensuring maximum reuse in all environments.

Simple ways to tighten security

If all the access for some possibly insecure servers goes through functions, the database user of these servers can only be granted access to the needed functions and nothing else. They can't see the table data or even the fact that these tables exist. So, even if the server is compromised, all it can do is continue to call the same functions. Also, there is no possibility of stealing passwords, e-mails, or other sensitive information by issuing its own queries such as SELECT * FROM users; and getting all the data there is in the database.

Also, the most important thing is that programming in a server is fun!

lock icon The rest of the chapter is locked
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
PostgreSQL Server Programming - Second Edition
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Modal Close icon
Modal Close icon