exec-ddl
CREATE TABLE xy (x INT, y INT);
----

exec-ddl
CREATE TABLE kv (k INT PRIMARY KEY, v INT);
----

exec-ddl
CREATE TABLE txn_timestamps (ts TIMESTAMP);
----

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  BEGIN
    RETURN a;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 2);
----
project
 ├── columns: f:4
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:4]
           ├── args
           │    ├── const: 1
           │    └── const: 2
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_return_1:3
                     ├── project
                     │    ├── columns: stmt_return_1:3
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── variable: a:1 [as=stmt_return_1:3]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  BEGIN
    RETURN a + b;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 2);
----
project
 ├── columns: f:4
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:4]
           ├── args
           │    ├── const: 1
           │    └── const: 2
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_return_1:3
                     ├── project
                     │    ├── columns: stmt_return_1:3
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── plus [as=stmt_return_1:3]
                     │              ├── variable: a:1
                     │              └── variable: b:2
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    c INT;
  BEGIN
    RETURN c;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 2);
----
project
 ├── columns: f:5
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:5]
           ├── args
           │    ├── const: 1
           │    └── const: 2
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_return_1:4
                     ├── project
                     │    ├── columns: stmt_return_1:4
                     │    ├── project
                     │    │    ├── columns: c:3
                     │    │    ├── values
                     │    │    │    └── tuple
                     │    │    └── projections
                     │    │         └── cast: INT8 [as=c:3]
                     │    │              └── null
                     │    └── projections
                     │         └── variable: c:3 [as=stmt_return_1:4]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    c INT := 0;
  BEGIN
    RETURN c;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 2);
----
project
 ├── columns: f:5
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:5]
           ├── args
           │    ├── const: 1
           │    └── const: 2
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_return_1:4!null
                     ├── project
                     │    ├── columns: stmt_return_1:4!null
                     │    ├── project
                     │    │    ├── columns: c:3!null
                     │    │    ├── values
                     │    │    │    └── tuple
                     │    │    └── projections
                     │    │         └── const: 0 [as=c:3]
                     │    └── projections
                     │         └── variable: c:3 [as=stmt_return_1:4]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    c INT;
  BEGIN
    c := 0;
    RETURN c;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 2);
----
project
 ├── columns: f:6
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:6]
           ├── args
           │    ├── const: 1
           │    └── const: 2
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_return_1:5!null
                     ├── project
                     │    ├── columns: stmt_return_1:5!null
                     │    ├── project
                     │    │    ├── columns: c:4!null
                     │    │    ├── project
                     │    │    │    ├── columns: c:3
                     │    │    │    ├── values
                     │    │    │    │    └── tuple
                     │    │    │    └── projections
                     │    │    │         └── cast: INT8 [as=c:3]
                     │    │    │              └── null
                     │    │    └── projections
                     │    │         └── const: 0 [as=c:4]
                     │    └── projections
                     │         └── variable: c:4 [as=stmt_return_1:5]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    c INT;
  BEGIN
    IF a < b THEN
      c := a;
    END IF;
    RETURN c;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 2);
----
project
 ├── columns: f:12
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:12]
           ├── args
           │    ├── const: 1
           │    └── const: 2
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_if_3:11
                     ├── project
                     │    ├── columns: stmt_if_3:11
                     │    ├── project
                     │    │    ├── columns: c:3
                     │    │    ├── values
                     │    │    │    └── tuple
                     │    │    └── projections
                     │    │         └── cast: INT8 [as=c:3]
                     │    │              └── null
                     │    └── projections
                     │         └── case [as=stmt_if_3:11]
                     │              ├── true
                     │              ├── when
                     │              │    ├── lt
                     │              │    │    ├── variable: a:1
                     │              │    │    └── variable: b:2
                     │              │    └── subquery
                     │              │         └── project
                     │              │              ├── columns: stmt_if_1:9
                     │              │              ├── project
                     │              │              │    ├── columns: c:8
                     │              │              │    ├── values
                     │              │              │    │    └── tuple
                     │              │              │    └── projections
                     │              │              │         └── variable: a:1 [as=c:8]
                     │              │              └── projections
                     │              │                   └── udf: stmt_if_1 [as=stmt_if_1:9]
                     │              │                        ├── tail-call
                     │              │                        ├── args
                     │              │                        │    ├── variable: a:1
                     │              │                        │    ├── variable: b:2
                     │              │                        │    └── variable: c:8
                     │              │                        ├── params: a:4 b:5 c:6
                     │              │                        └── body
                     │              │                             └── project
                     │              │                                  ├── columns: stmt_return_2:7
                     │              │                                  ├── values
                     │              │                                  │    └── tuple
                     │              │                                  └── projections
                     │              │                                       └── variable: c:6 [as=stmt_return_2:7]
                     │              └── subquery
                     │                   └── project
                     │                        ├── columns: stmt_if_1:10
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── udf: stmt_if_1 [as=stmt_if_1:10]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    ├── variable: a:1
                     │                                  │    ├── variable: b:2
                     │                                  │    └── variable: c:3
                     │                                  ├── params: a:4 b:5 c:6
                     │                                  └── body
                     │                                       └── project
                     │                                            ├── columns: stmt_return_2:7
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── variable: c:6 [as=stmt_return_2:7]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    c INT;
  BEGIN
    IF a < b THEN
      c := a;
    ELSE
      c := b;
    END IF;
    RETURN c;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 2);
----
project
 ├── columns: f:13
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:13]
           ├── args
           │    ├── const: 1
           │    └── const: 2
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_if_3:12
                     ├── project
                     │    ├── columns: stmt_if_3:12
                     │    ├── project
                     │    │    ├── columns: c:3
                     │    │    ├── values
                     │    │    │    └── tuple
                     │    │    └── projections
                     │    │         └── cast: INT8 [as=c:3]
                     │    │              └── null
                     │    └── projections
                     │         └── case [as=stmt_if_3:12]
                     │              ├── true
                     │              ├── when
                     │              │    ├── lt
                     │              │    │    ├── variable: a:1
                     │              │    │    └── variable: b:2
                     │              │    └── subquery
                     │              │         └── project
                     │              │              ├── columns: stmt_if_1:9
                     │              │              ├── project
                     │              │              │    ├── columns: c:8
                     │              │              │    ├── values
                     │              │              │    │    └── tuple
                     │              │              │    └── projections
                     │              │              │         └── variable: a:1 [as=c:8]
                     │              │              └── projections
                     │              │                   └── udf: stmt_if_1 [as=stmt_if_1:9]
                     │              │                        ├── tail-call
                     │              │                        ├── args
                     │              │                        │    ├── variable: a:1
                     │              │                        │    ├── variable: b:2
                     │              │                        │    └── variable: c:8
                     │              │                        ├── params: a:4 b:5 c:6
                     │              │                        └── body
                     │              │                             └── project
                     │              │                                  ├── columns: stmt_return_2:7
                     │              │                                  ├── values
                     │              │                                  │    └── tuple
                     │              │                                  └── projections
                     │              │                                       └── variable: c:6 [as=stmt_return_2:7]
                     │              └── subquery
                     │                   └── project
                     │                        ├── columns: stmt_if_1:11
                     │                        ├── project
                     │                        │    ├── columns: c:10
                     │                        │    ├── values
                     │                        │    │    └── tuple
                     │                        │    └── projections
                     │                        │         └── variable: b:2 [as=c:10]
                     │                        └── projections
                     │                             └── udf: stmt_if_1 [as=stmt_if_1:11]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    ├── variable: a:1
                     │                                  │    ├── variable: b:2
                     │                                  │    └── variable: c:10
                     │                                  ├── params: a:4 b:5 c:6
                     │                                  └── body
                     │                                       └── project
                     │                                            ├── columns: stmt_return_2:7
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── variable: c:6 [as=stmt_return_2:7]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    c INT;
  BEGIN
    IF a < b THEN
      c := a;
    ELSE
      RETURN 100;
    END IF;
    RETURN c;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 2);
----
project
 ├── columns: f:12
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:12]
           ├── args
           │    ├── const: 1
           │    └── const: 2
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_if_4:11
                     ├── project
                     │    ├── columns: stmt_if_4:11
                     │    ├── project
                     │    │    ├── columns: c:3
                     │    │    ├── values
                     │    │    │    └── tuple
                     │    │    └── projections
                     │    │         └── cast: INT8 [as=c:3]
                     │    │              └── null
                     │    └── projections
                     │         └── case [as=stmt_if_4:11]
                     │              ├── true
                     │              ├── when
                     │              │    ├── lt
                     │              │    │    ├── variable: a:1
                     │              │    │    └── variable: b:2
                     │              │    └── subquery
                     │              │         └── project
                     │              │              ├── columns: stmt_if_1:9
                     │              │              ├── project
                     │              │              │    ├── columns: c:8
                     │              │              │    ├── values
                     │              │              │    │    └── tuple
                     │              │              │    └── projections
                     │              │              │         └── variable: a:1 [as=c:8]
                     │              │              └── projections
                     │              │                   └── udf: stmt_if_1 [as=stmt_if_1:9]
                     │              │                        ├── tail-call
                     │              │                        ├── args
                     │              │                        │    ├── variable: a:1
                     │              │                        │    ├── variable: b:2
                     │              │                        │    └── variable: c:8
                     │              │                        ├── params: a:4 b:5 c:6
                     │              │                        └── body
                     │              │                             └── project
                     │              │                                  ├── columns: stmt_return_2:7
                     │              │                                  ├── values
                     │              │                                  │    └── tuple
                     │              │                                  └── projections
                     │              │                                       └── variable: c:6 [as=stmt_return_2:7]
                     │              └── subquery
                     │                   └── project
                     │                        ├── columns: stmt_return_3:10!null
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── const: 100 [as=stmt_return_3:10]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    c INT;
  BEGIN
    IF a < b THEN
      RETURN 100;
    ELSE
      c := b;
    END IF;
    RETURN c;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 2);
----
project
 ├── columns: f:12
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:12]
           ├── args
           │    ├── const: 1
           │    └── const: 2
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_if_4:11
                     ├── project
                     │    ├── columns: stmt_if_4:11
                     │    ├── project
                     │    │    ├── columns: c:3
                     │    │    ├── values
                     │    │    │    └── tuple
                     │    │    └── projections
                     │    │         └── cast: INT8 [as=c:3]
                     │    │              └── null
                     │    └── projections
                     │         └── case [as=stmt_if_4:11]
                     │              ├── true
                     │              ├── when
                     │              │    ├── lt
                     │              │    │    ├── variable: a:1
                     │              │    │    └── variable: b:2
                     │              │    └── subquery
                     │              │         └── project
                     │              │              ├── columns: stmt_return_3:8!null
                     │              │              ├── values
                     │              │              │    └── tuple
                     │              │              └── projections
                     │              │                   └── const: 100 [as=stmt_return_3:8]
                     │              └── subquery
                     │                   └── project
                     │                        ├── columns: stmt_if_1:10
                     │                        ├── project
                     │                        │    ├── columns: c:9
                     │                        │    ├── values
                     │                        │    │    └── tuple
                     │                        │    └── projections
                     │                        │         └── variable: b:2 [as=c:9]
                     │                        └── projections
                     │                             └── udf: stmt_if_1 [as=stmt_if_1:10]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    ├── variable: a:1
                     │                                  │    ├── variable: b:2
                     │                                  │    └── variable: c:9
                     │                                  ├── params: a:4 b:5 c:6
                     │                                  └── body
                     │                                       └── project
                     │                                            ├── columns: stmt_return_2:7
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── variable: c:6 [as=stmt_return_2:7]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    c INT;
  BEGIN
    IF a < b THEN
      RETURN 100;
    ELSE
      RETURN 0;
    END IF;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 2);
----
project
 ├── columns: f:16
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:16]
           ├── args
           │    ├── const: 1
           │    └── const: 2
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_if_7:15
                     ├── project
                     │    ├── columns: stmt_if_7:15
                     │    ├── project
                     │    │    ├── columns: c:3
                     │    │    ├── values
                     │    │    │    └── tuple
                     │    │    └── projections
                     │    │         └── cast: INT8 [as=c:3]
                     │    │              └── null
                     │    └── projections
                     │         └── case [as=stmt_if_7:15]
                     │              ├── true
                     │              ├── when
                     │              │    ├── lt
                     │              │    │    ├── variable: a:1
                     │              │    │    └── variable: b:2
                     │              │    └── subquery
                     │              │         └── project
                     │              │              ├── columns: stmt_return_5:13!null
                     │              │              ├── values
                     │              │              │    └── tuple
                     │              │              └── projections
                     │              │                   └── const: 100 [as=stmt_return_5:13]
                     │              └── subquery
                     │                   └── project
                     │                        ├── columns: stmt_return_6:14!null
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── const: 0 [as=stmt_return_6:14]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    i INT := a;
  BEGIN
    LOOP
      RETURN 100;
    END LOOP;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 2);
----
project
 ├── columns: f:18
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:18]
           ├── args
           │    ├── const: 1
           │    └── const: 2
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_loop_5:17
                     ├── project
                     │    ├── columns: stmt_loop_5:17
                     │    ├── project
                     │    │    ├── columns: i:3
                     │    │    ├── values
                     │    │    │    └── tuple
                     │    │    └── projections
                     │    │         └── variable: a:1 [as=i:3]
                     │    └── projections
                     │         └── udf: stmt_loop_5 [as=stmt_loop_5:17]
                     │              ├── args
                     │              │    ├── variable: a:1
                     │              │    ├── variable: b:2
                     │              │    └── variable: i:3
                     │              ├── params: a:13 b:14 i:15
                     │              └── body
                     │                   └── project
                     │                        ├── columns: stmt_return_6:16!null
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── const: 100 [as=stmt_return_6:16]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    i INT := a;
  BEGIN
    LOOP
      IF a < b THEN
        RETURN 0;
      END IF;
      RETURN 100;
    END LOOP;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 2);
----
project
 ├── columns: f:24
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:24]
           ├── args
           │    ├── const: 1
           │    └── const: 2
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_loop_5:23
                     ├── project
                     │    ├── columns: stmt_loop_5:23
                     │    ├── project
                     │    │    ├── columns: i:3
                     │    │    ├── values
                     │    │    │    └── tuple
                     │    │    └── projections
                     │    │         └── variable: a:1 [as=i:3]
                     │    └── projections
                     │         └── udf: stmt_loop_5 [as=stmt_loop_5:23]
                     │              ├── args
                     │              │    ├── variable: a:1
                     │              │    ├── variable: b:2
                     │              │    └── variable: i:3
                     │              ├── params: a:13 b:14 i:15
                     │              └── body
                     │                   └── project
                     │                        ├── columns: stmt_if_9:22
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── case [as=stmt_if_9:22]
                     │                                  ├── true
                     │                                  ├── when
                     │                                  │    ├── lt
                     │                                  │    │    ├── variable: a:13
                     │                                  │    │    └── variable: b:14
                     │                                  │    └── subquery
                     │                                  │         ├── tail-call
                     │                                  │         └── project
                     │                                  │              ├── columns: stmt_return_8:20!null
                     │                                  │              ├── values
                     │                                  │              │    └── tuple
                     │                                  │              └── projections
                     │                                  │                   └── const: 0 [as=stmt_return_8:20]
                     │                                  └── subquery
                     │                                       ├── tail-call
                     │                                       └── project
                     │                                            ├── columns: stmt_if_6:21
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: stmt_if_6 [as=stmt_if_6:21]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    ├── variable: a:13
                     │                                                      │    ├── variable: b:14
                     │                                                      │    └── variable: i:15
                     │                                                      ├── params: a:16 b:17 i:18
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: stmt_return_7:19!null
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── const: 100 [as=stmt_return_7:19]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    i INT := a;
  BEGIN
    LOOP
      IF i >= b THEN EXIT; END IF;
      IF i = 8 THEN RETURN 100; END IF;
      i := i + 1;
    END LOOP;
    RETURN i;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 2);
----
project
 ├── columns: f:26
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:26]
           ├── args
           │    ├── const: 1
           │    └── const: 2
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_loop_3:25
                     ├── project
                     │    ├── columns: stmt_loop_3:25
                     │    ├── project
                     │    │    ├── columns: i:3
                     │    │    ├── values
                     │    │    │    └── tuple
                     │    │    └── projections
                     │    │         └── variable: a:1 [as=i:3]
                     │    └── projections
                     │         └── udf: stmt_loop_3 [as=stmt_loop_3:25]
                     │              ├── args
                     │              │    ├── variable: a:1
                     │              │    ├── variable: b:2
                     │              │    └── variable: i:3
                     │              ├── params: a:8 b:9 i:10
                     │              └── body
                     │                   └── project
                     │                        ├── columns: stmt_if_8:24
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── case [as=stmt_if_8:24]
                     │                                  ├── true
                     │                                  ├── when
                     │                                  │    ├── ge
                     │                                  │    │    ├── variable: i:10
                     │                                  │    │    └── variable: b:9
                     │                                  │    └── subquery
                     │                                  │         ├── tail-call
                     │                                  │         └── project
                     │                                  │              ├── columns: loop_exit_1:22
                     │                                  │              ├── values
                     │                                  │              │    └── tuple
                     │                                  │              └── projections
                     │                                  │                   └── udf: loop_exit_1 [as=loop_exit_1:22]
                     │                                  │                        ├── tail-call
                     │                                  │                        ├── args
                     │                                  │                        │    ├── variable: a:8
                     │                                  │                        │    ├── variable: b:9
                     │                                  │                        │    └── variable: i:10
                     │                                  │                        ├── params: a:4 b:5 i:6
                     │                                  │                        └── body
                     │                                  │                             └── project
                     │                                  │                                  ├── columns: stmt_return_2:7
                     │                                  │                                  ├── values
                     │                                  │                                  │    └── tuple
                     │                                  │                                  └── projections
                     │                                  │                                       └── variable: i:6 [as=stmt_return_2:7]
                     │                                  └── subquery
                     │                                       ├── tail-call
                     │                                       └── project
                     │                                            ├── columns: stmt_if_4:23
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: stmt_if_4 [as=stmt_if_4:23]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    ├── variable: a:8
                     │                                                      │    ├── variable: b:9
                     │                                                      │    └── variable: i:10
                     │                                                      ├── params: a:11 b:12 i:13
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: stmt_if_7:21
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── case [as=stmt_if_7:21]
                     │                                                                          ├── true
                     │                                                                          ├── when
                     │                                                                          │    ├── eq
                     │                                                                          │    │    ├── variable: i:13
                     │                                                                          │    │    └── const: 8
                     │                                                                          │    └── subquery
                     │                                                                          │         ├── tail-call
                     │                                                                          │         └── project
                     │                                                                          │              ├── columns: stmt_return_6:19!null
                     │                                                                          │              ├── values
                     │                                                                          │              │    └── tuple
                     │                                                                          │              └── projections
                     │                                                                          │                   └── const: 100 [as=stmt_return_6:19]
                     │                                                                          └── subquery
                     │                                                                               ├── tail-call
                     │                                                                               └── project
                     │                                                                                    ├── columns: stmt_if_5:20
                     │                                                                                    ├── values
                     │                                                                                    │    └── tuple
                     │                                                                                    └── projections
                     │                                                                                         └── udf: stmt_if_5 [as=stmt_if_5:20]
                     │                                                                                              ├── tail-call
                     │                                                                                              ├── args
                     │                                                                                              │    ├── variable: a:11
                     │                                                                                              │    ├── variable: b:12
                     │                                                                                              │    └── variable: i:13
                     │                                                                                              ├── params: a:14 b:15 i:16
                     │                                                                                              └── body
                     │                                                                                                   └── project
                     │                                                                                                        ├── columns: stmt_loop_3:18
                     │                                                                                                        ├── project
                     │                                                                                                        │    ├── columns: i:17
                     │                                                                                                        │    ├── values
                     │                                                                                                        │    │    └── tuple
                     │                                                                                                        │    └── projections
                     │                                                                                                        │         └── plus [as=i:17]
                     │                                                                                                        │              ├── variable: i:16
                     │                                                                                                        │              └── const: 1
                     │                                                                                                        └── projections
                     │                                                                                                             └── udf: stmt_loop_3 [as=stmt_loop_3:18]
                     │                                                                                                                  ├── tail-call
                     │                                                                                                                  ├── args
                     │                                                                                                                  │    ├── variable: a:14
                     │                                                                                                                  │    ├── variable: b:15
                     │                                                                                                                  │    └── variable: i:17
                     │                                                                                                                  └── recursive-call
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    i INT := a;
  BEGIN
    LOOP
      IF i >= b THEN EXIT; END IF;
      i := i + 1;
    END LOOP;
    RETURN i;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 5);
----
project
 ├── columns: f:20
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:20]
           ├── args
           │    ├── const: 1
           │    └── const: 5
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_loop_3:19
                     ├── project
                     │    ├── columns: stmt_loop_3:19
                     │    ├── project
                     │    │    ├── columns: i:3
                     │    │    ├── values
                     │    │    │    └── tuple
                     │    │    └── projections
                     │    │         └── variable: a:1 [as=i:3]
                     │    └── projections
                     │         └── udf: stmt_loop_3 [as=stmt_loop_3:19]
                     │              ├── args
                     │              │    ├── variable: a:1
                     │              │    ├── variable: b:2
                     │              │    └── variable: i:3
                     │              ├── params: a:8 b:9 i:10
                     │              └── body
                     │                   └── project
                     │                        ├── columns: stmt_if_5:18
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── case [as=stmt_if_5:18]
                     │                                  ├── true
                     │                                  ├── when
                     │                                  │    ├── ge
                     │                                  │    │    ├── variable: i:10
                     │                                  │    │    └── variable: b:9
                     │                                  │    └── subquery
                     │                                  │         ├── tail-call
                     │                                  │         └── project
                     │                                  │              ├── columns: loop_exit_1:16
                     │                                  │              ├── values
                     │                                  │              │    └── tuple
                     │                                  │              └── projections
                     │                                  │                   └── udf: loop_exit_1 [as=loop_exit_1:16]
                     │                                  │                        ├── tail-call
                     │                                  │                        ├── args
                     │                                  │                        │    ├── variable: a:8
                     │                                  │                        │    ├── variable: b:9
                     │                                  │                        │    └── variable: i:10
                     │                                  │                        ├── params: a:4 b:5 i:6
                     │                                  │                        └── body
                     │                                  │                             └── project
                     │                                  │                                  ├── columns: stmt_return_2:7
                     │                                  │                                  ├── values
                     │                                  │                                  │    └── tuple
                     │                                  │                                  └── projections
                     │                                  │                                       └── variable: i:6 [as=stmt_return_2:7]
                     │                                  └── subquery
                     │                                       ├── tail-call
                     │                                       └── project
                     │                                            ├── columns: stmt_if_4:17
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: stmt_if_4 [as=stmt_if_4:17]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    ├── variable: a:8
                     │                                                      │    ├── variable: b:9
                     │                                                      │    └── variable: i:10
                     │                                                      ├── params: a:11 b:12 i:13
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: stmt_loop_3:15
                     │                                                                ├── project
                     │                                                                │    ├── columns: i:14
                     │                                                                │    ├── values
                     │                                                                │    │    └── tuple
                     │                                                                │    └── projections
                     │                                                                │         └── plus [as=i:14]
                     │                                                                │              ├── variable: i:13
                     │                                                                │              └── const: 1
                     │                                                                └── projections
                     │                                                                     └── udf: stmt_loop_3 [as=stmt_loop_3:15]
                     │                                                                          ├── tail-call
                     │                                                                          ├── args
                     │                                                                          │    ├── variable: a:11
                     │                                                                          │    ├── variable: b:12
                     │                                                                          │    └── variable: i:14
                     │                                                                          └── recursive-call
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    sum INT := 0;
    i INT := a;
  BEGIN
    IF a IS NOT NULL AND b is NOT NULL THEN
      LOOP
        IF i >= b THEN EXIT; END IF;
        sum := sum + i;
        i := i + 1;
      END LOOP;
    END IF;
    RETURN sum;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(0, 0);
----
project
 ├── columns: f:32
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:32]
           ├── args
           │    ├── const: 0
           │    └── const: 0
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_if_7:31
                     ├── project
                     │    ├── columns: stmt_if_7:31
                     │    ├── project
                     │    │    ├── columns: i:4 sum:3!null
                     │    │    ├── project
                     │    │    │    ├── columns: sum:3!null
                     │    │    │    ├── values
                     │    │    │    │    └── tuple
                     │    │    │    └── projections
                     │    │    │         └── const: 0 [as=sum:3]
                     │    │    └── projections
                     │    │         └── variable: a:1 [as=i:4]
                     │    └── projections
                     │         └── case [as=stmt_if_7:31]
                     │              ├── true
                     │              ├── when
                     │              │    ├── and
                     │              │    │    ├── is-not
                     │              │    │    │    ├── variable: a:1
                     │              │    │    │    └── null
                     │              │    │    └── is-not
                     │              │    │         ├── variable: b:2
                     │              │    │         └── null
                     │              │    └── subquery
                     │              │         └── project
                     │              │              ├── columns: stmt_loop_4:29
                     │              │              ├── values
                     │              │              │    └── tuple
                     │              │              └── projections
                     │              │                   └── udf: stmt_loop_4 [as=stmt_loop_4:29]
                     │              │                        ├── tail-call
                     │              │                        ├── args
                     │              │                        │    ├── variable: a:1
                     │              │                        │    ├── variable: b:2
                     │              │                        │    ├── variable: sum:3
                     │              │                        │    └── variable: i:4
                     │              │                        ├── params: a:15 b:16 sum:17 i:18
                     │              │                        └── body
                     │              │                             └── project
                     │              │                                  ├── columns: stmt_if_6:28
                     │              │                                  ├── values
                     │              │                                  │    └── tuple
                     │              │                                  └── projections
                     │              │                                       └── case [as=stmt_if_6:28]
                     │              │                                            ├── true
                     │              │                                            ├── when
                     │              │                                            │    ├── ge
                     │              │                                            │    │    ├── variable: i:18
                     │              │                                            │    │    └── variable: b:16
                     │              │                                            │    └── subquery
                     │              │                                            │         ├── tail-call
                     │              │                                            │         └── project
                     │              │                                            │              ├── columns: loop_exit_3:26
                     │              │                                            │              ├── values
                     │              │                                            │              │    └── tuple
                     │              │                                            │              └── projections
                     │              │                                            │                   └── udf: loop_exit_3 [as=loop_exit_3:26]
                     │              │                                            │                        ├── tail-call
                     │              │                                            │                        ├── args
                     │              │                                            │                        │    ├── variable: a:15
                     │              │                                            │                        │    ├── variable: b:16
                     │              │                                            │                        │    ├── variable: sum:17
                     │              │                                            │                        │    └── variable: i:18
                     │              │                                            │                        ├── params: a:10 b:11 sum:12 i:13
                     │              │                                            │                        └── body
                     │              │                                            │                             └── project
                     │              │                                            │                                  ├── columns: stmt_if_1:14
                     │              │                                            │                                  ├── values
                     │              │                                            │                                  │    └── tuple
                     │              │                                            │                                  └── projections
                     │              │                                            │                                       └── udf: stmt_if_1 [as=stmt_if_1:14]
                     │              │                                            │                                            ├── tail-call
                     │              │                                            │                                            ├── args
                     │              │                                            │                                            │    ├── variable: a:10
                     │              │                                            │                                            │    ├── variable: b:11
                     │              │                                            │                                            │    ├── variable: sum:12
                     │              │                                            │                                            │    └── variable: i:13
                     │              │                                            │                                            ├── params: a:5 b:6 sum:7 i:8
                     │              │                                            │                                            └── body
                     │              │                                            │                                                 └── project
                     │              │                                            │                                                      ├── columns: stmt_return_2:9
                     │              │                                            │                                                      ├── values
                     │              │                                            │                                                      │    └── tuple
                     │              │                                            │                                                      └── projections
                     │              │                                            │                                                           └── variable: sum:7 [as=stmt_return_2:9]
                     │              │                                            └── subquery
                     │              │                                                 ├── tail-call
                     │              │                                                 └── project
                     │              │                                                      ├── columns: stmt_if_5:27
                     │              │                                                      ├── values
                     │              │                                                      │    └── tuple
                     │              │                                                      └── projections
                     │              │                                                           └── udf: stmt_if_5 [as=stmt_if_5:27]
                     │              │                                                                ├── tail-call
                     │              │                                                                ├── args
                     │              │                                                                │    ├── variable: a:15
                     │              │                                                                │    ├── variable: b:16
                     │              │                                                                │    ├── variable: sum:17
                     │              │                                                                │    └── variable: i:18
                     │              │                                                                ├── params: a:19 b:20 sum:21 i:22
                     │              │                                                                └── body
                     │              │                                                                     └── project
                     │              │                                                                          ├── columns: stmt_loop_4:25
                     │              │                                                                          ├── project
                     │              │                                                                          │    ├── columns: i:24 sum:23
                     │              │                                                                          │    ├── project
                     │              │                                                                          │    │    ├── columns: sum:23
                     │              │                                                                          │    │    ├── values
                     │              │                                                                          │    │    │    └── tuple
                     │              │                                                                          │    │    └── projections
                     │              │                                                                          │    │         └── plus [as=sum:23]
                     │              │                                                                          │    │              ├── variable: sum:21
                     │              │                                                                          │    │              └── variable: i:22
                     │              │                                                                          │    └── projections
                     │              │                                                                          │         └── plus [as=i:24]
                     │              │                                                                          │              ├── variable: i:22
                     │              │                                                                          │              └── const: 1
                     │              │                                                                          └── projections
                     │              │                                                                               └── udf: stmt_loop_4 [as=stmt_loop_4:25]
                     │              │                                                                                    ├── tail-call
                     │              │                                                                                    ├── args
                     │              │                                                                                    │    ├── variable: a:19
                     │              │                                                                                    │    ├── variable: b:20
                     │              │                                                                                    │    ├── variable: sum:23
                     │              │                                                                                    │    └── variable: i:24
                     │              │                                                                                    └── recursive-call
                     │              └── subquery
                     │                   └── project
                     │                        ├── columns: stmt_if_1:30
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── udf: stmt_if_1 [as=stmt_if_1:30]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    ├── variable: a:1
                     │                                  │    ├── variable: b:2
                     │                                  │    ├── variable: sum:3
                     │                                  │    └── variable: i:4
                     │                                  ├── params: a:5 b:6 sum:7 i:8
                     │                                  └── body
                     │                                       └── project
                     │                                            ├── columns: stmt_return_2:9
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── variable: sum:7 [as=stmt_return_2:9]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    sum INT := 0;
    i INT := a;
  BEGIN
    LOOP
      IF i >= b THEN EXIT; END IF;
      IF i = 2 THEN
        i := i + 1;
        CONTINUE;
      END IF;
      sum := sum + i;
      i := i + 1;
    END LOOP;
    RETURN sum;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(5, -5);
