-- pljs.prepare() uses the parameter types it is given. -- -- It had plv8's condition the wrong way round: given type names, it parsed -- them, discarded them and had the parser infer the types instead; given -- none, it prepared with no parameters at all. So a declared type changed -- nothing -- `SELECT $1 AS x` with ['int4'] bound $1 as text -- a parameter -- declared as a domain was never checked against it, and a plan without type -- names could not use a parameter. -- 1) The declared type is the parameter's type. DO $$ const plan = pljs.prepare('SELECT pg_typeof($1)::text AS t, $1 AS x', ['int4']); const row = plan.execute(['7'])[0]; pljs.elog(NOTICE, row.t + ' ' + row.x + ' ' + typeof row.x); plan.free(); $$ LANGUAGE pljs; NOTICE: integer 7 number -- The same through the trailing-arguments form. DO $$ const plan = pljs.prepare('SELECT pg_typeof($1)::text AS t, pg_typeof($2)::text AS u', 'int8', 'numeric'); pljs.elog(NOTICE, JSON.stringify(plan.execute([1, 2])[0])); plan.free(); $$ LANGUAGE pljs; NOTICE: {"t":"bigint","u":"numeric"} -- Arguments passed after the SQL rather than in an array are the caller's, -- borrowed from QuickJS. pljs_values_to_array() put them into an array -- without taking a reference, so freeing the array released the caller's -- string constants, and the backend crashed when the block's bytecode was -- freed -- on main as well, for every function that takes trailing arguments. DO $$ pljs.elog(NOTICE, pljs.execute('SELECT $1 || $2 AS s', 'a', 'b')[0].s); const plan = pljs.prepare('SELECT $1 || $2 AS s', 'text', 'text'); pljs.elog(NOTICE, plan.execute('c', 'd')[0].s); const cursor = plan.cursor('e', 'f'); pljs.elog(NOTICE, cursor.fetch().s); cursor.close(); plan.free(); $$ LANGUAGE pljs; NOTICE: ab NOTICE: cd NOTICE: ef -- 2) A value is converted to a declared domain, and checked against it, by -- both execute() and cursor(). CREATE DOMAIN pdt_pos AS int4 CHECK (VALUE > 0); DO $$ const plan = pljs.prepare('SELECT $1 AS x', ['pdt_pos']); for (const v of [5, 0]) { try { pljs.elog(NOTICE, 'execute ' + plan.execute([v])[0].x); } catch (e) { pljs.elog(NOTICE, 'execute: ' + e.message); } try { const cursor = plan.cursor([v]); pljs.elog(NOTICE, 'cursor ' + cursor.fetch().x); cursor.close(); } catch (e) { pljs.elog(NOTICE, 'cursor: ' + e.message); } } plan.free(); $$ LANGUAGE pljs; NOTICE: execute 5 NOTICE: cursor 5 NOTICE: execute: value for domain pdt_pos violates check constraint "pdt_pos_check" NOTICE: cursor: value for domain pdt_pos violates check constraint "pdt_pos_check" -- 3) An unknown type name is a JavaScript error, and says which. DO $$ try { pljs.prepare('SELECT $1 AS x', ['pdt_no_such_type']); } catch (e) { pljs.elog(NOTICE, e.message); } $$ LANGUAGE pljs; NOTICE: type "pdt_no_such_type" does not exist -- 4) Without type names, the parameters' types are inferred. DO $$ const plan = pljs.prepare('SELECT $1::int4 + 1 AS x'); pljs.elog(NOTICE, 'inferred ' + plan.execute([41])[0].x); plan.free(); $$ LANGUAGE pljs; NOTICE: inferred 42 -- An empty list of type names, or an undefined or null one, names none, and -- the types are inferred too. An empty list prepared the plan with no -- parameters, which failed with "there is no parameter $1"; an undefined one -- was a type named "undefined". DO $$ for (const types of [[], undefined, null]) { const plan = pljs.prepare('SELECT $1::int4 + 1 AS x', types); pljs.elog(NOTICE, JSON.stringify(types) + ' inferred ' + plan.execute([41])[0].x); plan.free(); } $$ LANGUAGE pljs; NOTICE: [] inferred 42 NOTICE: undefined inferred 42 NOTICE: null inferred 42 -- 5) Type names declare every parameter, as in plv8. DO $$ try { pljs.prepare('SELECT $1::int4 + $2::int4 AS x', ['int4']); } catch (e) { pljs.elog(NOTICE, e.message); } $$ LANGUAGE pljs; NOTICE: there is no parameter $2 -- 6) A declared length or precision applies too, as it does to a column. It -- was parsed and thrown away, so numeric(5,2) kept 3.14159 and varchar(3) -- kept 'abcdef'. DO $$ const plan = pljs.prepare('SELECT $1 AS n, $2 AS v, $3 AS c', ['numeric(5,2)', 'varchar(3)', 'char(4)']); pljs.elog(NOTICE, JSON.stringify(plan.execute([3.14159, 'abc', 'ab'])[0])); try { plan.execute([1, 'abcdef', 'ab']); } catch (e) { pljs.elog(NOTICE, 'execute: ' + e.message); } try { plan.cursor([1234.5, 'a', 'ab']); } catch (e) { pljs.elog(NOTICE, 'cursor: ' + e.message); } plan.free(); $$ LANGUAGE pljs; NOTICE: {"n":3.14,"v":"abc","c":"ab "} NOTICE: execute: value too long for type character varying(3) NOTICE: cursor: numeric field overflow DROP DOMAIN pdt_pos;