PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

22
PL/PGSQL AN INTRODUCTION ON HOW TO USE IMPERATIVE PROGRAMMING IN POSTGRESQL

description

When you start tackling complex high-volume data problems, SQL might not be enough. When your logic needs to be close to your data, PostgreSQL has a secret weapon, PL/pgSQL. An imperative language that extends SQL, PL/pgSQL lets you execute complex read and write logic directly on the database. The benefits include increased speed and concurrency, code reuse among many applications, strong consistency guarantees, and greater query flexibility. This presentation will show you how PL/pgSQL functions can speed up your indexes, accelerate complex queries, activate custom triggers on data changes, and handle complex write situations. Afterwards, you will be able to take control of your data directly at the source, within PostgreSQL itself. Key points discussed: * PL/pgSQL: what it is and why it matters * Volatility: living in an uncertain data world * Upserts: a little update with a little insert * Triggers: keeping data consistent * Code Blocks: control when you need it SQL examples given during the presentation are available here: http://www.reactive.io/academy/presentations/postgresql/plpgsql/plpgsql-examples.zip

Transcript of PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

Page 1: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

PL/PGSQL

AN INTRODUCTION ON HOW TO USE IMPERATIVE

PROGRAMMING IN POSTGRESQL

Page 2: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

Robert Sosinski

Founder & Engineering Fellow

Page 3: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

AGENDA

PL/pgSQL: what it is and why it matters

Volatility: living in an uncertain data world

Upserts: a little update with a little insert

Triggers: keeping data consistent

Code Blocks: control when you need it

Summary: bringing it all together

Questions: fire away

Page 4: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

WHAT IS PL/PGSQL

Procedural

Language

postgres

Structured

Query

Language

Page 5: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

HOW PL MEETS SQL

PL/pgSQL SQL

Page 6: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

WHY SQL IS NOT ENOUGH

With SQL, every statement must be executed individually by

the database server

1. Complex actions can require multiple round-trips

2. Intermediate results have to be marshaled and

transferred

3. Multiple clients must implement redundant logic

4. Secure information might require exposure

5. Indexes and common filters might need complex logic

6. Consistency can require client-side interaction

Page 7: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

TWO WAYS TO PL/PGSQL

Stored Procedure

• Stored directly on the database server

• Can be used in queries and indexes

• Able to be activated upon trigger conditions

• May take input parameters and return values

Anonymous Code Block

• Sent to and parsed by the database when needed

• Able to write to the database but unable to return a value

Page 8: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

MORE PL THEN JUST PGSQL

PL/PerlPL/TCL

PL/Python

PL/Ruby PL/PHPPL/V8PL/Java

Page 9: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

DIVING HEAD FIRST

Secure access database

for a fictitious company

with high performance needs.

Page 10: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

LETS WRITE SOME

FUNCTIONS

Open Postgres Terminal

Page 11: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

DEALING WITH VOLATILITY

Volatility Categories

provide assumptions

the the query planner on

how to optimize a function call

Page 12: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

IMMUTABLE

Characteristics

• Cannot modify the database

• Should not read from the database (side effect free)

• Similar inputs should always return the same output

• Can be used when creating indexes

• Database may replace a function call with a constant

Examples

• Our check_access_for_user function for user types

• The lower function for string types

Page 13: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

STABLE

Characteristics

• Cannot modify the database

• Able to read from database

• Similar inputs should always return the same output on a

per statement basis

• Can be used in index conditions, but not creation

• Can give caching benefits when called multiple times

Examples

• The current_timestamp and now function

Page 14: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

VOLATILE

Characteristics

• Default volatility classification if none is specified

• Can read from and write to the database

• Can return different data on each call

• Can be used as trigger functions

• Planner will not make any assumptions for optimization

Examples

• The random and timeofday functions

• Sequence functions such as nextval and currval

Page 15: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

LETS TRY TO WRITE SOME

STABLE FUNCTIONS

Open Postgres Terminal

Page 16: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

PULLING THE TRIGGER

Trigger Function

• The function to be run when a triggered event happens

• Written using PL/pgSQL or any other PL language

• Has special arguments available for control flow

• If returns NULL, the event is canceled, else it continues

Trigger Definition

• The event that causes the trigger to be run

• Always attached directly to the table or view

• Can be run BEFORE, AFTER or INSTEAD OF

• Can be run run for EACH ROW or EACH STATEMENT

Page 17: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

LETS PULL SOME TRIGGERS

Open Postgres Terminal

Page 18: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

ANONYMOUS CODE BLOCKS

The Good

• You do not have to store the function on the server

• Can run arbitrary PL/pgSQL when it is needed

• Good for admin functions stored in your application

The Bad

• Cannot take arguments, so must be injected by your app

• Unable to return a value, but you can get feedback

• Must be parsed each time they are used

Page 19: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

LETS “DO” SOMETHING

Open Postgres Terminal

Page 20: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

SUMMARY

Powerful: have direct imperative access to your data

Easy: syntax is familiar and simple to use

Fast: because you are never leaving the database

Secure: you do not need to send data back and forth

Flexible: store the logic on the database or in your app

Page 21: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

CONDENSED SUMMARY

Direct access

to your data means

better control

of your business

Page 22: PL/pgSQL - An Introduction on Using Imperative Programming in PostgreSQL

THANKS

Open For Questions