----
project
 ├── columns: f:33
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:33]
           ├── args
           │    ├── const: 5
           │    └── const: -5
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_loop_3:32
                     ├── project
                     │    ├── columns: stmt_loop_3:32
                     │    ├── project
                     │    │    ├── columns: i:4 sum:3!null
                     │    │    ├── project
                     │    │    │    ├── columns: sum:3!null
                     │    │    │    ├── values
                     │    │    │    │    └── tuple
                     │    │    │    └── projections
                     │    │    │         └── const: 0 [as=sum:3]
                     │    │    └── projections
                     │    │         └── variable: a:1 [as=i:4]
                     │    └── projections
                     │         └── udf: stmt_loop_3 [as=stmt_loop_3:32]
                     │              ├── args
                     │              │    ├── variable: a:1
                     │              │    ├── variable: b:2
                     │              │    ├── variable: sum:3
                     │              │    └── variable: i:4
                     │              ├── params: a:10 b:11 sum:12 i:13
                     │              └── body
                     │                   └── project
                     │                        ├── columns: stmt_if_7:31
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── case [as=stmt_if_7:31]
                     │                                  ├── true
                     │                                  ├── when
                     │                                  │    ├── ge
                     │                                  │    │    ├── variable: i:13
                     │                                  │    │    └── variable: b:11
                     │                                  │    └── subquery
                     │                                  │         ├── tail-call
                     │                                  │         └── project
                     │                                  │              ├── columns: loop_exit_1:29
                     │                                  │              ├── values
                     │                                  │              │    └── tuple
                     │                                  │              └── projections
                     │                                  │                   └── udf: loop_exit_1 [as=loop_exit_1:29]
                     │                                  │                        ├── tail-call
                     │                                  │                        ├── args
                     │                                  │                        │    ├── variable: a:10
                     │                                  │                        │    ├── variable: b:11
                     │                                  │                        │    ├── variable: sum:12
                     │                                  │                        │    └── variable: i:13
                     │                                  │                        ├── params: a:5 b:6 sum:7 i:8
                     │                                  │                        └── body
                     │                                  │                             └── project
                     │                                  │                                  ├── columns: stmt_return_2:9
                     │                                  │                                  ├── values
                     │                                  │                                  │    └── tuple
                     │                                  │                                  └── projections
                     │                                  │                                       └── variable: sum:7 [as=stmt_return_2:9]
                     │                                  └── subquery
                     │                                       ├── tail-call
                     │                                       └── project
                     │                                            ├── columns: stmt_if_4:30
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: stmt_if_4 [as=stmt_if_4:30]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    ├── variable: a:10
                     │                                                      │    ├── variable: b:11
                     │                                                      │    ├── variable: sum:12
                     │                                                      │    └── variable: i:13
                     │                                                      ├── params: a:14 b:15 sum:16 i:17
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: stmt_if_6:28
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── case [as=stmt_if_6:28]
                     │                                                                          ├── true
                     │                                                                          ├── when
                     │                                                                          │    ├── eq
                     │                                                                          │    │    ├── variable: i:17
                     │                                                                          │    │    └── const: 2
                     │                                                                          │    └── subquery
                     │                                                                          │         ├── tail-call
                     │                                                                          │         └── project
                     │                                                                          │              ├── columns: stmt_loop_3:26
                     │                                                                          │              ├── project
                     │                                                                          │              │    ├── columns: i:25
                     │                                                                          │              │    ├── values
                     │                                                                          │              │    │    └── tuple
                     │                                                                          │              │    └── projections
                     │                                                                          │              │         └── plus [as=i:25]
                     │                                                                          │              │              ├── variable: i:17
                     │                                                                          │              │              └── const: 1
                     │                                                                          │              └── projections
                     │                                                                          │                   └── udf: stmt_loop_3 [as=stmt_loop_3:26]
                     │                                                                          │                        ├── tail-call
                     │                                                                          │                        ├── args
                     │                                                                          │                        │    ├── variable: a:14
                     │                                                                          │                        │    ├── variable: b:15
                     │                                                                          │                        │    ├── variable: sum:16
                     │                                                                          │                        │    └── variable: i:25
                     │                                                                          │                        └── recursive-call
                     │                                                                          └── subquery
                     │                                                                               ├── tail-call
                     │                                                                               └── project
                     │                                                                                    ├── columns: stmt_if_5:27
                     │                                                                                    ├── values
                     │                                                                                    │    └── tuple
                     │                                                                                    └── projections
                     │                                                                                         └── udf: stmt_if_5 [as=stmt_if_5:27]
                     │                                                                                              ├── tail-call
                     │                                                                                              ├── args
                     │                                                                                              │    ├── variable: a:14
                     │                                                                                              │    ├── variable: b:15
                     │                                                                                              │    ├── variable: sum:16
                     │                                                                                              │    └── variable: i:17
                     │                                                                                              ├── params: a:18 b:19 sum:20 i:21
                     │                                                                                              └── body
                     │                                                                                                   └── project
                     │                                                                                                        ├── columns: stmt_loop_3:24
                     │                                                                                                        ├── project
                     │                                                                                                        │    ├── columns: i:23 sum:22
                     │                                                                                                        │    ├── project
                     │                                                                                                        │    │    ├── columns: sum:22
                     │                                                                                                        │    │    ├── values
                     │                                                                                                        │    │    │    └── tuple
                     │                                                                                                        │    │    └── projections
                     │                                                                                                        │    │         └── plus [as=sum:22]
                     │                                                                                                        │    │              ├── variable: sum:20
                     │                                                                                                        │    │              └── variable: i:21
                     │                                                                                                        │    └── projections
                     │                                                                                                        │         └── plus [as=i:23]
                     │                                                                                                        │              ├── variable: i:21
                     │                                                                                                        │              └── const: 1
                     │                                                                                                        └── projections
                     │                                                                                                             └── udf: stmt_loop_3 [as=stmt_loop_3:24]
                     │                                                                                                                  ├── tail-call
                     │                                                                                                                  ├── args
                     │                                                                                                                  │    ├── variable: a:18
                     │                                                                                                                  │    ├── variable: b:19
                     │                                                                                                                  │    ├── variable: sum:22
                     │                                                                                                                  │    └── variable: i:23
                     │                                                                                                                  └── recursive-call
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(a INT, b INT) RETURNS INT AS $$
  DECLARE
    sum INT := 0;
    i INT := a;
    j INT;
  BEGIN
    LOOP
      IF i >= b THEN EXIT; END IF;
      j := 0;
      LOOP
        IF j >= i THEN EXIT; END IF;
        sum := sum + j;
        j := j + 1;
      END LOOP;
      i := i + 1;
    END LOOP;
    RETURN sum;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(1, 5)
----
project
 ├── columns: f:51
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:51]
           ├── args
           │    ├── const: 1
           │    └── const: 5
           ├── params: a:1 b:2
           └── body
                └── limit
                     ├── columns: stmt_loop_3:50
                     ├── project
                     │    ├── columns: stmt_loop_3:50
                     │    ├── project
                     │    │    ├── columns: j:5 sum:3!null i:4
                     │    │    ├── project
                     │    │    │    ├── columns: i:4 sum:3!null
                     │    │    │    ├── project
                     │    │    │    │    ├── columns: sum:3!null
                     │    │    │    │    ├── values
                     │    │    │    │    │    └── tuple
                     │    │    │    │    └── projections
                     │    │    │    │         └── const: 0 [as=sum:3]
                     │    │    │    └── projections
                     │    │    │         └── variable: a:1 [as=i:4]
                     │    │    └── projections
                     │    │         └── cast: INT8 [as=j:5]
                     │    │              └── null
                     │    └── projections
                     │         └── udf: stmt_loop_3 [as=stmt_loop_3:50]
                     │              ├── args
                     │              │    ├── variable: a:1
                     │              │    ├── variable: b:2
                     │              │    ├── variable: sum:3
                     │              │    ├── variable: i:4
                     │              │    └── variable: j:5
                     │              ├── params: a:12 b:13 sum:14 i:15 j:16
                     │              └── body
                     │                   └── project
                     │                        ├── columns: stmt_if_9:49
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── case [as=stmt_if_9:49]
                     │                                  ├── true
                     │                                  ├── when
                     │                                  │    ├── ge
                     │                                  │    │    ├── variable: i:15
                     │                                  │    │    └── variable: b:13
                     │                                  │    └── subquery
                     │                                  │         ├── tail-call
                     │                                  │         └── project
                     │                                  │              ├── columns: loop_exit_1:47
                     │                                  │              ├── values
                     │                                  │              │    └── tuple
                     │                                  │              └── projections
                     │                                  │                   └── udf: loop_exit_1 [as=loop_exit_1:47]
                     │                                  │                        ├── tail-call
                     │                                  │                        ├── args
                     │                                  │                        │    ├── variable: a:12
                     │                                  │                        │    ├── variable: b:13
                     │                                  │                        │    ├── variable: sum:14
                     │                                  │                        │    ├── variable: i:15
                     │                                  │                        │    └── variable: j:16
                     │                                  │                        ├── params: a:6 b:7 sum:8 i:9 j:10
                     │                                  │                        └── body
                     │                                  │                             └── project
                     │                                  │                                  ├── columns: stmt_return_2:11
                     │                                  │                                  ├── values
                     │                                  │                                  │    └── tuple
                     │                                  │                                  └── projections
                     │                                  │                                       └── variable: sum:8 [as=stmt_return_2:11]
                     │                                  └── subquery
                     │                                       ├── tail-call
                     │                                       └── project
                     │                                            ├── columns: stmt_if_4:48
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: stmt_if_4 [as=stmt_if_4:48]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    ├── variable: a:12
                     │                                                      │    ├── variable: b:13
                     │                                                      │    ├── variable: sum:14
                     │                                                      │    ├── variable: i:15
                     │                                                      │    └── variable: j:16
                     │                                                      ├── params: a:17 b:18 sum:19 i:20 j:21
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: stmt_loop_6:46
                     │                                                                ├── project
                     │                                                                │    ├── columns: j:22!null
                     │                                                                │    ├── values
                     │                                                                │    │    └── tuple
                     │                                                                │    └── projections
                     │                                                                │         └── const: 0 [as=j:22]
                     │                                                                └── projections
                     │                                                                     └── udf: stmt_loop_6 [as=stmt_loop_6:46]
                     │                                                                          ├── tail-call
                     │                                                                          ├── args
                     │                                                                          │    ├── variable: a:17
                     │                                                                          │    ├── variable: b:18
                     │                                                                          │    ├── variable: sum:19
                     │                                                                          │    ├── variable: i:20
                     │                                                                          │    └── variable: j:22
                     │                                                                          ├── params: a:30 b:31 sum:32 i:33 j:34
                     │                                                                          └── body
                     │                                                                               └── project
                     │                                                                                    ├── columns: stmt_if_8:45
                     │                                                                                    ├── values
                     │                                                                                    │    └── tuple
                     │                                                                                    └── projections
                     │                                                                                         └── case [as=stmt_if_8:45]
                     │                                                                                              ├── true
                     │                                                                                              ├── when
                     │                                                                                              │    ├── ge
                     │                                                                                              │    │    ├── variable: j:34
                     │                                                                                              │    │    └── variable: i:33
                     │                                                                                              │    └── subquery
                     │                                                                                              │         ├── tail-call
                     │                                                                                              │         └── project
                     │                                                                                              │              ├── columns: loop_exit_5:43
                     │                                                                                              │              ├── values
                     │                                                                                              │              │    └── tuple
                     │                                                                                              │              └── projections
                     │                                                                                              │                   └── udf: loop_exit_5 [as=loop_exit_5:43]
                     │                                                                                              │                        ├── tail-call
                     │                                                                                              │                        ├── args
                     │                                                                                              │                        │    ├── variable: a:30
                     │                                                                                              │                        │    ├── variable: b:31
                     │                                                                                              │                        │    ├── variable: sum:32
                     │                                                                                              │                        │    ├── variable: i:33
                     │                                                                                              │                        │    └── variable: j:34
                     │                                                                                              │                        ├── params: a:23 b:24 sum:25 i:26 j:27
                     │                                                                                              │                        └── body
                     │                                                                                              │                             └── project
                     │                                                                                              │                                  ├── columns: stmt_loop_3:29
                     │                                                                                              │                                  ├── project
                     │                                                                                              │                                  │    ├── columns: i:28
                     │                                                                                              │                                  │    ├── values
                     │                                                                                              │                                  │    │    └── tuple
                     │                                                                                              │                                  │    └── projections
                     │                                                                                              │                                  │         └── plus [as=i:28]
                     │                                                                                              │                                  │              ├── variable: i:26
                     │                                                                                              │                                  │              └── const: 1
                     │                                                                                              │                                  └── projections
                     │                                                                                              │                                       └── udf: stmt_loop_3 [as=stmt_loop_3:29]
                     │                                                                                              │                                            ├── tail-call
                     │                                                                                              │                                            ├── args
                     │                                                                                              │                                            │    ├── variable: a:23
                     │                                                                                              │                                            │    ├── variable: b:24
                     │                                                                                              │                                            │    ├── variable: sum:25
                     │                                                                                              │                                            │    ├── variable: i:28
                     │                                                                                              │                                            │    └── variable: j:27
                     │                                                                                              │                                            └── recursive-call
                     │                                                                                              └── subquery
                     │                                                                                                   ├── tail-call
                     │                                                                                                   └── project
                     │                                                                                                        ├── columns: stmt_if_7:44
                     │                                                                                                        ├── values
                     │                                                                                                        │    └── tuple
                     │                                                                                                        └── projections
                     │                                                                                                             └── udf: stmt_if_7 [as=stmt_if_7:44]
                     │                                                                                                                  ├── tail-call
                     │                                                                                                                  ├── args
                     │                                                                                                                  │    ├── variable: a:30
                     │                                                                                                                  │    ├── variable: b:31
                     │                                                                                                                  │    ├── variable: sum:32
                     │                                                                                                                  │    ├── variable: i:33
                     │                                                                                                                  │    └── variable: j:34
                     │                                                                                                                  ├── params: a:35 b:36 sum:37 i:38 j:39
                     │                                                                                                                  └── body
                     │                                                                                                                       └── project
                     │                                                                                                                            ├── columns: stmt_loop_6:42
                     │                                                                                                                            ├── project
                     │                                                                                                                            │    ├── columns: j:41 sum:40
                     │                                                                                                                            │    ├── project
                     │                                                                                                                            │    │    ├── columns: sum:40
                     │                                                                                                                            │    │    ├── values
                     │                                                                                                                            │    │    │    └── tuple
                     │                                                                                                                            │    │    └── projections
                     │                                                                                                                            │    │         └── plus [as=sum:40]
                     │                                                                                                                            │    │              ├── variable: sum:37
                     │                                                                                                                            │    │              └── variable: j:39
                     │                                                                                                                            │    └── projections
                     │                                                                                                                            │         └── plus [as=j:41]
                     │                                                                                                                            │              ├── variable: j:39
                     │                                                                                                                            │              └── const: 1
                     │                                                                                                                            └── projections
                     │                                                                                                                                 └── udf: stmt_loop_6 [as=stmt_loop_6:42]
                     │                                                                                                                                      ├── tail-call
                     │                                                                                                                                      ├── args
                     │                                                                                                                                      │    ├── variable: a:35
                     │                                                                                                                                      │    ├── variable: b:36
                     │                                                                                                                                      │    ├── variable: sum:40
                     │                                                                                                                                      │    ├── variable: i:38
                     │                                                                                                                                      │    └── variable: j:41
                     │                                                                                                                                      └── recursive-call
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(n INT) RETURNS INT AS $$
  DECLARE
    sum INT := 0;
    i INT := 0;
  BEGIN
    WHILE i < n LOOP
      sum := sum + i;
      i := i + 1;
    END LOOP;
    RETURN sum;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(5)
----
project
 ├── columns: f:21
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:21]
           ├── args
           │    └── const: 5
           ├── params: n:1
           └── body
                └── limit
                     ├── columns: stmt_loop_3:20
                     ├── project
                     │    ├── columns: stmt_loop_3:20
                     │    ├── project
                     │    │    ├── columns: i:3!null sum:2!null
                     │    │    ├── project
                     │    │    │    ├── columns: sum:2!null
                     │    │    │    ├── values
                     │    │    │    │    └── tuple
                     │    │    │    └── projections
                     │    │    │         └── const: 0 [as=sum:2]
                     │    │    └── projections
                     │    │         └── const: 0 [as=i:3]
                     │    └── projections
                     │         └── udf: stmt_loop_3 [as=stmt_loop_3:20]
                     │              ├── args
                     │              │    ├── variable: n:1
                     │              │    ├── variable: sum:2
                     │              │    └── variable: i:3
                     │              ├── params: n:8 sum:9 i:10
                     │              └── body
                     │                   └── project
                     │                        ├── columns: stmt_if_5:19
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── case [as=stmt_if_5:19]
                     │                                  ├── true
                     │                                  ├── when
                     │                                  │    ├── lt
                     │                                  │    │    ├── variable: i:10
                     │                                  │    │    └── variable: n:8
                     │                                  │    └── subquery
                     │                                  │         ├── tail-call
                     │                                  │         └── project
                     │                                  │              ├── columns: stmt_if_4:17
                     │                                  │              ├── project
                     │                                  │              │    ├── columns: i:16 sum:15
                     │                                  │              │    ├── project
                     │                                  │              │    │    ├── columns: sum:15
                     │                                  │              │    │    ├── values
                     │                                  │              │    │    │    └── tuple
                     │                                  │              │    │    └── projections
                     │                                  │              │    │         └── plus [as=sum:15]
                     │                                  │              │    │              ├── variable: sum:9
                     │                                  │              │    │              └── variable: i:10
                     │                                  │              │    └── projections
                     │                                  │              │         └── plus [as=i:16]
                     │                                  │              │              ├── variable: i:10
                     │                                  │              │              └── const: 1
                     │                                  │              └── projections
                     │                                  │                   └── udf: stmt_if_4 [as=stmt_if_4:17]
                     │                                  │                        ├── tail-call
                     │                                  │                        ├── args
                     │                                  │                        │    ├── variable: n:8
                     │                                  │                        │    ├── variable: sum:15
                     │                                  │                        │    └── variable: i:16
                     │                                  │                        ├── params: n:11 sum:12 i:13
                     │                                  │                        └── body
                     │                                  │                             └── project
                     │                                  │                                  ├── columns: stmt_loop_3:14
                     │                                  │                                  ├── values
                     │                                  │                                  │    └── tuple
                     │                                  │                                  └── projections
                     │                                  │                                       └── udf: stmt_loop_3 [as=stmt_loop_3:14]
                     │                                  │                                            ├── tail-call
                     │                                  │                                            ├── args
                     │                                  │                                            │    ├── variable: n:11
                     │                                  │                                            │    ├── variable: sum:12
                     │                                  │                                            │    └── variable: i:13
                     │                                  │                                            └── recursive-call
                     │                                  └── subquery
                     │                                       ├── tail-call
                     │                                       └── project
                     │                                            ├── columns: loop_exit_1:18
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: loop_exit_1 [as=loop_exit_1:18]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    ├── variable: n:8
                     │                                                      │    ├── variable: sum:9
                     │                                                      │    └── variable: i:10
                     │                                                      ├── params: n:4 sum:5 i:6
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: stmt_return_2:7
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── variable: sum:5 [as=stmt_return_2:7]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(n INT) RETURNS INT AS $$
  DECLARE
    sum INT := 0;
    i INT := 0;
  BEGIN
    LOOP
      EXIT WHEN i > n;
      sum := sum + i;
      i := i + 1;
    END LOOP;
    RETURN sum;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(5)
----
project
 ├── columns: f:21
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:21]
           ├── args
           │    └── const: 5
           ├── params: n:1
           └── body
                └── limit
                     ├── columns: stmt_loop_3:20
                     ├── project
                     │    ├── columns: stmt_loop_3:20
                     │    ├── project
                     │    │    ├── columns: i:3!null sum:2!null
                     │    │    ├── project
                     │    │    │    ├── columns: sum:2!null
                     │    │    │    ├── values
                     │    │    │    │    └── tuple
                     │    │    │    └── projections
                     │    │    │         └── const: 0 [as=sum:2]
                     │    │    └── projections
                     │    │         └── const: 0 [as=i:3]
                     │    └── projections
                     │         └── udf: stmt_loop_3 [as=stmt_loop_3:20]
                     │              ├── args
                     │              │    ├── variable: n:1
                     │              │    ├── variable: sum:2
                     │              │    └── variable: i:3
                     │              ├── params: n:8 sum:9 i:10
                     │              └── body
                     │                   └── project
                     │                        ├── columns: stmt_if_5:19
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── case [as=stmt_if_5:19]
                     │                                  ├── true
                     │                                  ├── when
                     │                                  │    ├── gt
                     │                                  │    │    ├── variable: i:10
                     │                                  │    │    └── variable: n:8
                     │                                  │    └── subquery
                     │                                  │         ├── tail-call
                     │                                  │         └── project
                     │                                  │              ├── columns: loop_exit_1:17
                     │                                  │              ├── values
                     │                                  │              │    └── tuple
                     │                                  │              └── projections
                     │                                  │                   └── udf: loop_exit_1 [as=loop_exit_1:17]
                     │                                  │                        ├── tail-call
                     │                                  │                        ├── args
                     │                                  │                        │    ├── variable: n:8
                     │                                  │                        │    ├── variable: sum:9
                     │                                  │                        │    └── variable: i:10
                     │                                  │                        ├── params: n:4 sum:5 i:6
                     │                                  │                        └── body
                     │                                  │                             └── project
                     │                                  │                                  ├── columns: stmt_return_2:7
                     │                                  │                                  ├── values
                     │                                  │                                  │    └── tuple
                     │                                  │                                  └── projections
                     │                                  │                                       └── variable: sum:5 [as=stmt_return_2:7]
                     │                                  └── subquery
                     │                                       ├── tail-call
                     │                                       └── project
                     │                                            ├── columns: stmt_if_4:18
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: stmt_if_4 [as=stmt_if_4:18]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    ├── variable: n:8
                     │                                                      │    ├── variable: sum:9
                     │                                                      │    └── variable: i:10
                     │                                                      ├── params: n:11 sum:12 i:13
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: stmt_loop_3:16
                     │                                                                ├── project
                     │                                                                │    ├── columns: i:15 sum:14
                     │                                                                │    ├── project
                     │                                                                │    │    ├── columns: sum:14
                     │                                                                │    │    ├── values
                     │                                                                │    │    │    └── tuple
                     │                                                                │    │    └── projections
                     │                                                                │    │         └── plus [as=sum:14]
                     │                                                                │    │              ├── variable: sum:12
                     │                                                                │    │              └── variable: i:13
                     │                                                                │    └── projections
                     │                                                                │         └── plus [as=i:15]
                     │                                                                │              ├── variable: i:13
                     │                                                                │              └── const: 1
                     │                                                                └── projections
                     │                                                                     └── udf: stmt_loop_3 [as=stmt_loop_3:16]
                     │                                                                          ├── tail-call
                     │                                                                          ├── args
                     │                                                                          │    ├── variable: n:11
                     │                                                                          │    ├── variable: sum:14
                     │                                                                          │    └── variable: i:15
                     │                                                                          └── recursive-call
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(n INT) RETURNS INT AS $$
  DECLARE
    sum INT := 0;
    i INT := 0;
  BEGIN
    LOOP
      CONTINUE WHEN i % 2 = 0;
      sum := sum + i;
      i := i + 1;
    END LOOP;
    RETURN sum;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(5)
----
project
 ├── columns: f:21
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:21]
           ├── args
           │    └── const: 5
           ├── params: n:1
           └── body
                └── limit
                     ├── columns: stmt_loop_3:20
                     ├── project
                     │    ├── columns: stmt_loop_3:20
                     │    ├── project
                     │    │    ├── columns: i:3!null sum:2!null
                     │    │    ├── project
                     │    │    │    ├── columns: sum:2!null
                     │    │    │    ├── values
                     │    │    │    │    └── tuple
                     │    │    │    └── projections
                     │    │    │         └── const: 0 [as=sum:2]
                     │    │    └── projections
                     │    │         └── const: 0 [as=i:3]
                     │    └── projections
                     │         └── udf: stmt_loop_3 [as=stmt_loop_3:20]
                     │              ├── args
                     │              │    ├── variable: n:1
                     │              │    ├── variable: sum:2
                     │              │    └── variable: i:3
                     │              ├── params: n:8 sum:9 i:10
                     │              └── body
                     │                   └── project
                     │                        ├── columns: stmt_if_5:19
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── case [as=stmt_if_5:19]
                     │                                  ├── true
                     │                                  ├── when
                     │                                  │    ├── eq
                     │                                  │    │    ├── mod
                     │                                  │    │    │    ├── variable: i:10
                     │                                  │    │    │    └── const: 2
                     │                                  │    │    └── const: 0
                     │                                  │    └── subquery
                     │                                  │         ├── tail-call
                     │                                  │         └── project
                     │                                  │              ├── columns: stmt_loop_3:17
                     │                                  │              ├── values
                     │                                  │              │    └── tuple
                     │                                  │              └── projections
                     │                                  │                   └── udf: stmt_loop_3 [as=stmt_loop_3:17]
                     │                                  │                        ├── tail-call
                     │                                  │                        ├── args
                     │                                  │                        │    ├── variable: n:8
                     │                                  │                        │    ├── variable: sum:9
                     │                                  │                        │    └── variable: i:10
                     │                                  │                        └── recursive-call
                     │                                  └── subquery
                     │                                       ├── tail-call
                     │                                       └── project
                     │                                            ├── columns: stmt_if_4:18
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: stmt_if_4 [as=stmt_if_4:18]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    ├── variable: n:8
                     │                                                      │    ├── variable: sum:9
                     │                                                      │    └── variable: i:10
                     │                                                      ├── params: n:11 sum:12 i:13
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: stmt_loop_3:16
                     │                                                                ├── project
                     │                                                                │    ├── columns: i:15 sum:14
                     │                                                                │    ├── project
                     │                                                                │    │    ├── columns: sum:14
                     │                                                                │    │    ├── values
                     │                                                                │    │    │    └── tuple
                     │                                                                │    │    └── projections
                     │                                                                │    │         └── plus [as=sum:14]
                     │                                                                │    │              ├── variable: sum:12
                     │                                                                │    │              └── variable: i:13
                     │                                                                │    └── projections
                     │                                                                │         └── plus [as=i:15]
                     │                                                                │              ├── variable: i:13
                     │                                                                │              └── const: 1
                     │                                                                └── projections
                     │                                                                     └── udf: stmt_loop_3 [as=stmt_loop_3:16]
                     │                                                                          ├── tail-call
                     │                                                                          ├── args
                     │                                                                          │    ├── variable: n:11
                     │                                                                          │    ├── variable: sum:14
                     │                                                                          │    └── variable: i:15
                     │                                                                          └── recursive-call
                     └── const: 1

