Book Image

PostgreSQL 11 Server Side Programming Quick Start Guide

By : Luca Ferrari
Book Image

PostgreSQL 11 Server Side Programming Quick Start Guide

By: Luca Ferrari

Overview of this book

PostgreSQL is a rock-solid, scalable, and safe enterprise-level relational database. With a broad range of features and stability, it is ever increasing in popularity.This book shows you how to take advantage of PostgreSQL 11 features for server-side programming. Server-side programming enables strong data encapsulation and coherence. The book begins with the importance of server-side programming and explains the risks of leaving all the checks outside the database. To build your capabilities further, you will learn how to write stored procedures, both functions and the new PostgreSQL 11 procedures, and create triggers to perform encapsulation and maintain data consistency. You will also learn how to produce extensions, the easiest way to package your programs for easy and solid deployment on different PostgreSQL installations.
Table of Contents (12 chapters)

Throwing away a query result set

Any SQL statement that returns values must put its result into one or more variables by using INTO, for example. This means that invoking other functions that return values, or executing statements that return a result set (such as INSERT, UPDATE or DELETE with RETURNING or SELECT) is not possible until the returned values are stored in variables. This means that even a simple block code such as Listing 35 will fail because the value returned by the now() function is implicitly discarded:

testdb=> DO $code$ BEGIN
SELECT now();
END $code$;

ERROR: query has no destination for result data
Listing 35: A throw-away statement that doesn't work

If we have the need to execute a query and throw away its results, the PERFORM statement must be used. This statement can be thought of an alias for SELECT. It is in fact possible to write the query text...