--
|
-- Miscellaneous topics
|
--
|
-- Verify that we can parse new-style CREATE FUNCTION/PROCEDURE
|
do
|
$$
|
declare procedure int; -- check we still recognize non-keywords as vars
|
begin
|
create function test1() returns int
|
begin atomic
|
select 2 + 2;
|
end;
|
create or replace procedure test2(x int)
|
begin atomic
|
select x + 2;
|
end;
|
end
|
$$;
|
\sf test1
|
CREATE OR REPLACE FUNCTION public.test1()
|
RETURNS integer
|
LANGUAGE sql
|
BEGIN ATOMIC
|
SELECT (2 + 2);
|
END
|
\sf test2
|
CREATE OR REPLACE PROCEDURE public.test2(IN x integer)
|
LANGUAGE sql
|
BEGIN ATOMIC
|
SELECT (x + 2);
|
END
|
-- Test %TYPE and %ROWTYPE error cases
|
create table misc_table(f1 int);
|
do $$ declare x foo%type; begin end $$;
|
ERROR: variable "foo" does not exist
|
CONTEXT: compilation of PL/pgSQL function "inline_code_block" near line 1
|
do $$ declare x notice%type; begin end $$; -- covers unreserved-keyword case
|
ERROR: variable "notice" does not exist
|
CONTEXT: compilation of PL/pgSQL function "inline_code_block" near line 1
|
do $$ declare x foo.bar%type; begin end $$;
|
ERROR: relation "foo" does not exist
|
CONTEXT: compilation of PL/pgSQL function "inline_code_block" near line 1
|
do $$ declare x foo.bar.baz%type; begin end $$;
|
ERROR: schema "foo" does not exist
|
CONTEXT: compilation of PL/pgSQL function "inline_code_block" near line 1
|
do $$ declare x public.foo.bar%type; begin end $$;
|
ERROR: relation "public.foo" does not exist
|
CONTEXT: compilation of PL/pgSQL function "inline_code_block" near line 1
|
do $$ declare x public.misc_table.zed%type; begin end $$;
|
ERROR: column "zed" of relation "misc_table" does not exist
|
CONTEXT: compilation of PL/pgSQL function "inline_code_block" near line 1
|
do $$ declare x foo%rowtype; begin end $$;
|
ERROR: relation "foo" does not exist
|
CONTEXT: compilation of PL/pgSQL function "inline_code_block" near line 1
|
do $$ declare x notice%rowtype; begin end $$; -- covers unreserved-keyword case
|
ERROR: relation "notice" does not exist
|
CONTEXT: compilation of PL/pgSQL function "inline_code_block" near line 1
|
do $$ declare x foo.bar%rowtype; begin end $$;
|
ERROR: schema "foo" does not exist
|
CONTEXT: compilation of PL/pgSQL function "inline_code_block" near line 1
|
do $$ declare x foo.bar.baz%rowtype; begin end $$;
|
ERROR: cross-database references are not implemented: "foo.bar.baz"
|
CONTEXT: compilation of PL/pgSQL function "inline_code_block" near line 1
|
do $$ declare x public.foo%rowtype; begin end $$;
|
ERROR: relation "public.foo" does not exist
|
CONTEXT: compilation of PL/pgSQL function "inline_code_block" near line 1
|
do $$ declare x public.misc_table%rowtype; begin end $$;
|
-- Test handling of an unreserved keyword as a variable name
|
-- and record field name.
|
do $$
|
declare
|
execute int;
|
r record;
|
begin
|
execute := 10;
|
raise notice 'execute = %', execute;
|
select 1 as strict into r;
|
raise notice 'r.strict = %', r.strict;
|
end $$;
|
NOTICE: execute = 10
|
NOTICE: r.strict = 1
|
-- Test handling of a reserved keyword as a record field name.
|
do $$ declare r record;
|
begin
|
select 1 as x, 2 as foreach into r;
|
raise notice 'r.x = %', r.x;
|
raise notice 'r.foreach = %', r.foreach; -- fails
|
end $$;
|
NOTICE: r.x = 1
|
ERROR: field name "foreach" is a reserved key word
|
LINE 1: r.foreach
|
^
|
HINT: Use double quotes to quote it.
|
QUERY: r.foreach
|
CONTEXT: PL/pgSQL function inline_code_block line 5 at RAISE
|
do $$ declare r record;
|
begin
|
select 1 as x, 2 as foreach into r;
|
raise notice 'r.x = %', r.x;
|
raise notice 'r."foreach" = %', r."foreach"; -- ok
|
end $$;
|
NOTICE: r.x = 1
|
NOTICE: r."foreach" = 2
|