# Testing RAISE statements.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RAISE DEBUG 'foo';
    RAISE LOG 'foo';
    RAISE INFO 'foo';
    RAISE NOTICE 'foo';
    RAISE WARNING 'foo';
    return 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:12
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:12]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":11
                     ├── project
                     │    ├── columns: "_stmt_raise_1":11
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":11]
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:1
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:1]
                     │                   │              ├── const: 'DEBUG1'
                     │                   │              ├── const: 'foo'
                     │                   │              ├── const: ''
                     │                   │              ├── const: ''
                     │                   │              └── const: '00000'
                     │                   └── project
                     │                        ├── columns: "_stmt_raise_3":10
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── udf: _stmt_raise_3 [as="_stmt_raise_3":10]
                     │                                  ├── tail-call
                     │                                  └── body
                     │                                       ├── project
                     │                                       │    ├── columns: stmt_raise_4:2
                     │                                       │    ├── values
                     │                                       │    │    └── tuple
                     │                                       │    └── projections
                     │                                       │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_4:2]
                     │                                       │              ├── const: 'LOG'
                     │                                       │              ├── const: 'foo'
                     │                                       │              ├── const: ''
                     │                                       │              ├── const: ''
                     │                                       │              └── const: '00000'
                     │                                       └── project
                     │                                            ├── columns: "_stmt_raise_5":9
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: _stmt_raise_5 [as="_stmt_raise_5":9]
                     │                                                      ├── tail-call
                     │                                                      └── body
                     │                                                           ├── project
                     │                                                           │    ├── columns: stmt_raise_6:3
                     │                                                           │    ├── values
                     │                                                           │    │    └── tuple
                     │                                                           │    └── projections
                     │                                                           │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_6:3]
                     │                                                           │              ├── const: 'INFO'
                     │                                                           │              ├── const: 'foo'
                     │                                                           │              ├── const: ''
                     │                                                           │              ├── const: ''
                     │                                                           │              └── const: '00000'
                     │                                                           └── project
                     │                                                                ├── columns: "_stmt_raise_7":8
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── udf: _stmt_raise_7 [as="_stmt_raise_7":8]
                     │                                                                          ├── tail-call
                     │                                                                          └── body
                     │                                                                               ├── project
                     │                                                                               │    ├── columns: stmt_raise_8:4
                     │                                                                               │    ├── values
                     │                                                                               │    │    └── tuple
                     │                                                                               │    └── projections
                     │                                                                               │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_8:4]
                     │                                                                               │              ├── const: 'NOTICE'
                     │                                                                               │              ├── const: 'foo'
                     │                                                                               │              ├── const: ''
                     │                                                                               │              ├── const: ''
                     │                                                                               │              └── const: '00000'
                     │                                                                               └── project
                     │                                                                                    ├── columns: "_stmt_raise_9":7
                     │                                                                                    ├── values
                     │                                                                                    │    └── tuple
                     │                                                                                    └── projections
                     │                                                                                         └── udf: _stmt_raise_9 [as="_stmt_raise_9":7]
                     │                                                                                              ├── tail-call
                     │                                                                                              └── body
                     │                                                                                                   ├── project
                     │                                                                                                   │    ├── columns: stmt_raise_10:5
                     │                                                                                                   │    ├── values
                     │                                                                                                   │    │    └── tuple
                     │                                                                                                   │    └── projections
                     │                                                                                                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_10:5]
                     │                                                                                                   │              ├── const: 'WARNING'
                     │                                                                                                   │              ├── const: 'foo'
                     │                                                                                                   │              ├── const: ''
                     │                                                                                                   │              ├── const: ''
                     │                                                                                                   │              └── const: '00000'
                     │                                                                                                   └── project
                     │                                                                                                        ├── columns: stmt_return_11:6!null
                     │                                                                                                        ├── values
                     │                                                                                                        │    └── tuple
                     │                                                                                                        └── projections
                     │                                                                                                             └── const: 0 [as=stmt_return_11:6]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RAISE NOTICE '%', 1;
    RAISE NOTICE 'foo: %, %, %', 1, 2, 3;
    RAISE NOTICE '%%';
    RAISE NOTICE '%%%', 1;
    RAISE NOTICE '%%%foo%% bar%%%% %% %%%% ba%z%', 1, 2, 3;
    RETURN 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:12
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:12]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":11
                     ├── project
                     │    ├── columns: "_stmt_raise_1":11
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":11]
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:1
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:1]
                     │                   │              ├── const: 'NOTICE'
                     │                   │              ├── concat
                     │                   │              │    ├── concat
                     │                   │              │    │    ├── const: ''
                     │                   │              │    │    └── coalesce
                     │                   │              │    │         ├── cast: STRING
                     │                   │              │    │         │    └── const: 1
                     │                   │              │    │         └── const: '<NULL>'
                     │                   │              │    └── const: ''
                     │                   │              ├── const: ''
                     │                   │              ├── const: ''
                     │                   │              └── const: '00000'
                     │                   └── project
                     │                        ├── columns: "_stmt_raise_3":10
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── udf: _stmt_raise_3 [as="_stmt_raise_3":10]
                     │                                  ├── tail-call
                     │                                  └── body
                     │                                       ├── project
                     │                                       │    ├── columns: stmt_raise_4:2
                     │                                       │    ├── values
                     │                                       │    │    └── tuple
                     │                                       │    └── projections
                     │                                       │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_4:2]
                     │                                       │              ├── const: 'NOTICE'
                     │                                       │              ├── concat
                     │                                       │              │    ├── concat
                     │                                       │              │    │    ├── concat
                     │                                       │              │    │    │    ├── concat
                     │                                       │              │    │    │    │    ├── concat
                     │                                       │              │    │    │    │    │    ├── concat
                     │                                       │              │    │    │    │    │    │    ├── const: 'foo: '
                     │                                       │              │    │    │    │    │    │    └── coalesce
                     │                                       │              │    │    │    │    │    │         ├── cast: STRING
                     │                                       │              │    │    │    │    │    │         │    └── const: 1
                     │                                       │              │    │    │    │    │    │         └── const: '<NULL>'
                     │                                       │              │    │    │    │    │    └── const: ', '
                     │                                       │              │    │    │    │    └── coalesce
                     │                                       │              │    │    │    │         ├── cast: STRING
                     │                                       │              │    │    │    │         │    └── const: 2
                     │                                       │              │    │    │    │         └── const: '<NULL>'
                     │                                       │              │    │    │    └── const: ', '
                     │                                       │              │    │    └── coalesce
                     │                                       │              │    │         ├── cast: STRING
                     │                                       │              │    │         │    └── const: 3
                     │                                       │              │    │         └── const: '<NULL>'
                     │                                       │              │    └── const: ''
                     │                                       │              ├── const: ''
                     │                                       │              ├── const: ''
                     │                                       │              └── const: '00000'
                     │                                       └── project
                     │                                            ├── columns: "_stmt_raise_5":9
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: _stmt_raise_5 [as="_stmt_raise_5":9]
                     │                                                      ├── tail-call
                     │                                                      └── body
                     │                                                           ├── project
                     │                                                           │    ├── columns: stmt_raise_6:3
                     │                                                           │    ├── values
                     │                                                           │    │    └── tuple
                     │                                                           │    └── projections
                     │                                                           │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_6:3]
                     │                                                           │              ├── const: 'NOTICE'
                     │                                                           │              ├── concat
                     │                                                           │              │    ├── concat
                     │                                                           │              │    │    ├── const: ''
                     │                                                           │              │    │    └── const: '%'
                     │                                                           │              │    └── const: ''
                     │                                                           │              ├── const: ''
                     │                                                           │              ├── const: ''
                     │                                                           │              └── const: '00000'
                     │                                                           └── project
                     │                                                                ├── columns: "_stmt_raise_7":8
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── udf: _stmt_raise_7 [as="_stmt_raise_7":8]
                     │                                                                          ├── tail-call
                     │                                                                          └── body
                     │                                                                               ├── project
                     │                                                                               │    ├── columns: stmt_raise_8:4
                     │                                                                               │    ├── values
                     │                                                                               │    │    └── tuple
                     │                                                                               │    └── projections
                     │                                                                               │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_8:4]
                     │                                                                               │              ├── const: 'NOTICE'
                     │                                                                               │              ├── concat
                     │                                                                               │              │    ├── concat
                     │                                                                               │              │    │    ├── concat
                     │                                                                               │              │    │    │    ├── concat
                     │                                                                               │              │    │    │    │    ├── const: ''
                     │                                                                               │              │    │    │    │    └── const: '%'
                     │                                                                               │              │    │    │    └── const: ''
                     │                                                                               │              │    │    └── coalesce
                     │                                                                               │              │    │         ├── cast: STRING
                     │                                                                               │              │    │         │    └── const: 1
                     │                                                                               │              │    │         └── const: '<NULL>'
                     │                                                                               │              │    └── const: ''
                     │                                                                               │              ├── const: ''
                     │                                                                               │              ├── const: ''
                     │                                                                               │              └── const: '00000'
                     │                                                                               └── project
                     │                                                                                    ├── columns: "_stmt_raise_9":7
                     │                                                                                    ├── values
                     │                                                                                    │    └── tuple
                     │                                                                                    └── projections
                     │                                                                                         └── udf: _stmt_raise_9 [as="_stmt_raise_9":7]
                     │                                                                                              ├── tail-call
                     │                                                                                              └── body
                     │                                                                                                   ├── project
                     │                                                                                                   │    ├── columns: stmt_raise_10:5
                     │                                                                                                   │    ├── values
                     │                                                                                                   │    │    └── tuple
                     │                                                                                                   │    └── projections
                     │                                                                                                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_10:5]
                     │                                                                                                   │              ├── const: 'NOTICE'
                     │                                                                                                   │              ├── concat
                     │                                                                                                   │              │    ├── concat
                     │                                                                                                   │              │    │    ├── concat
                     │                                                                                                   │              │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    ├── concat
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    ├── const: ''
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    └── const: '%'
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    └── const: ''
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    └── coalesce
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │         ├── cast: STRING
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │         │    └── const: 1
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │         └── const: '<NULL>'
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    └── const: 'foo'
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    └── const: '%'
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    │    └── const: ' bar'
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    │    └── const: '%'
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    │    └── const: ''
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    │    └── const: '%'
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    │    └── const: ' '
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    │    └── const: '%'
                     │                                                                                                   │              │    │    │    │    │    │    │    │    │    └── const: ' '
                     │                                                                                                   │              │    │    │    │    │    │    │    │    └── const: '%'
                     │                                                                                                   │              │    │    │    │    │    │    │    └── const: ''
                     │                                                                                                   │              │    │    │    │    │    │    └── const: '%'
                     │                                                                                                   │              │    │    │    │    │    └── const: ' ba'
                     │                                                                                                   │              │    │    │    │    └── coalesce
                     │                                                                                                   │              │    │    │    │         ├── cast: STRING
                     │                                                                                                   │              │    │    │    │         │    └── const: 2
                     │                                                                                                   │              │    │    │    │         └── const: '<NULL>'
                     │                                                                                                   │              │    │    │    └── const: 'z'
                     │                                                                                                   │              │    │    └── coalesce
                     │                                                                                                   │              │    │         ├── cast: STRING
                     │                                                                                                   │              │    │         │    └── const: 3
                     │                                                                                                   │              │    │         └── const: '<NULL>'
                     │                                                                                                   │              │    └── const: ''
                     │                                                                                                   │              ├── const: ''
                     │                                                                                                   │              ├── const: ''
                     │                                                                                                   │              └── const: '00000'
                     │                                                                                                   └── project
                     │                                                                                                        ├── columns: stmt_return_11:6!null
                     │                                                                                                        ├── values
                     │                                                                                                        │    └── tuple
                     │                                                                                                        └── projections
                     │                                                                                                             └── const: 0 [as=stmt_return_11:6]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RAISE NOTICE division_by_zero;
    RAISE NOTICE null_value_not_allowed;
    RAISE NOTICE reading_sql_data_not_permitted;
    RAISE NOTICE SQLSTATE '22012';
    RAISE NOTICE SQLSTATE '22004';
    RAISE NOTICE SQLSTATE '39004';
    RAISE NOTICE SQLSTATE '2F004';
    RAISE NOTICE SQLSTATE '38004';
    return 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:18
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:18]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":17
                     ├── project
                     │    ├── columns: "_stmt_raise_1":17
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":17]
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:1
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:1]
                     │                   │              ├── const: 'NOTICE'
                     │                   │              ├── const: 'division_by_zero'
                     │                   │              ├── const: ''
                     │                   │              ├── const: ''
                     │                   │              └── const: 'division_by_zero'
                     │                   └── project
                     │                        ├── columns: "_stmt_raise_3":16
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── udf: _stmt_raise_3 [as="_stmt_raise_3":16]
                     │                                  ├── tail-call
                     │                                  └── body
                     │                                       ├── project
                     │                                       │    ├── columns: stmt_raise_4:2
                     │                                       │    ├── values
                     │                                       │    │    └── tuple
                     │                                       │    └── projections
                     │                                       │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_4:2]
                     │                                       │              ├── const: 'NOTICE'
                     │                                       │              ├── const: 'null_value_not_allowed'
                     │                                       │              ├── const: ''
                     │                                       │              ├── const: ''
                     │                                       │              └── const: 'null_value_not_allowed'
                     │                                       └── project
                     │                                            ├── columns: "_stmt_raise_5":15
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: _stmt_raise_5 [as="_stmt_raise_5":15]
                     │                                                      ├── tail-call
                     │                                                      └── body
                     │                                                           ├── project
                     │                                                           │    ├── columns: stmt_raise_6:3
                     │                                                           │    ├── values
                     │                                                           │    │    └── tuple
                     │                                                           │    └── projections
                     │                                                           │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_6:3]
                     │                                                           │              ├── const: 'NOTICE'
                     │                                                           │              ├── const: 'reading_sql_data_not_permitted'
                     │                                                           │              ├── const: ''
                     │                                                           │              ├── const: ''
                     │                                                           │              └── const: 'reading_sql_data_not_permitted'
                     │                                                           └── project
                     │                                                                ├── columns: "_stmt_raise_7":14
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── udf: _stmt_raise_7 [as="_stmt_raise_7":14]
                     │                                                                          ├── tail-call
                     │                                                                          └── body
                     │                                                                               ├── project
                     │                                                                               │    ├── columns: stmt_raise_8:4
                     │                                                                               │    ├── values
                     │                                                                               │    │    └── tuple
                     │                                                                               │    └── projections
                     │                                                                               │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_8:4]
                     │                                                                               │              ├── const: 'NOTICE'
                     │                                                                               │              ├── const: '22012'
                     │                                                                               │              ├── const: ''
                     │                                                                               │              ├── const: ''
                     │                                                                               │              └── const: '22012'
                     │                                                                               └── project
                     │                                                                                    ├── columns: "_stmt_raise_9":13
                     │                                                                                    ├── values
                     │                                                                                    │    └── tuple
                     │                                                                                    └── projections
                     │                                                                                         └── udf: _stmt_raise_9 [as="_stmt_raise_9":13]
                     │                                                                                              ├── tail-call
                     │                                                                                              └── body
                     │                                                                                                   ├── project
                     │                                                                                                   │    ├── columns: stmt_raise_10:5
                     │                                                                                                   │    ├── values
                     │                                                                                                   │    │    └── tuple
                     │                                                                                                   │    └── projections
                     │                                                                                                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_10:5]
                     │                                                                                                   │              ├── const: 'NOTICE'
                     │                                                                                                   │              ├── const: '22004'
                     │                                                                                                   │              ├── const: ''
                     │                                                                                                   │              ├── const: ''
                     │                                                                                                   │              └── const: '22004'
                     │                                                                                                   └── project
                     │                                                                                                        ├── columns: "_stmt_raise_11":12
                     │                                                                                                        ├── values
                     │                                                                                                        │    └── tuple
                     │                                                                                                        └── projections
                     │                                                                                                             └── udf: _stmt_raise_11 [as="_stmt_raise_11":12]
                     │                                                                                                                  ├── tail-call
                     │                                                                                                                  └── body
                     │                                                                                                                       ├── project
                     │                                                                                                                       │    ├── columns: stmt_raise_12:6
                     │                                                                                                                       │    ├── values
                     │                                                                                                                       │    │    └── tuple
                     │                                                                                                                       │    └── projections
                     │                                                                                                                       │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_12:6]
                     │                                                                                                                       │              ├── const: 'NOTICE'
                     │                                                                                                                       │              ├── const: '39004'
                     │                                                                                                                       │              ├── const: ''
                     │                                                                                                                       │              ├── const: ''
                     │                                                                                                                       │              └── const: '39004'
                     │                                                                                                                       └── project
                     │                                                                                                                            ├── columns: "_stmt_raise_13":11
                     │                                                                                                                            ├── values
                     │                                                                                                                            │    └── tuple
                     │                                                                                                                            └── projections
                     │                                                                                                                                 └── udf: _stmt_raise_13 [as="_stmt_raise_13":11]
                     │                                                                                                                                      ├── tail-call
                     │                                                                                                                                      └── body
                     │                                                                                                                                           ├── project
                     │                                                                                                                                           │    ├── columns: stmt_raise_14:7
                     │                                                                                                                                           │    ├── values
                     │                                                                                                                                           │    │    └── tuple
                     │                                                                                                                                           │    └── projections
                     │                                                                                                                                           │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_14:7]
                     │                                                                                                                                           │              ├── const: 'NOTICE'
                     │                                                                                                                                           │              ├── const: '2F004'
                     │                                                                                                                                           │              ├── const: ''
                     │                                                                                                                                           │              ├── const: ''
                     │                                                                                                                                           │              └── const: '2F004'
                     │                                                                                                                                           └── project
                     │                                                                                                                                                ├── columns: "_stmt_raise_15":10
                     │                                                                                                                                                ├── values
                     │                                                                                                                                                │    └── tuple
                     │                                                                                                                                                └── projections
                     │                                                                                                                                                     └── udf: _stmt_raise_15 [as="_stmt_raise_15":10]
                     │                                                                                                                                                          ├── tail-call
                     │                                                                                                                                                          └── body
                     │                                                                                                                                                               ├── project
                     │                                                                                                                                                               │    ├── columns: stmt_raise_16:8
                     │                                                                                                                                                               │    ├── values
                     │                                                                                                                                                               │    │    └── tuple
                     │                                                                                                                                                               │    └── projections
                     │                                                                                                                                                               │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_16:8]
                     │                                                                                                                                                               │              ├── const: 'NOTICE'
                     │                                                                                                                                                               │              ├── const: '38004'
                     │                                                                                                                                                               │              ├── const: ''
                     │                                                                                                                                                               │              ├── const: ''
                     │                                                                                                                                                               │              └── const: '38004'
                     │                                                                                                                                                               └── project
                     │                                                                                                                                                                    ├── columns: stmt_return_17:9!null
                     │                                                                                                                                                                    ├── values
                     │                                                                                                                                                                    │    └── tuple
                     │                                                                                                                                                                    └── projections
                     │                                                                                                                                                                         └── const: 0 [as=stmt_return_17:9]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RAISE NOTICE USING MESSAGE = 'foo';
    RAISE NOTICE USING MESSAGE = format('%s %s!','Hello','World');
    RAISE NOTICE USING MESSAGE = 'foo', DETAIL = 'bar', HINT = 'baz';
    RAISE NOTICE 'foo' USING ERRCODE = 'division_by_zero';
    RAISE NOTICE 'foo' USING ERRCODE = '22012';
    -- If no message is specified, the error code is used.
    RAISE NOTICE USING ERRCODE = 'division_by_zero';
    RETURN 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:14
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:14]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":13
                     ├── project
                     │    ├── columns: "_stmt_raise_1":13
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":13]
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:1
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:1]
                     │                   │              ├── const: 'NOTICE'
                     │                   │              ├── const: 'foo'
                     │                   │              ├── const: ''
                     │                   │              ├── const: ''
                     │                   │              └── const: '00000'
                     │                   └── project
                     │                        ├── columns: "_stmt_raise_3":12
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── udf: _stmt_raise_3 [as="_stmt_raise_3":12]
                     │                                  ├── tail-call
                     │                                  └── body
                     │                                       ├── project
                     │                                       │    ├── columns: stmt_raise_4:2
                     │                                       │    ├── values
                     │                                       │    │    └── tuple
                     │                                       │    └── projections
                     │                                       │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_4:2]
                     │                                       │              ├── const: 'NOTICE'
                     │                                       │              ├── function: format
                     │                                       │              │    ├── const: '%s %s!'
                     │                                       │              │    ├── const: 'Hello'
                     │                                       │              │    └── const: 'World'
                     │                                       │              ├── const: ''
                     │                                       │              ├── const: ''
                     │                                       │              └── const: '00000'
                     │                                       └── project
                     │                                            ├── columns: "_stmt_raise_5":11
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: _stmt_raise_5 [as="_stmt_raise_5":11]
                     │                                                      ├── tail-call
                     │                                                      └── body
                     │                                                           ├── project
                     │                                                           │    ├── columns: stmt_raise_6:3
                     │                                                           │    ├── values
                     │                                                           │    │    └── tuple
                     │                                                           │    └── projections
                     │                                                           │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_6:3]
                     │                                                           │              ├── const: 'NOTICE'
                     │                                                           │              ├── const: 'foo'
                     │                                                           │              ├── const: 'bar'
                     │                                                           │              ├── const: 'baz'
                     │                                                           │              └── const: '00000'
                     │                                                           └── project
                     │                                                                ├── columns: "_stmt_raise_7":10
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── udf: _stmt_raise_7 [as="_stmt_raise_7":10]
                     │                                                                          ├── tail-call
                     │                                                                          └── body
                     │                                                                               ├── project
                     │                                                                               │    ├── columns: stmt_raise_8:4
                     │                                                                               │    ├── values
                     │                                                                               │    │    └── tuple
                     │                                                                               │    └── projections
                     │                                                                               │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_8:4]
                     │                                                                               │              ├── const: 'NOTICE'
                     │                                                                               │              ├── const: 'foo'
                     │                                                                               │              ├── const: ''
                     │                                                                               │              ├── const: ''
                     │                                                                               │              └── const: 'division_by_zero'
                     │                                                                               └── project
                     │                                                                                    ├── columns: "_stmt_raise_9":9
                     │                                                                                    ├── values
                     │                                                                                    │    └── tuple
                     │                                                                                    └── projections
                     │                                                                                         └── udf: _stmt_raise_9 [as="_stmt_raise_9":9]
                     │                                                                                              ├── tail-call
                     │                                                                                              └── body
                     │                                                                                                   ├── project
                     │                                                                                                   │    ├── columns: stmt_raise_10:5
                     │                                                                                                   │    ├── values
                     │                                                                                                   │    │    └── tuple
                     │                                                                                                   │    └── projections
                     │                                                                                                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_10:5]
                     │                                                                                                   │              ├── const: 'NOTICE'
                     │                                                                                                   │              ├── const: 'foo'
                     │                                                                                                   │              ├── const: ''
                     │                                                                                                   │              ├── const: ''
                     │                                                                                                   │              └── const: '22012'
                     │                                                                                                   └── project
                     │                                                                                                        ├── columns: "_stmt_raise_11":8
                     │                                                                                                        ├── values
                     │                                                                                                        │    └── tuple
                     │                                                                                                        └── projections
                     │                                                                                                             └── udf: _stmt_raise_11 [as="_stmt_raise_11":8]
                     │                                                                                                                  ├── tail-call
                     │                                                                                                                  └── body
                     │                                                                                                                       ├── project
                     │                                                                                                                       │    ├── columns: stmt_raise_12:6
                     │                                                                                                                       │    ├── values
                     │                                                                                                                       │    │    └── tuple
                     │                                                                                                                       │    └── projections
                     │                                                                                                                       │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_12:6]
                     │                                                                                                                       │              ├── const: 'NOTICE'
                     │                                                                                                                       │              ├── const: 'division_by_zero'
                     │                                                                                                                       │              ├── const: ''
                     │                                                                                                                       │              ├── const: ''
                     │                                                                                                                       │              └── const: 'division_by_zero'
                     │                                                                                                                       └── project
                     │                                                                                                                            ├── columns: stmt_return_13:7!null
                     │                                                                                                                            ├── values
                     │                                                                                                                            │    └── tuple
                     │                                                                                                                            └── projections
                     │                                                                                                                                 └── const: 0 [as=stmt_return_13:7]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  DECLARE
    i INT := 0;
  BEGIN
    RAISE NOTICE '1: i = %', i;
    i := 100;
    RAISE NOTICE '2: i = %', i;
    i := (SELECT count(*) FROM xy);
    RAISE NOTICE '3: i = %', i;
    RAISE NOTICE 'max_x: %', (SELECT max(x) FROM xy);
    return i;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:29
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:29]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":28
                     ├── project
                     │    ├── columns: "_stmt_raise_1":28
                     │    ├── barrier
                     │    │    ├── columns: i:1!null
                     │    │    └── project
                     │    │         ├── columns: i:1!null
                     │    │         ├── values
                     │    │         │    └── tuple
                     │    │         └── projections
                     │    │              └── const: 0 [as=i:1]
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":28]
                     │              ├── args
                     │              │    └── variable: i:1
                     │              ├── params: i:2
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:3
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:3]
                     │                   │              ├── const: 'NOTICE'
                     │                   │              ├── concat
                     │                   │              │    ├── concat
                     │                   │              │    │    ├── const: '1: i = '
                     │                   │              │    │    └── coalesce
                     │                   │              │    │         ├── cast: STRING
                     │                   │              │    │         │    └── variable: i:2
                     │                   │              │    │         └── const: '<NULL>'
                     │                   │              │    └── const: ''
                     │                   │              ├── const: ''
                     │                   │              ├── const: ''
                     │                   │              └── const: '00000'
                     │                   └── project
                     │                        ├── columns: "_stmt_raise_3":27
                     │                        ├── barrier
                     │                        │    ├── columns: i:4!null
                     │                        │    └── project
                     │                        │         ├── columns: i:4!null
                     │                        │         ├── values
                     │                        │         │    └── tuple
                     │                        │         └── projections
                     │                        │              └── const: 100 [as=i:4]
                     │                        └── projections
                     │                             └── udf: _stmt_raise_3 [as="_stmt_raise_3":27]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    └── variable: i:4
                     │                                  ├── params: i:5
                     │                                  └── body
                     │                                       ├── project
                     │                                       │    ├── columns: stmt_raise_4:6
                     │                                       │    ├── values
                     │                                       │    │    └── tuple
                     │                                       │    └── projections
                     │                                       │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_4:6]
                     │                                       │              ├── const: 'NOTICE'
                     │                                       │              ├── concat
                     │                                       │              │    ├── concat
                     │                                       │              │    │    ├── const: '2: i = '
                     │                                       │              │    │    └── coalesce
                     │                                       │              │    │         ├── cast: STRING
                     │                                       │              │    │         │    └── variable: i:5
                     │                                       │              │    │         └── const: '<NULL>'
                     │                                       │              │    └── const: ''
                     │                                       │              ├── const: ''
                     │                                       │              ├── const: ''
                     │                                       │              └── const: '00000'
                     │                                       └── project
                     │                                            ├── columns: "_stmt_raise_5":26
                     │                                            ├── barrier
                     │                                            │    ├── columns: i:13
                     │                                            │    └── project
                     │                                            │         ├── columns: i:13
                     │                                            │         ├── values
                     │                                            │         │    └── tuple
                     │                                            │         └── projections
                     │                                            │              └── subquery [as=i:13]
                     │                                            │                   └── max1-row
                     │                                            │                        ├── columns: count_rows:12!null
                     │                                            │                        └── scalar-group-by
                     │                                            │                             ├── columns: count_rows:12!null
                     │                                            │                             ├── project
                     │                                            │                             │    └── scan xy
                     │                                            │                             │         └── columns: x:7 y:8 rowid:9!null crdb_internal_mvcc_timestamp:10 tableoid:11
                     │                                            │                             └── aggregations
                     │                                            │                                  └── count-rows [as=count_rows:12]
                     │                                            └── projections
                     │                                                 └── udf: _stmt_raise_5 [as="_stmt_raise_5":26]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    └── variable: i:13
                     │                                                      ├── params: i:14
                     │                                                      └── body
                     │                                                           ├── project
                     │                                                           │    ├── columns: stmt_raise_6:15
                     │                                                           │    ├── values
                     │                                                           │    │    └── tuple
                     │                                                           │    └── projections
                     │                                                           │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_6:15]
                     │                                                           │              ├── const: 'NOTICE'
                     │                                                           │              ├── concat
                     │                                                           │              │    ├── concat
                     │                                                           │              │    │    ├── const: '3: i = '
                     │                                                           │              │    │    └── coalesce
                     │                                                           │              │    │         ├── cast: STRING
                     │                                                           │              │    │         │    └── variable: i:14
                     │                                                           │              │    │         └── const: '<NULL>'
                     │                                                           │              │    └── const: ''
                     │                                                           │              ├── const: ''
                     │                                                           │              ├── const: ''
                     │                                                           │              └── const: '00000'
                     │                                                           └── project
                     │                                                                ├── columns: "_stmt_raise_7":25
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── udf: _stmt_raise_7 [as="_stmt_raise_7":25]
                     │                                                                          ├── tail-call
                     │                                                                          ├── args
                     │                                                                          │    └── variable: i:14
                     │                                                                          ├── params: i:16
                     │                                                                          └── body
                     │                                                                               ├── project
                     │                                                                               │    ├── columns: stmt_raise_8:23
                     │                                                                               │    ├── values
                     │                                                                               │    │    └── tuple
                     │                                                                               │    └── projections
                     │                                                                               │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_8:23]
                     │                                                                               │              ├── const: 'NOTICE'
                     │                                                                               │              ├── concat
                     │                                                                               │              │    ├── concat
                     │                                                                               │              │    │    ├── const: 'max_x: '
                     │                                                                               │              │    │    └── coalesce
                     │                                                                               │              │    │         ├── cast: STRING
                     │                                                                               │              │    │         │    └── subquery
                     │                                                                               │              │    │         │         └── max1-row
                     │                                                                               │              │    │         │              ├── columns: max:22
                     │                                                                               │              │    │         │              └── scalar-group-by
                     │                                                                               │              │    │         │                   ├── columns: max:22
                     │                                                                               │              │    │         │                   ├── project
                     │                                                                               │              │    │         │                   │    ├── columns: x:17
                     │                                                                               │              │    │         │                   │    └── scan xy
                     │                                                                               │              │    │         │                   │         └── columns: x:17 y:18 rowid:19!null crdb_internal_mvcc_timestamp:20 tableoid:21
                     │                                                                               │              │    │         │                   └── aggregations
                     │                                                                               │              │    │         │                        └── max [as=max:22]
                     │                                                                               │              │    │         │                             └── variable: x:17
                     │                                                                               │              │    │         └── const: '<NULL>'
                     │                                                                               │              │    └── const: ''
                     │                                                                               │              ├── const: ''
                     │                                                                               │              ├── const: ''
                     │                                                                               │              └── const: '00000'
                     │                                                                               └── project
                     │                                                                                    ├── columns: stmt_return_9:24
                     │                                                                                    ├── values
                     │                                                                                    │    └── tuple
                     │                                                                                    └── projections
                     │                                                                                         └── variable: i:16 [as=stmt_return_9:24]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  DECLARE
    i INT := 0;
  BEGIN
    LOOP
      IF i >= 5 THEN EXIT; END IF;
      RAISE NOTICE 'i = %', i;
      i := i + 1;
    END LOOP;
    RAISE NOTICE 'finished with i = %', i;
    RETURN 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:18
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:18]
           └── body
                └── limit
                     ├── columns: stmt_loop_5:17
                     ├── project
                     │    ├── columns: stmt_loop_5:17
                     │    ├── barrier
                     │    │    ├── columns: i:1!null
                     │    │    └── project
                     │    │         ├── columns: i:1!null
                     │    │         ├── values
                     │    │         │    └── tuple
                     │    │         └── projections
                     │    │              └── const: 0 [as=i:1]
                     │    └── projections
                     │         └── udf: stmt_loop_5 [as=stmt_loop_5:17]
                     │              ├── args
                     │              │    └── variable: i:1
                     │              ├── params: i:7
                     │              └── body
                     │                   └── project
                     │                        ├── columns: stmt_if_9:16
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── case [as=stmt_if_9:16]
                     │                                  ├── true
                     │                                  ├── when
                     │                                  │    ├── ge
                     │                                  │    │    ├── variable: i:7
                     │                                  │    │    └── const: 5
                     │                                  │    └── subquery
                     │                                  │         ├── tail-call
                     │                                  │         └── project
                     │                                  │              ├── columns: loop_exit_1:14
                     │                                  │              ├── values
                     │                                  │              │    └── tuple
                     │                                  │              └── projections
                     │                                  │                   └── udf: loop_exit_1 [as=loop_exit_1:14]
                     │                                  │                        ├── tail-call
                     │                                  │                        ├── args
                     │                                  │                        │    └── variable: i:7
                     │                                  │                        ├── params: i:2
                     │                                  │                        └── body
                     │                                  │                             └── project
                     │                                  │                                  ├── columns: "_stmt_raise_2":6
                     │                                  │                                  ├── values
                     │                                  │                                  │    └── tuple
                     │                                  │                                  └── projections
                     │                                  │                                       └── udf: _stmt_raise_2 [as="_stmt_raise_2":6]
                     │                                  │                                            ├── tail-call
                     │                                  │                                            ├── args
                     │                                  │                                            │    └── variable: i:2
                     │                                  │                                            ├── params: i:3
                     │                                  │                                            └── body
                     │                                  │                                                 ├── project
                     │                                  │                                                 │    ├── columns: stmt_raise_3:4
                     │                                  │                                                 │    ├── values
                     │                                  │                                                 │    │    └── tuple
                     │                                  │                                                 │    └── projections
                     │                                  │                                                 │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_3:4]
                     │                                  │                                                 │              ├── const: 'NOTICE'
                     │                                  │                                                 │              ├── concat
                     │                                  │                                                 │              │    ├── concat
                     │                                  │                                                 │              │    │    ├── const: 'finished with i = '
                     │                                  │                                                 │              │    │    └── coalesce
                     │                                  │                                                 │              │    │         ├── cast: STRING
                     │                                  │                                                 │              │    │         │    └── variable: i:3
                     │                                  │                                                 │              │    │         └── const: '<NULL>'
                     │                                  │                                                 │              │    └── const: ''
                     │                                  │                                                 │              ├── const: ''
                     │                                  │                                                 │              ├── const: ''
                     │                                  │                                                 │              └── const: '00000'
                     │                                  │                                                 └── project
                     │                                  │                                                      ├── columns: stmt_return_4:5!null
                     │                                  │                                                      ├── values
                     │                                  │                                                      │    └── tuple
                     │                                  │                                                      └── projections
                     │                                  │                                                           └── const: 0 [as=stmt_return_4:5]
                     │                                  └── subquery
                     │                                       ├── tail-call
                     │                                       └── project
                     │                                            ├── columns: stmt_if_6:15
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: stmt_if_6 [as=stmt_if_6:15]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    └── variable: i:7
                     │                                                      ├── params: i:8
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: "_stmt_raise_7":13
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── udf: _stmt_raise_7 [as="_stmt_raise_7":13]
                     │                                                                          ├── tail-call
                     │                                                                          ├── args
                     │                                                                          │    └── variable: i:8
                     │                                                                          ├── params: i:9
                     │                                                                          └── body
                     │                                                                               ├── project
                     │                                                                               │    ├── columns: stmt_raise_8:10
                     │                                                                               │    ├── values
                     │                                                                               │    │    └── tuple
                     │                                                                               │    └── projections
                     │                                                                               │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_8:10]
                     │                                                                               │              ├── const: 'NOTICE'
                     │                                                                               │              ├── concat
                     │                                                                               │              │    ├── concat
                     │                                                                               │              │    │    ├── const: 'i = '
                     │                                                                               │              │    │    └── coalesce
                     │                                                                               │              │    │         ├── cast: STRING
                     │                                                                               │              │    │         │    └── variable: i:9
                     │                                                                               │              │    │         └── const: '<NULL>'
                     │                                                                               │              │    └── const: ''
                     │                                                                               │              ├── const: ''
                     │                                                                               │              ├── const: ''
                     │                                                                               │              └── const: '00000'
                     │                                                                               └── project
                     │                                                                                    ├── columns: stmt_loop_5:12
                     │                                                                                    ├── project
                     │                                                                                    │    ├── columns: i:11
                     │                                                                                    │    ├── values
                     │                                                                                    │    │    └── tuple
                     │                                                                                    │    └── projections
                     │                                                                                    │         └── plus [as=i:11]
                     │                                                                                    │              ├── variable: i:9
                     │                                                                                    │              └── const: 1
                     │                                                                                    └── projections
                     │                                                                                         └── udf: stmt_loop_5 [as=stmt_loop_5:12]
                     │                                                                                              ├── tail-call
                     │                                                                                              ├── args
                     │                                                                                              │    └── variable: i:11
                     │                                                                                              └── recursive-call
                     └── const: 1

