Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Some function/stored procedure tips 4

Status
Not open for further replies.

newbiepg

Programmer
Nov 6, 2002
181
IN
before you start using plpgsql you need to initialize the call handler with this command

create function plpgsql_call_handler()
returns opaque as '/usr/lib/pgsql/plpgsql.so' language 'c'


if you are not sure where the plpgsql.so file is
type locate plpgsql.so at the command line

after initializing the call handler you need to add the plpgsql language . This can be done by typing

create function plpgsql_call_handler()
returns opaque
as '/usr/local/pgsql/plpgsql.so'
language 'c'



the basic function syntax is
create function function_name() returns int4 as'
declare
abc integer ; --declare a variable without assigning a value
def varchar ;=''mystring''; --declare a variable with a value

begin

insert into some_table (col1) values (10);
return 10;
end;
'language 'plpgsql';


a function always has to return a value
you can return any normal data type boolean, text, varchar, integer, double
date, time , void etc
a trigger normally returns opaque

you can also pass parameters with functions, a single function can have
upto 16 parameters.

aliases are used to call parameters values passed. The first parameter is $1, the second is $2
and so on.

you may also declare constant variables and variables with default values

this is an example of using functions with parameters

create function sec_func(int4,int4,int8,text,varchar) returns int4 as'
begin
myint constant integer := 5;
mystring char default ''T'';
firstint alias for $1;
secondint alias for $2;
third alias for $3;
fourth alias for $4;
fifth alias for $5;
ret_val int4;

select into ret_val employee_id from masters where code_id = firstint and dept_id = secondint;
return ret_value;
end;
'language 'plpgsql';

this function need to be called by
select sec_func(3,4,cast(5 as int8),cast('trial text' as text),'some text');

note that numbers passed as parameters are by default int4
so we need to cast 5 int int8 or bigint.

%type and %rowtype
------------------

sometimes we need to declare a variable according to the datatype of a column
this can be achoieved using %type

create function third_func(text) returns varchar as'
declare
fir_text alias for $1;
sec_text mytable.last_name%type;
--here in the line above will assign the variable sec_text the datatype of
--of table mytable and column last_name.
begin

--some code here

end;
'language 'plpgsql';

sometimes we need to get the structure of a table's row
we use %rowtype then

e.g.

create function third(int4) returns varchar as'

begin

myvar alias for $1;
mysecvar mytable%rowtype;
mythirdvar varchar;

begin

select into mysecvar * from mytable where code_id = myvar;
--now mysecvar is a recordset
mythirdvar := mysecvar.first_name|| ' '|| mysecvar.last_name;
--|| is the concatenation symbol
--first_name and last_name are columns in the table mytable
return mythirdvar;
end;
'language 'plpgsql';


looping syntax
---------------

there is the if/then/end if loop

if some condition then
do something;
end if;

otherwise
if some condition then
do something;
else
some more code;
end if;

there is also the loop /end loop statement

loop
some code;
end loop;


the while- loop statement is like this
while myvar<10 loop
some more code;
end loop;


displaying messages
-------------------

wee can display messages in the output we can use the raise debug ,raise notice and raise exception
statements

the differences are mostly in the log files and severity.
raise debug can be viewed by the client and ignored by the database in production mode.
raise notice gets a mention in the postgresql log and is seen by the client. raise exception causes both
mentioned before and also causes a transaction to rollback.

a simple example is given below

create function mess() returns varchar as'
declare
myret :=''done'';
begin
raise notice ''hello there'';
raise debug ''this is the debug message'';
raise exception ''this is the exception message'';

return myret;
end;
'language 'plpgsql';


call the function
select mess();
you will get some messages shown at the console too.

nesting and recursive functions
---------------------------------

as of 7.1.3 version the database did not support recursive functions
however it is possible to call a function inside another without returning a value

to do this we need to use

e.g.

create function test(int4,int4,int4) returns int4 as'
declare
first alias for $1;
sec alias for $2;
third alias for $3;

perform another_funct(first,sec);
return (first + sec);

end;
'language 'plpgsq';

not confused yet?

go to this link this has some great functions including functions in c;
 
Hi there:

thanx for your post, very useful. I have a question, Do you know how to see the code of a function once it has been created. something like \d but with functions?

thanks in advance...
 
Hi pedrosolorzano,

You can install a front end administrative tool like phpPgAdmin 3.0 which is a web based php front end administrative tool for a Postgres database. My guess is that the functions are part of a postgres system table like pg_* which could be queried to display functions, but I'm not sure what the table name would be. PhpPgAdmin 3.0 allow you to display, edit, or create functions.

Regards,

LelandJ

Leland F. Jackson, CPA
Software - Master (TM)
Nothing Runs Like the Fox
 
you can also try pg_dump
this wont allow you to edit the functions
phpPgAdmin is the best solution
 
if the function is in sql ot plpgsql you can use

select prosrc, pronargs, prorettype, proargtypes from pg_proc where proname = 'yourfunction_name';

prosrc will be the code
pronargs the number of arguments
prorettype the oid of the return type of the function you should see it in pg_type relation
proargtypes is a vector of oids of the types of the arguments, again in pg_type you can see the details

for more details see here and for all the catalogs here
 
about the \d commands

\df -> command lists all functions
\df your_function -> gives info about your_function
\df+ -> more info about all function (includes the code if interpreted language)
\df+ your_function -> gives more info about your_function (includes the code for interpreted languages)
 
Thankx so much to everybody.

\df+ works perfect, and seems to be an alias to

select prosrc, pronargs, prorettype, proargtypes from pg_proc where proname = 'yourfunction_name';

Thanks again ceco.

Pedro Andrés Solorzano
Pontificia Universidad Javeriana
Bogotá, Colombia, SurAmérica.

Let me know if this post have actually helped you, by clicking the purple star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top