Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
src/pl/plpgsql/src/sql/plpgsql_record.sql
@@ -414,6 +414,140 @@
select sillyaddtwo(42);
select sillyaddtwo(43);
-- check that a record variable's declared type is re-resolved by name, so a
-- variable can come to mean a different type while the function is running.
-- The re-resolution happens only once the type cache entry the variable was
-- compiled against has been invalidated, so which of the two results below
-- you get depends on whether something invalidated it in the meantime.
create schema recmut1;
create schema recmut2;
create type recmut1.rtype as (a int);
-- Without an invalidation the variable keeps the type it was compiled with,
-- and assigning the wider value silently drops the extra column.  Pin
-- debug_discard_caches, otherwise this case does not reach the assignment
-- with a valid type cache entry.
set debug_discard_caches = 0;
set search_path = recmut1, public;
do $$
declare r rtype;                               -- compiled against recmut1.rtype
begin
  create type recmut2.rtype as (a int, b int);
  set search_path = recmut2, recmut1, public;  -- "rtype" now names recmut2.rtype
  r := row(2,3)::recmut2.rtype;
  raise notice 'without invalidation: %', r;
end$$;
reset search_path;
reset debug_discard_caches;
drop type recmut2.rtype;
-- With one, the same code follows the name to the other type instead.
set search_path = recmut1, public;
do $$
declare r rtype;
begin
  create type recmut2.rtype as (a int, b int);
  set search_path = recmut2, recmut1, public;
  alter type recmut1.rtype rename attribute a to a2;
  alter type recmut1.rtype rename attribute a2 to a;
  r := row(2,3)::recmut2.rtype;
  raise notice 'with invalidation:    %', r;
end$$;
reset search_path;
drop schema recmut2 cascade;
drop schema recmut1 cascade;
-- Assignment between different descriptor vintages.  When the attribute types
-- and tuple layout are unchanged, it would be OK for this to work.
-- Copy a source with the old attribute name into a target with the new name.
create type recmove as (a int);
do $$
declare s recmove; d recmove;
begin
  s := row(1)::recmove;
  alter type recmove rename attribute a to renamed;
  d := row(2)::recmove;
  d := s;
  raise notice 'd.renamed = %', d.renamed;
end$$;
drop type recmove;
-- Assign a current expanded record into a target initialized before a no-op
-- type alteration.
create type recmove as (a int);
do $$
declare r1 recmove; r2 recmove;
begin
  r2 := row(1)::recmove;
  alter type recmove alter attribute a type int;
  r1 := row(2)::recmove;
  r2 := r1;
  raise notice 'r2.a = %', r2.a;
end$$;
drop type recmove;
-- Same, with a plain composite datum as the source.
create type recmove as (a int);
do $$
declare r recmove;
begin
  r := row(1)::recmove;
  alter type recmove alter attribute a type int;
  r := row(2)::recmove;
  raise notice 'r.a = %', r.a;
end$$;
drop type recmove;
-- Copy an executor-derived record with an older descriptor, then assign a
-- fresh datum.  The first copy must not prevent the subsequent assignment.
create type recmove as (a int);
create table recmove_src (a int);
insert into recmove_src values (1);
do $$
declare r1 recmove; r2 recmove;
begin
  select * into r1 from recmove_src;
  alter type recmove alter attribute a type int;
  r2 := row(2)::recmove;
  r2 := r1;
  r2 := row(3)::recmove;
  raise notice 'r2.a = %', r2.a;
end$$;
drop table recmove_src;
drop type recmove;
-- Adding or dropping a column changes the layout, so assignment between
-- variables set before and after the change may fail.
create table recmove_tab (f1 int, f2 int);
insert into recmove_tab values (1, 2);
do $$
declare r1 recmove_tab; r2 recmove_tab;
begin
  select * into r1 from recmove_tab;
  alter table recmove_tab add column f3 int;
  r2 := row(3, 4, 5)::recmove_tab;
  r2 := r1;
  raise notice 'r2 = %', r2;
end$$;
drop table recmove_tab;
create table recmove_tab (f1 int, f2 int);
insert into recmove_tab values (1, 2);
do $$
declare r1 recmove_tab; r2 recmove_tab;
begin
  select * into r1 from recmove_tab;
  alter table recmove_tab drop column f2;
  r2 := row(3)::recmove_tab;
  r2 := r1;
  raise notice 'r2 = %', r2;
end$$;
drop table recmove_tab;
-- check access to system columns in a record variable
create function sillytrig() returns trigger language plpgsql as
@@ -430,6 +564,231 @@
update mutable set f2 = f2 || ' baz';
table mutable;
-- check that system columns survive assignment between record variables only
-- when the source tuple can be copied verbatim; where a conversion is needed
-- they are silently dropped.  For a domain source that depends on whether the
-- composite type was invalidated between the two statements, so those cases
-- are run both ways.  debug_discard_caches is pinned so that the ANALYZE is
-- the only invalidation, and each ANALYZE case gets its own table because
-- ANALYZE only invalidates when it actually changes the statistics.
set debug_discard_caches = 0;
create table sysmove(f1 int);
insert into sysmove values (1);
create domain sysmoved as sysmove;
do $$
declare s sysmove; d sysmove;
begin
  select * into s from sysmove;
  d := s;
  raise notice 'rowtype -> rowtype: src %, dst %', s.ctid, d.ctid;
end$$;
do $$
declare s record; d sysmove;
begin
  select * into s from sysmove;
  d := s;
  raise notice 'record  -> rowtype: src %, dst %', s.ctid, d.ctid;
end$$;
do $$
declare s sysmoved; d sysmove;
begin
  select * into s from sysmove;
  d := s;
  raise notice 'domain  -> rowtype: src %, dst %', s.ctid, d.ctid;
