Исходный код СУБД 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
# An inlined SQL function's targetlist uses NULL::int4 for dropped columns.
# Its whole-row Var retains the named composite type.  EPQ must be able to
# restore that datum into the subquery's targetlist-described scan slot.
 
setup
{
  CREATE TYPE epq_row AS (gone int, wide bigint, varlen text, id int, v int);
  ALTER TYPE epq_row DROP ATTRIBUTE gone;
  ALTER TYPE epq_row DROP ATTRIBUTE wide;
  ALTER TYPE epq_row DROP ATTRIBUTE varlen;
  CREATE TYPE epq_enum AS ENUM ('x');
  CREATE FUNCTION epq_cast(epq_enum) RETURNS int
    LANGUAGE plpgsql VOLATILE AS $$BEGIN RETURN 42; END$$;
  CREATE CAST (epq_enum AS int) WITH FUNCTION epq_cast(epq_enum) AS ASSIGNMENT;
 
  -- ORDER BY prevents coercion of v in place.  The volatile cast in the
  -- return-coercion projection then keeps that subquery from being pulled up.
  CREATE FUNCTION epq_function() RETURNS SETOF epq_row
    LANGUAGE sql STABLE AS
    $$SELECT 1 AS id, 'x'::epq_enum AS v ORDER BY 2$$;
  CREATE TABLE epq_lock (id int PRIMARY KEY, n int);
  INSERT INTO epq_lock VALUES (1, 0);
}
 
teardown
{
  DROP TABLE epq_lock;
  DROP FUNCTION epq_function();
  DROP CAST (epq_enum AS int);
  DROP FUNCTION epq_cast(epq_enum);
  DROP TYPE epq_enum, epq_row;
}
 
session s1
setup { SET default_transaction_isolation = 'read committed'; }
step begin1 { BEGIN; }
step update1 { UPDATE epq_lock SET n = n + 1 WHERE id = 1; }
step commit1 { COMMIT; }
 
session s2
setup { SET default_transaction_isolation = 'read committed'; }
step read2
{
  SELECT t.id, s.v
  FROM epq_lock t JOIN epq_function() s ON s.id = t.id
  FOR UPDATE OF t;
}
step explain2
{
  EXPLAIN (COSTS OFF)
  SELECT t.id, s.v
  FROM epq_lock t JOIN epq_function() s ON s.id = t.id
  FOR UPDATE OF t;
}
 
# Without a concurrent update, no EPQ reconstruction is needed.
permutation begin1 update1 commit1 read2
 
# EPQ must return the same row despite the dropped-column placeholders,
# including bigint and text attributes with different length, alignment,
# and pass-by-value metadata from int4.  The function is inlined as a
# subquery, which is not pulled up.
permutation explain2 begin1 update1 read2 commit1