Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

cleans up the functions docs

Copplee9c6d2244b94
diff --git a/web/docs/guides/database/connecting-to-postgres.mdx b/web/docs/guides/database/connecting-to-postgres.mdx
index 23dc620c7d5..19b667ea185 100644
--- a/web/docs/guides/database/connecting-to-postgres.mdx
+++ b/web/docs/guides/database/connecting-to-postgres.mdx
@@ -27,13 +27,20 @@ You can use these simple questions to determine which connection method to use:
- Are you connecting to your database and then _disconnecting_ immediately (e.g. a serverless environment)? If yes, use a connection pool.
-## HTTP API
+## API
-Supabase provides an auto-updating API. This is the easiest way to get started if you are managing data (fetching, inserting, updating).
+Supabase provides an auto-updating [API](http://localhost:3005/docs/guides/api). This is the easiest way to get started if you are managing data (fetching, inserting, updating).
-You cannot manage the database schema
-via the API (for security reasons). To do that you can use the dashboard or connect directly to your database.
+### Interfaces
+
+We provides several types of API to suit your preferences and use-case:
+
+- [REST](/docs/guides/api#rest-api): interact with your database through a REST interface.
+- [GraphQL](/docs/guides/api#graphql-api): interact with your database through a GraphQL interface.
+- [Realtime](/docs/guides/api#realtime-api): listen to database changes over websockets.
+
+You cannot manage the database schema via the API (for security reasons). To do that you can use the dashboard or connect directly to your database.
### Finding your API URL
diff --git a/web/docs/guides/database/functions.mdx b/web/docs/guides/database/functions.mdx
index c3c61c0838b..eccee60bcba 100644
--- a/web/docs/guides/database/functions.mdx
+++ b/web/docs/guides/database/functions.mdx
@@ -11,12 +11,6 @@ import TabItem from '@theme/TabItem';
Postgres has built-in support for [SQL functions](https://www.postgresql.org/docs/current/sql-createfunction.html).
These functions live inside your database, and they can be [used with the API](/docs/reference/javascript/rpc).
-## Database vs Edge Functions
-
-For data-intensive operations we recommend using Database Functions, which are executed within your database, and can be called remotely using the REST and GraphQL API.
-
-If you are looking for Typescript Functions which are globally deployed, check out [Edge Functions](/docs/guides/functions).
-
## Quick demo
<iframe
@@ -29,7 +23,7 @@ If you are looking for Typescript Functions which are globally deployed, check o
allowFullScreen
></iframe>
-## Creating Functions
+## Getting started
Supabase provides several options for creating database functions. You can use the Dashboard or create them directly using SQL.
We provide a SQL editor within the Dashboard, or you can [connect](/docs/guides/database/connecting/connecting-to-postgres) to your database
@@ -41,75 +35,10 @@ and run the SQL queries yourself.
3. Enter the SQL to create or replace your Database function.
4. Click "Run" or cmd+enter (ctrl+enter).
-## Examples
-
-### Data set
-
-For this guide we'll use the following example data:
-
-
-<Tabs
-defaultValue="Data"
-values={[
- {label: 'Data', value: 'Data'},
- {label: 'SQL', value: 'SQL'},
-]}>
-
-<TabItem value="SQL">
-
-```sql
-create table planets (
- id serial primary key,
- name text
-);
-
-insert into planets (id, name)
-values
- (1, 'Tattoine'),
- (2, 'Alderaan'),
- (3, 'Kashyyyk');
-
-create table people (
- id serial primary key,
- name text,
- planet_id bigint references planets
-);
-
-insert into people (id, name, planet_id)
-values
- (1, 'Anakin Skywalker', 1),
- (2, 'Luke Skywalker', 1),
- (3, 'Princess Leia', 2),
- (4, 'Chewbacca', 3);
-```
-
-</TabItem>
-<TabItem value="Data">
-
-<h4>Planets</h4>
-
-| id | name |
-| ---- | ---- |
-| 1 | Tattoine |
-| 2 | Alderaan |
-| 3 | Kashyyyk |
-<h4>People</h4>
+## Simple Functions
-| id | name | planet_id |
-| ---- | ---- | ---- |
-| 1 | Anakin Skywalker | 1 |
-| 2 | Luke Skywalker | 1 |
-| 3 | Princess Leia | 2 |
-| 4 | Chewbacca | 3 |
-
-</TabItem>
-
-</Tabs>
-
-### Basic function
-
-A basic example which returns a string "hello world".
+Let's create a basic Database Function which returns a string "hello world".
```sql
create or replace function hello_world() -- 1
@@ -135,7 +64,9 @@ At it's most basic a function has the following parts:
</details>
-#### Execute
+<br />
+
+After the Function is created, we have several ways of "executing" the function - either directly inside the database using SQL, or with one of the client libraries.
<Tabs
defaultValue="SQL"
@@ -177,9 +108,73 @@ Reference: [rpc()](/docs/reference/dart/rpc)
</Tabs>
-### Return table sets
+## Returning data sets
+
+Database Functions can also return data sets from [Tables](/docs/guides/database/tables) or Views.
+
+For example, if we had a database with some Star Wars data inside:
+
+<Tabs
+defaultValue="Data"
+values={[
+ {label: 'Data', value: 'Data'},
+ {label: 'SQL', value: 'SQL'},
+]}>
+
+<TabItem value="SQL">
+
+```sql
+create table planets (
+ id serial primary key,
+ name text
+);
+
+insert into planets (id, name)
+values
+ (1, 'Tattoine'),
+ (2, 'Alderaan'),
+ (3, 'Kashyyyk');
+
+create table people (
+ id serial primary key,
+ name text,
+ planet_id bigint references planets
+);
+
+insert into people (id, name, planet_id)
+values
+ (1, 'Anakin Skywalker', 1),
+ (2, 'Luke Skywalker', 1),
+ (3, 'Princess Leia', 2),
+ (4, 'Chewbacca', 3);
+```
+
+</TabItem>
+<TabItem value="Data">
+
+<h4>Planets</h4>
+
+| id | name |
+| ---- | ---- |
+| 1 | Tattoine |
+| 2 | Alderaan |
+| 3 | Kashyyyk |
+
+<h4>People</h4>
+
+| id | name | planet_id |
+| ---- | ---- | ---- |
+| 1 | Anakin Skywalker | 1 |
+| 2 | Luke Skywalker | 1 |
+| 3 | Princess Leia | 2 |
+| 4 | Chewbacca | 3 |
+
+</TabItem>
+
+</Tabs>
-A basic example which return all the `planets` in our example data set.
+
+We could create a function which returns all the planets:
```sql
create or replace function get_planets()
@@ -190,8 +185,6 @@ as $$
$$;
```
-#### Execute
-
Because this function returns a table set, we can also apply filters and selectors. For example, if we only wanted the first planet:
<Tabs
@@ -219,8 +212,6 @@ const { data, error } = supabase
.eq('id', 1)
```
-Reference: [rpc()](/docs/reference/javascript/rpc)
-
</TabItem>
<TabItem value="Dart">
@@ -231,16 +222,14 @@ final res = await supabase
.execute();
```
-Reference: [rpc()](/docs/reference/dart/rpc)
-
</TabItem>
</Tabs>
-### With parameters
+## Passing parameters
-A basic example which will insert a new planet into the `planets` and return the new ID. Note that this time we're using the `plpgsql` language.
+Let's create a Function to insert a new planet into the `planets` table and return the new ID. Note that this time we're using the `plpgsql` language.
```sql
create or replace function add_planet(name text)
@@ -259,9 +248,7 @@ end;
$$;
```
-#### Execute
-
-Because this function returns a table set, we can also apply filters and selectors. For example, if we only wanted the `name` of the first planet:
+Once again, you can execute this function either inside your database using a `select` query, or with the client libraries:
<Tabs
defaultValue="SQL"
@@ -285,7 +272,6 @@ const { data, error } = await supabase
.rpc('add_planet', { name: 'Jakku' })
```
-Reference: [rpc()](/docs/reference/javascript/rpc)
</TabItem>
<TabItem value="Dart">
@@ -296,14 +282,81 @@ final res = await supabase
.execute();
```
-Reference: [rpc()](/docs/reference/dart/rpc)
-
</TabItem>
</Tabs>
+## Suggestions
+
+### Database Functions vs Edge Functions
+
+For data-intensive operations we recommend using [Database Functions](/docs/guides/database/functions), which are executed within your database
+and can be called remotely using the [REST and GraphQL API](/docs/guides/database/api).
+
+For use-cases which require low-latency we recommend [Edge Functions](/docs/guides/database/functions), which are globally-distributed and can be written in Typescript.
+
+### Security `definer` vs `invoker`
+
+Postgres allows you to specify whether you want the function to be executed as the user _calling_ the function (`invoker`), or as the _creator_ of the function (`definer`). For example:
+
+```sql
+create function hello_world()
+returns text
+language plpgsql
+security definer set search_path = public
+as $$
+begin
+ select 'hello world';
+end;
+$$;
+```
+
+It is best practice to use `security invoker` (which is also the default). If you ever use `security invoker`, you _must_ set the `search_path`.
+This limits the potential damage if you allow access to schemas which the user executing the function should not have.
+
## Resources
+
+- Official Client libraries: [JavaScript](/docs/reference/javascript/rpc) and [Dart](/docs/reference/dart/rpc)
+- Community client libraries: [github.com/supabase-community](https://github.com/supabase-community)
- PostgreSQL Official Docs: [Chapter 9. Functions and Operators](https://www.postgresql.org/docs/current/functions.html)
- PostgreSQL Reference: [CREATE FUNCTION](https://www.postgresql.org/docs/9.1/sql-createfunction.html)
+
+## Deep Dive
+
+### Create Database Functions
+
+<iframe
+ className="w-full video-with-border"
+ width="640"
+ height="385"
+ src="https://www.youtube-nocookie.com/embed/MJZCCpCYEqk"
+ frameBorder="1"
+ allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
+ allowFullScreen
+></iframe>
+
+### Call Database Functions using JavaScript
+
+<iframe
+ className="w-full video-with-border"
+ width="640"
+ height="385"
+ src="https://www.youtube-nocookie.com/embed/I6nnp9AINJk"
+ frameBorder="1"
+ allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
+ allowFullScreen
+></iframe>
+
+### Using Database Functions to call an external API
+
+<iframe
+ className="w-full video-with-border"
+ width="640"
+ height="385"
+ src="https://www.youtube-nocookie.com/embed/I6nnp9AINJk"
+ frameBorder="1"
+ allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
+ allowFullScreen
+></iframe>
diff --git a/web/docs/guides/functions.mdx b/web/docs/guides/functions.mdx
index ce07d17a72d..e7a75bd2875 100644
--- a/web/docs/guides/functions.mdx
+++ b/web/docs/guides/functions.mdx
@@ -251,12 +251,15 @@ You can find a list of useful [Edge Function Examples](https://github.com/supaba
</div>
</div>
-## Suggestions and Limitations
+## Suggestions
-### Database vs Edge Functions
+### Database Functions vs Edge Functions
+
+For data-intensive operations we recommend using [Database Functions](/docs/guides/database/functions), which are executed within your database
+and can be called remotely using the [REST and GraphQL API](/docs/guides/database/api).
+
+For use-cases which require low-latency we recommend [Edge Functions](/docs/guides/database/functions), which are globally-distributed and can be written in Typescript.
-For data-intensive operations we recommend using [Database Functions](/docs/guides/database/functions), which are executed within your database,
-and can be called remotely using the [REST and GraphQL API](/docs/guides/database/functions).
### Organizing your Edge Functions
@@ -280,7 +283,7 @@ We recommend developing “fat functions”. This means that you should develop
We recommend using hyphens to name functions because hyphens are the most URL-friendly of all the naming conventions (snake_case, camelCase, PascalCase).
-### Limitations
+## Limitations
Edge Functions are Experimental. They aren't going away, but there will be breaking changes.