gizmodel/sql/catalog/routine
Stored routines and triggers as the catalog keeps them: one text per object (Engine.setObject), written by the parser from CREATE FUNCTION, PROCEDURE or TRIGGER in any dialect and read back by the binder (which inlines functions, proc.giz) and the session (which runs the rest).
import * as Routine from "gizmodel/sql/catalog/routine";Types
Param
type Param = { name: string; ty: string; mode: string; default: string; }A parameter: its name ("" when unnamed, then $n), declared type, mode (in, out, inout) and default expression text.
name:stringty:stringmode:stringdefault:string
Routine
type Routine = { kind: string; name: string; params: Vec<Param>; returns: string; columns: Vec<Param>; language: string; body: string; strict: bool; attrs: string; }kind: function or procedure. returns: the declared result type ("" for none, "trigger",
"setof T", or "table" with columns). language: sql, plpgsql or plsql. body: its source. strict: a NULL argument
makes the result NULL without a run (RETURNS NULL ON NULL INPUT). attrs: the other attributes, one
"key=value" a line (Parser.routineAttr: volatile, secdef, leakproof, parallel, cost, rows, body, set:NAME).
kind:stringname:stringparams:Vec<M170.Param>returns:stringcolumns:Vec<M170.Param>language:stringbody:stringstrict:boolattrs:string
Trigger
type Trigger = { name: string; table: string; timing: string; events: Vec<string>; columns: Vec<string>; row: bool; when: string; func: string; body: string; language: string; mode: string; args: Vec<string>; oldTable: string; newTable: string; }enabled: it fires (ALTER TRIGGER ... DISABLE stops it).
name:stringtable:stringtiming:stringevents:Vec<string>columns:Vec<string>row:boolwhen:stringfunc:stringbody:stringlanguage:stringmode:string— When it fires, as pg_trigger.tgenabled: O unless session_replication_role is replica, R only then, A always, D never.args:Vec<string>oldTable:stringnewTable:string
AggregateDef
type AggregateDef = { sfunc: string; stype: string; initcond: string; hasInit: bool; finalfunc: string; args: Vec<string>; }A user-defined aggregate (CREATE AGGREGATE) as the session folds it: SFUNC and STYPE, INITCOND (hasInit),
FINALFUNC ("" for none) and the argument types (none for name(*)).
sfunc:stringstype:stringinitcond:stringhasInit:boolfinalfunc:stringargs:Vec<string>
Functions
routineText
function routineText(r: Routine): stringRoutine R as the catalog stores it.
Effects: fail, alloc.
routineOf
function routineOf(text: string): RoutineThe first routine stored under a name (routinesOf lists every overload).
Effects: fail, alloc.
triggerText
function triggerText(t: Trigger): stringThe triggers of one table are one object (the session reads them once per statement).
Effects: fail, alloc.
modeFires
function modeFires(mode: string, replica: bool): boolWhether a trigger of MODE fires in a session whose session_replication_role is REPLICA or not.
triggerOf
function triggerOf(text: string): TriggerThe trigger that the stored TEXT holds.
Effects: fail, alloc.
triggersOf
function triggersOf(text: string): Vec<Trigger>The triggers that the stored TEXT holds.
Effects: fail, alloc.
triggersText
function triggersText(ts: Vec<Trigger>): stringTriggers TS as the catalog stores them.
Effects: fail, alloc.
cloneModeText
function cloneModeText(text: string, name: string, mode: string): stringThe "trigger mode" text of a partition with clone NAME set to MODE, or back to its parent's for "".
Effects: fail, alloc.
inOnly
function inOnly(p: Param): boolWhether p only takes a value in (IN, or VARIADIC: the rest of the arguments as an array).
callParams
function callParams(r: Routine): Vec<Param>The parameters a call passes, in order: a PostgreSQL function's IN, INOUT and VARIADIC ones; all of a procedure's and of a PL/SQL or T-SQL function's (its caller passes a variable for each OUT one).
Effects: fail, alloc.
routinesOf
function routinesOf(text: string): Vec<Routine>The routines that the stored TEXT holds.
Effects: fail, alloc.
routinesText
function routinesText(rs: Vec<Routine>): stringRoutines RS as the catalog stores them.
Effects: fail, alloc.
takesCall
function takesCall(text: string, n: i64): boolWhether one overload of text takes a call with n arguments.
Effects: fail, alloc.
overload
function overload(text: string, n: i64): RoutineThe overload a call with n arguments takes: one with exactly n input parameters, else one whose defaults or
VARIADIC parameter fill the gap, else the first (its errors name the mismatch). n < 0 takes the first.
Effects: fail, alloc.
overloadFor
function overloadFor(text: string, args: Vec<Expr>): RoutineThe overload a call with ARGS takes: as overload, but an overload whose array parameters are where the array
arguments are comes first (embed(text, text) and embed(text, text[])).
Effects: fail, alloc.
overloadAmbiguity
function overloadAmbiguity(text: string, args: Vec<Expr>): string"function f(types) is not unique" when the overload overloadFor takes for args and another one that takes as many
arguments by its defaults have the same types there (PostgreSQL's 42725); "" when it is unique.
Effects: fail, alloc.
shownTypes
function shownTypes(ts: Vec<string>): stringArgument types as PostgreSQL prints them in "function f(...) does not exist".
Effects: fail, alloc.
shownWithMod
function shownWithMod(t: string): stringType T as format_type shows it, with its modifier: numeric(10,4).
Effects: fail, alloc.
signature
function signature(ps: Vec<Param>): stringThe input parameter types of ps, as DROP FUNCTION f(types) and overloads compare them.
Effects: fail, alloc.
routineKey
function routineKey(r: Routine): stringThe name and signature that key routine R's call counts (pg_stat_user_functions).
Effects: fail, alloc.
typesKey
function typesKey(ts: Vec<string>): stringTypes ts as signature writes them: each by its key, comma-separated.
Effects: fail, alloc.
typeKey
function typeKey(ty: string): stringThe key of the type TY in a signature: lower case, without pg_catalog.
Effects: fail, alloc.
aggregateEntries
function aggregateEntries(text: string): Vec<string>The catalog text of an aggregate's overloads: each is "signature\u0003options", "\u0004" between them.
Effects: fail, alloc.
aggregateDef
function aggregateDef(text: string, arity: i64): AggregateDefThe overload of text (aggregateEntries) that takes arity arguments, else the first.
Effects: fail, alloc.
attrOf
function attrOf(attrs: string, key: string): stringThe value of attribute key of a routine's attrs, "" when it has none.
Effects: fail, alloc.
withAttr
function withAttr(attrs: string, line: string): stringAttrs with line applied: it replaces the line of its key. "reset:NAME" drops the setting NAME, "reset:" all.
Effects: fail, alloc.