Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# EvalPlanQual rechecks a non-target relation by storing the whole-row datum
# the main plan formed for it (ROW_MARK_COPY) into the EPQ plan's scan slot.
# The EPQ plan is only initialized at the first recheck, so for a function
# returning a composite type, the scan slot's descriptor may be built from a
# later definition of the type than the datum.
#
# The altered column changes from bytea to text, for the reasons explained in
# src/test/regress/sql/rowtypes_alter.sql.
 
setup
{
    CREATE TYPE rta_type AS (b bytea);
    CREATE FUNCTION rta_func() RETURNS rta_type LANGUAGE plpgsql AS $$
    BEGIN
        RETURN ROW('\x4649584d45'::bytea);
    END;
    $$;
    CREATE FUNCTION rta_exec(cmd text) RETURNS int LANGUAGE plpgsql AS $$
    BEGIN
        EXECUTE cmd;
        RETURN 1;
    END;
    $$;
    CREATE TABLE rta_lockme ();
    CREATE TABLE rta_tgt (s text, n int);
    INSERT INTO rta_tgt VALUES (NULL, NULL);
}
 
teardown
{
    DROP TABLE rta_tgt, rta_lockme;
    DROP FUNCTION rta_exec(text), rta_func();
    DROP TYPE rta_type;
}
 
session s1
setup { SET default_transaction_isolation = 'read committed'; }
# The ALTER runs after x::text was computed, but before the recheck.  As an
# uncorrelated subselect it is evaluated once, not again during the recheck.
step s1_upd_alter
{
    UPDATE rta_tgt
    SET s = x::text,
        n = (SELECT rta_exec('ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text'))
    FROM rta_func() x
    RETURNING s;
}
# Same with a column reference.  That fails the recheck's Var type check, but
# only after the datum was deformed with the wrong descriptor.
step s1_upd_alter_col
{
    UPDATE rta_tgt
    SET s = x.b::text,
        n = (SELECT rta_exec('ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text'))
    FROM rta_func() x
    RETURNING s;
}
# Same, but s3 alters.  Acquiring the lock s3 holds processes the
# invalidation before the recheck.
step s1_upd_lock
{
    UPDATE rta_tgt
    SET s = x::text,
        n = (SELECT rta_exec('LOCK rta_lockme'))
    FROM rta_func() x
    RETURNING s;
}
 
session s2
step s2_upd { BEGIN; UPDATE rta_tgt SET n = 0; }
step s2_commit { COMMIT; }
 
session s3
step s3_lock { BEGIN; LOCK rta_lockme; }
step s3_alter { ALTER TYPE rta_type ALTER ATTRIBUTE b TYPE text; COMMIT; }
 
# no recheck
permutation s1_upd_alter
# recheck after s1 altered the type
permutation s2_upd s1_upd_alter s2_commit
permutation s2_upd s1_upd_alter_col s2_commit
# recheck after s3 altered the type
permutation s2_upd s3_lock s1_upd_lock s3_alter s2_commit