Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
PostgreSQL Server Programming

You're reading from  PostgreSQL Server Programming

Product type Book
Published in Jun 2013
Publisher Packt
ISBN-13 9781849516983
Pages 264 pages
Edition 1st Edition
Languages

Table of Contents (17) Chapters

PostgreSQL Server Programming
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
1. What Is a PostgreSQL Server? 2. Server Programming Environment 3. Your First PL/pgSQL Function 4. Returning Structured Data 5. PL/pgSQL Trigger Functions 6. Debugging PL/pgSQL 7. Using Unrestricted Languages 8. Writing Advanced Functions in C 9. Scaling Your Database with PL/Proxy 10. Publishing Your Code as PostgreSQL Extensions Index

On caching


Yet another place where server-side programming can be used is for caching values, which are expensive to compute. The basic pattern here is:

  1. Check if the value is cached.

  2. If not or the value is too old, compute and cache it.

  3. Return the cached value.

For example, calculating sales for a company is the perfect item to cache. Perhaps, a large retail company has 1,000 stores with potentially millions of individual sales transactions per day. If the corporate headquarters is looking for sales trends, it is much more efficient if the daily sales numbers were precalculated at the store level instead of summing up millions of daily transactions.

If the value is simple, like looking up a user's information from a single table based on the user ID, you don't need to do anything. The value becomes cached in PostgreSQL's internal page cache, and all lookups to it are so fast that even on a very fast network most of the time spent doing the lookups are in the network, not in the actual lookup. In such a case, getting data from a PostgreSQL database is as fast as getting it from any other in-memory cache (like memcached) but without any extra overhead in managing the cache.

Another use-case of caching is implementing materialized views. These are views which are precomputed only when needed, not each time one selects from that view. Some SQL databases have materialized views as a separate database object, but in PostgreSQL you have to do it all yourself, using other database features for automating the whole process.

You have been reading a chapter from
PostgreSQL Server Programming
Published in: Jun 2013 Publisher: Packt ISBN-13: 9781849516983
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 $15.99/month. Cancel anytime}