linkend="plpgsql-quote-tips">.
Here is an
Oracle PL/SQL> function:
-CREATE OR REPLACE FUNCTION cs_fmt_browser_version(v_name IN varchar,
- v_version IN varchar)
+CREATE OR REPLACE FUNCTION cs_fmt_browser_version(v_name varchar,
+ v_version varchar)
RETURN varchar IS
BEGIN
IF v_version IS NULL THEN
- Let's go through this function and see the differences to
PL/pgSQL>:
+ Let's go through this function and see the differences compared to
-
- IN, OUT, and
- INOUT parameters passed to functions.
- INOUT, for example, means that the
- parameter will receive a value and return
- another.
PostgreSQL> only has IN
- parameters, and hence there is no specification of the parameter kind.
-
-
-
The RETURN key word in the function
|| ' LANGUAGE plpgsql;' ;
EXECUTE func_cmd;
- RETURN;
END;
$func$ LANGUAGE plpgsql;
shows how to port a function
with OUT> parameters and string manipulation.
-
PostgreSQL> does not have an
- instr function, but you can work around it
+
PostgreSQL> does not have a built-in
+ instr function, but you can create one
using a combination of other
functions.
instr> In
linkend="plpgsql-porting-appendix"> there is a
The following
Oracle PL/SQL procedure is used
to parse a URL and return several elements (host, path, and query).
- In
PostgreSQL>, functions can return only one value.
- One way to work around this is to make the return value a composite
- type (row type).
Here is a possible translation into
PL/pgSQL>:
-CREATE TYPE cs_parse_url_result AS (
- v_host VARCHAR,
- v_path VARCHAR,
- v_query VARCHAR
-);
-
-CREATE OR REPLACE FUNCTION cs_parse_url(v_url VARCHAR)
-RETURNS cs_parse_url_result AS $$
+CREATE OR REPLACE FUNCTION cs_parse_url(
+ v_url IN VARCHAR,
+ v_host OUT VARCHAR, -- This will be passed back
+ v_path OUT VARCHAR, -- This one too
+ v_query OUT VARCHAR) -- And this one
+AS $$
DECLARE
- res cs_parse_url_result;
a_pos1 INTEGER;
a_pos2 INTEGER;
BEGIN
- res.v_host := NULL;
- res.v_path := NULL;
- res.v_query := NULL;
+ v_host := NULL;
+ v_path := NULL;
+ v_query := NULL;
a_pos1 := instr(v_url, '//');
IF a_pos1 = 0 THEN
- RETURN res;
+ RETURN;
END IF;
a_pos2 := instr(v_url, '/', a_pos1 + 2);
IF a_pos2 = 0 THEN
- res.v_host := substr(v_url, a_pos1 + 2);
- res.v_path := '/';
- RETURN res;
+ v_host := substr(v_url, a_pos1 + 2);
+ v_path := '/';
+ RETURN;
END IF;
- res.v_host := substr(v_url, a_pos1 + 2, a_pos2 - a_pos1 - 2);
+ v_host := substr(v_url, a_pos1 + 2, a_pos2 - a_pos1 - 2);
a_pos1 := instr(v_url, '?', a_pos2 + 1);
IF a_pos1 = 0 THEN
- res.v_path := substr(v_url, a_pos2);
- RETURN res;
+ v_path := substr(v_url, a_pos2);
+ RETURN;
END IF;
- res.v_path := substr(v_url, a_pos2, a_pos1 - a_pos2);
- res.v_query := substr(v_url, a_pos1 + 1);
- RETURN res;
+ v_path := substr(v_url, a_pos2, a_pos1 - a_pos2);
+ v_query := substr(v_url, a_pos1 + 1);
END;
$$ LANGUAGE plpgsql;
WHEN unique_violation THEN
-- don't worry if it already exists
END;
-
- RETURN;
END;
$$ LANGUAGE plpgsql;
* procedural language
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.68 2005/04/05 18:05:46 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.69 2005/04/07 14:53:04 tgl Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
else if (plpgsql_curr_compile->fn_rettype == VOIDOID)
{
if (yylex() != ';')
- yyerror("function returning void cannot specify RETURN expression");
+ yyerror("RETURN cannot have a parameter in function returning void");
}
else if (plpgsql_curr_compile->fn_retistuple)
{
* procedural language
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_comp.c,v 1.86 2005/04/05 06:22:16 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_comp.c,v 1.87 2005/04/07 14:53:04 tgl Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
int parse_rc;
Oid rettypeid;
int numargs;
- int num_in_args;
- int num_out_args;
+ int num_in_args = 0;
+ int num_out_args = 0;
Oid *argtypes;
char **argnames;
char *argmodes;
/*
* Create the variables for the procedure's parameters.
*/
- num_in_args = num_out_args = 0;
for (i = 0; i < numargs; i++)
{
char buf[32];
parse_rc = plpgsql_yyparse();
if (parse_rc != 0)
elog(ERROR, "plpgsql parser returned %d", parse_rc);
+ function->action = plpgsql_yylval.program;
plpgsql_scanner_finish();
pfree(proc_source);
/*
- * If that was successful, complete the function's info.
+ * If it has OUT parameters or returns VOID or returns a set, we allow
+ * control to fall off the end without an explicit RETURN statement.
+ * The easiest way to implement this is to add a RETURN statement to the
+ * end of the statement list during parsing. However, if the outer block
+ * has an EXCEPTION clause, we need to make a new outer block, since the
+ * added RETURN shouldn't act like it is inside the EXCEPTION clause.
+ */
+ if (num_out_args > 0 || function->fn_rettype == VOIDOID ||
+ function->fn_retset)
+ {
+ if (function->action->exceptions != NIL)
+ {
+ PLpgSQL_stmt_block *new;
+
+ new = palloc0(sizeof(PLpgSQL_stmt_block));
+ new->cmd_type = PLPGSQL_STMT_BLOCK;
+ new->body = list_make1(function->action);
+
+ function->action = new;
+ }
+ if (function->action->body == NIL ||
+ ((PLpgSQL_stmt *) llast(function->action->body))->cmd_type != PLPGSQL_STMT_RETURN)
+ {
+ PLpgSQL_stmt_return *new;
+
+ new = palloc0(sizeof(PLpgSQL_stmt_return));
+ new->cmd_type = PLPGSQL_STMT_RETURN;
+ new->expr = NULL;
+ new->retvarno = function->out_param_varno;
+
+ function->action->body = lappend(function->action->body, new);
+ }
+ }
+
+ /*
+ * Complete the function's info
*/
function->fn_nargs = procStruct->pronargs;
for (i = 0; i < function->fn_nargs; i++)
function->datums = palloc(sizeof(PLpgSQL_datum *) * plpgsql_nDatums);
for (i = 0; i < plpgsql_nDatums; i++)
function->datums[i] = plpgsql_Datums[i];
- function->action = plpgsql_yylval.program;
/* Debug dump for completed functions */
if (plpgsql_DumpExecTree)
* procedural language
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.134 2005/04/05 06:22:16 tgl Exp $
+ * $PostgreSQL: pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.135 2005/04/07 14:53:04 tgl Exp $
*
* This software is copyrighted by Jan Wieck - Hamburg.
*
&(estate->retisnull),
&(estate->rettype));
}
+
+ return PLPGSQL_RC_RETURN;
}
+ /*
+ * Special hack for function returning VOID: instead of NULL, return a
+ * non-null VOID value. This is of dubious importance but is kept for
+ * backwards compatibility. Note that the only other way to get here
+ * is to have written "RETURN NULL" in a function returning tuple.
+ */
if (estate->fn_rettype == VOIDOID)
{
- /* Special hack for function returning VOID */
- Assert(stmt->retvarno < 0 && stmt->expr == NULL);
estate->retval = (Datum) 0;
estate->retisnull = false;
estate->rettype = VOIDOID;
(1 row)
--
--- Test handling of OUT parameters, including polymorphic cases
+-- Test handling of OUT parameters, including polymorphic cases.
+-- Note that RETURN is optional with OUT params; we try both ways.
--
-- wrong way to do it:
create function f1(in i int, out j int) returns int as $$
create or replace function f1(inout i int) as $$
begin
i := i+1;
- return;
end$$ language plpgsql;
select f1(42);
f1
j := i;
j := j+1;
k := 'foo';
- return;
end$$ language plpgsql;
select f1(42);
f1
j := j+1;
k := 'foot';
return next;
- return;
end$$ language plpgsql;
select * from f1(42);
j | k
begin
return 5;
end;$$ language plpgsql;
-ERROR: function returning void cannot specify RETURN expression at or near "5" at character 72
+ERROR: RETURN cannot have a parameter in function returning void at or near "5" at character 72
LINE 3: return 5;
^
+-- VOID functions are allowed to omit RETURN
+create function void_return_expr() returns void as $$
+begin
+ perform 2+2;
+end;$$ language plpgsql;
+select void_return_expr();
+ void_return_expr
+------------------
+
+(1 row)
+
+-- but ordinary functions are not
+create function missing_return_expr() returns int as $$
+begin
+ perform 2+2;
+end;$$ language plpgsql;
+select missing_return_expr();
+ERROR: control reached end of function without RETURN
+CONTEXT: PL/pgSQL function "missing_return_expr"
+drop function void_return_expr();
+drop function missing_return_expr();
SELECT * FROM test_ret_rec_dyn(5) AS (a int, b numeric, c text);
--
--- Test handling of OUT parameters, including polymorphic cases
+-- Test handling of OUT parameters, including polymorphic cases.
+-- Note that RETURN is optional with OUT params; we try both ways.
--
-- wrong way to do it:
create or replace function f1(inout i int) as $$
begin
i := i+1;
- return;
end$$ language plpgsql;
select f1(42);
j := i;
j := j+1;
k := 'foo';
- return;
end$$ language plpgsql;
select f1(42);
j := j+1;
k := 'foot';
return next;
- return;
end$$ language plpgsql;
select * from f1(42);
begin
return 5;
end;$$ language plpgsql;
+
+-- VOID functions are allowed to omit RETURN
+create function void_return_expr() returns void as $$
+begin
+ perform 2+2;
+end;$$ language plpgsql;
+
+select void_return_expr();
+
+-- but ordinary functions are not
+create function missing_return_expr() returns int as $$
+begin
+ perform 2+2;
+end;$$ language plpgsql;
+
+select missing_return_expr();
+
+drop function void_return_expr();
+drop function missing_return_expr();