The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Adds initial draft
Coppleac2de7e6932f
diff --git a/apps/docs/pages/guides/database/partitions.mdx b/apps/docs/pages/guides/database/partitions.mdxnew file mode 100644index 00000000000..70439130c35--- /dev/null+++ b/apps/docs/pages/guides/database/partitions.mdx@@ -0,0 +1,196 @@+import Layout from '~/layouts/DefaultGuideLayout'++export const meta = {+ id: 'partitions',+ title: 'Partitioning tables',+ description: 'Organizing tables into partitions in Postgres.',+}++Table partitioning is a technique that allows you to divide a large table into smaller, more manageable parts called “partitions”.++<div>+ <img alt="multi database" className="dark:hidden" src="/docs/img/database/partitions-light.png" />+ <img+ alt="multi database"+ className="hidden dark:block"+ src="/docs/img/database/partitions-dark.png"+ />+</div>++Each partition contains a subset of the data based on a specified criteria, such as a range of values or a specific condition. Partitioning can significantly improve query performance and simplify data management for large datasets.++## **Benefits of Table Partitioning**++- **Improved query performance:** allows queries to target specific partitions, reducing the amount of data scanned and improving query execution time.+- **Scalability:** With partitioning, you can add or remove partitions as your data grows or changes, enabling better scalability and flexibility.+- **Efficient data management:** simplifies tasks such as data loading, archiving, and deletion by operating on smaller partitions instead of the entire table.+- **Enhanced maintenance operations:** can optimize vacuuming and indexing, leading to faster maintenance tasks.++## **Partitioning Methods**++Postgres supports various partitioning methods based on how you want to partition your data. The commonly used methods are:++1. **Range Partitioning**: Data is divided into partitions based on a specified range of values. For example, you can partition a sales table by date, where each partition represents a specific time range (e.g., one partition for each month).+2. **List Partitioning**: Data is divided into partitions based on a specified list of values. For instance, you can partition a customer table by region, where each partition contains customers from a specific region (e.g., one partition for customers in the US, another for customers in Europe).+3. **Hash Partitioning**: Data is distributed across partitions using a hash function. This method provides a way to evenly distribute data among partitions, which can be useful for load balancing. However, it doesn't allow direct querying based on specific values.++## **Creating Partitioned Tables**++Let's consider an example of range partitioning for a sales table based on the order date. We'll create monthly partitions to store data for each month:++```sql+create table sales (+ id bigint generated by default as identity,+ order_date date not null,+ customer_id bigint,+ amount bigint,++ -- We need to include all the+ -- partitioning columns in constraints:+ primary key (order_date, id)+)+partition by range (order_date);++create table sales_2000_01+ partition of sales+ for values from ('2000-01-01') to ('2000-02-01');++create table sales_2000_02+ partition of sales+ for values from ('2000-02-01') to ('2000-03-01');++```++To create a partitioned table you append `partition by range (<column_name>)` to the table creation statement. The column that you are partitioning with _must_ be included in any unique index, which is the reason why we specify a composite primary key here (`primary key (order_date, id)`).++## **Querying Partitioned Tables**++To query a partitioned table, you have two options:++1. Querying the parent table+2. Querying specific partitions++### **Querying the parent table**++When you query the parent table, Postgres automatically routes the query to the relevant partitions based on the conditions specified in the query. This allows you to retrieve data from all partitions simultaneously.++Example:++```sql+select *+from sales+where order_date >= '2000-01-01' and order_date < '2000-03-01';+```++This query will retrieve data from both the `sales_2000_01` and `sales_2000_02` partitions.++### **Querying specific partitions**++If you only need to retrieve data from a specific partition, you can directly query that partition instead of the parent table. This approach is useful when you want to target a specific range or condition within a partition.++```sql+select *+from sales_2000_02;+```++This query will retrieve data only from the `sales_2000_02` partition.++## When to partition your tables++There is no real threshold to determine when you should use partitions. Partitions introduce complexity, and complexity should be avoided until it’s needed. A few guidelines:++- If you are considering performance, avoid partitions until you see performance degradation on non-partitioned tables.+- If you are using partitions as a management tool, it’s fine to create the partitions any time.+- If you don’t know how you should partition your data, then it’s probably too early.++## Examples++Here are simple examples for each of the partitioning types in Postgres.++### \***\*Range Partitioning\*\***++Let's consider a range partitioning example for a table that stores sales data based on the order date. We'll create monthly partitions to store data for each month.++In this example, the **`sales`** table is partitioned into two partitions: **`sales_january`** and **`sales_february`**. The data in these partitions is based on the specified range of order dates:++```sql+create table sales (+ id bigint generated by default as identity,+ order_date date not null,+ customer_id bigint,+ amount bigint,++ -- We need to include all the+ -- partitioning columns in constraints:+ primary key (order_date, id)+)+partition by range (order_date);++create table sales_2000_01+ partition of sales+ for values from ('2000-01-01') to ('2000-02-01');++create table sales_2000_02+ partition of sales+ for values from ('2000-02-01') to ('2000-03-01');+```++### List Partitioning++Let's consider a list partitioning example for a table that stores customer data based on their region. We'll create partitions to store customers from different regions.++In this example, the **`customers`** table is partitioned into two partitions: `customers_americas` and `customers_asia`. The data in these partitions is based on the specified list of regions:++```sql+-- Create the partitioned table+create table customers (+ id bigint generated by default as identity,+ name text,+ country text,++ -- We need to include all the+ -- partitioning columns in constraints:+ primary key (country, id)+)+partition by list(country);++create table customers_americas+ partition of customers+ for values in ('US', 'CANADA')++create table customers_asia+ partition of customers+ for values in ('INDIA', 'CHINA', 'JAPAN');+```++### \***\*Hash Partitioning\*\***++You can use hash partitioning to evenly distribute data.++In this example, the **`products`** table is partitioned into two partitions: `products_one` and `products_two`. The data is distributed across these partitions using a hash function:++```sql+create table products (+ id bigint generated by default as identity,+ name text,+ category text,+ price bigint+)+partition by hash (id);++create table products_one+ partition of products+ for values with (modulus 2, remainder 1);++create table products_two+ partition of products+ for values with (modulus 2, remainder 0);+```++## Other tools++There are several other tools available for Postgres partitioning, most notably `[pg_partman](https://github.com/pgpartman/pg_partman)`. Native partitioning was introduced in Postgres 10 and is generally thought to have better performance.++export const Page = ({ children }) => <Layout meta={meta} children={children} />++export default Pagediff --git a/apps/docs/public/img/database/partitions-dark.png b/apps/docs/public/img/database/partitions-dark.pngnew file mode 100644index 00000000000..56c0fa50ed4Binary files /dev/null and b/apps/docs/public/img/database/partitions-dark.png differdiff --git a/apps/docs/public/img/database/partitions-light.png b/apps/docs/public/img/database/partitions-light.pngnew file mode 100644index 00000000000..4e1da41b0c5Binary files /dev/null and b/apps/docs/public/img/database/partitions-light.png differdiff --git a/examples/enterprise-patterns/partitions/supabase/.gitignore b/examples/enterprise-patterns/partitions/supabase/.gitignorenew file mode 100644index 00000000000..773c7c3e0a1--- /dev/null+++ b/examples/enterprise-patterns/partitions/supabase/.gitignore@@ -0,0 +1,3 @@+# Supabase+.branches+.tempdiff --git a/examples/enterprise-patterns/partitions/supabase/config.toml b/examples/enterprise-patterns/partitions/supabase/config.tomlnew file mode 100644index 00000000000..1f53e6ac273--- /dev/null+++ b/examples/enterprise-patterns/partitions/supabase/config.toml@@ -0,0 +1,82 @@+# A string used to distinguish different Supabase projects on the same host. Defaults to the working+# directory name when running `supabase init`.+project_id = "partitions"++[api]+# Port to use for the API URL.+port = 54321+# Schemas to expose in your API. Tables, views and stored procedures in this schema will get API+# endpoints. public and storage are always included.+schemas = ["public", "storage", "graphql_public"]+# Extra schemas to add to the search_path of every request. public is always included.+extra_search_path = ["public", "extensions"]+# The maximum number of rows returns from a view, table, or stored procedure. Limits payload size+# for accidental or malicious requests.+max_rows = 1000++[db]+# Port to use for the local database URL.+port = 54322+# The database major version to use. This has to be the same as your remote database's. Run `SHOW+# server_version;` on the remote database to check.+major_version = 15++[studio]+# Port to use for Supabase Studio.+port = 54323++# Email testing server. Emails sent with the local dev setup are not actually sent - rather, they+# are monitored, and you can view the emails that would have been sent from the web interface.+[inbucket]+# Port to use for the email testing server web interface.+port = 54324+smtp_port = 54325+pop3_port = 54326++[storage]+# The maximum file size allowed (e.g. "5MB", "500KB").+file_size_limit = "50MiB"++[auth]+# The base URL of your website. Used as an allow-list for redirects and for constructing URLs used+# in emails.+site_url = "http://localhost:3000"+# A list of *exact* URLs that auth providers are permitted to redirect to post authentication.+additional_redirect_urls = ["https://localhost:3000"]+# How long tokens are valid for, in seconds. Defaults to 3600 (1 hour), maximum 604,800 seconds (one+# week).+jwt_expiry = 3600+# Allow/disallow new user signups to your project.+enable_signup = true++[auth.email]+# Allow/disallow new user signups via email to your project.+enable_signup = true+# If enabled, a user will be required to confirm any email change on both the old, and new email+# addresses. If disabled, only the new email is required to confirm.+double_confirm_changes = true+# If enabled, users need to confirm their email address before signing in.+enable_confirmations = false++# Use an external OAuth provider. The full list of providers are: `apple`, `azure`, `bitbucket`,+# `discord`, `facebook`, `github`, `gitlab`, `google`, `keycloak`, `linkedin`, `notion`, `twitch`,+# `twitter`, `slack`, `spotify`, `workos`, `zoom`.+[auth.external.apple]+enabled = false+client_id = ""+secret = ""+# Overrides the default auth redirectUrl.+redirect_uri = ""+# Overrides the default auth provider URL. Used to support self-hosted gitlab, single-tenant Azure,+# or any other third-party OIDC providers.+url = ""++[analytics]+enabled = false+port = 54327+vector_port = 54328+# Setup BigQuery project to enable log viewer on local development stack.+# See: https://supabase.com/docs/guides/getting-started/local-development#enabling-local-logging+gcp_project_id = ""+gcp_project_number = ""+gcp_jwt_path = "supabase/gcloud.json"diff --git a/examples/enterprise-patterns/partitions/supabase/migrations/20000101003625_sales.sql b/examples/enterprise-patterns/partitions/supabase/migrations/20000101003625_sales.sqlnew file mode 100644index 00000000000..6735210ab3c--- /dev/null+++ b/examples/enterprise-patterns/partitions/supabase/migrations/20000101003625_sales.sql@@ -0,0 +1,20 @@++create table sales (+ id bigint generated by default as identity,+ order_date date not null,+ customer_id bigint,+ amount bigint,++ -- We need to include all the+ -- partitioning columns in constraints:+ primary key (order_date, id)+)+partition by range (order_date);++create table sales_2000_01+ partition of sales+ for values from ('2000-01-01') to ('2000-02-01');++create table sales_2000_02+ partition of sales+ for values from ('2000-02-01') to ('2000-03-01');\ No newline at end of filediff --git a/examples/enterprise-patterns/partitions/supabase/migrations/20000101003626_customers.sql b/examples/enterprise-patterns/partitions/supabase/migrations/20000101003626_customers.sqlnew file mode 100644index 00000000000..dba097e5026--- /dev/null+++ b/examples/enterprise-patterns/partitions/supabase/migrations/20000101003626_customers.sql@@ -0,0 +1,20 @@+-- Create the partitioned table+create table customers (+ id bigint generated by default as identity,+ name text,+ country text,++ -- We need to include all the+ -- partitioning columns in constraints:+ primary key (country, id)+)+partition by list(country);+++create table customers_americas+ partition of customers+ for values in ('US', 'CANADA')++create table customers_asia+ partition of customers+ for values in ('INDIA', 'CHINA', 'JAPAN');diff --git a/examples/enterprise-patterns/partitions/supabase/migrations/20000101003627_products.sql b/examples/enterprise-patterns/partitions/supabase/migrations/20000101003627_products.sqlnew file mode 100644index 00000000000..51dd1ad1817--- /dev/null+++ b/examples/enterprise-patterns/partitions/supabase/migrations/20000101003627_products.sql@@ -0,0 +1,15 @@+create table products (+ id bigint generated by default as identity primary key,+ name text,+ category text,+ price bigint+)+partition by hash (id);++create table products_one+ partition of products+ for values with (modulus 2, remainder 1);++create table products_two+ partition of products+ for values with (modulus 2, remainder 0);\ No newline at end of filediff --git a/examples/enterprise-patterns/partitions/supabase/seed.sql b/examples/enterprise-patterns/partitions/supabase/seed.sqlnew file mode 100644index 00000000000..1f5de4f6f12--- /dev/null+++ b/examples/enterprise-patterns/partitions/supabase/seed.sql@@ -0,0 +1,78 @@+insert into sales (order_date, amount, customer_id)+values+ ('2000-01-01', 100, 1),+ ('2000-01-02', 200, 2),+ ('2000-01-03', 300, 1),+ ('2000-01-04', 400, 2),+ ('2000-01-05', 500, 1),+ ('2000-01-06', 600, 2),+ ('2000-01-07', 700, 1),+ ('2000-01-08', 800, 2),+ ('2000-01-09', 900, 1),+ ('2000-01-10', 1000, 2),+ ('2000-01-11', 1100, 1),+ ('2000-01-12', 1200, 2),+ ('2000-01-13', 1300, 1),+ ('2000-01-14', 1400, 2),+ ('2000-01-15', 1500, 1),+ ('2000-01-16', 1600, 2),+ ('2000-01-17', 1700, 1),+ ('2000-01-18', 1800, 2),+ ('2000-01-19', 1900, 1),+ ('2000-01-20', 2000, 2),+ ('2000-02-01', 100, 1),+ ('2000-02-02', 200, 2),+ ('2000-02-03', 300, 1),+ ('2000-02-04', 400, 2),+ ('2000-02-05', 500, 1),+ ('2000-02-06', 600, 2),+ ('2000-02-07', 700, 1),+ ('2000-02-08', 800, 2),+ ('2000-02-09', 900, 1),+ ('2000-02-10', 1000, 2),+ ('2000-02-11', 1100, 1),+ ('2000-02-12', 1200, 2),+ ('2000-02-13', 1300, 1),+ ('2000-02-14', 1400, 2),+ ('2000-02-15', 1500, 1),+ ('2000-02-16', 1600, 2),+ ('2000-02-17', 1700, 1),+ ('2000-02-18', 1800, 2),+ ('2000-02-19', 1900, 1),+ ('2000-02-20', 2000, 2);++insert into customers (name, country)+values+ ('John Smith', 'US'),+ ('Alice Johnson', 'US'),+ ('Michael Brown', 'US'),+ ('Emily Davis', 'US'),+ ('Robert Johnson', 'US'),+ ('Sarah Wilson', 'US'),+ ('David Thompson', 'CANADA'),+ ('Jennifer Lee', 'CANADA'),+ ('Christopher Martin', 'CANADA'),+ ('Emma Rodriguez', 'CANADA'),+ ('Li Wei', 'CHINA'),+ ('Aarav Patel', 'INDIA'),+ ('Yuki Tanaka', 'JAPAN'),+ ('Ravi Kumar', 'INDIA'),+ ('Zhang Wei', 'CHINA'),+ ('Hiroshi Yamamoto', 'JAPAN'),+ ('Mei Chen', 'CHINA'),+ ('Ananya Gupta', 'INDIA'),+ ('Shinji Kimura', 'JAPAN'),+ ('Sanjay Singh', 'INDIA');++insert into products (name, category, price)+values+ ('Product 1', 'Category A', 100),+ ('Product 2', 'Category B', 200),+ ('Product 3', 'Category A', 150),+ ('Product 4', 'Category C', 300),+ ('Product 5', 'Category B', 250),+ ('Product 6', 'Category C', 350),+ ('Product 7', 'Category A', 120),+ ('Product 8', 'Category B', 180),+ ('Product 9', 'Category C', 270),+ ('Product 10', 'Category A', 220);