# Testing RAISE statement with EXCEPTION log level.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RAISE EXCEPTION 'foo';
    return 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:4
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:4]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":3
                     ├── project
                     │    ├── columns: "_stmt_raise_1":3
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":3]
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:1
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:1]
                     │                   │              ├── const: 'ERROR'
                     │                   │              ├── const: 'foo'
                     │                   │              ├── const: ''
                     │                   │              ├── const: ''
                     │                   │              └── const: 'P0001'
                     │                   └── project
                     │                        ├── columns: stmt_return_3:2!null
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── const: 0 [as=stmt_return_3:2]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RAISE EXCEPTION division_by_zero;
    return 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:4
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:4]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":3
                     ├── project
                     │    ├── columns: "_stmt_raise_1":3
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":3]
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:1
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:1]
                     │                   │              ├── const: 'ERROR'
                     │                   │              ├── const: 'division_by_zero'
                     │                   │              ├── const: ''
                     │                   │              ├── const: ''
                     │                   │              └── const: 'division_by_zero'
                     │                   └── project
                     │                        ├── columns: stmt_return_3:2!null
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── const: 0 [as=stmt_return_3:2]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RAISE EXCEPTION SQLSTATE '22012';
    return 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:4
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:4]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":3
                     ├── project
                     │    ├── columns: "_stmt_raise_1":3
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":3]
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:1
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:1]
                     │                   │              ├── const: 'ERROR'
                     │                   │              ├── const: '22012'
                     │                   │              ├── const: ''
                     │                   │              ├── const: ''
                     │                   │              └── const: '22012'
                     │                   └── project
                     │                        ├── columns: stmt_return_3:2!null
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── const: 0 [as=stmt_return_3:2]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  DECLARE
    i INT := 0;
  BEGIN
    LOOP
      IF i >= 5 THEN EXIT; END IF;
      IF i = 3 THEN
        RAISE EXCEPTION 'i = %', i;
      END IF;
      RAISE NOTICE 'i = %', i;
      i := i + 1;
    END LOOP;
    RAISE NOTICE 'finished with i = %', i;
    RETURN 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:25
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:25]
           └── body
                └── limit
                     ├── columns: stmt_loop_5:24
                     ├── project
                     │    ├── columns: stmt_loop_5:24
                     │    ├── barrier
                     │    │    ├── columns: i:1!null
                     │    │    └── project
                     │    │         ├── columns: i:1!null
                     │    │         ├── values
                     │    │         │    └── tuple
                     │    │         └── projections
                     │    │              └── const: 0 [as=i:1]
                     │    └── projections
                     │         └── udf: stmt_loop_5 [as=stmt_loop_5:24]
                     │              ├── args
                     │              │    └── variable: i:1
                     │              ├── params: i:7
                     │              └── body
                     │                   └── project
                     │                        ├── columns: stmt_if_13:23
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── case [as=stmt_if_13:23]
                     │                                  ├── true
                     │                                  ├── when
                     │                                  │    ├── ge
                     │                                  │    │    ├── variable: i:7
                     │                                  │    │    └── const: 5
                     │                                  │    └── subquery
                     │                                  │         ├── tail-call
                     │                                  │         └── project
                     │                                  │              ├── columns: loop_exit_1:21
                     │                                  │              ├── values
                     │                                  │              │    └── tuple
                     │                                  │              └── projections
                     │                                  │                   └── udf: loop_exit_1 [as=loop_exit_1:21]
                     │                                  │                        ├── tail-call
                     │                                  │                        ├── args
                     │                                  │                        │    └── variable: i:7
                     │                                  │                        ├── params: i:2
                     │                                  │                        └── body
                     │                                  │                             └── project
                     │                                  │                                  ├── columns: "_stmt_raise_2":6
                     │                                  │                                  ├── values
                     │                                  │                                  │    └── tuple
                     │                                  │                                  └── projections
                     │                                  │                                       └── udf: _stmt_raise_2 [as="_stmt_raise_2":6]
                     │                                  │                                            ├── tail-call
                     │                                  │                                            ├── args
                     │                                  │                                            │    └── variable: i:2
                     │                                  │                                            ├── params: i:3
                     │                                  │                                            └── body
                     │                                  │                                                 ├── project
                     │                                  │                                                 │    ├── columns: stmt_raise_3:4
                     │                                  │                                                 │    ├── values
                     │                                  │                                                 │    │    └── tuple
                     │                                  │                                                 │    └── projections
                     │                                  │                                                 │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_3:4]
                     │                                  │                                                 │              ├── const: 'NOTICE'
                     │                                  │                                                 │              ├── concat
                     │                                  │                                                 │              │    ├── concat
                     │                                  │                                                 │              │    │    ├── const: 'finished with i = '
                     │                                  │                                                 │              │    │    └── coalesce
                     │                                  │                                                 │              │    │         ├── cast: STRING
                     │                                  │                                                 │              │    │         │    └── variable: i:3
                     │                                  │                                                 │              │    │         └── const: '<NULL>'
                     │                                  │                                                 │              │    └── const: ''
                     │                                  │                                                 │              ├── const: ''
                     │                                  │                                                 │              ├── const: ''
                     │                                  │                                                 │              └── const: '00000'
                     │                                  │                                                 └── project
                     │                                  │                                                      ├── columns: stmt_return_4:5!null
                     │                                  │                                                      ├── values
                     │                                  │                                                      │    └── tuple
                     │                                  │                                                      └── projections
                     │                                  │                                                           └── const: 0 [as=stmt_return_4:5]
                     │                                  └── subquery
                     │                                       ├── tail-call
                     │                                       └── project
                     │                                            ├── columns: stmt_if_6:22
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: stmt_if_6 [as=stmt_if_6:22]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    └── variable: i:7
                     │                                                      ├── params: i:8
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: stmt_if_12:20
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── case [as=stmt_if_12:20]
                     │                                                                          ├── true
                     │                                                                          ├── when
                     │                                                                          │    ├── eq
                     │                                                                          │    │    ├── variable: i:8
                     │                                                                          │    │    └── const: 3
                     │                                                                          │    └── subquery
                     │                                                                          │         ├── tail-call
                     │                                                                          │         └── project
                     │                                                                          │              ├── columns: "_stmt_raise_10":18
                     │                                                                          │              ├── values
                     │                                                                          │              │    └── tuple
                     │                                                                          │              └── projections
                     │                                                                          │                   └── udf: _stmt_raise_10 [as="_stmt_raise_10":18]
                     │                                                                          │                        ├── tail-call
                     │                                                                          │                        ├── args
                     │                                                                          │                        │    └── variable: i:8
                     │                                                                          │                        ├── params: i:15
                     │                                                                          │                        └── body
                     │                                                                          │                             ├── project
                     │                                                                          │                             │    ├── columns: stmt_raise_11:16
                     │                                                                          │                             │    ├── values
                     │                                                                          │                             │    │    └── tuple
                     │                                                                          │                             │    └── projections
                     │                                                                          │                             │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_11:16]
                     │                                                                          │                             │              ├── const: 'ERROR'
                     │                                                                          │                             │              ├── concat
                     │                                                                          │                             │              │    ├── concat
                     │                                                                          │                             │              │    │    ├── const: 'i = '
                     │                                                                          │                             │              │    │    └── coalesce
                     │                                                                          │                             │              │    │         ├── cast: STRING
                     │                                                                          │                             │              │    │         │    └── variable: i:15
                     │                                                                          │                             │              │    │         └── const: '<NULL>'
                     │                                                                          │                             │              │    └── const: ''
                     │                                                                          │                             │              ├── const: ''
                     │                                                                          │                             │              ├── const: ''
                     │                                                                          │                             │              └── const: 'P0001'
                     │                                                                          │                             └── project
                     │                                                                          │                                  ├── columns: stmt_if_7:17
                     │                                                                          │                                  ├── values
                     │                                                                          │                                  │    └── tuple
                     │                                                                          │                                  └── projections
                     │                                                                          │                                       └── udf: stmt_if_7 [as=stmt_if_7:17]
                     │                                                                          │                                            ├── tail-call
                     │                                                                          │                                            ├── args
                     │                                                                          │                                            │    └── variable: i:15
                     │                                                                          │                                            ├── params: i:9
                     │                                                                          │                                            └── body
                     │                                                                          │                                                 └── project
                     │                                                                          │                                                      ├── columns: "_stmt_raise_8":14
                     │                                                                          │                                                      ├── values
                     │                                                                          │                                                      │    └── tuple
                     │                                                                          │                                                      └── projections
                     │                                                                          │                                                           └── udf: _stmt_raise_8 [as="_stmt_raise_8":14]
                     │                                                                          │                                                                ├── tail-call
                     │                                                                          │                                                                ├── args
                     │                                                                          │                                                                │    └── variable: i:9
                     │                                                                          │                                                                ├── params: i:10
                     │                                                                          │                                                                └── body
                     │                                                                          │                                                                     ├── project
                     │                                                                          │                                                                     │    ├── columns: stmt_raise_9:11
                     │                                                                          │                                                                     │    ├── values
                     │                                                                          │                                                                     │    │    └── tuple
                     │                                                                          │                                                                     │    └── projections
                     │                                                                          │                                                                     │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_9:11]
                     │                                                                          │                                                                     │              ├── const: 'NOTICE'
                     │                                                                          │                                                                     │              ├── concat
                     │                                                                          │                                                                     │              │    ├── concat
                     │                                                                          │                                                                     │              │    │    ├── const: 'i = '
                     │                                                                          │                                                                     │              │    │    └── coalesce
                     │                                                                          │                                                                     │              │    │         ├── cast: STRING
                     │                                                                          │                                                                     │              │    │         │    └── variable: i:10
                     │                                                                          │                                                                     │              │    │         └── const: '<NULL>'
                     │                                                                          │                                                                     │              │    └── const: ''
                     │                                                                          │                                                                     │              ├── const: ''
                     │                                                                          │                                                                     │              ├── const: ''
                     │                                                                          │                                                                     │              └── const: '00000'
                     │                                                                          │                                                                     └── project
                     │                                                                          │                                                                          ├── columns: stmt_loop_5:13
                     │                                                                          │                                                                          ├── project
                     │                                                                          │                                                                          │    ├── columns: i:12
                     │                                                                          │                                                                          │    ├── values
                     │                                                                          │                                                                          │    │    └── tuple
                     │                                                                          │                                                                          │    └── projections
                     │                                                                          │                                                                          │         └── plus [as=i:12]
                     │                                                                          │                                                                          │              ├── variable: i:10
                     │                                                                          │                                                                          │              └── const: 1
                     │                                                                          │                                                                          └── projections
                     │                                                                          │                                                                               └── udf: stmt_loop_5 [as=stmt_loop_5:13]
                     │                                                                          │                                                                                    ├── tail-call
                     │                                                                          │                                                                                    ├── args
                     │                                                                          │                                                                                    │    └── variable: i:12
                     │                                                                          │                                                                                    └── recursive-call
                     │                                                                          └── subquery
                     │                                                                               ├── tail-call
                     │                                                                               └── project
                     │                                                                                    ├── columns: stmt_if_7:19
                     │                                                                                    ├── values
                     │                                                                                    │    └── tuple
                     │                                                                                    └── projections
                     │                                                                                         └── udf: stmt_if_7 [as=stmt_if_7:19]
                     │                                                                                              ├── tail-call
                     │                                                                                              ├── args
                     │                                                                                              │    └── variable: i:8
                     │                                                                                              ├── params: i:9
                     │                                                                                              └── body
                     │                                                                                                   └── project
                     │                                                                                                        ├── columns: "_stmt_raise_8":14
                     │                                                                                                        ├── values
                     │                                                                                                        │    └── tuple
                     │                                                                                                        └── projections
                     │                                                                                                             └── udf: _stmt_raise_8 [as="_stmt_raise_8":14]
                     │                                                                                                                  ├── tail-call
                     │                                                                                                                  ├── args
                     │                                                                                                                  │    └── variable: i:9
                     │                                                                                                                  ├── params: i:10
                     │                                                                                                                  └── body
                     │                                                                                                                       ├── project
                     │                                                                                                                       │    ├── columns: stmt_raise_9:11
                     │                                                                                                                       │    ├── values
                     │                                                                                                                       │    │    └── tuple
                     │                                                                                                                       │    └── projections
                     │                                                                                                                       │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_9:11]
                     │                                                                                                                       │              ├── const: 'NOTICE'
                     │                                                                                                                       │              ├── concat
                     │                                                                                                                       │              │    ├── concat
                     │                                                                                                                       │              │    │    ├── const: 'i = '
                     │                                                                                                                       │              │    │    └── coalesce
                     │                                                                                                                       │              │    │         ├── cast: STRING
                     │                                                                                                                       │              │    │         │    └── variable: i:10
                     │                                                                                                                       │              │    │         └── const: '<NULL>'
                     │                                                                                                                       │              │    └── const: ''
                     │                                                                                                                       │              ├── const: ''
                     │                                                                                                                       │              ├── const: ''
                     │                                                                                                                       │              └── const: '00000'
                     │                                                                                                                       └── project
                     │                                                                                                                            ├── columns: stmt_loop_5:13
                     │                                                                                                                            ├── project
                     │                                                                                                                            │    ├── columns: i:12
                     │                                                                                                                            │    ├── values
                     │                                                                                                                            │    │    └── tuple
                     │                                                                                                                            │    └── projections
                     │                                                                                                                            │         └── plus [as=i:12]
                     │                                                                                                                            │              ├── variable: i:10
                     │                                                                                                                            │              └── const: 1
                     │                                                                                                                            └── projections
                     │                                                                                                                                 └── udf: stmt_loop_5 [as=stmt_loop_5:13]
                     │                                                                                                                                      ├── tail-call
                     │                                                                                                                                      ├── args
                     │                                                                                                                                      │    └── variable: i:12
                     │                                                                                                                                      └── recursive-call
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RAISE EXCEPTION USING ERRCODE = 'division_by_zero';
    return 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:4
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:4]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":3
                     ├── project
                     │    ├── columns: "_stmt_raise_1":3
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":3]
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:1
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:1]
                     │                   │              ├── const: 'ERROR'
                     │                   │              ├── const: 'division_by_zero'
                     │                   │              ├── const: ''
                     │                   │              ├── const: ''
                     │                   │              └── const: 'division_by_zero'
                     │                   └── project
                     │                        ├── columns: stmt_return_3:2!null
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── const: 0 [as=stmt_return_3:2]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RAISE EXCEPTION USING ERRCODE = '22012';
    return 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:4
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:4]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":3
                     ├── project
                     │    ├── columns: "_stmt_raise_1":3
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":3]
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:1
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:1]
                     │                   │              ├── const: 'ERROR'
                     │                   │              ├── const: '22012'
                     │                   │              ├── const: ''
                     │                   │              ├── const: ''
                     │                   │              └── const: '22012'
                     │                   └── project
                     │                        ├── columns: stmt_return_3:2!null
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── const: 0 [as=stmt_return_3:2]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RAISE EXCEPTION USING DETAIL = 'use default errcode for the code and message';
    return 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:4
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:4]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":3
                     ├── project
                     │    ├── columns: "_stmt_raise_1":3
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":3]
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:1
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:1]
                     │                   │              ├── const: 'ERROR'
                     │                   │              ├── const: 'P0001'
                     │                   │              ├── const: 'use default errcode for the code and message'
                     │                   │              ├── const: ''
                     │                   │              └── const: 'P0001'
                     │                   └── project
                     │                        ├── columns: stmt_return_3:2!null
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── const: 0 [as=stmt_return_3:2]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RAISE 'foo';
    return 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:4
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:4]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":3
                     ├── project
                     │    ├── columns: "_stmt_raise_1":3
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":3]
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:1
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:1]
                     │                   │              ├── const: 'ERROR'
                     │                   │              ├── const: 'foo'
                     │                   │              ├── const: ''
                     │                   │              ├── const: ''
                     │                   │              └── const: 'P0001'
                     │                   └── project
                     │                        ├── columns: stmt_return_3:2!null
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── const: 0 [as=stmt_return_3:2]
                     └── const: 1

# Testing IF statements with ELSIF branches.
exec-ddl
CREATE OR REPLACE FUNCTION f(n INT) RETURNS INT AS $$
  DECLARE
    i INT := -1;
  BEGIN
    IF n = 0 THEN
      RETURN 0;
    ELSIF n = 1 THEN
      i := 100;
    ELSIF n = 2 THEN
      i := 200;
      RETURN i;
    END IF;
    RETURN i;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(0);
----
project
 ├── columns: f:13
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:13]
           ├── args
           │    └── const: 0
           ├── params: n:1
           └── body
                └── limit
                     ├── columns: stmt_if_5:12
                     ├── project
                     │    ├── columns: stmt_if_5:12
                     │    ├── project
                     │    │    ├── columns: i:2!null
                     │    │    ├── values
                     │    │    │    └── tuple
                     │    │    └── projections
                     │    │         └── const: -1 [as=i:2]
                     │    └── projections
                     │         └── case [as=stmt_if_5:12]
                     │              ├── true
                     │              ├── when
                     │              │    ├── eq
                     │              │    │    ├── variable: n:1
                     │              │    │    └── const: 0
                     │              │    └── subquery
                     │              │         └── project
                     │              │              ├── columns: stmt_return_3:6!null
                     │              │              ├── values
                     │              │              │    └── tuple
                     │              │              └── projections
                     │              │                   └── const: 0 [as=stmt_return_3:6]
                     │              ├── when
                     │              │    ├── eq
                     │              │    │    ├── variable: n:1
                     │              │    │    └── const: 1
                     │              │    └── subquery
                     │              │         └── project
                     │              │              ├── columns: stmt_if_1:8
                     │              │              ├── project
                     │              │              │    ├── columns: i:7!null
                     │              │              │    ├── values
                     │              │              │    │    └── tuple
                     │              │              │    └── projections
                     │              │              │         └── const: 100 [as=i:7]
                     │              │              └── projections
                     │              │                   └── udf: stmt_if_1 [as=stmt_if_1:8]
                     │              │                        ├── tail-call
                     │              │                        ├── args
                     │              │                        │    ├── variable: n:1
                     │              │                        │    └── variable: i:7
                     │              │                        ├── params: n:3 i:4
                     │              │                        └── body
                     │              │                             └── project
                     │              │                                  ├── columns: stmt_return_2:5
                     │              │                                  ├── values
                     │              │                                  │    └── tuple
                     │              │                                  └── projections
                     │              │                                       └── variable: i:4 [as=stmt_return_2:5]
                     │              ├── when
                     │              │    ├── eq
                     │              │    │    ├── variable: n:1
                     │              │    │    └── const: 2
                     │              │    └── subquery
                     │              │         └── project
                     │              │              ├── columns: stmt_return_4:10!null
                     │              │              ├── project
                     │              │              │    ├── columns: i:9!null
                     │              │              │    ├── values
                     │              │              │    │    └── tuple
                     │              │              │    └── projections
                     │              │              │         └── const: 200 [as=i:9]
                     │              │              └── projections
                     │              │                   └── variable: i:9 [as=stmt_return_4:10]
                     │              └── subquery
                     │                   └── project
                     │                        ├── columns: stmt_if_1:11
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── udf: stmt_if_1 [as=stmt_if_1:11]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    ├── variable: n:1
                     │                                  │    └── variable: i:2
                     │                                  ├── params: n:3 i:4
                     │                                  └── body
                     │                                       └── project
                     │                                            ├── columns: stmt_return_2:5
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── variable: i:4 [as=stmt_return_2:5]
                     └── const: 1

# Testing EXCEPTION blocks.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RETURN 1 // 0;
  EXCEPTION
    WHEN division_by_zero THEN
      RETURN 100;
    WHEN invalid_regular_expression THEN
      RETURN 200;
    WHEN SQLSTATE '2200C' THEN
      RETURN 300;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:6
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:6]
           └── body
                └── limit
                     ├── columns: nested_block_7:5
                     ├── project
                     │    ├── columns: nested_block_7:5
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: nested_block_7 [as=nested_block_7:5]
                     │              ├── body
                     │              │    └── project
                     │              │         ├── columns: stmt_return_8:4!null
                     │              │         ├── values
                     │              │         │    └── tuple
                     │              │         └── projections
                     │              │              └── floor-div [as=stmt_return_8:4]
                     │              │                   ├── const: 1
                     │              │                   └── const: 0
                     │              └── exception-handler
                     │                   ├── SQLSTATE '22012'
                     │                   │    └── project
                     │                   │         ├── columns: stmt_return_2:1!null
                     │                   │         ├── values
                     │                   │         │    └── tuple
                     │                   │         └── projections
                     │                   │              └── const: 100 [as=stmt_return_2:1]
                     │                   ├── SQLSTATE '2201B'
                     │                   │    └── project
                     │                   │         ├── columns: stmt_return_4:2!null
                     │                   │         ├── values
                     │                   │         │    └── tuple
                     │                   │         └── projections
                     │                   │              └── const: 200 [as=stmt_return_4:2]
                     │                   └── SQLSTATE '2200C'
                     │                        └── project
                     │                             ├── columns: stmt_return_6:3!null
                     │                             ├── values
                     │                             │    └── tuple
                     │                             └── projections
                     │                                  └── const: 300 [as=stmt_return_6:3]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(i INT) RETURNS INT AS $$
  BEGIN
    IF i > 0 THEN
      RETURN 1 // 0;
    ELSE
      RETURN sqrt(i::FLOAT)::INT;
    END IF;
  EXCEPTION
    WHEN SQLSTATE '22012' OR indicator_overflow THEN
      RETURN 100;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(0);
----
project
 ├── columns: f:14
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:14]
           ├── args
           │    └── const: 0
           ├── params: i:1
           └── body
                └── limit
                     ├── columns: nested_block_3:13
                     ├── project
                     │    ├── columns: nested_block_3:13
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: nested_block_3 [as=nested_block_3:13]
                     │              ├── args
                     │              │    └── variable: i:1
                     │              ├── params: i:4
                     │              ├── body
                     │              │    └── project
                     │              │         ├── columns: stmt_if_10:12
                     │              │         ├── values
                     │              │         │    └── tuple
                     │              │         └── projections
                     │              │              └── case [as=stmt_if_10:12]
                     │              │                   ├── true
                     │              │                   ├── when
                     │              │                   │    ├── gt
                     │              │                   │    │    ├── variable: i:4
                     │              │                   │    │    └── const: 0
                     │              │                   │    └── subquery
                     │              │                   │         ├── tail-call
                     │              │                   │         └── project
                     │              │                   │              ├── columns: stmt_return_8:10!null
                     │              │                   │              ├── values
                     │              │                   │              │    └── tuple
                     │              │                   │              └── projections
                     │              │                   │                   └── floor-div [as=stmt_return_8:10]
                     │              │                   │                        ├── const: 1
                     │              │                   │                        └── const: 0
                     │              │                   └── subquery
                     │              │                        ├── tail-call
                     │              │                        └── project
                     │              │                             ├── columns: stmt_return_9:11
                     │              │                             ├── values
                     │              │                             │    └── tuple
                     │              │                             └── projections
                     │              │                                  └── cast: INT8 [as=stmt_return_9:11]
                     │              │                                       └── function: sqrt
                     │              │                                            └── cast: FLOAT8
                     │              │                                                 └── variable: i:4
                     │              └── exception-handler
                     │                   ├── SQLSTATE '22012'
                     │                   │    └── project
                     │                   │         ├── columns: stmt_return_2:3!null
                     │                   │         ├── values
                     │                   │         │    └── tuple
                     │                   │         └── projections
                     │                   │              └── const: 100 [as=stmt_return_2:3]
                     │                   └── SQLSTATE '22022'
                     │                        └── project
                     │                             ├── columns: stmt_return_2:3!null
                     │                             ├── values
                     │                             │    └── tuple
                     │                             └── projections
                     │                                  └── const: 100 [as=stmt_return_2:3]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(i INT) RETURNS INT AS $$
  BEGIN
    IF i > 0 THEN
      RETURN 1 // 0;
    ELSE
      RETURN sqrt(i::FLOAT)::INT;
    END IF;
  EXCEPTION
    WHEN division_by_zero THEN
      RETURN 100;
    WHEN SQLSTATE '22022' THEN
      RETURN -1;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(0);
----
project
 ├── columns: f:16
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:16]
           ├── args
           │    └── const: 0
           ├── params: i:1
           └── body
                └── limit
                     ├── columns: nested_block_5:15
                     ├── project
                     │    ├── columns: nested_block_5:15
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: nested_block_5 [as=nested_block_5:15]
                     │              ├── args
                     │              │    └── variable: i:1
                     │              ├── params: i:6
                     │              ├── body
                     │              │    └── project
                     │              │         ├── columns: stmt_if_12:14
                     │              │         ├── values
                     │              │         │    └── tuple
                     │              │         └── projections
                     │              │              └── case [as=stmt_if_12:14]
                     │              │                   ├── true
                     │              │                   ├── when
                     │              │                   │    ├── gt
                     │              │                   │    │    ├── variable: i:6
                     │              │                   │    │    └── const: 0
                     │              │                   │    └── subquery
                     │              │                   │         ├── tail-call
                     │              │                   │         └── project
                     │              │                   │              ├── columns: stmt_return_10:12!null
                     │              │                   │              ├── values
                     │              │                   │              │    └── tuple
                     │              │                   │              └── projections
                     │              │                   │                   └── floor-div [as=stmt_return_10:12]
                     │              │                   │                        ├── const: 1
                     │              │                   │                        └── const: 0
                     │              │                   └── subquery
                     │              │                        ├── tail-call
                     │              │                        └── project
                     │              │                             ├── columns: stmt_return_11:13
                     │              │                             ├── values
                     │              │                             │    └── tuple
                     │              │                             └── projections
                     │              │                                  └── cast: INT8 [as=stmt_return_11:13]
                     │              │                                       └── function: sqrt
                     │              │                                            └── cast: FLOAT8
                     │              │                                                 └── variable: i:6
                     │              └── exception-handler
                     │                   ├── SQLSTATE '22012'
                     │                   │    └── project
                     │                   │         ├── columns: stmt_return_2:3!null
                     │                   │         ├── values
                     │                   │         │    └── tuple
                     │                   │         └── projections
                     │                   │              └── const: 100 [as=stmt_return_2:3]
                     │                   └── SQLSTATE '22022'
                     │                        └── project
                     │                             ├── columns: stmt_return_4:5!null
                     │                             ├── values
                     │                             │    └── tuple
                     │                             └── projections
                     │                                  └── const: -1 [as=stmt_return_4:5]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RAISE division_by_zero;
    RETURN 0;
  EXCEPTION
    WHEN division_by_zero THEN
      RETURN 101;
    WHEN SQLSTATE '22P02' THEN
      RETURN 102;
    WHEN invalid_binary_representation THEN
      RETURN 103;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:8
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:8]
           └── body
                └── limit
                     ├── columns: nested_block_7:7
                     ├── project
                     │    ├── columns: nested_block_7:7
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: nested_block_7 [as=nested_block_7:7]
                     │              ├── body
                     │              │    └── project
                     │              │         ├── columns: "_stmt_raise_8":6
                     │              │         ├── values
                     │              │         │    └── tuple
                     │              │         └── projections
                     │              │              └── udf: _stmt_raise_8 [as="_stmt_raise_8":6]
                     │              │                   ├── tail-call
                     │              │                   └── body
                     │              │                        ├── project
                     │              │                        │    ├── columns: stmt_raise_9:4
                     │              │                        │    ├── values
                     │              │                        │    │    └── tuple
                     │              │                        │    └── projections
                     │              │                        │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_9:4]
                     │              │                        │              ├── const: 'ERROR'
                     │              │                        │              ├── const: 'division_by_zero'
                     │              │                        │              ├── const: ''
                     │              │                        │              ├── const: ''
                     │              │                        │              └── const: 'division_by_zero'
                     │              │                        └── project
                     │              │                             ├── columns: stmt_return_10:5!null
                     │              │                             ├── values
                     │              │                             │    └── tuple
                     │              │                             └── projections
                     │              │                                  └── const: 0 [as=stmt_return_10:5]
                     │              └── exception-handler
                     │                   ├── SQLSTATE '22012'
                     │                   │    └── project
                     │                   │         ├── columns: stmt_return_2:1!null
                     │                   │         ├── values
                     │                   │         │    └── tuple
                     │                   │         └── projections
                     │                   │              └── const: 101 [as=stmt_return_2:1]
                     │                   ├── SQLSTATE '22P02'
                     │                   │    └── project
                     │                   │         ├── columns: stmt_return_4:2!null
                     │                   │         ├── values
                     │                   │         │    └── tuple
                     │                   │         └── projections
                     │                   │              └── const: 102 [as=stmt_return_4:2]
                     │                   └── SQLSTATE '22P03'
                     │                        └── project
                     │                             ├── columns: stmt_return_6:3!null
                     │                             ├── values
                     │                             │    └── tuple
                     │                             └── projections
                     │                                  └── const: 103 [as=stmt_return_6:3]
                     └── const: 1

