File: lua54.sql

package info (click to toggle)
postgresql-pllua 1%3A2.0.10-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,316 kB
  • sloc: ansic: 14,369; sql: 2,181; makefile: 163; sh: 59; javascript: 38
file content (61 lines) | stat: -rw-r--r-- 1,394 bytes parent folder | download | duplicates (2)
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
--

\set VERBOSITY terse

--

-- 5.4-specific features.

-- simple close metamethod

do language pllua $$
  local tc <close>
    = setmetatable({}, { __close = function() print("close called") end })
  spi.error("error")
$$;

-- close metamethod on cursor
begin;
do language pllua $$
  for r in spi.rows([[ select * from generate_series(1,5) i ]]) do
    print(r)
    if r.i > 2 then break end
  end
$$;
select * from pg_cursors;  -- should be empty
commit;

-- lua error in close method
do language pllua $$
  local tc <close>
    = setmetatable({}, { __close = function() error("inner error") end })
  error("outer error")
$$;

-- db access in close method with outer lua error
do language pllua $$
  local tc <close>
    = setmetatable({}, { __close = function() print(pgtype.numeric(0)) end })
  error("outer error")
$$;

-- db access in close method with outer db error
do language pllua $$
  local tc <close>
    = setmetatable({}, { __close = function() print(pgtype.numeric(0)) end })
  spi.error("outer error")
$$;

-- close metamethod in SRF
create function pg_temp.sf1(n integer) returns setof integer
  language pllua
  as $$
    local x <close>
        = setmetatable({}, { __close = function() print("close called") end })
    for i = n, n+3 do
        coroutine.yield(i)
    end
$$;
select * from (values (1),(2)) v(n), lateral (select * from pg_temp.sf1(v.n) limit 1) s;

--end