end$$;
do $$
declare s sysmoved; d record;
begin
  select * into s from sysmove;
  d := s;
  raise notice 'domain  -> record:  src %, dst %', s.ctid, d.ctid;
end$$;
create table sysmove2(f1 int);
insert into sysmove2 values (1);
create domain sysmoved2 as sysmove2;
do $$
declare s sysmoved2; d sysmove2;
begin
  select * into s from sysmove2;
  analyze sysmove2;
  d := s;
  raise notice 'domain  -> rowtype, invalidated: src %, dst %', s.ctid, d.ctid;
end$$;
create table sysmove3(f1 int);
insert into sysmove3 values (1);
create domain sysmoved3 as sysmove3;
do $$
declare s sysmoved3; d record;
begin
  select * into s from sysmove3;
  analyze sysmove3;
  d := s;
  raise notice 'domain  -> record,  invalidated: src %, dst %', s.ctid, d.ctid;
end$$;
-- A rename round trip also invalidates the descriptor without changing the
-- row type.  Copying the tuple should still preserve its system columns.
do $$
declare s sysmoved; d sysmove;
begin
  select * into s from sysmove;
  alter table sysmove rename column f1 to f2;
  alter table sysmove rename column f2 to f1;
  d := s;
  raise notice 'domain  -> rowtype, renamed: src %, dst %', s.ctid, d.ctid;
end$$;
drop domain sysmoved, sysmoved2, sysmoved3;
drop table sysmove, sysmove2, sysmove3;
reset debug_discard_caches;
-- Preserve system columns when assigning a rowtype parameter whose declared
-- type name now refers to a different table.
create table sysparam (x int);
insert into sysparam values (1);
create function sysparam_probe(s sysparam) returns boolean language plpgsql as $$
declare d sysparam;
begin
  select * into d from sysparam;
  alter table sysparam rename to sysparam_old;
  create table sysparam (x int8);
  select * into s from sysparam_old;
  d := s;
  raise notice 'tableoid kept: %, ctid kept: %',
    d.tableoid is not null, d.ctid is not null;
  return d.ctid is not null;
end$$;
select sysparam_probe(null::sysparam);
drop function sysparam_probe(sysparam_old);
drop table sysparam, sysparam_old;
-- Check behavior when using variables used before/after attribute rename, a
-- harmless operation.
CREATE TYPE altered_type AS (a int);
DO $$
DECLARE
  -- variable first assigned before DDL and then again after
  r_again_after altered_type;
  -- variable assigned before DDL
  r_before altered_type;
  -- variable assigned after DDL
  r_after altered_type;
BEGIN
  r_again_after := ROW(1)::altered_type;
  r_before = ROW(2)::altered_type;
  ALTER TYPE altered_type RENAME ATTRIBUTE a TO b;
  BEGIN
    RAISE NOTICE 'r_before: %', r_before;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_before failed: %', sqlerrm;
  END;
  BEGIN
    r_again_after := ROW(3)::altered_type;
    RAISE NOTICE 'r_again_after: %', r_again_after;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_again_after failed: %', sqlerrm;
  END;
  BEGIN
    r_after = ROW(4)::altered_type;
    RAISE NOTICE 'r_after: %', r_after;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_after failed: %', sqlerrm;
  END;
END $$;
DROP TYPE altered_type;
-- Check behavior when using variables used before/after SET TYPE that doesn't
-- actually change the type.
CREATE TYPE altered_type AS (a int);
DO $$
DECLARE
  -- variable first assigned before DDL and then again after
  r_again_after altered_type;
  -- variable assigned before DDL
  r_before altered_type;
  -- variable assigned after DDL
  r_after altered_type;
BEGIN
  r_again_after := ROW(1)::altered_type;
  r_before = ROW(2)::altered_type;
  ALTER TYPE altered_type ALTER ATTRIBUTE a TYPE int;
  BEGIN
    RAISE NOTICE 'r_before: %', r_before;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_before failed: %', sqlerrm;
  END;
  BEGIN
    r_again_after := ROW(3)::altered_type;
    RAISE NOTICE 'r_again_after: %', r_again_after;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_again_after failed: %', sqlerrm;
  END;
  BEGIN
    r_after = ROW(4)::altered_type;
    RAISE NOTICE 'r_after: %', r_after;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_after failed: %', sqlerrm;
  END;
END $$;
DROP TYPE altered_type;
-- Check behavior when using variables used before/after SET TYPE that does
-- actually change the type.
CREATE TYPE altered_type AS (a bytea);
DO $$
DECLARE
  -- variable first assigned before DDL and then again after
  r_again_after altered_type;
  -- variable assigned before DDL
  r_before altered_type;
  -- variable assigned after DDL
  r_after altered_type;
BEGIN
  r_again_after := ROW('1')::altered_type;
  r_before = ROW('2')::altered_type;
  ALTER TYPE altered_type ALTER ATTRIBUTE a TYPE text;
  BEGIN
    RAISE NOTICE 'r_before: %', r_before;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_before failed: %', sqlerrm;
  END;
  BEGIN
    r_again_after := ROW(3)::altered_type;
    RAISE NOTICE 'r_again_after: %', r_again_after;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_again_after failed: %', sqlerrm;
  END;
  BEGIN
    r_after = ROW(4)::altered_type;
    RAISE NOTICE 'r_after: %', r_after;
  EXCEPTION WHEN others THEN
    RAISE NOTICE 'r_after failed: %', sqlerrm;
  END;
END $$;
DROP TYPE altered_type;
-- check returning a composite datum from a trigger
create or replace function sillytrig() returns trigger language plpgsql as