# The exception block does not catch errors thrown during variable declaration.
exec-ddl
CREATE OR REPLACE FUNCTION f(n INT) RETURNS INT AS $$
  DECLARE
    i INT := 100 // n;
  BEGIN
    RETURN i;
  EXCEPTION
    WHEN division_by_zero THEN
      RETURN 101;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(0);
----
project
 ├── columns: f:10
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:10]
           ├── args
           │    └── const: 0
           ├── params: n:1
           └── body
                └── limit
                     ├── columns: nested_block_3:9
                     ├── project
                     │    ├── columns: nested_block_3:9
                     │    ├── barrier
                     │    │    ├── columns: i:2
                     │    │    └── project
                     │    │         ├── columns: i:2
                     │    │         ├── values
                     │    │         │    └── tuple
                     │    │         └── projections
                     │    │              └── floor-div [as=i:2]
                     │    │                   ├── const: 100
                     │    │                   └── variable: n:1
                     │    └── projections
                     │         └── udf: nested_block_3 [as=nested_block_3:9]
                     │              ├── args
                     │              │    ├── variable: n:1
                     │              │    └── variable: i:2
                     │              ├── params: n:6 i:7
                     │              ├── body
                     │              │    └── project
                     │              │         ├── columns: stmt_return_4:8
                     │              │         ├── values
                     │              │         │    └── tuple
                     │              │         └── projections
                     │              │              └── variable: i:7 [as=stmt_return_4:8]
                     │              └── exception-handler
                     │                   └── SQLSTATE '22012'
                     │                        └── project
                     │                             ├── columns: stmt_return_2:5!null
                     │                             ├── values
                     │                             │    └── tuple
                     │                             └── projections
                     │                                  └── const: 101 [as=stmt_return_2:5]
                     └── const: 1

# Each variable assignment is wrapped in an exception handler.
exec-ddl
CREATE OR REPLACE FUNCTION f(i INT, j INT, k INT) RETURNS INT AS $$
  DECLARE
    x INT := 0;
  BEGIN
    x := 1 // i;
    x := 2 // j;
    x := 3 // k;
    RETURN x;
  EXCEPTION
    WHEN division_by_zero THEN
      RETURN x;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(0, 0, 0);
----
project
 ├── columns: f:34
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:34]
           ├── args
           │    ├── const: 0
           │    ├── const: 0
           │    └── const: 0
           ├── params: i:1 j:2 k:3
           └── body
                └── limit
                     ├── columns: nested_block_3:33
                     ├── project
                     │    ├── columns: nested_block_3:33
                     │    ├── barrier
                     │    │    ├── columns: x:4!null
                     │    │    └── project
                     │    │         ├── columns: x:4!null
                     │    │         ├── values
                     │    │         │    └── tuple
                     │    │         └── projections
                     │    │              └── const: 0 [as=x:4]
                     │    └── projections
                     │         └── udf: nested_block_3 [as=nested_block_3:33]
                     │              ├── args
                     │              │    ├── variable: i:1
                     │              │    ├── variable: j:2
                     │              │    ├── variable: k:3
                     │              │    └── variable: x:4
                     │              ├── params: i:10 j:11 k:12 x:13
                     │              ├── body
                     │              │    └── project
                     │              │         ├── columns: assign_exception_block_4:32
                     │              │         ├── barrier
                     │              │         │    ├── columns: x:14
                     │              │         │    └── project
                     │              │         │         ├── columns: x:14
                     │              │         │         ├── values
                     │              │         │         │    └── tuple
                     │              │         │         └── projections
                     │              │         │              └── floor-div [as=x:14]
                     │              │         │                   ├── const: 1
                     │              │         │                   └── variable: i:10
                     │              │         └── projections
                     │              │              └── udf: assign_exception_block_4 [as=assign_exception_block_4:32]
                     │              │                   ├── tail-call
                     │              │                   ├── args
                     │              │                   │    ├── variable: i:10
                     │              │                   │    ├── variable: j:11
                     │              │                   │    ├── variable: k:12
                     │              │                   │    └── variable: x:14
                     │              │                   ├── params: i:15 j:16 k:17 x:18
                     │              │                   └── body
                     │              │                        └── project
                     │              │                             ├── columns: assign_exception_block_5:31
                     │              │                             ├── barrier
                     │              │                             │    ├── columns: x:19
                     │              │                             │    └── project
                     │              │                             │         ├── columns: x:19
                     │              │                             │         ├── values
                     │              │                             │         │    └── tuple
                     │              │                             │         └── projections
                     │              │                             │              └── floor-div [as=x:19]
                     │              │                             │                   ├── const: 2
                     │              │                             │                   └── variable: j:16
                     │              │                             └── projections
                     │              │                                  └── udf: assign_exception_block_5 [as=assign_exception_block_5:31]
                     │              │                                       ├── tail-call
                     │              │                                       ├── args
                     │              │                                       │    ├── variable: i:15
                     │              │                                       │    ├── variable: j:16
                     │              │                                       │    ├── variable: k:17
                     │              │                                       │    └── variable: x:19
                     │              │                                       ├── params: i:20 j:21 k:22 x:23
                     │              │                                       └── body
                     │              │                                            └── project
                     │              │                                                 ├── columns: assign_exception_block_6:30
                     │              │                                                 ├── barrier
                     │              │                                                 │    ├── columns: x:24
                     │              │                                                 │    └── project
                     │              │                                                 │         ├── columns: x:24
                     │              │                                                 │         ├── values
                     │              │                                                 │         │    └── tuple
                     │              │                                                 │         └── projections
                     │              │                                                 │              └── floor-div [as=x:24]
                     │              │                                                 │                   ├── const: 3
                     │              │                                                 │                   └── variable: k:22
                     │              │                                                 └── projections
                     │              │                                                      └── udf: assign_exception_block_6 [as=assign_exception_block_6:30]
                     │              │                                                           ├── tail-call
                     │              │                                                           ├── args
                     │              │                                                           │    ├── variable: i:20
                     │              │                                                           │    ├── variable: j:21
                     │              │                                                           │    ├── variable: k:22
                     │              │                                                           │    └── variable: x:24
                     │              │                                                           ├── params: i:25 j:26 k:27 x:28
                     │              │                                                           └── body
                     │              │                                                                └── project
                     │              │                                                                     ├── columns: stmt_return_7:29
                     │              │                                                                     ├── values
                     │              │                                                                     │    └── tuple
                     │              │                                                                     └── projections
                     │              │                                                                          └── variable: x:28 [as=stmt_return_7:29]
                     │              └── exception-handler
                     │                   └── SQLSTATE '22012'
                     │                        └── project
                     │                             ├── columns: stmt_return_2:9
                     │                             ├── values
                     │                             │    └── tuple
                     │                             └── projections
                     │                                  └── variable: x:8 [as=stmt_return_2:9]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(i INT) RETURNS INT AS $$
  DECLARE
    x INT;
  BEGIN
    IF i = 0 THEN
      x := 100;
      RAISE division_by_zero;
    ELSE
      x := 200;
      RAISE division_by_zero;
    END IF;
    RETURN 0;
  EXCEPTION
    WHEN division_by_zero THEN
      RETURN x;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(0);
----
project
 ├── columns: f:31
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:31]
           ├── args
           │    └── const: 0
           ├── params: i:1
           └── body
                └── limit
                     ├── columns: nested_block_3:30
                     ├── project
                     │    ├── columns: nested_block_3:30
                     │    ├── barrier
                     │    │    ├── columns: x:2
                     │    │    └── project
                     │    │         ├── columns: x:2
                     │    │         ├── values
                     │    │         │    └── tuple
                     │    │         └── projections
                     │    │              └── cast: INT8 [as=x:2]
                     │    │                   └── null
                     │    └── projections
                     │         └── udf: nested_block_3 [as=nested_block_3:30]
                     │              ├── args
                     │              │    ├── variable: i:1
                     │              │    └── variable: x:2
                     │              ├── params: i:6 x:7
                     │              ├── body
                     │              │    └── project
                     │              │         ├── columns: stmt_if_12:29
                     │              │         ├── values
                     │              │         │    └── tuple
                     │              │         └── projections
                     │              │              └── case [as=stmt_if_12:29]
                     │              │                   ├── true
                     │              │                   ├── when
                     │              │                   │    ├── eq
                     │              │                   │    │    ├── variable: i:6
                     │              │                   │    │    └── const: 0
                     │              │                   │    └── subquery
                     │              │                   │         ├── tail-call
                     │              │                   │         └── project
                     │              │                   │              ├── columns: assign_exception_block_6:19
                     │              │                   │              ├── barrier
                     │              │                   │              │    ├── columns: x:11!null
                     │              │                   │              │    └── project
                     │              │                   │              │         ├── columns: x:11!null
                     │              │                   │              │         ├── values
                     │              │                   │              │         │    └── tuple
                     │              │                   │              │         └── projections
                     │              │                   │              │              └── const: 100 [as=x:11]
                     │              │                   │              └── projections
                     │              │                   │                   └── udf: assign_exception_block_6 [as=assign_exception_block_6:19]
                     │              │                   │                        ├── tail-call
                     │              │                   │                        ├── args
                     │              │                   │                        │    ├── variable: i:6
                     │              │                   │                        │    └── variable: x:11
                     │              │                   │                        ├── params: i:12 x:13
                     │              │                   │                        └── body
                     │              │                   │                             └── project
                     │              │                   │                                  ├── columns: "_stmt_raise_7":18
                     │              │                   │                                  ├── values
                     │              │                   │                                  │    └── tuple
                     │              │                   │                                  └── projections
                     │              │                   │                                       └── udf: _stmt_raise_7 [as="_stmt_raise_7":18]
                     │              │                   │                                            ├── tail-call
                     │              │                   │                                            ├── args
                     │              │                   │                                            │    ├── variable: i:12
                     │              │                   │                                            │    └── variable: x:13
                     │              │                   │                                            ├── params: i:14 x:15
                     │              │                   │                                            └── body
                     │              │                   │                                                 ├── project
                     │              │                   │                                                 │    ├── columns: stmt_raise_8:16
                     │              │                   │                                                 │    ├── values
                     │              │                   │                                                 │    │    └── tuple
                     │              │                   │                                                 │    └── projections
                     │              │                   │                                                 │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_8:16]
                     │              │                   │                                                 │              ├── const: 'ERROR'
                     │              │                   │                                                 │              ├── const: 'division_by_zero'
                     │              │                   │                                                 │              ├── const: ''
                     │              │                   │                                                 │              ├── const: ''
                     │              │                   │                                                 │              └── const: 'division_by_zero'
                     │              │                   │                                                 └── project
                     │              │                   │                                                      ├── columns: stmt_if_4:17
                     │              │                   │                                                      ├── values
                     │              │                   │                                                      │    └── tuple
                     │              │                   │                                                      └── projections
                     │              │                   │                                                           └── udf: stmt_if_4 [as=stmt_if_4:17]
                     │              │                   │                                                                ├── tail-call
                     │              │                   │                                                                ├── args
                     │              │                   │                                                                │    ├── variable: i:14
                     │              │                   │                                                                │    └── variable: x:15
                     │              │                   │                                                                ├── params: i:8 x:9
                     │              │                   │                                                                └── body
                     │              │                   │                                                                     └── project
                     │              │                   │                                                                          ├── columns: stmt_return_5:10!null
                     │              │                   │                                                                          ├── values
                     │              │                   │                                                                          │    └── tuple
                     │              │                   │                                                                          └── projections
                     │              │                   │                                                                               └── const: 0 [as=stmt_return_5:10]
                     │              │                   └── subquery
                     │              │                        ├── tail-call
                     │              │                        └── project
                     │              │                             ├── columns: assign_exception_block_9:28
                     │              │                             ├── barrier
                     │              │                             │    ├── columns: x:20!null
                     │              │                             │    └── project
                     │              │                             │         ├── columns: x:20!null
                     │              │                             │         ├── values
                     │              │                             │         │    └── tuple
                     │              │                             │         └── projections
                     │              │                             │              └── const: 200 [as=x:20]
                     │              │                             └── projections
                     │              │                                  └── udf: assign_exception_block_9 [as=assign_exception_block_9:28]
                     │              │                                       ├── tail-call
                     │              │                                       ├── args
                     │              │                                       │    ├── variable: i:6
                     │              │                                       │    └── variable: x:20
                     │              │                                       ├── params: i:21 x:22
                     │              │                                       └── body
                     │              │                                            └── project
                     │              │                                                 ├── columns: "_stmt_raise_10":27
                     │              │                                                 ├── values
                     │              │                                                 │    └── tuple
                     │              │                                                 └── projections
                     │              │                                                      └── udf: _stmt_raise_10 [as="_stmt_raise_10":27]
                     │              │                                                           ├── tail-call
                     │              │                                                           ├── args
                     │              │                                                           │    ├── variable: i:21
                     │              │                                                           │    └── variable: x:22
                     │              │                                                           ├── params: i:23 x:24
                     │              │                                                           └── body
                     │              │                                                                ├── project
                     │              │                                                                │    ├── columns: stmt_raise_11:25
                     │              │                                                                │    ├── values
                     │              │                                                                │    │    └── tuple
                     │              │                                                                │    └── projections
                     │              │                                                                │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_11:25]
                     │              │                                                                │              ├── const: 'ERROR'
                     │              │                                                                │              ├── const: 'division_by_zero'
                     │              │                                                                │              ├── const: ''
                     │              │                                                                │              ├── const: ''
                     │              │                                                                │              └── const: 'division_by_zero'
                     │              │                                                                └── project
                     │              │                                                                     ├── columns: stmt_if_4:26
                     │              │                                                                     ├── values
                     │              │                                                                     │    └── tuple
                     │              │                                                                     └── projections
                     │              │                                                                          └── udf: stmt_if_4 [as=stmt_if_4:26]
                     │              │                                                                               ├── tail-call
                     │              │                                                                               ├── args
                     │              │                                                                               │    ├── variable: i:23
                     │              │                                                                               │    └── variable: x:24
                     │              │                                                                               ├── params: i:8 x:9
                     │              │                                                                               └── body
                     │              │                                                                                    └── project
                     │              │                                                                                         ├── columns: stmt_return_5:10!null
                     │              │                                                                                         ├── values
                     │              │                                                                                         │    └── tuple
                     │              │                                                                                         └── projections
                     │              │                                                                                              └── const: 0 [as=stmt_return_5:10]
                     │              └── exception-handler
                     │                   └── SQLSTATE '22012'
                     │                        └── project
                     │                             ├── columns: stmt_return_2:5
                     │                             ├── values
                     │                             │    └── tuple
                     │                             └── projections
                     │                                  └── variable: x:4 [as=stmt_return_2:5]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f(n INT, a INT) RETURNS INT AS $$
  DECLARE
    x INT;
    i INT := 0;
  BEGIN
    LOOP IF i >= n THEN EXIT; END IF;
      x := 1 // (i - a);
      i := i + 1;
    END LOOP;
    RETURN -1;
  EXCEPTION
    WHEN division_by_zero THEN
      RETURN i;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f(0, 0);
----
project
 ├── columns: f:45
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:45]
           ├── args
           │    ├── const: 0
           │    └── const: 0
           ├── params: n:1 a:2
           └── body
                └── limit
                     ├── columns: nested_block_3:44
                     ├── project
                     │    ├── columns: nested_block_3:44
                     │    ├── barrier
                     │    │    ├── columns: x:3 i:4!null
                     │    │    └── project
                     │    │         ├── columns: i:4!null x:3
                     │    │         ├── project
                     │    │         │    ├── columns: x:3
                     │    │         │    ├── values
                     │    │         │    │    └── tuple
                     │    │         │    └── projections
                     │    │         │         └── cast: INT8 [as=x:3]
                     │    │         │              └── null
                     │    │         └── projections
                     │    │              └── const: 0 [as=i:4]
                     │    └── projections
                     │         └── udf: nested_block_3 [as=nested_block_3:44]
                     │              ├── args
                     │              │    ├── variable: n:1
                     │              │    ├── variable: a:2
                     │              │    ├── variable: x:3
                     │              │    └── variable: i:4
                     │              ├── params: n:10 a:11 x:12 i:13
                     │              ├── body
                     │              │    └── project
                     │              │         ├── columns: stmt_loop_6:43
                     │              │         ├── values
                     │              │         │    └── tuple
                     │              │         └── projections
                     │              │              └── udf: stmt_loop_6 [as=stmt_loop_6:43]
                     │              │                   ├── tail-call
                     │              │                   ├── args
                     │              │                   │    ├── variable: n:10
                     │              │                   │    ├── variable: a:11
                     │              │                   │    ├── variable: x:12
                     │              │                   │    └── variable: i:13
                     │              │                   ├── params: n:19 a:20 x:21 i:22
                     │              │                   └── body
                     │              │                        └── project
                     │              │                             ├── columns: stmt_if_10:42
                     │              │                             ├── values
                     │              │                             │    └── tuple
                     │              │                             └── projections
                     │              │                                  └── case [as=stmt_if_10:42]
                     │              │                                       ├── true
                     │              │                                       ├── when
                     │              │                                       │    ├── ge
                     │              │                                       │    │    ├── variable: i:22
                     │              │                                       │    │    └── variable: n:19
                     │              │                                       │    └── subquery
                     │              │                                       │         ├── tail-call
                     │              │                                       │         └── project
                     │              │                                       │              ├── columns: loop_exit_4:40
                     │              │                                       │              ├── values
                     │              │                                       │              │    └── tuple
                     │              │                                       │              └── projections
                     │              │                                       │                   └── udf: loop_exit_4 [as=loop_exit_4:40]
                     │              │                                       │                        ├── tail-call
                     │              │                                       │                        ├── args
                     │              │                                       │                        │    ├── variable: n:19
                     │              │                                       │                        │    ├── variable: a:20
                     │              │                                       │                        │    ├── variable: x:21
                     │              │                                       │                        │    └── variable: i:22
                     │              │                                       │                        ├── params: n:14 a:15 x:16 i:17
                     │              │                                       │                        └── body
                     │              │                                       │                             └── project
                     │              │                                       │                                  ├── columns: stmt_return_5:18!null
                     │              │                                       │                                  ├── values
                     │              │                                       │                                  │    └── tuple
                     │              │                                       │                                  └── projections
                     │              │                                       │                                       └── const: -1 [as=stmt_return_5:18]
                     │              │                                       └── subquery
                     │              │                                            ├── tail-call
                     │              │                                            └── project
                     │              │                                                 ├── columns: stmt_if_7:41
                     │              │                                                 ├── values
                     │              │                                                 │    └── tuple
                     │              │                                                 └── projections
                     │              │                                                      └── udf: stmt_if_7 [as=stmt_if_7:41]
                     │              │                                                           ├── tail-call
                     │              │                                                           ├── args
                     │              │                                                           │    ├── variable: n:19
                     │              │                                                           │    ├── variable: a:20
                     │              │                                                           │    ├── variable: x:21
                     │              │                                                           │    └── variable: i:22
                     │              │                                                           ├── params: n:23 a:24 x:25 i:26
                     │              │                                                           └── body
                     │              │                                                                └── project
                     │              │                                                                     ├── columns: assign_exception_block_8:39
                     │              │                                                                     ├── barrier
                     │              │                                                                     │    ├── columns: x:27
                     │              │                                                                     │    └── project
                     │              │                                                                     │         ├── columns: x:27
                     │              │                                                                     │         ├── values
                     │              │                                                                     │         │    └── tuple
                     │              │                                                                     │         └── projections
                     │              │                                                                     │              └── floor-div [as=x:27]
                     │              │                                                                     │                   ├── const: 1
                     │              │                                                                     │                   └── minus
                     │              │                                                                     │                        ├── variable: i:26
                     │              │                                                                     │                        └── variable: a:24
                     │              │                                                                     └── projections
                     │              │                                                                          └── udf: assign_exception_block_8 [as=assign_exception_block_8:39]
                     │              │                                                                               ├── tail-call
                     │              │                                                                               ├── args
                     │              │                                                                               │    ├── variable: n:23
                     │              │                                                                               │    ├── variable: a:24
                     │              │                                                                               │    ├── variable: x:27
                     │              │                                                                               │    └── variable: i:26
                     │              │                                                                               ├── params: n:28 a:29 x:30 i:31
                     │              │                                                                               └── body
                     │              │                                                                                    └── project
                     │              │                                                                                         ├── columns: assign_exception_block_9:38
                     │              │                                                                                         ├── barrier
                     │              │                                                                                         │    ├── columns: i:32
                     │              │                                                                                         │    └── project
                     │              │                                                                                         │         ├── columns: i:32
                     │              │                                                                                         │         ├── values
                     │              │                                                                                         │         │    └── tuple
                     │              │                                                                                         │         └── projections
                     │              │                                                                                         │              └── plus [as=i:32]
                     │              │                                                                                         │                   ├── variable: i:31
                     │              │                                                                                         │                   └── const: 1
                     │              │                                                                                         └── projections
                     │              │                                                                                              └── udf: assign_exception_block_9 [as=assign_exception_block_9:38]
                     │              │                                                                                                   ├── tail-call
                     │              │                                                                                                   ├── args
                     │              │                                                                                                   │    ├── variable: n:28
                     │              │                                                                                                   │    ├── variable: a:29
                     │              │                                                                                                   │    ├── variable: x:30
                     │              │                                                                                                   │    └── variable: i:32
                     │              │                                                                                                   ├── params: n:33 a:34 x:35 i:36
                     │              │                                                                                                   └── body
                     │              │                                                                                                        └── project
                     │              │                                                                                                             ├── columns: stmt_loop_6:37
                     │              │                                                                                                             ├── values
                     │              │                                                                                                             │    └── tuple
                     │              │                                                                                                             └── projections
                     │              │                                                                                                                  └── udf: stmt_loop_6 [as=stmt_loop_6:37]
                     │              │                                                                                                                       ├── tail-call
                     │              │                                                                                                                       ├── args
                     │              │                                                                                                                       │    ├── variable: n:33
                     │              │                                                                                                                       │    ├── variable: a:34
                     │              │                                                                                                                       │    ├── variable: x:35
                     │              │                                                                                                                       │    └── variable: i:36
                     │              │                                                                                                                       └── recursive-call
                     │              └── exception-handler
                     │                   └── SQLSTATE '22012'
                     │                        └── project
                     │                             ├── columns: stmt_return_2:9
                     │                             ├── values
                     │                             │    └── tuple
                     │                             └── projections
                     │                                  └── variable: i:8 [as=stmt_return_2:9]
                     └── const: 1

# Testing SQL statement execution.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    INSERT INTO kv VALUES (100, 100);
    DELETE FROM kv WHERE k = 100;
    INSERT INTO kv VALUES (100, 100);
    RETURN (SELECT v FROM kv WHERE k = 100);
  END
$$ LANGUAGE PLpgSQL;
----

# TODO(#115681): add norm rule to fold the non-output body statements of nested
# routines.
build format=show-scalars
SELECT f();
----
project
 ├── columns: f:29
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:29]
           └── body
                └── limit
                     ├── columns: "_stmt_exec_1":28
                     ├── project
                     │    ├── columns: "_stmt_exec_1":28
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: _stmt_exec_1 [as="_stmt_exec_1":28]
                     │              └── body
                     │                   ├── insert kv
                     │                   │    ├── columns: <none>
                     │                   │    ├── insert-mapping:
                     │                   │    │    ├── column1:5 => k:1
                     │                   │    │    └── column2:6 => v:2
                     │                   │    └── values
                     │                   │         ├── columns: column1:5!null column2:6!null
                     │                   │         └── tuple
                     │                   │              ├── const: 100
                     │                   │              └── const: 100
                     │                   └── project
                     │                        ├── columns: "_stmt_exec_2":27
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── udf: _stmt_exec_2 [as="_stmt_exec_2":27]
                     │                                  ├── tail-call
                     │                                  └── body
                     │                                       ├── delete kv
                     │                                       │    ├── columns: <none>
                     │                                       │    ├── fetch columns: k:11 v:12
                     │                                       │    └── select
                     │                                       │         ├── columns: k:11!null v:12 crdb_internal_mvcc_timestamp:13 tableoid:14
                     │                                       │         ├── scan kv
                     │                                       │         │    ├── columns: k:11!null v:12 crdb_internal_mvcc_timestamp:13 tableoid:14
                     │                                       │         │    └── flags: avoid-full-scan
                     │                                       │         └── filters
                     │                                       │              └── eq
                     │                                       │                   ├── variable: k:11
                     │                                       │                   └── const: 100
                     │                                       └── project
                     │                                            ├── columns: "_stmt_exec_3":26
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: _stmt_exec_3 [as="_stmt_exec_3":26]
                     │                                                      ├── tail-call
                     │                                                      └── body
                     │                                                           ├── insert kv
                     │                                                           │    ├── columns: <none>
                     │                                                           │    ├── insert-mapping:
                     │                                                           │    │    ├── column1:19 => k:15
                     │                                                           │    │    └── column2:20 => v:16
                     │                                                           │    └── values
                     │                                                           │         ├── columns: column1:19!null column2:20!null
                     │                                                           │         └── tuple
                     │                                                           │              ├── const: 100
                     │                                                           │              └── const: 100
                     │                                                           └── project
                     │                                                                ├── columns: stmt_return_4:25
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── subquery [as=stmt_return_4:25]
                     │                                                                          ├── tail-call
                     │                                                                          └── max1-row
                     │                                                                               ├── columns: v:22
                     │                                                                               └── project
                     │                                                                                    ├── columns: v:22
                     │                                                                                    └── select
                     │                                                                                         ├── columns: k:21!null v:22 crdb_internal_mvcc_timestamp:23 tableoid:24
                     │                                                                                         ├── scan kv
                     │                                                                                         │    └── columns: k:21!null v:22 crdb_internal_mvcc_timestamp:23 tableoid:24
                     │                                                                                         └── filters
                     │                                                                                              └── eq
                     │                                                                                                   ├── variable: k:21
                     │                                                                                                   └── const: 100
                     └── const: 1

# Testing SELECT INTO.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  DECLARE
    i INT;
    j INT;
  BEGIN
    SELECT x, y INTO i, j FROM xy ORDER BY x DESC;
    RETURN i + j;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:17
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:17]
           └── body
                └── limit
                     ├── columns: "_stmt_exec_1":16
                     ├── project
                     │    ├── columns: "_stmt_exec_1":16
                     │    ├── project
                     │    │    ├── columns: j:2 i:1
                     │    │    ├── project
                     │    │    │    ├── columns: i:1
                     │    │    │    ├── values
                     │    │    │    │    └── tuple
                     │    │    │    └── projections
                     │    │    │         └── cast: INT8 [as=i:1]
                     │    │    │              └── null
                     │    │    └── projections
                     │    │         └── cast: INT8 [as=j:2]
                     │    │              └── null
                     │    └── projections
                     │         └── udf: _stmt_exec_1 [as="_stmt_exec_1":16]
                     │              ├── args
                     │              │    ├── variable: i:1
                     │              │    └── variable: j:2
                     │              ├── params: i:3 j:4
                     │              └── body
                     │                   └── project
                     │                        ├── columns: "_stmt_exec_ret_2":15
                     │                        ├── project
                     │                        │    ├── columns: i:13 j:14
                     │                        │    ├── right-join (cross)
                     │                        │    │    ├── columns: x:5 y:6
                     │                        │    │    ├── limit
                     │                        │    │    │    ├── columns: x:5 y:6
                     │                        │    │    │    ├── internal-ordering: -5
                     │                        │    │    │    ├── project
                     │                        │    │    │    │    ├── columns: x:5 y:6
                     │                        │    │    │    │    └── scan xy
                     │                        │    │    │    │         └── columns: x:5 y:6 rowid:7!null crdb_internal_mvcc_timestamp:8 tableoid:9
                     │                        │    │    │    └── const: 1
                     │                        │    │    ├── values
                     │                        │    │    │    └── tuple
                     │                        │    │    └── filters (true)
                     │                        │    └── projections
                     │                        │         ├── variable: x:5 [as=i:13]
                     │                        │         └── variable: y:6 [as=j:14]
                     │                        └── projections
                     │                             └── udf: _stmt_exec_ret_2 [as="_stmt_exec_ret_2":15]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    ├── variable: i:13
                     │                                  │    └── variable: j:14
                     │                                  ├── params: i:10 j:11
                     │                                  └── body
                     │                                       └── project
                     │                                            ├── columns: stmt_return_3:12
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── plus [as=stmt_return_3:12]
                     │                                                      ├── variable: i:10
                     │                                                      └── variable: j:11
                     └── const: 1

