-
+
PL/pgSQL - SQL Procedural Language
is with RETURNS TABLE>, for example:
-CREATE FUNCTION extended_sales(p_itemno int) RETURNS TABLE(quantity int, total numeric) AS $$
+CREATE FUNCTION extended_sales(p_itemno int)
+RETURNS TABLE(quantity int, total numeric) AS $$
BEGIN
- RETURN QUERY SELECT quantity, quantity * price FROM sales WHERE itemno = p_itemno;
+ RETURN QUERY SELECT quantity, quantity * price FROM sales
+ WHERE itemno = p_itemno;
END;
$$ LANGUAGE plpgsql;
-- Now "mviews" has one record from cs_materialized_views
- PERFORM cs_log('Refreshing materialized view ' || quote_ident(mviews.mv_name) || ' ...');
+ PERFORM cs_log('Refreshing materialized view '
+ || quote_ident(mviews.mv_name) || ' ...');
EXECUTE 'TRUNCATE TABLE ' || quote_ident(mviews.mv_name);
- EXECUTE 'INSERT INTO ' || quote_ident(mviews.mv_name) || ' ' || mviews.mv_query;
+ EXECUTE 'INSERT INTO '
+ || quote_ident(mviews.mv_name) || ' '
+ || mviews.mv_query;
END LOOP;
PERFORM cs_log('Done refreshing materialized views.');
OPEN FOR EXECUTE
-OPEN unbound_cursorvar NO SCROLL FOR EXECUTE query_string USING expression , ... ;
+OPEN unbound_cursorvar NO SCROLL FOR EXECUTE query_string
+ USING expression , ... ;
An example:
-OPEN curs1 FOR EXECUTE 'SELECT * FROM ' || quote_ident(tabname) ' WHERE col1 = $1' USING keyvalue;
+OPEN curs1 FOR EXECUTE 'SELECT * FROM ' || quote_ident(tabname)
+ || ' WHERE col1 = $1' USING keyvalue;
In this example, the table name is inserted into the query textually,
so use of quote_ident()> is recommended to guard against
END;
' LANGUAGE plpgsql;
+-- need to be in a transaction to use cursors.
BEGIN;
SELECT reffunc2();
This example will abort the transaction with the given error message
and hint:
-RAISE EXCEPTION 'Nonexistent ID --> %', user_id USING HINT = 'Please check your user id';
+RAISE EXCEPTION 'Nonexistent ID --> %', user_id
+ USING HINT = 'Please check your user id';
--
-- Function and trigger to amend summarized column(s) on UPDATE, INSERT, DELETE.
--
-CREATE OR REPLACE FUNCTION maint_sales_summary_bytime() RETURNS TRIGGER AS $maint_sales_summary_bytime$
+CREATE OR REPLACE FUNCTION maint_sales_summary_bytime() RETURNS TRIGGER
+AS $maint_sales_summary_bytime$
DECLARE
delta_time_key integer;
delta_amount_sold numeric(15,2);
-- (probably not too onerous, as DELETE + INSERT is how most
-- changes will be made).
IF ( OLD.time_key != NEW.time_key) THEN
- RAISE EXCEPTION 'Update of time_key : % -> % not allowed', OLD.time_key, NEW.time_key;
+ RAISE EXCEPTION 'Update of time_key : % -> % not allowed',
+ OLD.time_key, NEW.time_key;
END IF;
delta_time_key = OLD.time_key;
Another good way to develop in
PL/pgSQL> is with a
GUI database access tool that facilitates development in a
- procedural language. One example of such as a tool is
+ procedural language. One example of such a tool is
pgAdmin>, although others exist. These tools often
provide convenient features such as escaping single quotes and
making it easier to recreate and debug functions.
IF a_running_job_count > 0 THEN
COMMIT; -- free lock
- raise_application_error(-20000, 'Unable to create a new job: a job is currently running.');
+ raise_application_error(-20000,
+ 'Unable to create a new job: a job is currently running.');
END IF;
DELETE FROM cs_active_job;