postgresql manual plpgsql function declaration section not understand - plpgsql

https://www.postgresql.org/docs/14/plpgsql-declarations.html#PLPGSQL-DECLARATION-PARAMETERS
CREATE FUNCTION concat_selected_fields(in_t sometablename) RETURNS text AS $$
BEGIN
RETURN in_t.f1 || in_t.f3 || in_t.f5 || in_t.f7;
END;
$$ LANGUAGE plpgsql;
is in_t alias sometable alias name? I try to replicate it by following:
create function conca_selected_col_emp( in_t public.emp) returns text as $$
begin
return in_t.name || in_t.department;
end;
$$ LANGUAGE plpgsql;
Obviously, this will not work...

It should to work:
create table foo(a varchar, b varchar);
insert into foo values('hello', 'world');
create or replace function fx(arg foo)
returns varchar as $$
begin
return arg.a || ', ' || arg.b;
end;
$$ language plpgsql;
postgres=# select fx(foo) from foo;
┌──────────────┐
│ fx │
╞══════════════╡
│ hello, world │
└──────────────┘
(1 row)
or alternative syntax:
postgres=# select foo.fx from foo;
┌──────────────┐
│ fx │
╞══════════════╡
│ hello, world │
└──────────────┘
(1 row)

Related

Postgres session variables not working inside a function

I am setting a session variable inside a postgres function and the values are not getting set.
Any help is most appreciated. Thanks in advance.
I am using "PostgreSQL 10.6, compiled by Visual C++ build 1800, 64-bit"
My code is as follows:
The function:
CREATE FUNCTION set_rp_vals(iv_rp_company varchar, iv_rp_portfolio varchar)
RETURNS integer
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
DECLARE
l_retval integer;
BEGIN
l_retval := 1;
RAISE NOTICE '1.iv_rp_company: >>> %', iv_rp_company;
RAISE NOTICE '2.iv_rp_portfolio: >>> %', iv_rp_portfolio;
--set the session variable
set rp.company = iv_rp_company;
set rp.portfolio = iv_rp_portfolio;
RETURN l_retval;
EXCEPTION
WHEN OTHERS THEN
RETURN 9;
END;
$function$
;
The function call:
SELECT set_rp_vals(iv_rp_company := 'COMPAN',iv_rp_portfolio := 'PORTOF');
--Retrieving the session variables:
select
current_setting('rp.company') as company,
current_setting('rp.portfolio') as portfolio;
The value returned by the above query:
I would use the set_config() function for this:
CREATE OR REPLACE FUNCTION set_rp_vals(iv_rp_company varchar, iv_rp_portfolio varchar)
RETURNS integer
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$
DECLARE
l_retval integer;
BEGIN
l_retval := 1;
RAISE NOTICE '1.iv_rp_company: >>> %', iv_rp_company;
RAISE NOTICE '2.iv_rp_portfolio: >>> %', iv_rp_portfolio;
--set the session variable
perform set_config('rp.company', iv_rp_company, false);
perform set_config('rp.portfolio', iv_rp_portfolio, false);
RETURN l_retval;
EXCEPTION
WHEN OTHERS THEN
RETURN 9;
END;
$function$
;
CREATE FUNCTION
Execute with your values:
SELECT set_rp_vals(iv_rp_company := 'COMPAN',iv_rp_portfolio := 'PORTOF');
NOTICE: 1.iv_rp_company: >>> COMPAN
NOTICE: 2.iv_rp_portfolio: >>> PORTOF
┌─────────────┐
│ set_rp_vals │
├─────────────┤
│ 1 │
└─────────────┘
(1 row)
select
current_setting('rp.company') as company,
current_setting('rp.portfolio') as portfolio;
┌─────────┬───────────┐
│ company │ portfolio │
├─────────┼───────────┤
│ COMPAN │ PORTOF │
└─────────┴───────────┘
(1 row)

Pl/Pgsql, Passing array argument to INSERT