# It is possible to reference a previous value of a target variable in a
# SELECT INTO statement (note the "x < i" filter).
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  DECLARE
    i INT;
  BEGIN
    SELECT x INTO i FROM xy ORDER BY x DESC;
    RAISE NOTICE 'i = %', i;
    SELECT x INTO i FROM xy WHERE x < i ORDER BY x DESC;
    RAISE NOTICE 'i = %', i;
    RETURN 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:29
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:29]
           └── body
                └── limit
                     ├── columns: "_stmt_exec_1":28
                     ├── project
                     │    ├── columns: "_stmt_exec_1":28
                     │    ├── barrier
                     │    │    ├── columns: i:1
                     │    │    └── project
                     │    │         ├── columns: i:1
                     │    │         ├── values
                     │    │         │    └── tuple
                     │    │         └── projections
                     │    │              └── cast: INT8 [as=i:1]
                     │    │                   └── null
                     │    └── projections
                     │         └── udf: _stmt_exec_1 [as="_stmt_exec_1":28]
                     │              ├── args
                     │              │    └── variable: i:1
                     │              ├── params: i:2
                     │              └── body
                     │                   └── project
                     │                        ├── columns: "_stmt_exec_ret_2":27
                     │                        ├── barrier
                     │                        │    ├── columns: i:26
                     │                        │    └── project
                     │                        │         ├── columns: i:26
                     │                        │         ├── right-join (cross)
                     │                        │         │    ├── columns: x:3
                     │                        │         │    ├── limit
                     │                        │         │    │    ├── columns: x:3
                     │                        │         │    │    ├── internal-ordering: -3
                     │                        │         │    │    ├── project
                     │                        │         │    │    │    ├── columns: x:3
                     │                        │         │    │    │    └── scan xy
                     │                        │         │    │    │         └── columns: x:3 y:4 rowid:5!null crdb_internal_mvcc_timestamp:6 tableoid:7
                     │                        │         │    │    └── const: 1
                     │                        │         │    ├── values
                     │                        │         │    │    └── tuple
                     │                        │         │    └── filters (true)
                     │                        │         └── projections
                     │                        │              └── variable: x:3 [as=i:26]
                     │                        └── projections
                     │                             └── udf: _stmt_exec_ret_2 [as="_stmt_exec_ret_2":27]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    └── variable: i:26
                     │                                  ├── params: i:8
                     │                                  └── body
                     │                                       └── project
                     │                                            ├── columns: "_stmt_raise_3":25
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: _stmt_raise_3 [as="_stmt_raise_3":25]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    └── variable: i:8
                     │                                                      ├── params: i:9
                     │                                                      └── body
                     │                                                           ├── project
                     │                                                           │    ├── columns: stmt_raise_4:10
                     │                                                           │    ├── values
                     │                                                           │    │    └── tuple
                     │                                                           │    └── projections
                     │                                                           │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_4:10]
                     │                                                           │              ├── const: 'NOTICE'
                     │                                                           │              ├── concat
                     │                                                           │              │    ├── concat
                     │                                                           │              │    │    ├── const: 'i = '
                     │                                                           │              │    │    └── coalesce
                     │                                                           │              │    │         ├── cast: STRING
                     │                                                           │              │    │         │    └── variable: i:9
                     │                                                           │              │    │         └── const: '<NULL>'
                     │                                                           │              │    └── const: ''
                     │                                                           │              ├── const: ''
                     │                                                           │              ├── const: ''
                     │                                                           │              └── const: '00000'
                     │                                                           └── project
                     │                                                                ├── columns: "_stmt_exec_5":24
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── udf: _stmt_exec_5 [as="_stmt_exec_5":24]
                     │                                                                          ├── tail-call
                     │                                                                          ├── args
                     │                                                                          │    └── variable: i:9
                     │                                                                          ├── params: i:11
                     │                                                                          └── body
                     │                                                                               └── project
                     │                                                                                    ├── columns: "_stmt_exec_ret_6":23
                     │                                                                                    ├── barrier
                     │                                                                                    │    ├── columns: i:22
                     │                                                                                    │    └── project
                     │                                                                                    │         ├── columns: i:22
                     │                                                                                    │         ├── right-join (cross)
                     │                                                                                    │         │    ├── columns: x:12
                     │                                                                                    │         │    ├── limit
                     │                                                                                    │         │    │    ├── columns: x:12!null
                     │                                                                                    │         │    │    ├── internal-ordering: -12
                     │                                                                                    │         │    │    ├── project
                     │                                                                                    │         │    │    │    ├── columns: x:12!null
                     │                                                                                    │         │    │    │    └── select
                     │                                                                                    │         │    │    │         ├── columns: x:12!null y:13 rowid:14!null crdb_internal_mvcc_timestamp:15 tableoid:16
                     │                                                                                    │         │    │    │         ├── scan xy
                     │                                                                                    │         │    │    │         │    └── columns: x:12 y:13 rowid:14!null crdb_internal_mvcc_timestamp:15 tableoid:16
                     │                                                                                    │         │    │    │         └── filters
                     │                                                                                    │         │    │    │              └── lt
                     │                                                                                    │         │    │    │                   ├── variable: x:12
                     │                                                                                    │         │    │    │                   └── variable: i:11
                     │                                                                                    │         │    │    └── const: 1
                     │                                                                                    │         │    ├── values
                     │                                                                                    │         │    │    └── tuple
                     │                                                                                    │         │    └── filters (true)
                     │                                                                                    │         └── projections
                     │                                                                                    │              └── variable: x:12 [as=i:22]
                     │                                                                                    └── projections
                     │                                                                                         └── udf: _stmt_exec_ret_6 [as="_stmt_exec_ret_6":23]
                     │                                                                                              ├── tail-call
                     │                                                                                              ├── args
                     │                                                                                              │    └── variable: i:22
                     │                                                                                              ├── params: i:17
                     │                                                                                              └── body
                     │                                                                                                   └── project
                     │                                                                                                        ├── columns: "_stmt_raise_7":21
                     │                                                                                                        ├── values
                     │                                                                                                        │    └── tuple
                     │                                                                                                        └── projections
                     │                                                                                                             └── udf: _stmt_raise_7 [as="_stmt_raise_7":21]
                     │                                                                                                                  ├── tail-call
                     │                                                                                                                  ├── args
                     │                                                                                                                  │    └── variable: i:17
                     │                                                                                                                  ├── params: i:18
                     │                                                                                                                  └── body
                     │                                                                                                                       ├── project
                     │                                                                                                                       │    ├── columns: stmt_raise_8:19
                     │                                                                                                                       │    ├── values
                     │                                                                                                                       │    │    └── tuple
                     │                                                                                                                       │    └── projections
                     │                                                                                                                       │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_8:19]
                     │                                                                                                                       │              ├── const: 'NOTICE'
                     │                                                                                                                       │              ├── concat
                     │                                                                                                                       │              │    ├── concat
                     │                                                                                                                       │              │    │    ├── const: 'i = '
                     │                                                                                                                       │              │    │    └── coalesce
                     │                                                                                                                       │              │    │         ├── cast: STRING
                     │                                                                                                                       │              │    │         │    └── variable: i:18
                     │                                                                                                                       │              │    │         └── const: '<NULL>'
                     │                                                                                                                       │              │    └── const: ''
                     │                                                                                                                       │              ├── const: ''
                     │                                                                                                                       │              ├── const: ''
                     │                                                                                                                       │              └── const: '00000'
                     │                                                                                                                       └── project
                     │                                                                                                                            ├── columns: stmt_return_9:20!null
                     │                                                                                                                            ├── values
                     │                                                                                                                            │    └── tuple
                     │                                                                                                                            └── projections
                     │                                                                                                                                 └── const: 0 [as=stmt_return_9:20]
                     └── const: 1

# Testing OPEN statement.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  DECLARE
    curs REFCURSOR := 'foo';
  BEGIN
    OPEN curs FOR SELECT 1;
    RETURN 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:9
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:9]
           └── body
                └── limit
                     ├── columns: "_gen_cursor_name_3":8
                     ├── project
                     │    ├── columns: "_gen_cursor_name_3":8
                     │    ├── barrier
                     │    │    ├── columns: curs:1!null
                     │    │    └── project
                     │    │         ├── columns: curs:1!null
                     │    │         ├── values
                     │    │         │    └── tuple
                     │    │         └── projections
                     │    │              └── const: 'foo' [as=curs:1]
                     │    └── projections
                     │         └── udf: _gen_cursor_name_3 [as="_gen_cursor_name_3":8]
                     │              ├── args
                     │              │    └── variable: curs:1
                     │              ├── params: curs:5
                     │              └── body
                     │                   └── project
                     │                        ├── columns: "_stmt_open_1":7
                     │                        ├── barrier
                     │                        │    ├── columns: curs:6
                     │                        │    └── project
                     │                        │         ├── columns: curs:6
                     │                        │         ├── values
                     │                        │         │    └── tuple
                     │                        │         └── projections
                     │                        │              └── function: crdb_internal.plpgsql_gen_cursor_name [as=curs:6]
                     │                        │                   └── variable: curs:5
                     │                        └── projections
                     │                             └── udf: _stmt_open_1 [as="_stmt_open_1":7]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    └── variable: curs:6
                     │                                  ├── params: curs:2
                     │                                  └── body
                     │                                       ├── open-cursor
                     │                                       │    └── project
                     │                                       │         ├── columns: "?column?":3!null
                     │                                       │         ├── values
                     │                                       │         │    └── tuple
                     │                                       │         └── projections
                     │                                       │              └── const: 1 [as="?column?":3]
                     │                                       └── project
                     │                                            ├── columns: stmt_return_2:4!null
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── const: 0 [as=stmt_return_2:4]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  DECLARE
    i INT := 3;
    curs REFCURSOR := 'foo';
  BEGIN
    OPEN curs FOR SELECT * FROM xy WHERE x = i;
    RETURN 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:16
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:16]
           └── body
                └── limit
                     ├── columns: "_gen_cursor_name_3":15
                     ├── project
                     │    ├── columns: "_gen_cursor_name_3":15
                     │    ├── barrier
                     │    │    ├── columns: i:1!null curs:2!null
                     │    │    └── project
                     │    │         ├── columns: curs:2!null i:1!null
                     │    │         ├── project
                     │    │         │    ├── columns: i:1!null
                     │    │         │    ├── values
                     │    │         │    │    └── tuple
                     │    │         │    └── projections
                     │    │         │         └── const: 3 [as=i:1]
                     │    │         └── projections
                     │    │              └── const: 'foo' [as=curs:2]
                     │    └── projections
                     │         └── udf: _gen_cursor_name_3 [as="_gen_cursor_name_3":15]
                     │              ├── args
                     │              │    ├── variable: i:1
                     │              │    └── variable: curs:2
                     │              ├── params: i:11 curs:12
                     │              └── body
                     │                   └── project
                     │                        ├── columns: "_stmt_open_1":14
                     │                        ├── barrier
                     │                        │    ├── columns: curs:13
                     │                        │    └── project
                     │                        │         ├── columns: curs:13
                     │                        │         ├── values
                     │                        │         │    └── tuple
                     │                        │         └── projections
                     │                        │              └── function: crdb_internal.plpgsql_gen_cursor_name [as=curs:13]
                     │                        │                   └── variable: curs:12
                     │                        └── projections
                     │                             └── udf: _stmt_open_1 [as="_stmt_open_1":14]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    ├── variable: i:11
                     │                                  │    └── variable: curs:13
                     │                                  ├── params: i:3 curs:4
                     │                                  └── body
                     │                                       ├── open-cursor
                     │                                       │    └── project
                     │                                       │         ├── columns: x:5!null y:6
                     │                                       │         └── select
                     │                                       │              ├── columns: x:5!null y:6 rowid:7!null crdb_internal_mvcc_timestamp:8 tableoid:9
                     │                                       │              ├── scan xy
                     │                                       │              │    └── columns: x:5 y:6 rowid:7!null crdb_internal_mvcc_timestamp:8 tableoid:9
                     │                                       │              └── filters
                     │                                       │                   └── eq
                     │                                       │                        ├── variable: x:5
                     │                                       │                        └── variable: i:3
                     │                                       └── project
                     │                                            ├── columns: stmt_return_2:10!null
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── const: 0 [as=stmt_return_2:10]
                     └── const: 1

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  DECLARE
    curs REFCURSOR := 'foo';
    curs2 REFCURSOR := 'bar';
    curs3 REFCURSOR := 'baz';
  BEGIN
    OPEN curs FOR SELECT 1;
    OPEN curs2 FOR SELECT 2;
    OPEN curs3 FOR SELECT 3;
    RETURN 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:35
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:35]
           └── body
                └── limit
                     ├── columns: "_gen_cursor_name_7":34
                     ├── project
                     │    ├── columns: "_gen_cursor_name_7":34
                     │    ├── barrier
                     │    │    ├── columns: curs:1!null curs2:2!null curs3:3!null
                     │    │    └── project
                     │    │         ├── columns: curs3:3!null curs:1!null curs2:2!null
                     │    │         ├── project
                     │    │         │    ├── columns: curs2:2!null curs:1!null
                     │    │         │    ├── project
                     │    │         │    │    ├── columns: curs:1!null
                     │    │         │    │    ├── values
                     │    │         │    │    │    └── tuple
                     │    │         │    │    └── projections
                     │    │         │    │         └── const: 'foo' [as=curs:1]
                     │    │         │    └── projections
                     │    │         │         └── const: 'bar' [as=curs2:2]
                     │    │         └── projections
                     │    │              └── const: 'baz' [as=curs3:3]
                     │    └── projections
                     │         └── udf: _gen_cursor_name_7 [as="_gen_cursor_name_7":34]
                     │              ├── args
                     │              │    ├── variable: curs:1
                     │              │    ├── variable: curs2:2
                     │              │    └── variable: curs3:3
                     │              ├── params: curs:29 curs2:30 curs3:31
                     │              └── body
                     │                   └── project
                     │                        ├── columns: "_stmt_open_1":33
                     │                        ├── barrier
                     │                        │    ├── columns: curs:32
                     │                        │    └── project
                     │                        │         ├── columns: curs:32
                     │                        │         ├── values
                     │                        │         │    └── tuple
                     │                        │         └── projections
                     │                        │              └── function: crdb_internal.plpgsql_gen_cursor_name [as=curs:32]
                     │                        │                   └── variable: curs:29
                     │                        └── projections
                     │                             └── udf: _stmt_open_1 [as="_stmt_open_1":33]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    ├── variable: curs:32
                     │                                  │    ├── variable: curs2:30
                     │                                  │    └── variable: curs3:31
                     │                                  ├── params: curs:4 curs2:5 curs3:6
                     │                                  └── body
                     │                                       ├── open-cursor
                     │                                       │    └── project
                     │                                       │         ├── columns: "?column?":7!null
                     │                                       │         ├── values
                     │                                       │         │    └── tuple
                     │                                       │         └── projections
                     │                                       │              └── const: 1 [as="?column?":7]
                     │                                       └── project
                     │                                            ├── columns: "_gen_cursor_name_6":28
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: _gen_cursor_name_6 [as="_gen_cursor_name_6":28]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    ├── variable: curs:4
                     │                                                      │    ├── variable: curs2:5
                     │                                                      │    └── variable: curs3:6
                     │                                                      ├── params: curs:23 curs2:24 curs3:25
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: "_stmt_open_2":27
                     │                                                                ├── barrier
                     │                                                                │    ├── columns: curs2:26
                     │                                                                │    └── project
                     │                                                                │         ├── columns: curs2:26
                     │                                                                │         ├── values
                     │                                                                │         │    └── tuple
                     │                                                                │         └── projections
                     │                                                                │              └── function: crdb_internal.plpgsql_gen_cursor_name [as=curs2:26]
                     │                                                                │                   └── variable: curs2:24
                     │                                                                └── projections
                     │                                                                     └── udf: _stmt_open_2 [as="_stmt_open_2":27]
                     │                                                                          ├── tail-call
                     │                                                                          ├── args
                     │                                                                          │    ├── variable: curs:23
                     │                                                                          │    ├── variable: curs2:26
                     │                                                                          │    └── variable: curs3:25
                     │                                                                          ├── params: curs:8 curs2:9 curs3:10
                     │                                                                          └── body
                     │                                                                               ├── open-cursor
                     │                                                                               │    └── project
                     │                                                                               │         ├── columns: "?column?":11!null
                     │                                                                               │         ├── values
                     │                                                                               │         │    └── tuple
                     │                                                                               │         └── projections
                     │                                                                               │              └── const: 2 [as="?column?":11]
                     │                                                                               └── project
                     │                                                                                    ├── columns: "_gen_cursor_name_5":22
                     │                                                                                    ├── values
                     │                                                                                    │    └── tuple
                     │                                                                                    └── projections
                     │                                                                                         └── udf: _gen_cursor_name_5 [as="_gen_cursor_name_5":22]
                     │                                                                                              ├── tail-call
                     │                                                                                              ├── args
                     │                                                                                              │    ├── variable: curs:8
                     │                                                                                              │    ├── variable: curs2:9
                     │                                                                                              │    └── variable: curs3:10
                     │                                                                                              ├── params: curs:17 curs2:18 curs3:19
                     │                                                                                              └── body
                     │                                                                                                   └── project
                     │                                                                                                        ├── columns: "_stmt_open_3":21
                     │                                                                                                        ├── barrier
                     │                                                                                                        │    ├── columns: curs3:20
                     │                                                                                                        │    └── project
                     │                                                                                                        │         ├── columns: curs3:20
                     │                                                                                                        │         ├── values
                     │                                                                                                        │         │    └── tuple
                     │                                                                                                        │         └── projections
                     │                                                                                                        │              └── function: crdb_internal.plpgsql_gen_cursor_name [as=curs3:20]
                     │                                                                                                        │                   └── variable: curs3:19
                     │                                                                                                        └── projections
                     │                                                                                                             └── udf: _stmt_open_3 [as="_stmt_open_3":21]
                     │                                                                                                                  ├── tail-call
                     │                                                                                                                  ├── args
                     │                                                                                                                  │    ├── variable: curs:17
                     │                                                                                                                  │    ├── variable: curs2:18
                     │                                                                                                                  │    └── variable: curs3:20
                     │                                                                                                                  ├── params: curs:12 curs2:13 curs3:14
                     │                                                                                                                  └── body
                     │                                                                                                                       ├── open-cursor
                     │                                                                                                                       │    └── project
                     │                                                                                                                       │         ├── columns: "?column?":15!null
                     │                                                                                                                       │         ├── values
                     │                                                                                                                       │         │    └── tuple
                     │                                                                                                                       │         └── projections
                     │                                                                                                                       │              └── const: 3 [as="?column?":15]
                     │                                                                                                                       └── project
                     │                                                                                                                            ├── columns: stmt_return_4:16!null
                     │                                                                                                                            ├── values
                     │                                                                                                                            │    └── tuple
                     │                                                                                                                            └── projections
                     │                                                                                                                                 └── const: 0 [as=stmt_return_4:16]
                     └── const: 1

# Testing CLOSE statement.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  DECLARE
    curs REFCURSOR := 'foo';
  BEGIN
    OPEN curs FOR SELECT 1;
    CLOSE curs;
    RETURN 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-all
SELECT f();
----
project
 ├── columns: f:12(int)
 ├── cardinality: [1 - 1]
 ├── volatile
 ├── stats: [rows=1]
 ├── cost: 0.05
 ├── key: ()
 ├── fd: ()-->(12)
 ├── prune: (12)
 ├── values
 │    ├── cardinality: [1 - 1]
 │    ├── stats: [rows=1]
 │    ├── cost: 0.02
 │    ├── key: ()
 │    └── tuple [type=tuple]
 └── projections
      └── udf: f [as=f:12, type=int, volatile, udf]
           └── body
                └── limit
                     ├── columns: "_gen_cursor_name_5":11(int)
                     ├── cardinality: [1 - 1]
                     ├── volatile
                     ├── stats: [rows=1]
                     ├── key: ()
                     ├── fd: ()-->(11)
                     ├── project
                     │    ├── columns: "_gen_cursor_name_5":11(int)
                     │    ├── cardinality: [1 - 1]
                     │    ├── volatile
                     │    ├── stats: [rows=1]
                     │    ├── key: ()
                     │    ├── fd: ()-->(11)
                     │    ├── barrier
                     │    │    ├── columns: curs:1(refcursor!null)
                     │    │    ├── cardinality: [1 - 1]
                     │    │    ├── stats: [rows=1]
                     │    │    ├── key: ()
                     │    │    ├── fd: ()-->(1)
                     │    │    └── project
                     │    │         ├── columns: curs:1(refcursor!null)
                     │    │         ├── cardinality: [1 - 1]
                     │    │         ├── stats: [rows=1]
                     │    │         ├── key: ()
                     │    │         ├── fd: ()-->(1)
                     │    │         ├── values
                     │    │         │    ├── cardinality: [1 - 1]
                     │    │         │    ├── stats: [rows=1]
                     │    │         │    ├── key: ()
                     │    │         │    └── tuple [type=tuple]
                     │    │         └── projections
                     │    │              └── const: 'foo' [as=curs:1, type=refcursor]
                     │    └── projections
                     │         └── udf: _gen_cursor_name_5 [as="_gen_cursor_name_5":11, type=int, outer=(1), volatile, udf]
                     │              ├── args
                     │              │    └── variable: curs:1 [type=refcursor]
                     │              ├── params: curs:8(refcursor)
                     │              └── body
                     │                   └── project
                     │                        ├── columns: "_stmt_open_1":10(int)
                     │                        ├── outer: (8)
                     │                        ├── cardinality: [1 - 1]
                     │                        ├── volatile
                     │                        ├── stats: [rows=1]
                     │                        ├── key: ()
                     │                        ├── fd: ()-->(10)
                     │                        ├── barrier
                     │                        │    ├── columns: curs:9(refcursor)
                     │                        │    ├── outer: (8)
                     │                        │    ├── cardinality: [1 - 1]
                     │                        │    ├── volatile
                     │                        │    ├── stats: [rows=1]
                     │                        │    ├── key: ()
                     │                        │    ├── fd: ()-->(9)
                     │                        │    └── project
                     │                        │         ├── columns: curs:9(refcursor)
                     │                        │         ├── outer: (8)
                     │                        │         ├── cardinality: [1 - 1]
                     │                        │         ├── volatile
                     │                        │         ├── stats: [rows=1]
                     │                        │         ├── key: ()
                     │                        │         ├── fd: ()-->(9)
                     │                        │         ├── values
                     │                        │         │    ├── cardinality: [1 - 1]
                     │                        │         │    ├── stats: [rows=1]
                     │                        │         │    ├── key: ()
                     │                        │         │    └── tuple [type=tuple]
                     │                        │         └── projections
                     │                        │              └── function: crdb_internal.plpgsql_gen_cursor_name [as=curs:9, type=refcursor, outer=(8), volatile]
                     │                        │                   └── variable: curs:8 [type=refcursor]
                     │                        └── projections
                     │                             └── udf: _stmt_open_1 [as="_stmt_open_1":10, type=int, outer=(9), volatile, udf]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    └── variable: curs:9 [type=refcursor]
                     │                                  ├── params: curs:2(refcursor)
                     │                                  └── body
                     │                                       ├── open-cursor
                     │                                       │    └── project
                     │                                       │         ├── columns: "?column?":3(int!null)
                     │                                       │         ├── cardinality: [1 - 1]
                     │                                       │         ├── stats: [rows=1]
                     │                                       │         ├── key: ()
                     │                                       │         ├── fd: ()-->(3)
                     │                                       │         ├── values
                     │                                       │         │    ├── cardinality: [1 - 1]
                     │                                       │         │    ├── stats: [rows=1]
                     │                                       │         │    ├── key: ()
                     │                                       │         │    └── tuple [type=tuple]
                     │                                       │         └── projections
                     │                                       │              └── const: 1 [as="?column?":3, type=int]
                     │                                       └── project
                     │                                            ├── columns: "_stmt_close_2":7(int)
                     │                                            ├── outer: (2)
                     │                                            ├── cardinality: [1 - 1]
                     │                                            ├── volatile
                     │                                            ├── stats: [rows=1]
                     │                                            ├── key: ()
                     │                                            ├── fd: ()-->(7)
                     │                                            ├── values
                     │                                            │    ├── cardinality: [1 - 1]
                     │                                            │    ├── stats: [rows=1]
                     │                                            │    ├── key: ()
                     │                                            │    └── tuple [type=tuple]
                     │                                            └── projections
                     │                                                 └── udf: _stmt_close_2 [as="_stmt_close_2":7, type=int, outer=(2), volatile, udf]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    └── variable: curs:2 [type=refcursor]
                     │                                                      ├── params: curs:4(refcursor)
                     │                                                      └── body
                     │                                                           ├── project
                     │                                                           │    ├── columns: stmt_close_3:5(int)
                     │                                                           │    ├── outer: (4)
                     │                                                           │    ├── cardinality: [1 - 1]
                     │                                                           │    ├── volatile
                     │                                                           │    ├── stats: [rows=1]
                     │                                                           │    ├── key: ()
                     │                                                           │    ├── fd: ()-->(5)
                     │                                                           │    ├── values
                     │                                                           │    │    ├── cardinality: [1 - 1]
                     │                                                           │    │    ├── stats: [rows=1]
                     │                                                           │    │    ├── key: ()
                     │                                                           │    │    └── tuple [type=tuple]
                     │                                                           │    └── projections
                     │                                                           │         └── function: crdb_internal.plpgsql_close [as=stmt_close_3:5, type=int, outer=(4), volatile]
                     │                                                           │              └── variable: curs:4 [type=refcursor]
                     │                                                           └── project
                     │                                                                ├── columns: stmt_return_4:6(int!null)
                     │                                                                ├── cardinality: [1 - 1]
                     │                                                                ├── stats: [rows=1]
                     │                                                                ├── key: ()
                     │                                                                ├── fd: ()-->(6)
                     │                                                                ├── values
                     │                                                                │    ├── cardinality: [1 - 1]
                     │                                                                │    ├── stats: [rows=1]
                     │                                                                │    ├── key: ()
                     │                                                                │    └── tuple [type=tuple]
                     │                                                                └── projections
                     │                                                                     └── const: 0 [as=stmt_return_4:6, type=int]
                     └── const: 1 [type=int]

# Combined exception block and cursors.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  DECLARE
    curs REFCURSOR := 'foo';
  BEGIN
    OPEN curs FOR SELECT 1;
    RETURN 1 // 0;
  EXCEPTION
    WHEN division_by_zero THEN
      OPEN curs FOR SELECT 2;
      RETURN -1;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:19
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:19]
           └── body
                └── limit
                     ├── columns: nested_block_5:18
                     ├── project
                     │    ├── columns: nested_block_5:18
                     │    ├── barrier
                     │    │    ├── columns: curs:1!null
                     │    │    └── project
                     │    │         ├── columns: curs:1!null
                     │    │         ├── values
                     │    │         │    └── tuple
                     │    │         └── projections
                     │    │              └── const: 'foo' [as=curs:1]
                     │    └── projections
                     │         └── udf: nested_block_5 [as=nested_block_5:18]
                     │              ├── args
                     │              │    └── variable: curs:1
                     │              ├── params: curs:10
                     │              ├── body
                     │              │    └── project
                     │              │         ├── columns: "_gen_cursor_name_8":17
                     │              │         ├── values
                     │              │         │    └── tuple
                     │              │         └── projections
                     │              │              └── udf: _gen_cursor_name_8 [as="_gen_cursor_name_8":17]
                     │              │                   ├── tail-call
                     │              │                   ├── args
                     │              │                   │    └── variable: curs:10
                     │              │                   ├── params: curs:14
                     │              │                   └── body
                     │              │                        └── project
                     │              │                             ├── columns: "_stmt_open_6":16
                     │              │                             ├── barrier
                     │              │                             │    ├── columns: curs:15
                     │              │                             │    └── project
                     │              │                             │         ├── columns: curs:15
                     │              │                             │         ├── values
                     │              │                             │         │    └── tuple
                     │              │                             │         └── projections
                     │              │                             │              └── function: crdb_internal.plpgsql_gen_cursor_name [as=curs:15]
                     │              │                             │                   └── variable: curs:14
                     │              │                             └── projections
                     │              │                                  └── udf: _stmt_open_6 [as="_stmt_open_6":16]
                     │              │                                       ├── tail-call
                     │              │                                       ├── args
                     │              │                                       │    └── variable: curs:15
                     │              │                                       ├── params: curs:11
                     │              │                                       └── body
                     │              │                                            ├── open-cursor
                     │              │                                            │    └── project
                     │              │                                            │         ├── columns: "?column?":12!null
                     │              │                                            │         ├── values
                     │              │                                            │         │    └── tuple
                     │              │                                            │         └── projections
                     │              │                                            │              └── const: 1 [as="?column?":12]
                     │              │                                            └── project
                     │              │                                                 ├── columns: stmt_return_7:13!null
                     │              │                                                 ├── values
                     │              │                                                 │    └── tuple
                     │              │                                                 └── projections
                     │              │                                                      └── floor-div [as=stmt_return_7:13]
                     │              │                                                           ├── const: 1
                     │              │                                                           └── const: 0
                     │              └── exception-handler
                     │                   └── SQLSTATE '22012'
                     │                        └── project
                     │                             ├── columns: "_gen_cursor_name_4":9
                     │                             ├── values
                     │                             │    └── tuple
                     │                             └── projections
                     │                                  └── udf: _gen_cursor_name_4 [as="_gen_cursor_name_4":9]
                     │                                       ├── args
                     │                                       │    └── variable: curs:2
                     │                                       ├── params: curs:6
                     │                                       └── body
                     │                                            └── project
                     │                                                 ├── columns: "_stmt_open_2":8
                     │                                                 ├── barrier
                     │                                                 │    ├── columns: curs:7
                     │                                                 │    └── project
                     │                                                 │         ├── columns: curs:7
                     │                                                 │         ├── values
                     │                                                 │         │    └── tuple
                     │                                                 │         └── projections
                     │                                                 │              └── function: crdb_internal.plpgsql_gen_cursor_name [as=curs:7]
                     │                                                 │                   └── variable: curs:6
                     │                                                 └── projections
                     │                                                      └── udf: _stmt_open_2 [as="_stmt_open_2":8]
                     │                                                           ├── tail-call
                     │                                                           ├── args
                     │                                                           │    └── variable: curs:7
                     │                                                           ├── params: curs:3
                     │                                                           └── body
                     │                                                                ├── open-cursor
                     │                                                                │    └── project
                     │                                                                │         ├── columns: "?column?":4!null
                     │                                                                │         ├── values
                     │                                                                │         │    └── tuple
                     │                                                                │         └── projections
                     │                                                                │              └── const: 2 [as="?column?":4]
                     │                                                                └── project
                     │                                                                     ├── columns: stmt_return_3:5!null
                     │                                                                     ├── values
                     │                                                                     │    └── tuple
                     │                                                                     └── projections
                     │                                                                          └── const: -1 [as=stmt_return_3:5]
                     └── const: 1

