Successfully reported this slideshow.
PL/PGSQL
AN INTRODUCTION ON HOW TO USE IMPERATIVE
PROGRAMMING IN POSTGRESQL
Robert Sosinski
Founder & Engineering Fellow
AGENDA
PL/pgSQL: what it is and why it matters
Volatility: living in an uncertain data world
Upserts: a little update with...
WHAT IS PL/PGSQL
Procedural
Language
postgres
Structured
Query
Language
HOW PL MEETS SQL
PL/pgSQL SQL
WHY SQL IS NOT ENOUGH
With SQL, every statement must be executed individually by
the database server
1. Complex actions ca...
TWO WAYS TO PL/PGSQL
Stored Procedure
• Stored directly on the database server
• Can be used in queries and indexes
• Able...
MORE PL THEN JUST PGSQL
PL/Perl
PL/TCL
PL/Python
PL/Ruby PL/PHPPL/V8PL/Java
DIVING HEAD FIRST
Secure access database
for a fictitious company
with high performance needs.
LETS WRITE SOME
FUNCTIONS
Open Postgres Terminal
DEALING WITH VOLATILITY
Volatility Categories
provide assumptions
the the query planner on
how to optimize a function call
IMMUTABLE
Characteristics
• Cannot modify the database
• Should not read from the database (side effect free)
• Similar in...
STABLE
Characteristics
• Cannot modify the database
• Able to read from database
• Similar inputs should always return the...
VOLATILE
Characteristics
• Default volatility classification if none is specified
• Can read from and write to the databas...
LETS TRY TO WRITE SOME
STABLE FUNCTIONS
Open Postgres Terminal
PULLING THE TRIGGER
Trigger Function
• The function to be run when a triggered event happens
• Written using PL/pgSQL or a...
LETS PULL SOME TRIGGERS
Open Postgres Terminal
ANONYMOUS CODE BLOCKS
The Good
• You do not have to store the function on the server
• Can run arbitrary PL/pgSQL when it ...
LETS “DO” SOMETHING
Open Postgres Terminal
SUMMARY
Powerful: have direct imperative access to your data
Easy: syntax is familiar and simple to use
Fast: because you ...
CONDENSED SUMMARY
Direct access
to your data means
better control
of your business
THANKS
Open For Questions
Upcoming SlideShare
Loading in …5
×

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

1,313 views

Published on

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

Published in: Technology
  • Be the first to comment

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

  1. 1. PL/PGSQL AN INTRODUCTION ON HOW TO USE IMPERATIVE PROGRAMMING IN POSTGRESQL
  2. 2. Robert Sosinski Founder & Engineering Fellow
  3. 3. 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
  4. 4. WHAT IS PL/PGSQL Procedural Language postgres Structured Query Language
  5. 5. HOW PL MEETS SQL PL/pgSQL SQL
  6. 6. 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
  7. 7. 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
  8. 8. MORE PL THEN JUST PGSQL PL/Perl PL/TCL PL/Python PL/Ruby PL/PHPPL/V8PL/Java
  9. 9. DIVING HEAD FIRST Secure access database for a fictitious company with high performance needs.
  10. 10. LETS WRITE SOME FUNCTIONS Open Postgres Terminal
  11. 11. DEALING WITH VOLATILITY Volatility Categories provide assumptions the the query planner on how to optimize a function call
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. LETS TRY TO WRITE SOME STABLE FUNCTIONS Open Postgres Terminal
  16. 16. 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
  17. 17. LETS PULL SOME TRIGGERS Open Postgres Terminal
  18. 18. 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
  19. 19. LETS “DO” SOMETHING Open Postgres Terminal
  20. 20. 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
  21. 21. CONDENSED SUMMARY Direct access to your data means better control of your business
  22. 22. THANKS Open For Questions

×