For AI agents: the documentation index is at llms.txt. Guide pages have a markdown version at the same path with .md appended, before any query string or fragment.

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.

rpc() throws a PostgrestError of kind .invalidRequest if params fails to encode. It sends no request in that case.

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 Instrument: Decodable {
  let id: Int
  let name: String
}

let instrument: Instrument = try await supabase
  .rpc("list_stored_instruments")
  .eq("id", value: 1)
  .single()
  .execute()
  .value