Database

SQL Functions

Create, edit, and replace SQL or PL/pgSQL functions in the public schema from the Console. Functions power triggers, encapsulate queries, and can be granted to the Data API role. These are PostgreSQL functions — for TypeScript/JavaScript code in the managed runtime, see the CubiBase Functions docs.

Creating a function

The editor takes a name, an argument list, a return type, and a body. Bodies can contain multiple statements — unlike the SQL editor, a function body is a program, not a single query.

OptionValuesNotes
Languagesql, plpgsqlSQL for single queries; PL/pgSQL for logic and loops
VolatilityVOLATILE, STABLE, IMMUTABLETell the planner how the function behaves
SecurityINVOKER, DEFINERDEFINER runs with the function owner’s rights
Stricton / offSTRICT returns NULL immediately when any argument is NULL
Returns seton / offSETOF for functions that return multiple rows
Replaceon / offCREATE OR REPLACE keeps existing grants
Example · PL/pgSQL
CREATE FUNCTION public.customer_count(plan_name text)
RETURNS bigint
LANGUAGE plpgsql
STABLE
SECURITY INVOKER
SET search_path = public, pg_catalog
AS $$
BEGIN
  RETURN (SELECT count(*) FROM public.customers WHERE plan = plan_name);
END;
$$;

Every function is created with SET search_path = public, pg_catalog, so name resolution cannot be hijacked by objects in other schemas — important for SECURITY DEFINER functions.

Overloads and dropping

  • PostgreSQL identifies a function by name plus argument types. Two functions named search with different argument types can coexist.
  • When you drop (or toggle API access on) an overloaded name, the Console asks which signature you mean — pass the argument types, e.g. id uuid vs term text.
  • Replacing a function keeps its grants; dropping it removes them.

Editing a function

Every function you created has an Edit button. It opens the same form prefilled with the current definition and saves with CREATE OR REPLACE, so existing grants and dependent triggers or views keep working.

  • Editable: the body, argument defaults, volatility, security (INVOKER/DEFINER), STRICT, and the Data API grant.
  • Fixed: the name, argument names/types/modes, return type, and language — PostgreSQL identifies a function by that signature and cannot change it in place. To change the signature, create a new function and drop the old one.
  • Replacing keeps EXECUTE grants; dropping removes them.

Extension functions are read-only

Enabling an extension (Database → Extensions, e.g. pgcrypto or pg_trgm) installs its functions into the public schema. Those functions belong to the extension, not to you:

  • Hidden by default: the Functions screen lists only your own functions. Tick “Show extension functions” to see the rest, marked with an extension: pgcrypto badge.
  • Read-only: they cannot be edited, replaced, or dropped — the API refuses with 403 extension_function. PostgreSQL itself also refuses to drop a function an extension depends on.
  • They leave only with their extension: disable it on the Extensions screen and its functions disappear together.
  • You can still grant or revoke Data API EXECUTE on them — that is how you expose e.g. gen_random_uuid() over RPC without writing a wrapper.

Data API execute permission

Functions are private by default: the Console’s sql role can run them, the api role cannot. The Data API execute switch maps to a real grant:

SQL · what the switch runs
GRANT EXECUTE ON FUNCTION public.customer_count(text) TO example_project_db_api;
-- off:
REVOKE EXECUTE ON FUNCTION public.customer_count(text) FROM example_project_db_api;

With the grant on, the function is callable from your app at POST /rest/v1/rpc/{name} on the project host, with the publishable key and the end-user JWT. The body is a JSON object of named arguments; the call runs with the caller’s identity, so RLS applies to SECURITY INVOKER functions.

RPC · request
POST https://example-project.api.cubibase.com/rest/v1/rpc/customer_count
apikey: cb_pub_EXAMPLE_ONLY
Authorization: Bearer <USER_JWT>
Content-Type: application/json

{ "plan_name": "pro" }

SECURITY DEFINER, carefully

  • A DEFINER function runs with its owner’s privileges, so it can bypass the caller’s RLS policies. That is the point — and the risk.
  • Grant EXECUTE to the api role only when the function is meant to expose that capability.
  • Validate arguments inside the function; a DEFINER function is a door through your policies.

Additional resources