LOCAL Versus CASCADED View CHECK OPTION

Contrast WITH LOCAL CHECK OPTION against CASCADED on stacked views to see when writes enforce parent view filters versus only the top view.

SqlSecurity Boundaries

Code

snippet
CREATE TEMP TABLE work_items (
    id       integer PRIMARY KEY,
    status   text NOT NULL,
    priority integer NOT NULL
);

-- Base view has NO CHECK OPTION of its own
CREATE TEMP VIEW v_open_items AS
SELECT id, status, priority
FROM work_items
WHERE status <> 'closed';

-- LOCAL checks only v_local_high (priority >= 2), skipping v_open_items
CREATE TEMP VIEW v_local_high AS
SELECT id, status, priority
FROM v_open_items
WHERE priority >= 2
WITH LOCAL CHECK OPTION;

-- CASCADED checks v_cascaded_high AND every underlying view's WHERE clause
CREATE TEMP VIEW v_cascaded_high AS
SELECT id, status, priority
FROM v_open_items
WHERE priority >= 2
WITH CASCADED CHECK OPTION;

-- Succeeds through v_local_high even though status = 'closed' hides it from v_open_items
INSERT INTO v_local_high VALUES (1, 'closed', 3);

DO $$
BEGIN
    INSERT INTO v_cascaded_high VALUES (2, 'closed', 3);
EXCEPTION
    WHEN with_check_option_violation THEN
        RAISE NOTICE 'cascaded rejected: %', SQLERRM;
END $$;

DO $$
BEGIN
    INSERT INTO v_local_high VALUES (3, 'open', 1);
EXCEPTION
    WHEN with_check_option_violation THEN
        RAISE NOTICE 'local rejected low priority: %', SQLERRM;
END $$;

SELECT id, status, priority FROM work_items ORDER BY id;
SELECT id, status, priority FROM v_open_items ORDER BY id;

Implementation trace — LOCAL Versus CASCADED View CHECK OPTION

SELECT / CREATE statements share the script, touching work_items and v_open_items.

work_items initializes session-only temporary fixtures inside this file so zero persistent tables need prepping beforehand and everything cleans up on disconnect.

Mechanically, the statements are built from: a WHERE clause filtering the rows one statement applies to; an ORDER BY that fixes the output order; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity.

Putting it to use

The file sets up its own scratch surface: it builds the session-local tables it works with (work_items), so one full run against a throwaway database is enough.

Core lines

CREATE TEMP VIEW v_cascaded_high AS
SELECT id, status, priority
FROM v_open_items
WHERE priority >= 2
WITH CASCADED CHECK OPTION;
CREATE TEMP VIEW v_local_high AS
SELECT id, status, priority
FROM v_open_items
WHERE priority >= 2
WITH LOCAL CHECK OPTION;
CREATE TEMP VIEW v_open_items AS
SELECT id, status, priority
FROM work_items
WHERE status <> 'closed';

Interactive Tools for This Topic

FAQ

What objects does the SQL involve?
`work_items`, `v_open_items`.