Say I have a function with a text array parameter TEXT[]. If I do a EXECUTE FORMAT INSERT, how do I pass a quoted text string of that array to insert?
You should to use USING clause. The dynamic SQL can use a parameters on usual places (non SQL identifiers):
CREATE TABLE foo(a varchar[]);
CREATE OR REPLACE FUNCTION public.fx(tblname text, VARIADIC p character varying[])
RETURNS void LANGUAGE plpgsql AS $function$
BEGIN
EXECUTE format('insert into %I(a) VALUES($1)', tblname) USING p;
END;
$function$
SELECT fx('foo', 'Hi','Hello');
SELECT fx('foo', 'Hi','Hel''lo');
SELECT fx('foo', 'Hi','Hel"lo');
postgres=# SELECT * FROM foo;
┌────────────────┐
│ a │
╞════════════════╡
│ {Hi,Hel'lo} │
│ {Hi,"Hel\"lo"} │
│ {Hi,Hello} │
└────────────────┘
(3 rows)
Thank you, I now know when to use using, and format. Here's my revised code:
CREATE OR REPLACE FUNCTION add_property(catid INT, colname TEXT,
ty catalog_column_type, colval TEXT[])
RETURNS jsonb AS $$
DECLARE
tn TEXT;
BEGIN
--check table exists
SELECT tablename INTO tn FROM catalog WHERE catalogid=catid;
IF NOT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name=tn)
THEN
return jsonb_build_object('error','notable');
END IF;
--check if property exists for table
IF EXISTS(SELECT 1 FROM catalog_columns WHERE catalogid=catid AND
columnname=colname) THEN
return jsonb_build_object('error','exists');
END IF;
IF ty='INT'::catalog_column_type THEN
EXECUTE FORMAT ('ALTER TABLE %I ADD COLUMN %I INT',tn,colname);
ELSIF ty='TEXT'::catalog_column_type THEN
EXECUTE FORMAT ('ALTER TABLE %I ADD COLUMN %I TEXT',tn,colname);
ELSIF ty='ENUM'::catalog_column_type THEN
EXECUTE FORMAT ('ALTER TABLE %I ADD COLUMN %I INT',tn,colname);
ELSIF ty='BOOLEAN'::catalog_column_type THEN
EXECUTE FORMAT ('ALTER TABLE %I ADD COLUMN %I BOOLEAN',tn,colname);
END IF;
EXECUTE 'INSERT INTO catalog_columns (catalogid,columnname,'
|| 'columntype,columnnvalues) VALUES ($1,$2,$3,$4)' USING catid,colname,
ty,colval;
return jsonb_build_object('error','OK');
END;
$$ LANGUAGE plpgsql;

Passing Dynamic number of Parameters to Function

I need to pass dynamic number of parameters to a function as well as their data types and then return a table having those parameters as fields.
Is that possible to do that in postgres?
Any idea or example is appreciated
Here is an example that could probably be improved.
Beware of SQL injection!
CREATE OR REPLACE FUNCTION create_table(
tabname text,
VARIADIC coldef text[]
) RETURNS void
LANGUAGE plpgsql STRICT AS
$$DECLARE
l integer;
i integer;
sql text;
sep text := '';
BEGIN
l := array_upper(coldef, 1);
IF l % 2 <> 0 THEN
RAISE EXCEPTION 'Number of arguments must be odd';
END IF;
sql := 'CREATE TABLE ' || quote_ident(tabname) || '(';
FOR i IN 1 .. l/2 LOOP
sql := sql || sep || quote_ident(coldef[2*i-1]) || ' ' || quote_ident(coldef[2*i]);
sep := ', ';
END LOOP;
sql := sql || ')';
EXECUTE sql;
END;$$;
It can be used like this:
test=> SELECT create_table('tabname', 'col1', 'int4', 'col2', 'text');
test=> \d tabname
Table "laurenz.tabname"
┌────────┬─────────┬───────────┐
│ Column │ Type │ Modifiers │
├────────┼─────────┼───────────┤
│ col1 │ integer │ │
│ col2 │ text │ │
└────────┴─────────┴───────────┘

When a syntax error occurs in the declaration declare postgresql

