Postgres functions: rpc()

You can call Postgres functions as Remote Procedure Calls, logic in your database that you can execute from anywhere. Functions are useful when the logic rarely changes—like for password resets and updates.

1create or replace function hello_world() returns text as $$
2  select 'Hello world';
3$$ language sql;
1const { data, error } = await supabase.rpc('hello_world')

Parameters#

  • fnrequired
    FunctionName

    The function name to call

  • argsrequired
    object

    The arguments to pass to the function call

  • optionsrequired
    object

    Named parameters

      Properties
    • countoptional
      exact
      |
      planned
      |
      estimated

      Count algorithm to use to count rows returned by the function. Only applicable for set-returning functions.

      "exact": Exact but slow count algorithm. Performs a COUNT(*) under the hood.

      "planned": Approximated but fast count algorithm. Uses the Postgres statistics under the hood.

      "estimated": Uses exact count for low numbers and planned count for high numbers.

    • headoptional
      boolean

      When set to true, data will not be returned. Useful if you only need the count.

Examples#

Call a Postgres function without arguments#

1create function hello_world() returns text as $$
2  select 'Hello world';
3$$ language sql;

Call a Postgres function with arguments#

1create function echo(say text) returns text as $$
2  select say;
3$$ language sql;

Bulk processing#

You can process large payloads by passing in an array as an argument:

1create function add_one_each(arr int[]) returns int[] as $$
2  select array_agg(n + 1) from unnest(arr) as n;
3$$ language sql;

Call a Postgres function with filters#

Postgres functions that return tables can also be combined with Filters and Modifiers.

1create table
2  countries (id int8 primary key, name text);
3
4insert into
5  countries (id, name)
6values
7  (1, 'France'),
8  (2, 'United Kingdom');
9
10create function list_stored_countries() returns setof countries as $$
11  select * from countries;
12$$ language sql;