# Correlated subqueries.

# For testing, create the schema for the builtin tables.
exec-ddl
CREATE TABLE pg_stat_activity (
    datid OID NULL,
    datname NAME NULL,
    pid INTEGER NULL,
    usesysid OID NULL,
    username NAME NULL,
    application_name STRING NULL,
    client_addr INET NULL,
    client_hostname STRING NULL,
    client_port INTEGER NULL,
    backend_start TIMESTAMP WITH TIME ZONE NULL,
    xact_start TIMESTAMP WITH TIME ZONE NULL,
    query_start TIMESTAMP WITH TIME ZONE NULL,
    state_change TIMESTAMP WITH TIME ZONE NULL,
    wait_event_type STRING NULL,
    wait_event STRING NULL,
    state STRING NULL,
    backend_xid INTEGER NULL,
    backend_xmin INTEGER NULL,
    query STRING NULL
)
----
TABLE pg_stat_activity
 ├── datid oid
 ├── datname name
 ├── pid int
 ├── usesysid oid
 ├── username name
 ├── application_name string
 ├── client_addr inet
 ├── client_hostname string
 ├── client_port int
 ├── backend_start timestamptz
 ├── xact_start timestamptz
 ├── query_start timestamptz
 ├── state_change timestamptz
 ├── wait_event_type string
 ├── wait_event string
 ├── state string
 ├── backend_xid int
 ├── backend_xmin int
 ├── query string
 ├── rowid int not null (hidden)
 └── INDEX primary
      └── rowid int not null (hidden)

exec-ddl
CREATE TABLE pg_roles (
    oid OID NULL,
    rolname NAME NULL,
    rolsuper BOOL NULL,
    rolinherit BOOL NULL,
    rolcreaterole BOOL NULL,
    rolcreatedb BOOL NULL,
    rolcatupdate BOOL NULL,
    rolcanlogin BOOL NULL,
    rolreplication BOOL NULL,
    rolconnlimit INT NULL,
    rolpassword STRING NULL,
    rolvaliduntil TIMESTAMP WITH TIME ZONE NULL,
    rolbypassrls BOOL NULL,
    rolconfig STRING[] NULL
)
----
TABLE pg_roles
 ├── oid oid
 ├── rolname name
 ├── rolsuper bool
 ├── rolinherit bool
 ├── rolcreaterole bool
 ├── rolcreatedb bool
 ├── rolcatupdate bool
 ├── rolcanlogin bool
 ├── rolreplication bool
 ├── rolconnlimit int
 ├── rolpassword string
 ├── rolvaliduntil timestamptz
 ├── rolbypassrls bool
 ├── rolconfig string[]
 ├── rowid int not null (hidden)
 └── INDEX primary
      └── rowid int not null (hidden)

opt
SELECT
    pid AS "PID",
    username AS "User",
    datname AS "Database",
    backend_start AS "Backend start",
    CASE
    WHEN client_hostname IS NOT NULL
    AND client_hostname != ''
    THEN client_hostname::STRING
    || ':'
    || client_port::STRING
    WHEN client_addr IS NOT NULL
    AND client_addr::STRING != ''
    THEN client_addr::STRING || ':' || client_port::STRING
    WHEN client_port = -1 THEN 'local pipe'
    ELSE 'localhost:' || client_port::STRING
    END
        AS "Client",
    application_name AS "Application",
    query AS "Query",
    query_start AS "Query start",
    xact_start AS "Xact start"
FROM
    pg_stat_activity AS sa
WHERE
    (
        SELECT
            r.rolsuper OR r.oid = sa.usesysid
        FROM
            pg_roles AS r
        WHERE
            r.rolname = current_user()
    )
----
project
 ├── columns: PID:3(int) User:5(name) Database:2(name) "Backend start":10(timestamptz) Client:37(string) Application:6(string) Query:19(string) "Query start":12(timestamptz) "Xact start":11(timestamptz)
 ├── inner-join-apply
 │    ├── columns: datname:2(name) pid:3(int) usesysid:4(oid) username:5(name) application_name:6(string) client_addr:7(inet) client_hostname:8(string) client_port:9(int) backend_start:10(timestamptz) xact_start:11(timestamptz) query_start:12(timestamptz) query:19(string) "?column?":36(bool!null)
 │    ├── scan pg_stat_activity
 │    │    └── columns: datname:2(name) pid:3(int) usesysid:4(oid) username:5(name) application_name:6(string) client_addr:7(inet) client_hostname:8(string) client_port:9(int) backend_start:10(timestamptz) xact_start:11(timestamptz) query_start:12(timestamptz) query:19(string)
 │    ├── select
 │    │    ├── columns: "?column?":36(bool!null)
 │    │    ├── outer: (4)
 │    │    ├── cardinality: [0 - 1]
 │    │    ├── key: ()
 │    │    ├── fd: ()-->(36)
 │    │    ├── max1-row
 │    │    │    ├── columns: "?column?":36(bool)
 │    │    │    ├── outer: (4)
 │    │    │    ├── cardinality: [0 - 1]
 │    │    │    ├── key: ()
 │    │    │    ├── fd: ()-->(36)
 │    │    │    └── project
 │    │    │         ├── columns: "?column?":36(bool)
 │    │    │         ├── outer: (4)
 │    │    │         ├── select
 │    │    │         │    ├── columns: oid:21(oid) rolname:22(name!null) rolsuper:23(bool)
 │    │    │         │    ├── scan pg_roles
 │    │    │         │    │    └── columns: oid:21(oid) rolname:22(name) rolsuper:23(bool)
 │    │    │         │    └── filters [type=bool, outer=(22), constraints=(/22: (/NULL - ])]
 │    │    │         │         └── rolname = current_user() [type=bool, outer=(22), constraints=(/22: (/NULL - ])]
 │    │    │         └── projections [outer=(4,21,23)]
 │    │    │              └── rolsuper OR (oid = usesysid) [type=bool, outer=(4,21,23)]
 │    │    └── filters [type=bool, outer=(36), constraints=(/36: [/true - /true]; tight), fd=()-->(36)]
 │    │         └── variable: ?column? [type=bool, outer=(36), constraints=(/36: [/true - /true]; tight)]
 │    └── true [type=bool]
 └── projections [outer=(2,3,5-12,19)]
      └── CASE WHEN (client_hostname IS NOT NULL) AND (client_hostname != '') THEN (client_hostname || ':') || client_port::STRING WHEN (client_addr IS NOT NULL) AND (client_addr::STRING != '') THEN (client_addr::STRING || ':') || client_port::STRING WHEN client_port = -1 THEN 'local pipe' ELSE 'localhost:' || client_port::STRING END [type=string, outer=(7-9)]
