-- An error raised while a window object's method runs the executor ends the -- query, and JavaScript cannot catch it. -- -- A window function's arguments are evaluated, and the rows of its partition -- read, by the executor of the query that called it, and either can call any -- function. The methods that do so caught what that raised and handed it to -- JavaScript, which carried on: a PL/pgSQL function that raised had left its -- SPI connection on the stack -- "transaction left non-empty SPI stack" -- -- and the window function returned a result computed without the row. -- Nothing can roll the executor back to a savepoint, so the error ends the -- query instead, as it would for any other function. CREATE FUNCTION wee_check(x int4) RETURNS int4 LANGUAGE plpgsql AS $$ BEGIN PERFORM 1; IF x = 2 AND current_setting('wee.fail', true) = 'on' THEN PERFORM set_config('wee.fail', 'off', false); RAISE EXCEPTION 'bad row: %', x; END IF; RETURN x; END $$; CREATE FUNCTION wee_sum(v int4) RETURNS int4 WINDOW LANGUAGE pljs AS $$ const w = pljs.get_window_object(); const n = w.get_partition_row_count(); let total = 0; for (let i = 0; i < n; i++) { try { total += w.get_func_arg_in_partition(0, i, w.SEEK_HEAD, false); } catch (e) { pljs.elog(NOTICE, 'caught: ' + e.message); } } return total; $$; -- 1) Evaluating an argument. SELECT set_config('wee.fail', 'on', false); SELECT i, wee_sum(wee_check(i)) OVER () FROM generate_series(1, 3) AS i; -- 2) Reading the partition's rows, which evaluates the query below it. CREATE FUNCTION wee_count(v int4) RETURNS int4 WINDOW LANGUAGE pljs AS $$ const w = pljs.get_window_object(); try { return w.get_partition_row_count(); } catch (e) { pljs.elog(NOTICE, 'caught: ' + e.message); return -1; } $$; SELECT set_config('wee.fail', 'on', false); SELECT i, wee_count(i) OVER () FROM (SELECT wee_check(i) AS i FROM generate_series(1, 3) AS i OFFSET 0) AS s; -- 3) The window function's query can still fail and be caught as a whole, -- by a caller that ran it. CREATE FUNCTION wee_outer() RETURNS text LANGUAGE pljs AS $$ try { pljs.execute('SELECT wee_sum(wee_check(i)) OVER () ' + 'FROM generate_series(1, 3) AS i'); return 'no error'; } catch (e) { return 'caught from the query: ' + e.message; } $$; SELECT set_config('wee.fail', 'on', false); SELECT wee_outer(); -- 4) An argument that is not a seek type is still a JavaScript error. CREATE FUNCTION wee_seek(v int4) RETURNS text WINDOW LANGUAGE pljs AS $$ const w = pljs.get_window_object(); try { return String(w.get_func_arg_in_frame(0, 0, 42, false)); } catch (e) { return 'caught: ' + e.message; } $$; SELECT wee_seek(i) OVER () FROM generate_series(1, 1) AS i; -- 5) Without an error, nothing has changed. SELECT set_config('wee.fail', 'off', false); SELECT i, wee_sum(wee_check(i)) OVER (), wee_count(i) OVER () FROM generate_series(1, 3) AS i; -- The session is intact: building an Error walks QuickJS's stack frames. DO $$ try { throw new Error('after'); } catch (e) { pljs.elog(NOTICE, 'caught: ' + e.message); } $$ LANGUAGE pljs; -- A seek before the mark raises before anything is evaluated, and leaves -- nothing behind: JavaScript can catch it, as it can from set_mark_position(). CREATE FUNCTION wee_before_mark(v int4) RETURNS text WINDOW LANGUAGE pljs AS $$ const w = pljs.get_window_object(); w.get_func_arg_in_partition(0, 1, w.SEEK_HEAD, true); try { w.get_func_arg_in_partition(0, 0, w.SEEK_HEAD, false); return 'not raised'; } catch (e) { return 'caught: ' + e.message; } $$; SELECT wee_before_mark(i) OVER () FROM generate_series(1, 2) AS i; -- So does a position outside of the window, which rows_are_peers() raises for -- itself. CREATE FUNCTION wee_peers_out(v int4) RETURNS text WINDOW LANGUAGE pljs AS $$ const w = pljs.get_window_object(); try { w.rows_are_peers(0, 100); return 'not raised'; } catch (e) { return 'caught: ' + e.message; } $$; SELECT wee_peers_out(i) OVER (ORDER BY i) FROM generate_series(1, 2) AS i; SELECT 1 AS still_connected; DROP FUNCTION wee_check(int4), wee_sum(int4), wee_count(int4), wee_outer(), wee_seek(int4), wee_before_mark(int4), wee_peers_out(int4);