# Testing FETCH/MOVE.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  DECLARE
    curs REFCURSOR := 'foo';
    x INT;
  BEGIN
    OPEN curs FOR SELECT * FROM xy ORDER BY x;
    FETCH curs INTO x;
    RETURN x;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-all
SELECT f();
----
project
 ├── columns: f:24(int)
 ├── cardinality: [1 - 1]
 ├── volatile
 ├── stats: [rows=1]
 ├── cost: 0.05
 ├── key: ()
 ├── fd: ()-->(24)
 ├── prune: (24)
 ├── values
 │    ├── cardinality: [1 - 1]
 │    ├── stats: [rows=1]
 │    ├── cost: 0.02
 │    ├── key: ()
 │    └── tuple [type=tuple]
 └── projections
      └── udf: f [as=f:24, type=int, volatile, udf]
           └── body
                └── limit
                     ├── columns: "_gen_cursor_name_6":23(int)
                     ├── cardinality: [1 - 1]
                     ├── volatile
                     ├── stats: [rows=1]
                     ├── key: ()
                     ├── fd: ()-->(23)
                     ├── project
                     │    ├── columns: "_gen_cursor_name_6":23(int)
                     │    ├── cardinality: [1 - 1]
                     │    ├── volatile
                     │    ├── stats: [rows=1]
                     │    ├── key: ()
                     │    ├── fd: ()-->(23)
                     │    ├── barrier
                     │    │    ├── columns: curs:1(refcursor!null) x:2(int)
                     │    │    ├── cardinality: [1 - 1]
                     │    │    ├── immutable
                     │    │    ├── stats: [rows=1]
                     │    │    ├── key: ()
                     │    │    ├── fd: ()-->(1,2)
                     │    │    └── project
                     │    │         ├── columns: x:2(int) curs:1(refcursor!null)
                     │    │         ├── cardinality: [1 - 1]
                     │    │         ├── immutable
                     │    │         ├── stats: [rows=1]
                     │    │         ├── key: ()
                     │    │         ├── fd: ()-->(1,2)
                     │    │         ├── project
                     │    │         │    ├── columns: curs:1(refcursor!null)
                     │    │         │    ├── cardinality: [1 - 1]
                     │    │         │    ├── stats: [rows=1]
                     │    │         │    ├── key: ()
                     │    │         │    ├── fd: ()-->(1)
                     │    │         │    ├── prune: (1)
                     │    │         │    ├── values
                     │    │         │    │    ├── cardinality: [1 - 1]
                     │    │         │    │    ├── stats: [rows=1]
                     │    │         │    │    ├── key: ()
                     │    │         │    │    └── tuple [type=tuple]
                     │    │         │    └── projections
                     │    │         │         └── const: 'foo' [as=curs:1, type=refcursor]
                     │    │         └── projections
                     │    │              └── cast: INT8 [as=x:2, type=int, immutable]
                     │    │                   └── null [type=unknown]
                     │    └── projections
                     │         └── udf: _gen_cursor_name_6 [as="_gen_cursor_name_6":23, type=int, outer=(1,2), volatile, udf]
                     │              ├── args
                     │              │    ├── variable: curs:1 [type=refcursor]
                     │              │    └── variable: x:2 [type=int]
                     │              ├── params: curs:19(refcursor) x:20(int)
                     │              └── body
                     │                   └── project
                     │                        ├── columns: "_stmt_open_1":22(int)
                     │                        ├── outer: (19,20)
                     │                        ├── cardinality: [1 - 1]
                     │                        ├── volatile
                     │                        ├── stats: [rows=1]
                     │                        ├── key: ()
                     │                        ├── fd: ()-->(22)
                     │                        ├── barrier
                     │                        │    ├── columns: curs:21(refcursor)
                     │                        │    ├── outer: (19)
                     │                        │    ├── cardinality: [1 - 1]
                     │                        │    ├── volatile
                     │                        │    ├── stats: [rows=1]
                     │                        │    ├── key: ()
                     │                        │    ├── fd: ()-->(21)
                     │                        │    └── project
                     │                        │         ├── columns: curs:21(refcursor)
                     │                        │         ├── outer: (19)
                     │                        │         ├── cardinality: [1 - 1]
                     │                        │         ├── volatile
                     │                        │         ├── stats: [rows=1]
                     │                        │         ├── key: ()
                     │                        │         ├── fd: ()-->(21)
                     │                        │         ├── values
                     │                        │         │    ├── cardinality: [1 - 1]
                     │                        │         │    ├── stats: [rows=1]
                     │                        │         │    ├── key: ()
                     │                        │         │    └── tuple [type=tuple]
                     │                        │         └── projections
                     │                        │              └── function: crdb_internal.plpgsql_gen_cursor_name [as=curs:21, type=refcursor, outer=(19), volatile]
                     │                        │                   └── variable: curs:19 [type=refcursor]
                     │                        └── projections
                     │                             └── udf: _stmt_open_1 [as="_stmt_open_1":22, type=int, outer=(20,21), volatile, udf]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    ├── variable: curs:21 [type=refcursor]
                     │                                  │    └── variable: x:20 [type=int]
                     │                                  ├── params: curs:3(refcursor) x:4(int)
                     │                                  └── body
                     │                                       ├── open-cursor
                     │                                       │    └── project
                     │                                       │         ├── columns: t.public.xy.x:5(int) t.public.xy.y:6(int)
                     │                                       │         ├── stats: [rows=1000]
                     │                                       │         └── scan t.public.xy
                     │                                       │              ├── columns: t.public.xy.x:5(int) t.public.xy.y:6(int) t.public.xy.rowid:7(int!null) t.public.xy.crdb_internal_mvcc_timestamp:8(decimal) t.public.xy.tableoid:9(oid)
                     │                                       │              ├── stats: [rows=1000]
                     │                                       │              ├── key: (7)
                     │                                       │              ├── fd: (7)-->(5,6,8,9)
                     │                                       │              └── prune: (5-9)
                     │                                       └── project
                     │                                            ├── columns: "_stmt_fetch_2":18(int)
                     │                                            ├── outer: (3,4)
                     │                                            ├── cardinality: [1 - 1]
                     │                                            ├── volatile
                     │                                            ├── stats: [rows=1]
                     │                                            ├── key: ()
                     │                                            ├── fd: ()-->(18)
                     │                                            ├── values
                     │                                            │    ├── cardinality: [1 - 1]
                     │                                            │    ├── stats: [rows=1]
                     │                                            │    ├── key: ()
                     │                                            │    └── tuple [type=tuple]
                     │                                            └── projections
                     │                                                 └── udf: _stmt_fetch_2 [as="_stmt_fetch_2":18, type=int, outer=(3,4), volatile, udf]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    ├── variable: curs:3 [type=refcursor]
                     │                                                      │    └── variable: x:4 [type=int]
                     │                                                      ├── params: curs:10(refcursor) x:11(int)
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: "_stmt_fetch_ret_4":17(int)
                     │                                                                ├── outer: (10)
                     │                                                                ├── cardinality: [1 - 1]
                     │                                                                ├── volatile
                     │                                                                ├── stats: [rows=1]
                     │                                                                ├── key: ()
                     │                                                                ├── fd: ()-->(17)
                     │                                                                ├── barrier
                     │                                                                │    ├── columns: x:13(int)
                     │                                                                │    ├── outer: (10)
                     │                                                                │    ├── cardinality: [1 - 1]
                     │                                                                │    ├── volatile
                     │                                                                │    ├── stats: [rows=1]
                     │                                                                │    ├── key: ()
                     │                                                                │    ├── fd: ()-->(13)
                     │                                                                │    └── project
                     │                                                                │         ├── columns: x:13(int)
                     │                                                                │         ├── outer: (10)
                     │                                                                │         ├── cardinality: [1 - 1]
                     │                                                                │         ├── volatile
                     │                                                                │         ├── stats: [rows=1]
                     │                                                                │         ├── key: ()
                     │                                                                │         ├── fd: ()-->(13)
                     │                                                                │         ├── project
                     │                                                                │         │    ├── columns: stmt_fetch_3:12(tuple{int})
                     │                                                                │         │    ├── outer: (10)
                     │                                                                │         │    ├── cardinality: [1 - 1]
                     │                                                                │         │    ├── volatile
                     │                                                                │         │    ├── stats: [rows=1]
                     │                                                                │         │    ├── key: ()
                     │                                                                │         │    ├── fd: ()-->(12)
                     │                                                                │         │    ├── prune: (12)
                     │                                                                │         │    ├── values
                     │                                                                │         │    │    ├── cardinality: [1 - 1]
                     │                                                                │         │    │    ├── stats: [rows=1]
                     │                                                                │         │    │    ├── key: ()
                     │                                                                │         │    │    └── tuple [type=tuple]
                     │                                                                │         │    └── projections
                     │                                                                │         │         └── function: crdb_internal.plpgsql_fetch [as=stmt_fetch_3:12, type=tuple{int}, outer=(10), volatile]
                     │                                                                │         │              ├── variable: curs:10 [type=refcursor]
                     │                                                                │         │              ├── const: 0 [type=int]
                     │                                                                │         │              ├── const: 1 [type=int]
                     │                                                                │         │              └── tuple [type=tuple{int}]
                     │                                                                │         │                   └── null [type=int]
                     │                                                                │         └── projections
                     │                                                                │              └── column-access: 0 [as=x:13, type=int, outer=(12)]
                     │                                                                │                   └── variable: stmt_fetch_3:12 [type=tuple{int}]
                     │                                                                └── projections
                     │                                                                     └── udf: _stmt_fetch_ret_4 [as="_stmt_fetch_ret_4":17, type=int, outer=(10,13), udf]
                     │                                                                          ├── tail-call
                     │                                                                          ├── args
                     │                                                                          │    ├── variable: curs:10 [type=refcursor]
                     │                                                                          │    └── variable: x:13 [type=int]
                     │                                                                          ├── params: curs:14(refcursor) x:15(int)
                     │                                                                          └── body
                     │                                                                               └── project
                     │                                                                                    ├── columns: stmt_return_5:16(int)
                     │                                                                                    ├── outer: (15)
                     │                                                                                    ├── cardinality: [1 - 1]
                     │                                                                                    ├── stats: [rows=1]
                     │                                                                                    ├── key: ()
                     │                                                                                    ├── fd: ()-->(16)
                     │                                                                                    ├── values
                     │                                                                                    │    ├── cardinality: [1 - 1]
                     │                                                                                    │    ├── stats: [rows=1]
                     │                                                                                    │    ├── key: ()
                     │                                                                                    │    └── tuple [type=tuple]
                     │                                                                                    └── projections
                     │                                                                                         └── variable: x:15 [as=stmt_return_5:16, type=int, outer=(15)]
                     └── const: 1 [type=int]

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  DECLARE
    curs REFCURSOR := 'foo';
  BEGIN
    OPEN curs FOR SELECT * FROM xy ORDER BY x;
    MOVE curs;
    RETURN 0;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:16
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:16]
           └── body
                └── limit
                     ├── columns: "_gen_cursor_name_5":15
                     ├── project
                     │    ├── columns: "_gen_cursor_name_5":15
                     │    ├── barrier
                     │    │    ├── columns: curs:1!null
                     │    │    └── project
                     │    │         ├── columns: curs:1!null
                     │    │         ├── values
                     │    │         │    └── tuple
                     │    │         └── projections
                     │    │              └── const: 'foo' [as=curs:1]
                     │    └── projections
                     │         └── udf: _gen_cursor_name_5 [as="_gen_cursor_name_5":15]
                     │              ├── args
                     │              │    └── variable: curs:1
                     │              ├── params: curs:12
                     │              └── body
                     │                   └── project
                     │                        ├── columns: "_stmt_open_1":14
                     │                        ├── barrier
                     │                        │    ├── columns: curs:13
                     │                        │    └── project
                     │                        │         ├── columns: curs:13
                     │                        │         ├── values
                     │                        │         │    └── tuple
                     │                        │         └── projections
                     │                        │              └── function: crdb_internal.plpgsql_gen_cursor_name [as=curs:13]
                     │                        │                   └── variable: curs:12
                     │                        └── projections
                     │                             └── udf: _stmt_open_1 [as="_stmt_open_1":14]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    └── variable: curs:13
                     │                                  ├── params: curs:2
                     │                                  └── body
                     │                                       ├── open-cursor
                     │                                       │    └── project
                     │                                       │         ├── columns: x:3 y:4
                     │                                       │         └── scan xy
                     │                                       │              └── columns: x:3 y:4 rowid:5!null crdb_internal_mvcc_timestamp:6 tableoid:7
                     │                                       └── project
                     │                                            ├── columns: "_stmt_fetch_2":11
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: _stmt_fetch_2 [as="_stmt_fetch_2":11]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    └── variable: curs:2
                     │                                                      ├── params: curs:8
                     │                                                      └── body
                     │                                                           ├── project
                     │                                                           │    ├── columns: stmt_fetch_3:9
                     │                                                           │    ├── values
                     │                                                           │    │    └── tuple
                     │                                                           │    └── projections
                     │                                                           │         └── function: crdb_internal.plpgsql_fetch [as=stmt_fetch_3:9]
                     │                                                           │              ├── variable: curs:8
                     │                                                           │              ├── const: 0
                     │                                                           │              ├── const: 1
                     │                                                           │              └── tuple
                     │                                                           └── project
                     │                                                                ├── columns: stmt_return_4:10!null
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── const: 0 [as=stmt_return_4:10]
                     └── const: 1

# Correctly display the OTHERS branch.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
    RETURN 1 // 0;
  EXCEPTION
    WHEN assert_failure THEN
      RETURN -1;
    WHEN OTHERS THEN
      RETURN -2;
    WHEN query_canceled THEN
      RETURN -3;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f();
----
project
 ├── columns: f:6
 ├── values
 │    └── tuple
 └── projections
      └── udf: f [as=f:6]
           └── body
                └── limit
                     ├── columns: nested_block_7:5
                     ├── project
                     │    ├── columns: nested_block_7:5
                     │    ├── values
                     │    │    └── tuple
                     │    └── projections
                     │         └── udf: nested_block_7 [as=nested_block_7:5]
                     │              ├── body
                     │              │    └── project
                     │              │         ├── columns: stmt_return_8:4!null
                     │              │         ├── values
                     │              │         │    └── tuple
                     │              │         └── projections
                     │              │              └── floor-div [as=stmt_return_8:4]
                     │              │                   ├── const: 1
                     │              │                   └── const: 0
                     │              └── exception-handler
                     │                   ├── SQLSTATE 'P0004'
                     │                   │    └── project
                     │                   │         ├── columns: stmt_return_2:1!null
                     │                   │         ├── values
                     │                   │         │    └── tuple
                     │                   │         └── projections
                     │                   │              └── const: -1 [as=stmt_return_2:1]
                     │                   ├── OTHERS
                     │                   │    └── project
                     │                   │         ├── columns: stmt_return_4:2!null
                     │                   │         ├── values
                     │                   │         │    └── tuple
                     │                   │         └── projections
                     │                   │              └── const: -2 [as=stmt_return_4:2]
                     │                   └── SQLSTATE '57014'
                     │                        └── project
                     │                             ├── columns: stmt_return_6:3!null
                     │                             ├── values
                     │                             │    └── tuple
                     │                             └── projections
                     │                                  └── const: -3 [as=stmt_return_6:3]
                     └── const: 1

# Project a typed NULL for the end-of-function RAISE statement.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  BEGIN
  END
$$ LANGUAGE PLpgSQL;
----

build format=(show-scalars,show-types)
SELECT f();
----
project
 ├── columns: f:4(int)
 ├── values
 │    └── tuple [type=tuple]
 └── projections
      └── udf: f [as=f:4, type=int]
           └── body
                └── limit
                     ├── columns: "_end_of_function_1":3(int)
                     ├── project
                     │    ├── columns: "_end_of_function_1":3(int)
                     │    ├── values
                     │    │    └── tuple [type=tuple]
                     │    └── projections
                     │         └── udf: _end_of_function_1 [as="_end_of_function_1":3, type=int]
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:1(int)
                     │                   │    ├── values
                     │                   │    │    └── tuple [type=tuple]
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:1, type=int]
                     │                   │              ├── const: 'ERROR' [type=string]
                     │                   │              ├── const: 'control reached end of function without RETURN' [type=string]
                     │                   │              ├── const: '' [type=string]
                     │                   │              ├── const: '' [type=string]
                     │                   │              └── const: '2F005' [type=string]
                     │                   └── project
                     │                        ├── columns: end_of_function_3:2(int)
                     │                        ├── values
                     │                        │    └── tuple [type=tuple]
                     │                        └── projections
                     │                             └── null [as=end_of_function_3:2, type=int]
                     └── const: 1 [type=int]

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT AS $$
  DECLARE
    i INT := 0;
  BEGIN
    WHILE i < 10 LOOP
      i := i + 1;
    END LOOP;
  END
$$ LANGUAGE PLpgSQL;
----

build format=(show-scalars,show-types)
SELECT f();
----
project
 ├── columns: f:15(int)
 ├── values
 │    └── tuple [type=tuple]
 └── projections
      └── udf: f [as=f:15, type=int]
           └── body
                └── limit
                     ├── columns: stmt_loop_5:14(int)
                     ├── project
                     │    ├── columns: stmt_loop_5:14(int)
                     │    ├── barrier
                     │    │    ├── columns: i:1(int!null)
                     │    │    └── project
                     │    │         ├── columns: i:1(int!null)
                     │    │         ├── values
                     │    │         │    └── tuple [type=tuple]
                     │    │         └── projections
                     │    │              └── const: 0 [as=i:1, type=int]
                     │    └── projections
                     │         └── udf: stmt_loop_5 [as=stmt_loop_5:14, type=int]
                     │              ├── args
                     │              │    └── variable: i:1 [type=int]
                     │              ├── params: i:7(int)
                     │              └── body
                     │                   └── project
                     │                        ├── columns: stmt_if_7:13(int)
                     │                        ├── values
                     │                        │    └── tuple [type=tuple]
                     │                        └── projections
                     │                             └── case [as=stmt_if_7:13, type=int]
                     │                                  ├── true [type=bool]
                     │                                  ├── when [type=int]
                     │                                  │    ├── lt [type=bool]
                     │                                  │    │    ├── variable: i:7 [type=int]
                     │                                  │    │    └── const: 10 [type=int]
                     │                                  │    └── subquery [type=int]
                     │                                  │         ├── tail-call
                     │                                  │         └── project
                     │                                  │              ├── columns: stmt_if_6:11(int)
                     │                                  │              ├── project
                     │                                  │              │    ├── columns: i:10(int)
                     │                                  │              │    ├── values
                     │                                  │              │    │    └── tuple [type=tuple]
                     │                                  │              │    └── projections
                     │                                  │              │         └── plus [as=i:10, type=int]
                     │                                  │              │              ├── variable: i:7 [type=int]
                     │                                  │              │              └── const: 1 [type=int]
                     │                                  │              └── projections
                     │                                  │                   └── udf: stmt_if_6 [as=stmt_if_6:11, type=int]
                     │                                  │                        ├── tail-call
                     │                                  │                        ├── args
                     │                                  │                        │    └── variable: i:10 [type=int]
                     │                                  │                        ├── params: i:8(int)
                     │                                  │                        └── body
                     │                                  │                             └── project
                     │                                  │                                  ├── columns: stmt_loop_5:9(int)
                     │                                  │                                  ├── values
                     │                                  │                                  │    └── tuple [type=tuple]
                     │                                  │                                  └── projections
                     │                                  │                                       └── udf: stmt_loop_5 [as=stmt_loop_5:9, type=int]
                     │                                  │                                            ├── tail-call
                     │                                  │                                            ├── args
                     │                                  │                                            │    └── variable: i:8 [type=int]
                     │                                  │                                            └── recursive-call
                     │                                  └── subquery [type=int]
                     │                                       ├── tail-call
                     │                                       └── project
                     │                                            ├── columns: loop_exit_1:12(int)
                     │                                            ├── values
                     │                                            │    └── tuple [type=tuple]
                     │                                            └── projections
                     │                                                 └── udf: loop_exit_1 [as=loop_exit_1:12, type=int]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    └── variable: i:7 [type=int]
                     │                                                      ├── params: i:2(int)
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: "_end_of_function_2":6(int)
                     │                                                                ├── values
                     │                                                                │    └── tuple [type=tuple]
                     │                                                                └── projections
                     │                                                                     └── udf: _end_of_function_2 [as="_end_of_function_2":6, type=int]
                     │                                                                          ├── tail-call
                     │                                                                          ├── args
                     │                                                                          │    └── variable: i:2 [type=int]
                     │                                                                          ├── params: i:3(int)
                     │                                                                          └── body
                     │                                                                               ├── project
                     │                                                                               │    ├── columns: stmt_raise_3:4(int)
                     │                                                                               │    ├── values
                     │                                                                               │    │    └── tuple [type=tuple]
                     │                                                                               │    └── projections
                     │                                                                               │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_3:4, type=int]
                     │                                                                               │              ├── const: 'ERROR' [type=string]
                     │                                                                               │              ├── const: 'control reached end of function without RETURN' [type=string]
                     │                                                                               │              ├── const: '' [type=string]
                     │                                                                               │              ├── const: '' [type=string]
                     │                                                                               │              └── const: '2F005' [type=string]
                     │                                                                               └── project
                     │                                                                                    ├── columns: end_of_function_4:5(int)
                     │                                                                                    ├── values
                     │                                                                                    │    └── tuple [type=tuple]
                     │                                                                                    └── projections
                     │                                                                                         └── null [as=end_of_function_4:5, type=int]
                     └── const: 1 [type=int]

# Infer return type for a RECORD-returning routine.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS RECORD AS $$
  BEGIN
    RETURN ROW(False);
  END
$$ LANGUAGE PLpgSQL;
----

build format=(show-scalars,show-types)
SELECT f();
----
project
 ├── columns: f:2(tuple{bool})
 ├── values
 │    └── tuple [type=tuple]
 └── projections
      └── udf: f [as=f:2, type=tuple{bool}]
           └── body
                └── limit
                     ├── columns: stmt_return_1:1(tuple{bool}!null)
                     ├── project
                     │    ├── columns: stmt_return_1:1(tuple{bool}!null)
                     │    ├── values
                     │    │    └── tuple [type=tuple]
                     │    └── projections
                     │         └── tuple [as=stmt_return_1:1, type=tuple{bool}]
                     │              └── false [type=bool]
                     └── const: 1 [type=int]

# Regression test for #114826 - ensure that SQL statements return at least one
# row.
exec-ddl
CREATE TABLE t114826 (a INT);
----

exec-ddl
CREATE FUNCTION f114826() RETURNS INT AS $$
  DECLARE
    found INT;
  BEGIN
    RAISE NOTICE 'HERE 1';
    SELECT a INTO found FROM t114826 WHERE a = 3;
    RAISE NOTICE 'HERE 2';
    INSERT INTO t114826 (SELECT * FROM t114826) RETURNING a INTO found;
    RAISE NOTICE 'HERE 3';
    RETURN 10;
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT f114826();
----
project
 ├── columns: f114826:35
 ├── values
 │    └── tuple
 └── projections
      └── udf: f114826 [as=f114826:35]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":34
                     ├── project
                     │    ├── columns: "_stmt_raise_1":34
                     │    ├── barrier
                     │    │    ├── columns: found:1
                     │    │    └── project
                     │    │         ├── columns: found:1
                     │    │         ├── values
                     │    │         │    └── tuple
                     │    │         └── projections
                     │    │              └── cast: INT8 [as=found:1]
                     │    │                   └── null
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":34]
                     │              ├── args
                     │              │    └── variable: found:1
                     │              ├── params: found:2
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:3
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:3]
                     │                   │              ├── const: 'NOTICE'
                     │                   │              ├── const: 'HERE 1'
                     │                   │              ├── const: ''
                     │                   │              ├── const: ''
                     │                   │              └── const: '00000'
                     │                   └── project
                     │                        ├── columns: "_stmt_exec_3":33
                     │                        ├── values
                     │                        │    └── tuple
                     │                        └── projections
                     │                             └── udf: _stmt_exec_3 [as="_stmt_exec_3":33]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    └── variable: found:2
                     │                                  ├── params: found:4
                     │                                  └── body
                     │                                       └── project
                     │                                            ├── columns: "_stmt_exec_ret_4":32
                     │                                            ├── barrier
                     │                                            │    ├── columns: found:31
                     │                                            │    └── project
                     │                                            │         ├── columns: found:31
                     │                                            │         ├── right-join (cross)
                     │                                            │         │    ├── columns: a:5
                     │                                            │         │    ├── limit
                     │                                            │         │    │    ├── columns: a:5!null
                     │                                            │         │    │    ├── project
                     │                                            │         │    │    │    ├── columns: a:5!null
                     │                                            │         │    │    │    └── select
                     │                                            │         │    │    │         ├── columns: a:5!null rowid:6!null crdb_internal_mvcc_timestamp:7 tableoid:8
                     │                                            │         │    │    │         ├── scan t114826
                     │                                            │         │    │    │         │    └── columns: a:5 rowid:6!null crdb_internal_mvcc_timestamp:7 tableoid:8
                     │                                            │         │    │    │         └── filters
                     │                                            │         │    │    │              └── eq
                     │                                            │         │    │    │                   ├── variable: a:5
                     │                                            │         │    │    │                   └── const: 3
                     │                                            │         │    │    └── const: 1
                     │                                            │         │    ├── values
                     │                                            │         │    │    └── tuple
                     │                                            │         │    └── filters (true)
                     │                                            │         └── projections
                     │                                            │              └── variable: a:5 [as=found:31]
                     │                                            └── projections
                     │                                                 └── udf: _stmt_exec_ret_4 [as="_stmt_exec_ret_4":32]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    └── variable: found:31
                     │                                                      ├── params: found:9
                     │                                                      └── body
                     │                                                           └── project
                     │                                                                ├── columns: "_stmt_raise_5":30
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── udf: _stmt_raise_5 [as="_stmt_raise_5":30]
                     │                                                                          ├── tail-call
                     │                                                                          ├── args
                     │                                                                          │    └── variable: found:9
                     │                                                                          ├── params: found:10
                     │                                                                          └── body
                     │                                                                               ├── project
                     │                                                                               │    ├── columns: stmt_raise_6:11
                     │                                                                               │    ├── values
                     │                                                                               │    │    └── tuple
                     │                                                                               │    └── projections
                     │                                                                               │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_6:11]
                     │                                                                               │              ├── const: 'NOTICE'
                     │                                                                               │              ├── const: 'HERE 2'
                     │                                                                               │              ├── const: ''
                     │                                                                               │              ├── const: ''
                     │                                                                               │              └── const: '00000'
                     │                                                                               └── project
                     │                                                                                    ├── columns: "_stmt_exec_7":29
                     │                                                                                    ├── values
                     │                                                                                    │    └── tuple
                     │                                                                                    └── projections
                     │                                                                                         └── udf: _stmt_exec_7 [as="_stmt_exec_7":29]
                     │                                                                                              ├── tail-call
                     │                                                                                              ├── args
                     │                                                                                              │    └── variable: found:10
                     │                                                                                              ├── params: found:12
                     │                                                                                              └── body
                     │                                                                                                   └── project
                     │                                                                                                        ├── columns: "_stmt_exec_ret_8":28
                     │                                                                                                        ├── barrier
                     │                                                                                                        │    ├── columns: found:27
                     │                                                                                                        │    └── project
                     │                                                                                                        │         ├── columns: found:27
                     │                                                                                                        │         ├── right-join (cross)
                     │                                                                                                        │         │    ├── columns: a:13
                     │                                                                                                        │         │    ├── barrier
                     │                                                                                                        │         │    │    ├── columns: a:13
                     │                                                                                                        │         │    │    └── limit
                     │                                                                                                        │         │    │         ├── columns: a:13
                     │                                                                                                        │         │    │         ├── project
                     │                                                                                                        │         │    │         │    ├── columns: a:13
                     │                                                                                                        │         │    │         │    └── insert t114826
                     │                                                                                                        │         │    │         │         ├── columns: a:13 rowid:14!null
                     │                                                                                                        │         │    │         │         ├── insert-mapping:
                     │                                                                                                        │         │    │         │         │    ├── a:17 => a:13
                     │                                                                                                        │         │    │         │         │    └── rowid_default:21 => rowid:14
                     │                                                                                                        │         │    │         │         ├── return-mapping:
                     │                                                                                                        │         │    │         │         │    ├── a:17 => a:13
                     │                                                                                                        │         │    │         │         │    └── rowid_default:21 => rowid:14
                     │                                                                                                        │         │    │         │         └── project
                     │                                                                                                        │         │    │         │              ├── columns: rowid_default:21 a:17
                     │                                                                                                        │         │    │         │              ├── project
                     │                                                                                                        │         │    │         │              │    ├── columns: a:17
                     │                                                                                                        │         │    │         │              │    └── scan t114826
                     │                                                                                                        │         │    │         │              │         └── columns: a:17 rowid:18!null crdb_internal_mvcc_timestamp:19 tableoid:20
                     │                                                                                                        │         │    │         │              └── projections
                     │                                                                                                        │         │    │         │                   └── function: unique_rowid [as=rowid_default:21]
                     │                                                                                                        │         │    │         └── const: 1
                     │                                                                                                        │         │    ├── values
                     │                                                                                                        │         │    │    └── tuple
                     │                                                                                                        │         │    └── filters (true)
                     │                                                                                                        │         └── projections
                     │                                                                                                        │              └── variable: a:13 [as=found:27]
                     │                                                                                                        └── projections
                     │                                                                                                             └── udf: _stmt_exec_ret_8 [as="_stmt_exec_ret_8":28]
                     │                                                                                                                  ├── tail-call
                     │                                                                                                                  ├── args
                     │                                                                                                                  │    └── variable: found:27
                     │                                                                                                                  ├── params: found:22
                     │                                                                                                                  └── body
                     │                                                                                                                       └── project
                     │                                                                                                                            ├── columns: "_stmt_raise_9":26
                     │                                                                                                                            ├── values
                     │                                                                                                                            │    └── tuple
                     │                                                                                                                            └── projections
                     │                                                                                                                                 └── udf: _stmt_raise_9 [as="_stmt_raise_9":26]
                     │                                                                                                                                      ├── tail-call
                     │                                                                                                                                      ├── args
                     │                                                                                                                                      │    └── variable: found:22
                     │                                                                                                                                      ├── params: found:23
                     │                                                                                                                                      └── body
                     │                                                                                                                                           ├── project
                     │                                                                                                                                           │    ├── columns: stmt_raise_10:24
                     │                                                                                                                                           │    ├── values
                     │                                                                                                                                           │    │    └── tuple
                     │                                                                                                                                           │    └── projections
                     │                                                                                                                                           │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_10:24]
                     │                                                                                                                                           │              ├── const: 'NOTICE'
                     │                                                                                                                                           │              ├── const: 'HERE 3'
                     │                                                                                                                                           │              ├── const: ''
                     │                                                                                                                                           │              ├── const: ''
                     │                                                                                                                                           │              └── const: '00000'
                     │                                                                                                                                           └── project
                     │                                                                                                                                                ├── columns: stmt_return_11:25!null
                     │                                                                                                                                                ├── values
                     │                                                                                                                                                │    └── tuple
                     │                                                                                                                                                └── projections
                     │                                                                                                                                                     └── const: 10 [as=stmt_return_11:25]
                     └── const: 1

