SlideShare a Scribd company logo
Overview
Function Basics
By Example
PostgreSQL Functions By Example
Joe Conway
joe.conway@credativ.com
credativ Group
January 20, 2012
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Introduction
Uses
Varieties
Languages
What are Functions?
Full fledged SQL objects
Many other database objects are implemented with them
Fundamental part of PostgreSQL’s system architecture
Created with CREATE FUNCTION
Executed through normal SQL
target-list:
SELECT myfunc(f1) FROM foo;
FROM clause:
SELECT * FROM myfunc();
WHERE clause:
SELECT * FROM foo WHERE myfunc(f1) = 42;
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Introduction
Uses
Varieties
Languages
How are they Used?
Functions
Operators
Data types
Index methods
Casts
Triggers
Aggregates
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Introduction
Uses
Varieties
Languages
What Forms Can They Take?
PostgreSQL provides four kinds of functions:
SQL
Procedural Languages
Internal
C-language
Arguments
Base, composite, or combinations
Scalar or array
Pseudo or polymorphic
VARIADIC
IN/OUT/INOUT
Return
Singleton or set (SETOF)
Base or composite type
Pseudo or polymorphic
http://www.postgresql.org/docs/9.1/interactive/sql-createfunction.html
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Introduction
Uses
Varieties
Languages
SQL Functions
Behavior
Executes an arbitrary list of SQL statements separated by
semicolons
Last statement may be INSERT, UPDATE, or DELETE with
RETURNING clause
Arguments
Referenced by function body using $n: $1 is first arg, etc. . .
If composite type, then dot notation $1.name used to access
Only used as data values, not as identifiers
Return
If singleton, first row of last query result returned, NULL on no
result
If SETOF, all rows of last query result returned, empty set on
no result
http://www.postgresql.org/docs/9.1/interactive/xfunc-sql.html
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Introduction
Uses
Varieties
Languages
Procedural Languages
User-defined functions
Written in languages besides SQL and C
Task is passed to a special handler that knows the details of
the language
Handler could be self-contained (e.g. PL/pgSQL)
Handler could be dynamically loaded (e.g. PL/Perl)
http://www.postgresql.org/docs/9.1/interactive/xplang.html
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Introduction
Uses
Varieties
Languages
Internal Functions
Statically linked C functions
Could use CREATE FUNCTION to create additional alias
names for an internal function
Most internal functions expect to be declared STRICT
CREATE FUNCTION square_root(double precision)
RETURNS double precision AS
’dsqrt’
LANGUAGE internal STRICT;
http://www.postgresql.org/docs/9.1/interactive/xfunc-internal.html
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Introduction
Uses
Varieties
Languages
C Language Functions
User-defined functions written in C
Compiled into dynamically loadable objects (also called shared
libraries)
Loaded by the server on demand
contrib is good source of examples
Same as internal function coding conventions
Require PG MODULE MAGIC call
Needs separate topic
http://www.postgresql.org/docs/9.1/interactive/xfunc-c.html
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Introduction
Uses
Varieties
Languages
Language Availability
PostgreSQL includes the following server-side procedural
languages:
http://www.postgresql.org/docs/9.1/interactive/xplang.html
PL/pgSQL
Perl
Python
Tcl
Other languages available:
http://pgfoundry.org/softwaremap/trove_list.php?form_cat=311
Java
PHP
Ruby
R
Shell
others . . .
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Creation
Attributes
Creating New Functions
CREATE [ OR REPLACE ] FUNCTION
name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } defexpr ] [, ...
[ RETURNS rettype
| RETURNS TABLE ( colname coltype [, ...] ) ]
{ LANGUAGE langname
| WINDOW
| IMMUTABLE | STABLE | VOLATILE
| CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT
| [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER
| COST execution_cost
| ROWS result_rows
| SET configuration_parameter { TO value | = value | FROM CURRENT }
| AS ’definition’
| AS ’obj_file’, ’link_symbol’
} ...
[ WITH ( attribute [, ...] ) ]
http://www.postgresql.org/docs/9.1/interactive/sql-createfunction.html
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Creation
Attributes
Dollar Quoting
Works for all character strings
Particularly useful for function bodies
CREATE OR REPLACE FUNCTION dummy () RETURNS text AS
$Q$
DECLARE
result text;
BEGIN
PERFORM ’SELECT 1+1’;
RETURN ’ok’;
END;
$Q$
LANGUAGE plpgsql;
http://www.postgresql.org/docs/9.1/static/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Creation
Attributes
Function Overloading
IN argument signature used
Avoid ambiguities:
Type (e.g. REAL vs. DOUBLE PRECISION)
Function name same as IN composite field name
VARIADIC vs same type scalar
CREATE OR REPLACE FUNCTION foo (text) RETURNS text AS $$
SELECT $1
$$ LANGUAGE sql;
CREATE OR REPLACE FUNCTION foo (int) RETURNS text AS $$
SELECT ($1 + 1)::text
$$ LANGUAGE sql;
SELECT foo(’42’), foo(41);
foo | foo
-----+-----
42 | 42
(1 row)
http://www.postgresql.org/docs/9.1/interactive/xfunc-overload.html
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Creation
Attributes
Changing Existing Functions
Once created, dependent objects may be created
Must do DROP FUNCTION ... CASCADE to recreate
Or use OR REPLACE to avoid dropping dependent objects
Very useful for large dependency tree
Can’t be used in some circumstances (must drop/recreate
instead). You cannot:
change function name or argument types
change return type
change types of any OUT parameters
CREATE OR REPLACE FUNCTION ...;
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Creation
Attributes
Volatility
VOLATILE (default)
Each call can return a different result
Example: random() or timeofday()
Functions modifying table contents must be declared volatile
STABLE
Returns same result for same arguments within single query
Example: now()
Consider configuration settings that affect output
IMMUTABLE
Always returns the same result for the same arguments
Example: lower(’ABC’)
Unaffected by configuration settings
Not dependent on table contents
select lower(’ABC’), now(), timeofday() from generate_series(1,3);
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Creation
Attributes
Behavior with Null Input Values
CALLED ON NULL INPUT (default)
Function called normally with the null input values
RETURNS NULL ON NULL INPUT
Function not called when null input values are present
Instead, null is returned automatically
CREATE FUNCTION sum1 (int, int) RETURNS int AS $$
SELECT $1 + $2
$$ LANGUAGE SQL RETURNS NULL ON NULL INPUT;
CREATE FUNCTION sum2 (int, int) RETURNS int AS $$
SELECT COALESCE($1, 0) + COALESCE($2, 0)
$$ LANGUAGE SQL CALLED ON NULL INPUT;
SELECT sum1(9, NULL) IS NULL AS "true", sum2(9, NULL);
true | sum2
------+------
t | 9
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
Creation
Attributes
Security Attributes
SECURITY INVOKER (default)
Function executed with the rights of the current user
SECURITY DEFINER
Executed with rights of creator, like ”setuid”
CREATE TABLE foo (f1 int);
REVOKE ALL ON foo FROM public;
CREATE FUNCTION see_foo() RETURNS SETOF foo AS $$
SELECT * FROM foo
$$ LANGUAGE SQL SECURITY DEFINER;
c - guest
You are now connected to database "postgres" as user "guest".
SELECT * FROM foo;
ERROR: permission denied for relation foo
SELECT * FROM see_foo();
f1
----
(0 rows)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Simple
CREATE FUNCTION sum (text, text)
RETURNS text AS $$
SELECT $1 || ’ ’ || $2
$$ LANGUAGE SQL;
SELECT sum(’hello’, ’world’);
sum
-------------
hello world
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Custom Operator
CREATE OPERATOR + (
procedure = sum,
leftarg = text,
rightarg = text
);
SELECT ’hello’ + ’world’;
?column?
-------------
hello world
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Custom Aggregate
CREATE OR REPLACE FUNCTION concat_ws_comma(text, ANYELEMENT)
RETURNS text AS $$
SELECT concat_ws(’,’, $1, $2)
$$ LANGUAGE sql;
CREATE AGGREGATE str_agg (ANYELEMENT) (
sfunc = concat_ws_comma,
stype = text);
SELECT str_agg(f1) FROM foo;
str_agg
---------
41,42
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
SETOF with OUT Arguments
CREATE OR REPLACE FUNCTION sql_with_rows(OUT a int, OUT b text)
RETURNS SETOF RECORD AS $$
values (1,’a’),(2,’b’)
$$ LANGUAGE SQL;
select * from sql_with_rows();
a | b
---+---
1 | a
2 | b
(2 rows)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
INSERT RETURNING
CREATE TABLE foo (f0 serial, f1 int, f2 text);
CREATE OR REPLACE FUNCTION
sql_insert_returning(INOUT f1 int, INOUT f2 text, OUT id int) AS $$
INSERT INTO foo(f1, f2) VALUES ($1,$2) RETURNING f1, f2, f0
$$ LANGUAGE SQL;
SELECT * FROM sql_insert_returning(1,’a’);
f1 | f2 | id
----+----+----
1 | a | 1
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Composite Argument
CREATE TABLE emp (name text,
salary numeric,
age integer,
cubicle point);
CREATE FUNCTION double_salary(emp) RETURNS numeric AS $$
SELECT $1.salary * 2 AS salary;
$$ LANGUAGE SQL;
SELECT name, double_salary(emp.*) AS dream
FROM emp WHERE emp.cubicle ~= point ’(2,1)’;
SELECT name,
double_salary(ROW(name, salary*1.1, age, cubicle)) AS dream
FROM emp;
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Polymorphic
CREATE FUNCTION myappend(anyarray, anyelement) RETURNS anyarray AS
$$
SELECT $1 || $2;
$$ LANGUAGE SQL;
SELECT myappend(ARRAY[42,6], 21), myappend(ARRAY[’abc’,’def’], ’xyz’);
myappend | myappend
-----------+---------------
{42,6,21} | {abc,def,xyz}
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Target List versus FROM Clause
CREATE FUNCTION new_emp() RETURNS emp AS $$
SELECT ROW(’None’, 1000.0, 25, ’(2,2)’)::emp;
$$ LANGUAGE SQL;
SELECT new_emp();
new_emp
--------------------------
(None,1000.0,25,"(2,2)")
SELECT * FROM new_emp();
name | salary | age | cubicle
------+--------+-----+---------
None | 1000.0 | 25 | (2,2)
SELECT (new_emp()).name;
name
------
None
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
VARIADIC
CREATE FUNCTION mleast(VARIADIC numeric[]) RETURNS numeric AS $$
SELECT min($1[i]) FROM generate_subscripts($1, 1) g(i);
$$ LANGUAGE SQL;
SELECT mleast(10, -1, 5, 4.4);
mleast
--------
-1
(1 row)
SELECT mleast(42, 6, 42.42);
mleast
--------
6
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
DEFAULT Arguments
CREATE FUNCTION foo(a int, b int DEFAULT 2, c int DEFAULT 3)
RETURNS int LANGUAGE SQL AS $$SELECT $1 + $2 + $3$$;
SELECT foo(10, 20, 30);
foo
-----
60
(1 row)
SELECT foo(10, 20);
foo
-----
33
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
PL/pgSQL
PL/pgSQL is SQL plus procedural elements
variables
if/then/else
loops
cursors
error checking
Loading the language handler into a database:
createlang plpgsql dbname
http://www.postgresql.org/docs/9.1/interactive/plpgsql.html
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Simple
CREATE OR REPLACE FUNCTION sum (text, text)
RETURNS text AS $$
BEGIN
RETURN $1 || ’ ’ || $2;
END;
$$ LANGUAGE plpgsql;
SELECT sum(’hello’, ’world’);
sum
-------------
hello world
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Parameter ALIAS
CREATE OR REPLACE FUNCTION sum (int, int)
RETURNS int AS $$
DECLARE
i ALIAS FOR $1;
j ALIAS FOR $2;
sum int;
BEGIN
sum := i + j;
RETURN sum;
END;
$$ LANGUAGE plpgsql;
SELECT sum(41, 1);
sum
-----
42
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Named Parameters
CREATE OR REPLACE FUNCTION sum (i int, j int)
RETURNS int AS $$
DECLARE
sum int;
BEGIN
sum := i + j;
RETURN sum;
END;
$$ LANGUAGE plpgsql;
SELECT sum(41, 1);
sum
-----
42
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Control Structures: IF ...
CREATE OR REPLACE FUNCTION even (i int)
RETURNS boolean AS $$
DECLARE
tmp int;
BEGIN
tmp := i % 2;
IF tmp = 0 THEN RETURN true;
ELSE RETURN false;
END IF;
END;
$$ LANGUAGE plpgsql;
SELECT even(3), even(42);
even | even
------+------
f | t
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Control Structures: FOR ... LOOP
CREATE OR REPLACE FUNCTION factorial (i numeric)
RETURNS numeric AS $$
DECLARE
tmp numeric; result numeric;
BEGIN
result := 1;
FOR tmp IN 1 .. i LOOP
result := result * tmp;
END LOOP;
RETURN result;
END;
$$ LANGUAGE plpgsql;
SELECT factorial(42::numeric);
factorial
------------------------------------------------------
1405006117752879898543142606244511569936384000000000
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Control Structures: WHILE ... LOOP
CREATE OR REPLACE FUNCTION factorial (i numeric)
RETURNS numeric AS $$
DECLARE tmp numeric; result numeric;
BEGIN
result := 1; tmp := 1;
WHILE tmp <= i LOOP
result := result * tmp;
tmp := tmp + 1;
END LOOP;
RETURN result;
END;
$$ LANGUAGE plpgsql;
SELECT factorial(42::numeric);
factorial
------------------------------------------------------
1405006117752879898543142606244511569936384000000000
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Recursive
CREATE OR REPLACE FUNCTION factorial (i numeric)
RETURNS numeric AS $$
BEGIN
IF i = 0 THEN
RETURN 1;
ELSIF i = 1 THEN
RETURN 1;
ELSE
RETURN i * factorial(i - 1);
END IF;
END;
$$ LANGUAGE plpgsql;
SELECT factorial(42::numeric);
factorial
------------------------------------------------------
1405006117752879898543142606244511569936384000000000
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Record types
CREATE OR REPLACE FUNCTION format ()
RETURNS text AS $$
DECLARE
tmp RECORD;
BEGIN
SELECT INTO tmp 1 + 1 AS a, 2 + 2 AS b;
RETURN ’a = ’ || tmp.a || ’; b = ’ || tmp.b;
END;
$$ LANGUAGE plpgsql;
select format();
format
--------------
a = 2; b = 4
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
PERFORM
CREATE OR REPLACE FUNCTION func_w_side_fx() RETURNS void AS
$$ INSERT INTO foo VALUES (41),(42) $$ LANGUAGE sql;
CREATE OR REPLACE FUNCTION dummy ()
RETURNS text AS $$
BEGIN
PERFORM func_w_side_fx();
RETURN ’OK’;
END;
$$ LANGUAGE plpgsql;
SELECT dummy();
SELECT * FROM foo;
f1
----
41
42
(2 rows)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Dynamic SQL
CREATE OR REPLACE FUNCTION get_foo(i int)
RETURNS foo AS $$
DECLARE
rec RECORD;
BEGIN
EXECUTE ’SELECT * FROM foo WHERE f1 = ’ || i INTO rec;
RETURN rec;
END;
$$ LANGUAGE plpgsql;
SELECT * FROM get_foo(42);
f1
----
42
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Cursors
CREATE OR REPLACE FUNCTION totalbalance()
RETURNS numeric AS $$
DECLARE
tmp RECORD; result numeric;
BEGIN
result := 0.00;
FOR tmp IN SELECT * FROM foo LOOP
result := result + tmp.f1;
END LOOP;
RETURN result;
END;
$$ LANGUAGE plpgsql;
SELECT totalbalance();
totalbalance
--------------
83.00
(1 row)
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Error Handling
CREATE OR REPLACE FUNCTION safe_add(a integer, b integer)
RETURNS integer AS $$
BEGIN
RETURN a + b;
EXCEPTION
WHEN numeric_value_out_of_range THEN
-- do some important stuff
RETURN -1;
WHEN OTHERS THEN
-- do some other important stuff
RETURN -1;
END;
$$ LANGUAGE plpgsql;
http://www.postgresql.org/docs/9.1/interactive/errcodes-appendix.html
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Nested Exception Blocks
CREATE FUNCTION merge_db(key integer, data text)
RETURNS void AS $$
BEGIN
LOOP
UPDATE db SET b = data WHERE a = key;
IF found THEN RETURN;
END IF;
BEGIN
INSERT INTO db (a, b) VALUES (key, data);
RETURN;
EXCEPTION WHEN unique_violation THEN
-- do nothing
END;
END LOOP;
EXCEPTION WHEN OTHERS THEN
-- do something else
END;
$$ LANGUAGE plpgsql;
Joe Conway SCALE10X-PGDay
Overview
Function Basics
By Example
SQL Functions
PL/pgSQL Functions
Thank You
Questions?
Joe Conway SCALE10X-PGDay

More Related Content

Similar to Postgresql function_basics.pdf (20)

PDF
PostgreSQL Server Programming 2nd Edition Usama Dar
mavriclasme
 
KEY
Building and Distributing PostgreSQL Extensions Without Learning C
David Wheeler
 
PDF
Business logic with PostgreSQL and Python
Hubert Piotrowski
 
PPTX
PLPgSqL- Datatypes, Language structure.pptx
johnwick814916
 
PDF
Function Procedure Trigger Partition.pdf
Sanam Maharjan
 
PPTX
Lecture 3.2_Subprogrammm - Function.pptx
pproychd
 
PDF
PostgreSQL - Case Study
S.Shayan Daneshvar
 
PPT
Oracle sql ppt2
Madhavendra Dutt
 
PPTX
Built-Functions in MySqll (Advance Database System)
khurtdhangonzales
 
PPT
plsql les02
sasa_eldoby
 
PDF
Techday2010 Postgresql9
Dan-Claudiu Dragoș
 
PDF
Learning postgresql
DAVID RAUDALES
 
PDF
Tutorial: Implementing your first Postgres extension | PGConf EU 2019 | Burak...
Citus Data
 
PDF
Structured Query Language (SQL) - An Introduction
Rajeev Srivastava
 
PPTX
Rdbms chapter 1 function
dipumaliy
 
KEY
PostgreSQL
Reuven Lerner
 
PDF
PostgreSQL and PL/Java
Peter Eisentraut
 
PDF
Heroku Postgres SQL Tips, Tricks, Hacks
Salesforce Developers
 
PDF
Functions
Pragnavi Erva
 
PDF
Plpgsql internals
Pavel Stěhule
 
PostgreSQL Server Programming 2nd Edition Usama Dar
mavriclasme
 
Building and Distributing PostgreSQL Extensions Without Learning C
David Wheeler
 
Business logic with PostgreSQL and Python
Hubert Piotrowski
 
PLPgSqL- Datatypes, Language structure.pptx
johnwick814916
 
Function Procedure Trigger Partition.pdf
Sanam Maharjan
 
Lecture 3.2_Subprogrammm - Function.pptx
pproychd
 
PostgreSQL - Case Study
S.Shayan Daneshvar
 
Oracle sql ppt2
Madhavendra Dutt
 
Built-Functions in MySqll (Advance Database System)
khurtdhangonzales
 
plsql les02
sasa_eldoby
 
Techday2010 Postgresql9
Dan-Claudiu Dragoș
 
Learning postgresql
DAVID RAUDALES
 
Tutorial: Implementing your first Postgres extension | PGConf EU 2019 | Burak...
Citus Data
 
Structured Query Language (SQL) - An Introduction
Rajeev Srivastava
 
Rdbms chapter 1 function
dipumaliy
 
PostgreSQL
Reuven Lerner
 
PostgreSQL and PL/Java
Peter Eisentraut
 
Heroku Postgres SQL Tips, Tricks, Hacks
Salesforce Developers
 
Functions
Pragnavi Erva
 
Plpgsql internals
Pavel Stěhule
 

Recently uploaded (20)

PDF
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
PDF
Best Software Development at Best Prices
softechies7
 
PDF
Mastering VPC Architecture Build for Scale from Day 1.pdf
Devseccops.ai
 
DOCX
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
DOCX
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
 
PDF
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
PPTX
arctitecture application system design os dsa
za241967
 
PDF
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
 
PDF
Which Hiring Management Tools Offer the Best ROI?
HireME
 
PPTX
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
 
PDF
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
PDF
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
 
PDF
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
PPTX
For my supp to finally picking supp that work
necas19388
 
PPTX
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
Code Once; Run Everywhere - A Beginner’s Journey with React Native
Hasitha Walpola
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Best Software Development at Best Prices
softechies7
 
Mastering VPC Architecture Build for Scale from Day 1.pdf
Devseccops.ai
 
Best AI-Powered Wearable Tech for Remote Health Monitoring in 2025
SEOLIFT - SEO Company London
 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Zoho Creator Solution for EI by Elsner Technologies.docx
Elsner Technologies Pvt. Ltd.
 
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
arctitecture application system design os dsa
za241967
 
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
 
Which Hiring Management Tools Offer the Best ROI?
HireME
 
IObit Driver Booster Pro 12.4-12.5 license keys 2025-2026
chaudhryakashoo065
 
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Why Edge Computing Matters in Mobile Application Tech.pdf
IMG Global Infotech
 
Building scalbale cloud native apps with .NET 8
GillesMathieu10
 
For my supp to finally picking supp that work
necas19388
 
Wondershare Filmora Crack 14.5.18 + Key Full Download [Latest 2025]
HyperPc soft
 
Ad

Postgresql function_basics.pdf

  • 1. Overview Function Basics By Example PostgreSQL Functions By Example Joe Conway [email protected] credativ Group January 20, 2012 Joe Conway SCALE10X-PGDay
  • 2. Overview Function Basics By Example Introduction Uses Varieties Languages What are Functions? Full fledged SQL objects Many other database objects are implemented with them Fundamental part of PostgreSQL’s system architecture Created with CREATE FUNCTION Executed through normal SQL target-list: SELECT myfunc(f1) FROM foo; FROM clause: SELECT * FROM myfunc(); WHERE clause: SELECT * FROM foo WHERE myfunc(f1) = 42; Joe Conway SCALE10X-PGDay
  • 3. Overview Function Basics By Example Introduction Uses Varieties Languages How are they Used? Functions Operators Data types Index methods Casts Triggers Aggregates Joe Conway SCALE10X-PGDay
  • 4. Overview Function Basics By Example Introduction Uses Varieties Languages What Forms Can They Take? PostgreSQL provides four kinds of functions: SQL Procedural Languages Internal C-language Arguments Base, composite, or combinations Scalar or array Pseudo or polymorphic VARIADIC IN/OUT/INOUT Return Singleton or set (SETOF) Base or composite type Pseudo or polymorphic http://www.postgresql.org/docs/9.1/interactive/sql-createfunction.html Joe Conway SCALE10X-PGDay
  • 5. Overview Function Basics By Example Introduction Uses Varieties Languages SQL Functions Behavior Executes an arbitrary list of SQL statements separated by semicolons Last statement may be INSERT, UPDATE, or DELETE with RETURNING clause Arguments Referenced by function body using $n: $1 is first arg, etc. . . If composite type, then dot notation $1.name used to access Only used as data values, not as identifiers Return If singleton, first row of last query result returned, NULL on no result If SETOF, all rows of last query result returned, empty set on no result http://www.postgresql.org/docs/9.1/interactive/xfunc-sql.html Joe Conway SCALE10X-PGDay
  • 6. Overview Function Basics By Example Introduction Uses Varieties Languages Procedural Languages User-defined functions Written in languages besides SQL and C Task is passed to a special handler that knows the details of the language Handler could be self-contained (e.g. PL/pgSQL) Handler could be dynamically loaded (e.g. PL/Perl) http://www.postgresql.org/docs/9.1/interactive/xplang.html Joe Conway SCALE10X-PGDay
  • 7. Overview Function Basics By Example Introduction Uses Varieties Languages Internal Functions Statically linked C functions Could use CREATE FUNCTION to create additional alias names for an internal function Most internal functions expect to be declared STRICT CREATE FUNCTION square_root(double precision) RETURNS double precision AS ’dsqrt’ LANGUAGE internal STRICT; http://www.postgresql.org/docs/9.1/interactive/xfunc-internal.html Joe Conway SCALE10X-PGDay
  • 8. Overview Function Basics By Example Introduction Uses Varieties Languages C Language Functions User-defined functions written in C Compiled into dynamically loadable objects (also called shared libraries) Loaded by the server on demand contrib is good source of examples Same as internal function coding conventions Require PG MODULE MAGIC call Needs separate topic http://www.postgresql.org/docs/9.1/interactive/xfunc-c.html Joe Conway SCALE10X-PGDay
  • 9. Overview Function Basics By Example Introduction Uses Varieties Languages Language Availability PostgreSQL includes the following server-side procedural languages: http://www.postgresql.org/docs/9.1/interactive/xplang.html PL/pgSQL Perl Python Tcl Other languages available: http://pgfoundry.org/softwaremap/trove_list.php?form_cat=311 Java PHP Ruby R Shell others . . . Joe Conway SCALE10X-PGDay
  • 10. Overview Function Basics By Example Creation Attributes Creating New Functions CREATE [ OR REPLACE ] FUNCTION name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } defexpr ] [, ... [ RETURNS rettype | RETURNS TABLE ( colname coltype [, ...] ) ] { LANGUAGE langname | WINDOW | IMMUTABLE | STABLE | VOLATILE | CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT | [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER | COST execution_cost | ROWS result_rows | SET configuration_parameter { TO value | = value | FROM CURRENT } | AS ’definition’ | AS ’obj_file’, ’link_symbol’ } ... [ WITH ( attribute [, ...] ) ] http://www.postgresql.org/docs/9.1/interactive/sql-createfunction.html Joe Conway SCALE10X-PGDay
  • 11. Overview Function Basics By Example Creation Attributes Dollar Quoting Works for all character strings Particularly useful for function bodies CREATE OR REPLACE FUNCTION dummy () RETURNS text AS $Q$ DECLARE result text; BEGIN PERFORM ’SELECT 1+1’; RETURN ’ok’; END; $Q$ LANGUAGE plpgsql; http://www.postgresql.org/docs/9.1/static/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING Joe Conway SCALE10X-PGDay
  • 12. Overview Function Basics By Example Creation Attributes Function Overloading IN argument signature used Avoid ambiguities: Type (e.g. REAL vs. DOUBLE PRECISION) Function name same as IN composite field name VARIADIC vs same type scalar CREATE OR REPLACE FUNCTION foo (text) RETURNS text AS $$ SELECT $1 $$ LANGUAGE sql; CREATE OR REPLACE FUNCTION foo (int) RETURNS text AS $$ SELECT ($1 + 1)::text $$ LANGUAGE sql; SELECT foo(’42’), foo(41); foo | foo -----+----- 42 | 42 (1 row) http://www.postgresql.org/docs/9.1/interactive/xfunc-overload.html Joe Conway SCALE10X-PGDay
  • 13. Overview Function Basics By Example Creation Attributes Changing Existing Functions Once created, dependent objects may be created Must do DROP FUNCTION ... CASCADE to recreate Or use OR REPLACE to avoid dropping dependent objects Very useful for large dependency tree Can’t be used in some circumstances (must drop/recreate instead). You cannot: change function name or argument types change return type change types of any OUT parameters CREATE OR REPLACE FUNCTION ...; Joe Conway SCALE10X-PGDay
  • 14. Overview Function Basics By Example Creation Attributes Volatility VOLATILE (default) Each call can return a different result Example: random() or timeofday() Functions modifying table contents must be declared volatile STABLE Returns same result for same arguments within single query Example: now() Consider configuration settings that affect output IMMUTABLE Always returns the same result for the same arguments Example: lower(’ABC’) Unaffected by configuration settings Not dependent on table contents select lower(’ABC’), now(), timeofday() from generate_series(1,3); Joe Conway SCALE10X-PGDay
  • 15. Overview Function Basics By Example Creation Attributes Behavior with Null Input Values CALLED ON NULL INPUT (default) Function called normally with the null input values RETURNS NULL ON NULL INPUT Function not called when null input values are present Instead, null is returned automatically CREATE FUNCTION sum1 (int, int) RETURNS int AS $$ SELECT $1 + $2 $$ LANGUAGE SQL RETURNS NULL ON NULL INPUT; CREATE FUNCTION sum2 (int, int) RETURNS int AS $$ SELECT COALESCE($1, 0) + COALESCE($2, 0) $$ LANGUAGE SQL CALLED ON NULL INPUT; SELECT sum1(9, NULL) IS NULL AS "true", sum2(9, NULL); true | sum2 ------+------ t | 9 (1 row) Joe Conway SCALE10X-PGDay
  • 16. Overview Function Basics By Example Creation Attributes Security Attributes SECURITY INVOKER (default) Function executed with the rights of the current user SECURITY DEFINER Executed with rights of creator, like ”setuid” CREATE TABLE foo (f1 int); REVOKE ALL ON foo FROM public; CREATE FUNCTION see_foo() RETURNS SETOF foo AS $$ SELECT * FROM foo $$ LANGUAGE SQL SECURITY DEFINER; c - guest You are now connected to database "postgres" as user "guest". SELECT * FROM foo; ERROR: permission denied for relation foo SELECT * FROM see_foo(); f1 ---- (0 rows) Joe Conway SCALE10X-PGDay
  • 17. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Simple CREATE FUNCTION sum (text, text) RETURNS text AS $$ SELECT $1 || ’ ’ || $2 $$ LANGUAGE SQL; SELECT sum(’hello’, ’world’); sum ------------- hello world (1 row) Joe Conway SCALE10X-PGDay
  • 18. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Custom Operator CREATE OPERATOR + ( procedure = sum, leftarg = text, rightarg = text ); SELECT ’hello’ + ’world’; ?column? ------------- hello world (1 row) Joe Conway SCALE10X-PGDay
  • 19. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Custom Aggregate CREATE OR REPLACE FUNCTION concat_ws_comma(text, ANYELEMENT) RETURNS text AS $$ SELECT concat_ws(’,’, $1, $2) $$ LANGUAGE sql; CREATE AGGREGATE str_agg (ANYELEMENT) ( sfunc = concat_ws_comma, stype = text); SELECT str_agg(f1) FROM foo; str_agg --------- 41,42 (1 row) Joe Conway SCALE10X-PGDay
  • 20. Overview Function Basics By Example SQL Functions PL/pgSQL Functions SETOF with OUT Arguments CREATE OR REPLACE FUNCTION sql_with_rows(OUT a int, OUT b text) RETURNS SETOF RECORD AS $$ values (1,’a’),(2,’b’) $$ LANGUAGE SQL; select * from sql_with_rows(); a | b ---+--- 1 | a 2 | b (2 rows) Joe Conway SCALE10X-PGDay
  • 21. Overview Function Basics By Example SQL Functions PL/pgSQL Functions INSERT RETURNING CREATE TABLE foo (f0 serial, f1 int, f2 text); CREATE OR REPLACE FUNCTION sql_insert_returning(INOUT f1 int, INOUT f2 text, OUT id int) AS $$ INSERT INTO foo(f1, f2) VALUES ($1,$2) RETURNING f1, f2, f0 $$ LANGUAGE SQL; SELECT * FROM sql_insert_returning(1,’a’); f1 | f2 | id ----+----+---- 1 | a | 1 (1 row) Joe Conway SCALE10X-PGDay
  • 22. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Composite Argument CREATE TABLE emp (name text, salary numeric, age integer, cubicle point); CREATE FUNCTION double_salary(emp) RETURNS numeric AS $$ SELECT $1.salary * 2 AS salary; $$ LANGUAGE SQL; SELECT name, double_salary(emp.*) AS dream FROM emp WHERE emp.cubicle ~= point ’(2,1)’; SELECT name, double_salary(ROW(name, salary*1.1, age, cubicle)) AS dream FROM emp; Joe Conway SCALE10X-PGDay
  • 23. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Polymorphic CREATE FUNCTION myappend(anyarray, anyelement) RETURNS anyarray AS $$ SELECT $1 || $2; $$ LANGUAGE SQL; SELECT myappend(ARRAY[42,6], 21), myappend(ARRAY[’abc’,’def’], ’xyz’); myappend | myappend -----------+--------------- {42,6,21} | {abc,def,xyz} (1 row) Joe Conway SCALE10X-PGDay
  • 24. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Target List versus FROM Clause CREATE FUNCTION new_emp() RETURNS emp AS $$ SELECT ROW(’None’, 1000.0, 25, ’(2,2)’)::emp; $$ LANGUAGE SQL; SELECT new_emp(); new_emp -------------------------- (None,1000.0,25,"(2,2)") SELECT * FROM new_emp(); name | salary | age | cubicle ------+--------+-----+--------- None | 1000.0 | 25 | (2,2) SELECT (new_emp()).name; name ------ None Joe Conway SCALE10X-PGDay
  • 25. Overview Function Basics By Example SQL Functions PL/pgSQL Functions VARIADIC CREATE FUNCTION mleast(VARIADIC numeric[]) RETURNS numeric AS $$ SELECT min($1[i]) FROM generate_subscripts($1, 1) g(i); $$ LANGUAGE SQL; SELECT mleast(10, -1, 5, 4.4); mleast -------- -1 (1 row) SELECT mleast(42, 6, 42.42); mleast -------- 6 (1 row) Joe Conway SCALE10X-PGDay
  • 26. Overview Function Basics By Example SQL Functions PL/pgSQL Functions DEFAULT Arguments CREATE FUNCTION foo(a int, b int DEFAULT 2, c int DEFAULT 3) RETURNS int LANGUAGE SQL AS $$SELECT $1 + $2 + $3$$; SELECT foo(10, 20, 30); foo ----- 60 (1 row) SELECT foo(10, 20); foo ----- 33 (1 row) Joe Conway SCALE10X-PGDay
  • 27. Overview Function Basics By Example SQL Functions PL/pgSQL Functions PL/pgSQL PL/pgSQL is SQL plus procedural elements variables if/then/else loops cursors error checking Loading the language handler into a database: createlang plpgsql dbname http://www.postgresql.org/docs/9.1/interactive/plpgsql.html Joe Conway SCALE10X-PGDay
  • 28. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Simple CREATE OR REPLACE FUNCTION sum (text, text) RETURNS text AS $$ BEGIN RETURN $1 || ’ ’ || $2; END; $$ LANGUAGE plpgsql; SELECT sum(’hello’, ’world’); sum ------------- hello world (1 row) Joe Conway SCALE10X-PGDay
  • 29. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Parameter ALIAS CREATE OR REPLACE FUNCTION sum (int, int) RETURNS int AS $$ DECLARE i ALIAS FOR $1; j ALIAS FOR $2; sum int; BEGIN sum := i + j; RETURN sum; END; $$ LANGUAGE plpgsql; SELECT sum(41, 1); sum ----- 42 (1 row) Joe Conway SCALE10X-PGDay
  • 30. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Named Parameters CREATE OR REPLACE FUNCTION sum (i int, j int) RETURNS int AS $$ DECLARE sum int; BEGIN sum := i + j; RETURN sum; END; $$ LANGUAGE plpgsql; SELECT sum(41, 1); sum ----- 42 (1 row) Joe Conway SCALE10X-PGDay
  • 31. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Control Structures: IF ... CREATE OR REPLACE FUNCTION even (i int) RETURNS boolean AS $$ DECLARE tmp int; BEGIN tmp := i % 2; IF tmp = 0 THEN RETURN true; ELSE RETURN false; END IF; END; $$ LANGUAGE plpgsql; SELECT even(3), even(42); even | even ------+------ f | t (1 row) Joe Conway SCALE10X-PGDay
  • 32. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Control Structures: FOR ... LOOP CREATE OR REPLACE FUNCTION factorial (i numeric) RETURNS numeric AS $$ DECLARE tmp numeric; result numeric; BEGIN result := 1; FOR tmp IN 1 .. i LOOP result := result * tmp; END LOOP; RETURN result; END; $$ LANGUAGE plpgsql; SELECT factorial(42::numeric); factorial ------------------------------------------------------ 1405006117752879898543142606244511569936384000000000 (1 row) Joe Conway SCALE10X-PGDay
  • 33. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Control Structures: WHILE ... LOOP CREATE OR REPLACE FUNCTION factorial (i numeric) RETURNS numeric AS $$ DECLARE tmp numeric; result numeric; BEGIN result := 1; tmp := 1; WHILE tmp <= i LOOP result := result * tmp; tmp := tmp + 1; END LOOP; RETURN result; END; $$ LANGUAGE plpgsql; SELECT factorial(42::numeric); factorial ------------------------------------------------------ 1405006117752879898543142606244511569936384000000000 (1 row) Joe Conway SCALE10X-PGDay
  • 34. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Recursive CREATE OR REPLACE FUNCTION factorial (i numeric) RETURNS numeric AS $$ BEGIN IF i = 0 THEN RETURN 1; ELSIF i = 1 THEN RETURN 1; ELSE RETURN i * factorial(i - 1); END IF; END; $$ LANGUAGE plpgsql; SELECT factorial(42::numeric); factorial ------------------------------------------------------ 1405006117752879898543142606244511569936384000000000 (1 row) Joe Conway SCALE10X-PGDay
  • 35. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Record types CREATE OR REPLACE FUNCTION format () RETURNS text AS $$ DECLARE tmp RECORD; BEGIN SELECT INTO tmp 1 + 1 AS a, 2 + 2 AS b; RETURN ’a = ’ || tmp.a || ’; b = ’ || tmp.b; END; $$ LANGUAGE plpgsql; select format(); format -------------- a = 2; b = 4 (1 row) Joe Conway SCALE10X-PGDay
  • 36. Overview Function Basics By Example SQL Functions PL/pgSQL Functions PERFORM CREATE OR REPLACE FUNCTION func_w_side_fx() RETURNS void AS $$ INSERT INTO foo VALUES (41),(42) $$ LANGUAGE sql; CREATE OR REPLACE FUNCTION dummy () RETURNS text AS $$ BEGIN PERFORM func_w_side_fx(); RETURN ’OK’; END; $$ LANGUAGE plpgsql; SELECT dummy(); SELECT * FROM foo; f1 ---- 41 42 (2 rows) Joe Conway SCALE10X-PGDay
  • 37. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Dynamic SQL CREATE OR REPLACE FUNCTION get_foo(i int) RETURNS foo AS $$ DECLARE rec RECORD; BEGIN EXECUTE ’SELECT * FROM foo WHERE f1 = ’ || i INTO rec; RETURN rec; END; $$ LANGUAGE plpgsql; SELECT * FROM get_foo(42); f1 ---- 42 (1 row) Joe Conway SCALE10X-PGDay
  • 38. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Cursors CREATE OR REPLACE FUNCTION totalbalance() RETURNS numeric AS $$ DECLARE tmp RECORD; result numeric; BEGIN result := 0.00; FOR tmp IN SELECT * FROM foo LOOP result := result + tmp.f1; END LOOP; RETURN result; END; $$ LANGUAGE plpgsql; SELECT totalbalance(); totalbalance -------------- 83.00 (1 row) Joe Conway SCALE10X-PGDay
  • 39. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Error Handling CREATE OR REPLACE FUNCTION safe_add(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; EXCEPTION WHEN numeric_value_out_of_range THEN -- do some important stuff RETURN -1; WHEN OTHERS THEN -- do some other important stuff RETURN -1; END; $$ LANGUAGE plpgsql; http://www.postgresql.org/docs/9.1/interactive/errcodes-appendix.html Joe Conway SCALE10X-PGDay
  • 40. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Nested Exception Blocks CREATE FUNCTION merge_db(key integer, data text) RETURNS void AS $$ BEGIN LOOP UPDATE db SET b = data WHERE a = key; IF found THEN RETURN; END IF; BEGIN INSERT INTO db (a, b) VALUES (key, data); RETURN; EXCEPTION WHEN unique_violation THEN -- do nothing END; END LOOP; EXCEPTION WHEN OTHERS THEN -- do something else END; $$ LANGUAGE plpgsql; Joe Conway SCALE10X-PGDay
  • 41. Overview Function Basics By Example SQL Functions PL/pgSQL Functions Thank You Questions? Joe Conway SCALE10X-PGDay