Swift: Call a Postgres function

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.

create or replace function hello_world() returns text as $$
  select 'Hello world';
$$ language sql;

Examples

Call a Postgres function without arguments

let value: String = try await supabase
  .rpc("hello_world")
  .execute()
  .value

Call a Postgres function with arguments

let response: String = try await supabase
  .rpc("echo", params: ["say": "đź‘‹"])
  .execute()
  .value

Bulk processing

let response: [Int] = try await supabase
  .rpc("add_one_each", params: ["arr": [1, 2, 3]])
  .execute()
  .value

Call a Postgres function with filters

struct Country: Decodable \{
  let id: Int
  let name: String
\}

let country: Country = await supabase
  .rpc("list_stored_countries")
  .eq("id", value: 1)
  .single()
  .execute()
  .value