# Regression test for #119492 - don't push the in scope for non-root blocks.
# Doing so drops the expression that had been built up to that point.
exec-ddl
CREATE FUNCTION somefunc() RETURNS integer AS $$
DECLARE
    outer_quantity integer := 30;
BEGIN
    RAISE NOTICE 'Quantity here is %', outer_quantity;  -- Prints 30
    outer_quantity := 50;
    --
    -- Create a subblock
    --
    DECLARE
        inner_quantity integer := 80;
    BEGIN
        RAISE NOTICE 'Quantity here is %', inner_quantity;  -- Prints 80
        RAISE NOTICE 'Outer quantity here is %', outer_quantity;  -- Prints 50
    END;

    RAISE NOTICE 'Quantity here is %', outer_quantity;  -- Prints 50

    RETURN outer_quantity;
END;
$$ LANGUAGE plpgsql;
----

build format=show-scalars
SELECT somefunc();
----
project
 ├── columns: somefunc:21
 ├── values
 │    └── tuple
 └── projections
      └── udf: somefunc [as=somefunc:21]
           └── body
                └── limit
                     ├── columns: "_stmt_raise_1":20
                     ├── project
                     │    ├── columns: "_stmt_raise_1":20
                     │    ├── barrier
                     │    │    ├── columns: outer_quantity:1!null
                     │    │    └── project
                     │    │         ├── columns: outer_quantity:1!null
                     │    │         ├── values
                     │    │         │    └── tuple
                     │    │         └── projections
                     │    │              └── const: 30 [as=outer_quantity:1]
                     │    └── projections
                     │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":20]
                     │              ├── args
                     │              │    └── variable: outer_quantity:1
                     │              ├── params: outer_quantity:2
                     │              └── body
                     │                   ├── project
                     │                   │    ├── columns: stmt_raise_2:3
                     │                   │    ├── values
                     │                   │    │    └── tuple
                     │                   │    └── projections
                     │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:3]
                     │                   │              ├── const: 'NOTICE'
                     │                   │              ├── concat
                     │                   │              │    ├── concat
                     │                   │              │    │    ├── const: 'Quantity here is '
                     │                   │              │    │    └── coalesce
                     │                   │              │    │         ├── cast: STRING
                     │                   │              │    │         │    └── variable: outer_quantity:2
                     │                   │              │    │         └── const: '<NULL>'
                     │                   │              │    └── const: ''
                     │                   │              ├── const: ''
                     │                   │              ├── const: ''
                     │                   │              └── const: '00000'
                     │                   └── project
                     │                        ├── columns: "_stmt_raise_7":19
                     │                        ├── barrier
                     │                        │    ├── columns: outer_quantity:4!null inner_quantity:10!null
                     │                        │    └── project
                     │                        │         ├── columns: inner_quantity:10!null outer_quantity:4!null
                     │                        │         ├── project
                     │                        │         │    ├── columns: outer_quantity:4!null
                     │                        │         │    ├── values
                     │                        │         │    │    └── tuple
                     │                        │         │    └── projections
                     │                        │         │         └── const: 50 [as=outer_quantity:4]
                     │                        │         └── projections
                     │                        │              └── const: 80 [as=inner_quantity:10]
                     │                        └── projections
                     │                             └── udf: _stmt_raise_7 [as="_stmt_raise_7":19]
                     │                                  ├── tail-call
                     │                                  ├── args
                     │                                  │    ├── variable: outer_quantity:4
                     │                                  │    └── variable: inner_quantity:10
                     │                                  ├── params: outer_quantity:11 inner_quantity:12
                     │                                  └── body
                     │                                       ├── project
                     │                                       │    ├── columns: stmt_raise_8:13
                     │                                       │    ├── values
                     │                                       │    │    └── tuple
                     │                                       │    └── projections
                     │                                       │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_8:13]
                     │                                       │              ├── const: 'NOTICE'
                     │                                       │              ├── concat
                     │                                       │              │    ├── concat
                     │                                       │              │    │    ├── const: 'Quantity here is '
                     │                                       │              │    │    └── coalesce
                     │                                       │              │    │         ├── cast: STRING
                     │                                       │              │    │         │    └── variable: inner_quantity:12
                     │                                       │              │    │         └── const: '<NULL>'
                     │                                       │              │    └── const: ''
                     │                                       │              ├── const: ''
                     │                                       │              ├── const: ''
                     │                                       │              └── const: '00000'
                     │                                       └── project
                     │                                            ├── columns: "_stmt_raise_9":18
                     │                                            ├── values
                     │                                            │    └── tuple
                     │                                            └── projections
                     │                                                 └── udf: _stmt_raise_9 [as="_stmt_raise_9":18]
                     │                                                      ├── tail-call
                     │                                                      ├── args
                     │                                                      │    ├── variable: outer_quantity:11
                     │                                                      │    └── variable: inner_quantity:12
                     │                                                      ├── params: outer_quantity:14 inner_quantity:15
                     │                                                      └── body
                     │                                                           ├── project
                     │                                                           │    ├── columns: stmt_raise_10:16
                     │                                                           │    ├── values
                     │                                                           │    │    └── tuple
                     │                                                           │    └── projections
                     │                                                           │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_10:16]
                     │                                                           │              ├── const: 'NOTICE'
                     │                                                           │              ├── concat
                     │                                                           │              │    ├── concat
                     │                                                           │              │    │    ├── const: 'Outer quantity here is '
                     │                                                           │              │    │    └── coalesce
                     │                                                           │              │    │         ├── cast: STRING
                     │                                                           │              │    │         │    └── variable: outer_quantity:14
                     │                                                           │              │    │         └── const: '<NULL>'
                     │                                                           │              │    └── const: ''
                     │                                                           │              ├── const: ''
                     │                                                           │              ├── const: ''
                     │                                                           │              └── const: '00000'
                     │                                                           └── project
                     │                                                                ├── columns: post_nested_block_3:17
                     │                                                                ├── values
                     │                                                                │    └── tuple
                     │                                                                └── projections
                     │                                                                     └── udf: post_nested_block_3 [as=post_nested_block_3:17]
                     │                                                                          ├── tail-call
                     │                                                                          ├── args
                     │                                                                          │    └── variable: outer_quantity:14
                     │                                                                          ├── params: outer_quantity:5
                     │                                                                          └── body
                     │                                                                               └── project
                     │                                                                                    ├── columns: "_stmt_raise_4":9
                     │                                                                                    ├── values
                     │                                                                                    │    └── tuple
                     │                                                                                    └── projections
                     │                                                                                         └── udf: _stmt_raise_4 [as="_stmt_raise_4":9]
                     │                                                                                              ├── tail-call
                     │                                                                                              ├── args
                     │                                                                                              │    └── variable: outer_quantity:5
                     │                                                                                              ├── params: outer_quantity:6
                     │                                                                                              └── body
                     │                                                                                                   ├── project
                     │                                                                                                   │    ├── columns: stmt_raise_5:7
                     │                                                                                                   │    ├── values
                     │                                                                                                   │    │    └── tuple
                     │                                                                                                   │    └── projections
                     │                                                                                                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_5:7]
                     │                                                                                                   │              ├── const: 'NOTICE'
                     │                                                                                                   │              ├── concat
                     │                                                                                                   │              │    ├── concat
                     │                                                                                                   │              │    │    ├── const: 'Quantity here is '
                     │                                                                                                   │              │    │    └── coalesce
                     │                                                                                                   │              │    │         ├── cast: STRING
                     │                                                                                                   │              │    │         │    └── variable: outer_quantity:6
                     │                                                                                                   │              │    │         └── const: '<NULL>'
                     │                                                                                                   │              │    └── const: ''
                     │                                                                                                   │              ├── const: ''
                     │                                                                                                   │              ├── const: ''
                     │                                                                                                   │              └── const: '00000'
                     │                                                                                                   └── project
                     │                                                                                                        ├── columns: stmt_return_6:8
                     │                                                                                                        ├── values
                     │                                                                                                        │    └── tuple
                     │                                                                                                        └── projections
                     │                                                                                                             └── variable: outer_quantity:6 [as=stmt_return_6:8]
                     └── const: 1

# Use TxnControlExpr to COMMIT or ROLLBACK the current transaction, then
# continue from the new transaction.
exec-ddl
CREATE OR REPLACE PROCEDURE p() LANGUAGE PLpgSQL AS $$
  BEGIN
    INSERT INTO txn_timestamps VALUES (now());
    COMMIT;
    INSERT INTO txn_timestamps VALUES (now());
    ROLLBACK;
    INSERT INTO txn_timestamps VALUES (now());
  END;
$$;
----

build format=show-scalars
CALL p();
----
call
 └── procedure: p
      └── body
           └── limit
                ├── columns: "_stmt_exec_1":24
                ├── project
                │    ├── columns: "_stmt_exec_1":24
                │    ├── values
                │    │    └── tuple
                │    └── projections
                │         └── udf: _stmt_exec_1 [as="_stmt_exec_1":24]
                │              └── body
                │                   ├── insert txn_timestamps
                │                   │    ├── columns: <none>
                │                   │    ├── insert-mapping:
                │                   │    │    ├── column1:5 => ts:1
                │                   │    │    └── rowid_default:6 => rowid:2
                │                   │    └── project
                │                   │         ├── columns: rowid_default:6 column1:5
                │                   │         ├── values
                │                   │         │    ├── columns: column1:5
                │                   │         │    └── tuple
                │                   │         │         └── function: now
                │                   │         └── projections
                │                   │              └── function: unique_rowid [as=rowid_default:6]
                │                   └── project
                │                        ├── columns: "_stmt_commit_2":23
                │                        ├── barrier
                │                        │    └── values
                │                        │         └── tuple
                │                        └── projections
                │                             └── COMMIT; CALL _stmt_commit_2 [as="_stmt_commit_2":23]
                │                                  └── body
                │                                       └── project
                │                                            ├── columns: "_stmt_exec_3":22
                │                                            ├── values
                │                                            │    └── tuple
                │                                            └── projections
                │                                                 └── udf: _stmt_exec_3 [as="_stmt_exec_3":22]
                │                                                      ├── tail-call
                │                                                      └── body
                │                                                           ├── insert txn_timestamps
                │                                                           │    ├── columns: <none>
                │                                                           │    ├── insert-mapping:
                │                                                           │    │    ├── column1:11 => ts:7
                │                                                           │    │    └── rowid_default:12 => rowid:8
                │                                                           │    └── project
                │                                                           │         ├── columns: rowid_default:12 column1:11
                │                                                           │         ├── values
                │                                                           │         │    ├── columns: column1:11
                │                                                           │         │    └── tuple
                │                                                           │         │         └── function: now
                │                                                           │         └── projections
                │                                                           │              └── function: unique_rowid [as=rowid_default:12]
                │                                                           └── project
                │                                                                ├── columns: "_stmt_rollback_4":21
                │                                                                ├── barrier
                │                                                                │    └── values
                │                                                                │         └── tuple
                │                                                                └── projections
                │                                                                     └── ROLLBACK; CALL _stmt_rollback_4 [as="_stmt_rollback_4":21]
                │                                                                          └── body
                │                                                                               └── project
                │                                                                                    ├── columns: "_stmt_exec_5":20
                │                                                                                    ├── values
                │                                                                                    │    └── tuple
                │                                                                                    └── projections
                │                                                                                         └── udf: _stmt_exec_5 [as="_stmt_exec_5":20]
                │                                                                                              ├── tail-call
                │                                                                                              └── body
                │                                                                                                   ├── insert txn_timestamps
                │                                                                                                   │    ├── columns: <none>
                │                                                                                                   │    ├── insert-mapping:
                │                                                                                                   │    │    ├── column1:17 => ts:13
                │                                                                                                   │    │    └── rowid_default:18 => rowid:14
                │                                                                                                   │    └── project
                │                                                                                                   │         ├── columns: rowid_default:18 column1:17
                │                                                                                                   │         ├── values
                │                                                                                                   │         │    ├── columns: column1:17
                │                                                                                                   │         │    └── tuple
                │                                                                                                   │         │         └── function: now
                │                                                                                                   │         └── projections
                │                                                                                                   │              └── function: unique_rowid [as=rowid_default:18]
                │                                                                                                   └── project
                │                                                                                                        ├── columns: "_implicit_return":19
                │                                                                                                        ├── values
                │                                                                                                        │    └── tuple
                │                                                                                                        └── projections
                │                                                                                                             └── cast: VOID [as="_implicit_return":19]
                │                                                                                                                  └── null
                └── const: 1

# Regression test for #120692 - don't reset the concrete type for each block in
# a RECORD-returning PL/pgSQL function.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS RECORD AS $$
BEGIN
  IF true THEN
    RETURN (1:::INT8, NULL, -1.0:::DECIMAL);
  ELSIF false THEN
    BEGIN
      RETURN (1:::INT8, 1.0:::DECIMAL, NULL);
    END;
  END IF;
END;
$$ LANGUAGE PLpgSQL;
----

build format=(show-scalars,show-types)
SELECT f();
----
project
 ├── columns: f:9(tuple{int, unknown, decimal})
 ├── values
 │    └── tuple [type=tuple]
 └── projections
      └── udf: f [as=f:9, type=tuple{int, unknown, decimal}]
           └── body
                └── limit
                     ├── columns: stmt_if_8:8(tuple{int, unknown, decimal})
                     ├── project
                     │    ├── columns: stmt_if_8:8(tuple{int, unknown, decimal})
                     │    ├── values
                     │    │    └── tuple [type=tuple]
                     │    └── projections
                     │         └── case [as=stmt_if_8:8, type=tuple{int, unknown, decimal}]
                     │              ├── true [type=bool]
                     │              ├── when [type=tuple{int, unknown, decimal}]
                     │              │    ├── true [type=bool]
                     │              │    └── subquery [type=tuple{int, unknown, decimal}]
                     │              │         └── project
                     │              │              ├── columns: stmt_return_5:4(tuple{int, unknown, decimal})
                     │              │              ├── values
                     │              │              │    └── tuple [type=tuple]
                     │              │              └── projections
                     │              │                   └── tuple [as=stmt_return_5:4, type=tuple{int, unknown, decimal}]
                     │              │                        ├── const: 1 [type=int]
                     │              │                        ├── null [type=unknown]
                     │              │                        └── unary-minus [type=decimal]
                     │              │                             └── const: 1.0 [type=decimal]
                     │              ├── when [type=tuple{int, unknown, decimal}]
                     │              │    ├── false [type=bool]
                     │              │    └── subquery [type=tuple{int, unknown, decimal}]
                     │              │         └── project
                     │              │              ├── columns: stmt_return_7:6(tuple{int, unknown, decimal})
                     │              │              ├── values
                     │              │              │    └── tuple [type=tuple]
                     │              │              └── projections
                     │              │                   └── cast: RECORD [as=stmt_return_7:6, type=tuple{int, unknown, decimal}]
                     │              │                        └── cast: STRING [type=string]
                     │              │                             └── tuple [type=tuple{int, decimal, unknown}]
                     │              │                                  ├── const: 1 [type=int]
                     │              │                                  ├── const: 1.0 [type=decimal]
                     │              │                                  └── null [type=unknown]
                     │              └── subquery [type=tuple{int, unknown, decimal}]
                     │                   └── project
                     │                        ├── columns: stmt_if_1:7(tuple{int, unknown, decimal})
                     │                        ├── values
                     │                        │    └── tuple [type=tuple]
                     │                        └── projections
                     │                             └── udf: stmt_if_1 [as=stmt_if_1:7, type=tuple{int, unknown, decimal}]
                     │                                  ├── tail-call
                     │                                  └── body
                     │                                       └── project
                     │                                            ├── columns: "_end_of_function_2":3(tuple{int, unknown, decimal})
                     │                                            ├── values
                     │                                            │    └── tuple [type=tuple]
                     │                                            └── projections
                     │                                                 └── udf: _end_of_function_2 [as="_end_of_function_2":3, type=tuple{int, unknown, decimal}]
                     │                                                      ├── tail-call
                     │                                                      └── body
                     │                                                           ├── project
                     │                                                           │    ├── columns: stmt_raise_3:1(int)
                     │                                                           │    ├── values
                     │                                                           │    │    └── tuple [type=tuple]
                     │                                                           │    └── projections
                     │                                                           │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_3:1, type=int]
                     │                                                           │              ├── const: 'ERROR' [type=string]
                     │                                                           │              ├── const: 'control reached end of function without RETURN' [type=string]
                     │                                                           │              ├── const: '' [type=string]
                     │                                                           │              ├── const: '' [type=string]
                     │                                                           │              └── const: '2F005' [type=string]
                     │                                                           └── project
                     │                                                                ├── columns: end_of_function_4:2(tuple{int, unknown, decimal})
                     │                                                                ├── values
                     │                                                                │    └── tuple [type=tuple]
                     │                                                                └── projections
                     │                                                                     └── null [as=end_of_function_4:2, type=tuple{int, unknown, decimal}]
                     └── const: 1 [type=int]

# Regression test for #120916 - the nested call should not have the "tail-call"
# property, because it isn't a terminal statement.
exec-ddl
CREATE OR REPLACE FUNCTION f_nested(x INT) RETURNS INT AS $$
  BEGIN
    x := x * 2;
    RETURN x;
  END
$$ LANGUAGE PLpgSQL;
----

exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS RECORD AS $$
  DECLARE
    a INT := -2;
  BEGIN
    a := f_nested(a);
    RAISE NOTICE 'here';
    RETURN (a, -a);
  END
$$ LANGUAGE PLpgSQL;
----

build format=show-scalars
SELECT * FROM f() AS g(x INT, y INT);
----
project-set
 ├── columns: x:12 y:13
 ├── values
 │    └── tuple
 └── zip
      └── udf: f
           └── body
                └── project
                     ├── columns: column10:10 column11:11
                     ├── limit
                     │    ├── columns: "_stmt_raise_1":9
                     │    ├── project
                     │    │    ├── columns: "_stmt_raise_1":9
                     │    │    ├── barrier
                     │    │    │    ├── columns: a:5
                     │    │    │    └── barrier
                     │    │    │         ├── columns: a:5
                     │    │    │         └── project
                     │    │    │              ├── columns: a:5
                     │    │    │              ├── barrier
                     │    │    │              │    ├── columns: a:1!null
                     │    │    │              │    └── project
                     │    │    │              │         ├── columns: a:1!null
                     │    │    │              │         ├── values
                     │    │    │              │         │    └── tuple
                     │    │    │              │         └── projections
                     │    │    │              │              └── const: -2 [as=a:1]
                     │    │    │              └── projections
                     │    │    │                   └── udf: f_nested [as=a:5]
                     │    │    │                        ├── args
                     │    │    │                        │    └── variable: a:1
                     │    │    │                        ├── params: x:2
                     │    │    │                        └── body
                     │    │    │                             └── limit
                     │    │    │                                  ├── columns: stmt_return_1:4
                     │    │    │                                  ├── project
                     │    │    │                                  │    ├── columns: stmt_return_1:4
                     │    │    │                                  │    ├── project
                     │    │    │                                  │    │    ├── columns: x:3
                     │    │    │                                  │    │    ├── values
                     │    │    │                                  │    │    │    └── tuple
                     │    │    │                                  │    │    └── projections
                     │    │    │                                  │    │         └── mult [as=x:3]
                     │    │    │                                  │    │              ├── variable: x:2
                     │    │    │                                  │    │              └── const: 2
                     │    │    │                                  │    └── projections
                     │    │    │                                  │         └── variable: x:3 [as=stmt_return_1:4]
                     │    │    │                                  └── const: 1
                     │    │    └── projections
                     │    │         └── udf: _stmt_raise_1 [as="_stmt_raise_1":9]
                     │    │              ├── args
                     │    │              │    └── variable: a:5
                     │    │              ├── params: a:6
                     │    │              └── body
                     │    │                   ├── project
                     │    │                   │    ├── columns: stmt_raise_2:7
                     │    │                   │    ├── values
                     │    │                   │    │    └── tuple
                     │    │                   │    └── projections
                     │    │                   │         └── function: crdb_internal.plpgsql_raise [as=stmt_raise_2:7]
                     │    │                   │              ├── const: 'NOTICE'
                     │    │                   │              ├── const: 'here'
                     │    │                   │              ├── const: ''
                     │    │                   │              ├── const: ''
                     │    │                   │              └── const: '00000'
                     │    │                   └── project
                     │    │                        ├── columns: stmt_return_3:8
                     │    │                        ├── values
                     │    │                        │    └── tuple
                     │    │                        └── projections
                     │    │                             └── tuple [as=stmt_return_3:8]
                     │    │                                  ├── variable: a:6
                     │    │                                  └── unary-minus
                     │    │                                       └── variable: a:6
                     │    └── const: 1
                     └── projections
                          ├── column-access: 0 [as=column10:10]
                          │    └── variable: "_stmt_raise_1":9
                          └── column-access: 1 [as=column11:11]
                               └── variable: "_stmt_raise_1":9

# Integer-range FOR loop.
exec-ddl
CREATE OR REPLACE FUNCTION f() RETURNS INT LANGUAGE PLpgSQL AS $$
  BEGIN
    FOR i IN 1..3 LOOP
      RAISE NOTICE 'i: %', i;
    END LOOP;
    RAISE NOTICE 'DONE';
    RETURN 0;
  END
$$;
----

# Use "norm" so it's easier to see the structure of the loop.
norm format=show-scalars
SELECT f();
----
values
 ├── columns: f:40
 └── tuple
      └── udf: f
           └── body
                └── project
                     ├── columns: stmt_loop_6:39
                     ├── barrier
                     │    ├── columns: "_loop_lower":4!null "_loop_upper":5!null "_loop_step":6!null "_loop_counter":8!null i:9!null
                     │    └── project
                     │         ├── columns: i:9!null "_loop_counter":8!null "_loop_lower":4!null "_loop_upper":5!null "_loop_step":6!null
                     │         ├── barrier
                     │         │    ├── columns: "_loop_lower":4!null "_loop_upper":5!null "_loop_step":6!null "_plpgsql_runtime_check_5":7
                     │         │    └── values
                     │         │         ├── columns: "_loop_lower":4!null "_loop_upper":5!null "_loop_step":6!null "_plpgsql_runtime_check_5":7
                     │         │         └── tuple
                     │         │              ├── const: 1
                     │         │              ├── const: 3
                     │         │              ├── const: 1
                     │         │              └── null
                     │         └── projections
                     │              ├── variable: "_loop_lower":4 [as=i:9]
                     │              └── variable: "_loop_lower":4 [as="_loop_counter":8]
                     └── projections
                          └── udf: stmt_loop_6 [as=stmt_loop_6:39]
                               ├── tail-call
                               ├── args
                               │    ├── variable: i:9
                               │    ├── variable: "_loop_lower":4
                               │    ├── variable: "_loop_upper":5
                               │    ├── variable: "_loop_step":6
                               │    └── variable: "_loop_counter":8
                               ├── params: i:10 "_loop_lower":11 "_loop_upper":12 "_loop_step":13 "_loop_counter":14
                               └── body
                                    └── values
                                         ├── columns: stmt_if_11:35
                                         └── tuple
                                              └── case
                                                   ├── true
                                                   ├── when
                                                   │    ├── le
                                                   │    │    ├── variable: "_loop_counter":14
                                                   │    │    └── variable: "_loop_upper":12
                                                   │    └── subquery
                                                   │         ├── tail-call
                                                   │         └── values
                                                   │              ├── columns: "_stmt_raise_9":33
                                                   │              └── tuple
                                                   │                   └── udf: _stmt_raise_9
                                                   │                        ├── tail-call
                                                   │                        ├── args
                                                   │                        │    ├── variable: i:10
                                                   │                        │    ├── variable: "_loop_lower":11
                                                   │                        │    ├── variable: "_loop_upper":12
                                                   │                        │    ├── variable: "_loop_step":13
                                                   │                        │    └── variable: "_loop_counter":14
                                                   │                        ├── params: i:26 "_loop_lower":27 "_loop_upper":28 "_loop_step":29 "_loop_counter":30
                                                   │                        └── body
                                                   │                             ├── values
                                                   │                             │    ├── columns: stmt_raise_10:31
                                                   │                             │    └── tuple
                                                   │                             │         └── function: crdb_internal.plpgsql_raise
                                                   │                             │              ├── const: 'NOTICE'
                                                   │                             │              ├── concat
                                                   │                             │              │    ├── concat
                                                   │                             │              │    │    ├── const: 'i: '
                                                   │                             │              │    │    └── coalesce
                                                   │                             │              │    │         ├── cast: STRING
                                                   │                             │              │    │         │    └── variable: i:26
                                                   │                             │              │    │         └── const: '<NULL>'
                                                   │                             │              │    └── const: ''
                                                   │                             │              ├── const: ''
                                                   │                             │              ├── const: ''
                                                   │                             │              └── const: '00000'
                                                   │                             └── values
                                                   │                                  ├── columns: stmt_if_8:32
                                                   │                                  └── tuple
                                                   │                                       └── subquery
                                                   │                                            ├── tail-call
                                                   │                                            └── values
                                                   │                                                 ├── columns: stmt_loop_inc_7:25
                                                   │                                                 └── tuple
                                                   │                                                      └── udf: stmt_loop_inc_7
                                                   │                                                           ├── tail-call
                                                   │                                                           ├── args
                                                   │                                                           │    ├── variable: i:26
                                                   │                                                           │    ├── variable: "_loop_lower":27
                                                   │                                                           │    ├── variable: "_loop_upper":28
                                                   │                                                           │    ├── variable: "_loop_step":29
                                                   │                                                           │    └── variable: "_loop_counter":30
                                                   │                                                           ├── params: i:15 "_loop_lower":16 "_loop_upper":17 "_loop_step":18 "_loop_counter":19
                                                   │                                                           └── body
                                                   │                                                                └── project
                                                   │                                                                     ├── columns: stmt_loop_6:38
                                                   │                                                                     ├── barrier
                                                   │                                                                     │    ├── columns: "_loop_counter":36 i:37
                                                   │                                                                     │    └── project
                                                   │                                                                     │         ├── columns: i:37 "_loop_counter":36
                                                   │                                                                     │         ├── values
                                                   │                                                                     │         │    ├── columns: "_loop_counter":36
                                                   │                                                                     │         │    └── tuple
                                                   │                                                                     │         │         └── plus
                                                   │                                                                     │         │              ├── variable: "_loop_counter":19
                                                   │                                                                     │         │              └── variable: "_loop_step":18
                                                   │                                                                     │         └── projections
                                                   │                                                                     │              └── variable: "_loop_counter":36 [as=i:37]
                                                   │                                                                     └── projections
                                                   │                                                                          └── udf: stmt_loop_6 [as=stmt_loop_6:38]
                                                   │                                                                               ├── tail-call
                                                   │                                                                               ├── args
                                                   │                                                                               │    ├── variable: i:37
                                                   │                                                                               │    ├── variable: "_loop_lower":16
                                                   │                                                                               │    ├── variable: "_loop_upper":17
                                                   │                                                                               │    ├── variable: "_loop_step":18
                                                   │                                                                               │    └── variable: "_loop_counter":36
                                                   │                                                                               └── recursive-call
                                                   └── subquery
                                                        ├── tail-call
                                                        └── values
                                                             ├── columns: loop_exit_1:34
                                                             └── tuple
                                                                  └── udf: loop_exit_1
                                                                       ├── tail-call
                                                                       └── body
                                                                            └── values
                                                                                 ├── columns: "_stmt_raise_2":3
                                                                                 └── tuple
                                                                                      └── udf: _stmt_raise_2
                                                                                           ├── tail-call
                                                                                           └── body
                                                                                                ├── values
                                                                                                │    ├── columns: stmt_raise_3:1
                                                                                                │    └── tuple
                                                                                                │         └── function: crdb_internal.plpgsql_raise
                                                                                                │              ├── const: 'NOTICE'
                                                                                                │              ├── const: 'DONE'
                                                                                                │              ├── const: ''
                                                                                                │              ├── const: ''
                                                                                                │              └── const: '00000'
                                                                                                └── values
                                                                                                     ├── columns: stmt_return_4:2!null
                                                                                                     └── tuple
                                                                                                          └── const: 0