CREATE OR REPLACE FUNCTION fn_SplitArrayStr( anyelement , anyelement )
RETURNS anyarray
LANGUAGE SQL
AS $$
DECLARE f1 text , f2 text ;
BEGIN
f1 := $1::text ;
f2 := $2::text ;
SELECT * FROM UNNEST( string_to_array(f1, f2) ) as c1 ;
END ;
$$;
ERROR : Syntax error at or near "text"
LINE 2 : DECLARE f1 text , f2 text ;
How do I change?
I see two issues:
wrong language specification - PostgreSQL native procedural language is plpgsql.
DECLARE statement uses semicolon for separation between individual variable declarations.
postgres=# create or replace function foo()
returns int as $$
declare a int; b int;
begin
a := 10; b := 20;
return a + b;
end;
$$ language plpgsql;
CREATE FUNCTION
postgres=# select foo();
┌─────┐
│ foo │
╞═════╡
│ 30 │
└─────┘
(1 row)
You can try to specify the language at the end of the procedure
$$
LANGUAGE plpgsql;
You've written PL/PgSQL code but marked it LANGUAGE SQL. Use LANGUAGE plpgsql if you're writing pl/pgsql functions.

PL/pgSQL function does not return custom (record) types as expected

My function's job is to extract XML nodes. The code is as follows:
CREATE TYPE xml_node_looper_record AS (
allomany xml,
i integer,
actual_node text,
nodi_parts text[]
);
CREATE OR REPLACE FUNCTION ds.xml_node_looper_rec(rec xml_node_looper_record)
RETURNS SETOF xml_node_looper_record AS
$BODY$
DECLARE
nodes text[];
field_val text;
r xml_node_looper_record;
n integer;
BEGIN
nodes = xpath(rec.actual_node, rec.allomany);
IF nodes[1] IS NOT NULL THEN
rec.i = rec.i + 1;
FOR n IN 1..array_upper(nodes, 1) LOOP
IF rec.i = array_upper(rec.nodi_parts, 1) THEN
field_val = trim(ARRAY[xpath(rec.actual_node || '/text()', rec.allomany)]::text, ' {}"');
IF field_val IS NOT NULL AND field_val != '' THEN
RAISE NOTICE '% % % %', n, rec.actual_node, rec.i, field_val;
RETURN NEXT (NULL::xml, rec.i, rec.actual_node, ARRAY[field_val]::text[]);
END IF;
END IF;
SELECT ds.xml_node_looper_rec((rec.allomany, rec.i, rec.actual_node || '[' || n::text || ']' || rec.nodi_parts[rec.i + 1], rec.nodi_parts)) INTO r;
END LOOP;
END IF;
END;
$BODY$
LANGUAGE plpgsql VOLATILE COST 100;
As you can see the function is recursive and the goal is to collect field values from multiple XML nodes where we have no information, how many nodes we have under a tag. (I have a version with return table, but that method is too slow.) I use my own defined custom type, and when I check the return values with RAISE NOTICE, I can see the result in pgAdmin on the Messages tab, but the RETURN NEXT command returns nothing, only an empty table.
The parameters of my type:
allomany: an XML data
i: actual depth of nodi_parts
actual_node: XML node, I would like to extract. Can have multiple
nodes, I mark them with []. For example:
/tagone/tagtwo[]/tagthree[]/fieldname
nodi_parts: coming from actual_node, splitting it with []. For
example ARRAY["/tagone/tagtwo", "/tagthree", "/fieldname"]
What is the problem?
You don't propagate the result of nested calls. RETURN NEXT pushes a result to the stack related to the function call. But this stack is private - if caller doesn't fetch this stack, then the result is cleaned. Anyway - any function instance (the called function) has its own result stack. This stack is not shared.
The recursive table function in PL/pgSQL should to look like:
postgres=# CREATE OR REPLACE FUNCTION foo(level int) RETURNS SETOF int AS $$
BEGIN
IF level > 5 THEN RETURN; END IF;
RETURN NEXT level;
--!! must to take result of nested call
RETURN QUERY SELECT * FROM foo(level + 1);
RETURN;
END;
$$ LANGUAGE plpgsql;
postgres=# SELECT * FROM foo(1);
┌─────┐
│ foo │
╞═════╡
│ 1 │
│ 2 │
│ 3 │
│ 4 │
│ 5 │
└─────┘
(5 rows)
Your code is a equivalent of code:
postgres=# CREATE OR REPLACE FUNCTION foo(level int) RETURNS SETOF int AS $$
BEGIN
IF level > 5 THEN RETURN; END IF;
RETURN NEXT level;
-- error, only call of nested function, but returned table is lost
PERFORM foo(level + 1);
RETURN;
END;
$$ LANGUAGE plpgsql;
postgres=# SELECT * FROM foo(1);
┌─────┐
│ foo │
╞═════╡
│ 1 │
└─────┘
(1 row)