The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
migrate(docs): troubleshooting 27 (#30949)
Charis935c33621d9c
diff --git a/apps/docs/content/troubleshooting/inserting-into-sequenceserial-table-causes-duplicate-key-violates-unique-constraint-error-pi6DnC.mdx b/apps/docs/content/troubleshooting/inserting-into-sequenceserial-table-causes-duplicate-key-violates-unique-constraint-error-pi6DnC.mdxnew file mode 100644index 00000000000..2a3ab48a9df--- /dev/null+++ b/apps/docs/content/troubleshooting/inserting-into-sequenceserial-table-causes-duplicate-key-violates-unique-constraint-error-pi6DnC.mdx@@ -0,0 +1,38 @@+---+title = 'Inserting into Sequence/Serial Table Causes "duplicate key violates unique constraint" Error'+github_url = "https://github.com/orgs/supabase/discussions/21134"+date_created = "2024-02-09T03:10:39+00:00"+topics = ["database"]+keywords = ["duplicate", "sequence", "table", "key", "constraint"]++[[errors]]+message = "duplicate key violates unique constraint"+---++If you are receiving the below error for an auto-incremented table:++> ERROR: duplicate key violates unique constraint++it likely means that the table's sequence has somehow become out of sync, likely because of a mass import process (or something along those lines).++Call it a "bug by design", but it seems that you have to manually reset the a primary key index after restoring from a dump file.++You can run the following commands on your instance to see if your sequence is out-of-sync:++```sql+SELECT MAX(<sequenced_column>) FROM <table_name>;++SELECT nextval(pg_get_serial_sequence('<public.table_name>', '<sequenced_column_name>'));+```++If the values are off by more than 1, you need to resynchronize your sequence.++Back up your PG database by restarting in the [General Settings](https://supabase.com/dashboard/project/_/settings/general) (just in case). When you restore your database, you will have a backup saved. Alternatively, you can also just download your properties table instead as a backup.++Then you can run this:++```sql+SELECT SETVAL('public.<table_name>_<column_nam>_seq', (SELECT MAX(<column_name>) FROM <table_name>)+1);+```++That will set the sequence to the next available value that's higher than any existing primary key in the sequence.diff --git a/apps/docs/content/troubleshooting/insufficient-privilege-when-accessing-pgstatstatements-e5M_EQ.mdx b/apps/docs/content/troubleshooting/insufficient-privilege-when-accessing-pgstatstatements-e5M_EQ.mdxnew file mode 100644index 00000000000..ab6243de1b6--- /dev/null+++ b/apps/docs/content/troubleshooting/insufficient-privilege-when-accessing-pgstatstatements-e5M_EQ.mdx@@ -0,0 +1,18 @@+---+title = '"insufficient privilege" when accessing pg_stat_statements'+github_url = "https://github.com/orgs/supabase/discussions/20126"+date_created = "2024-01-03T10:59:49+00:00"+topics = ["database"]+keywords = ["privilege", "permissions", "postgres"]++[[errors]]+message = "insufficient privilege"+---++If you see the error "insufficient privilege" when accessing [pg_stat_statements](https://supabase.com/docs/guides/platform/performance#postgres-cumulative-statistics-system) or when accessing [Query Performance Report](https://supabase.com/dashboard/project/_/reports/query-performance), it means that the Postgres role does not have required permissions.++In this case, you can run the below command to allow the Postgres role to read all statistics from the system:++```+grant pg_read_all_stats to postgres;+```diff --git a/apps/docs/content/troubleshooting/new-variable-is-null-in-a-trigger-function--l9AOZ.mdx b/apps/docs/content/troubleshooting/new-variable-is-null-in-a-trigger-function--l9AOZ.mdxnew file mode 100644index 00000000000..3774aeb2130--- /dev/null+++ b/apps/docs/content/troubleshooting/new-variable-is-null-in-a-trigger-function--l9AOZ.mdx@@ -0,0 +1,17 @@+---+title = "NEW variable is null in a trigger function."+github_url = "https://github.com/orgs/supabase/discussions/15934"+date_created = "2023-07-20T19:05:52+00:00"+topics = ["database", "functions"]+keywords = ["trigger", "function", "statement", "row", "NEW", "OLD", "SQL"]+---++When you create a function and call it from a trigger the NEW variable is only passed in if the trigger is on ROW operations++The UI currently defaults to STATEMENT which will not provide NEW or OLD variables to the function.++++Also in SQL (reference: https://www.postgresql.org/docs/current/sql-createtrigger.html)++diff --git a/apps/docs/content/troubleshooting/partitioning-an-existing-table-with-same-name-VEnbzK.mdx b/apps/docs/content/troubleshooting/partitioning-an-existing-table-with-same-name-VEnbzK.mdxnew file mode 100644index 00000000000..fe9d29fbabc--- /dev/null+++ b/apps/docs/content/troubleshooting/partitioning-an-existing-table-with-same-name-VEnbzK.mdx@@ -0,0 +1,33 @@+---+title = "Partitioning an existing table with same name"+github_url = "https://github.com/orgs/supabase/discussions/21380"+date_created = "2024-02-20T08:39:24+00:00"+topics = ["database"]+keywords = ["partitioning", "table", "postgresql", "constraint", "index"]+---++Our documentation on table partitioning here: https://supabase.com/docs/guides/database/partitions++Read the below information if you want the partitioned table to retain the name of the original table.++- You can create empty "parent" partitioned table using CREATE TABLE parent_table LIKE source_table [ like_option ... ] PARTITION BY ...++LIKE option will copy column definitions, constraints etc. from source table automatically. For different like_options see https://www.postgresql.org/docs/current/sql-createtable.html (you could try INCLUDING ALL like option).++- Then you can create empty partitions for ranges that you like.++- Then you could insert rows from the old/original table into parent_table. They will be spread into partitions at almost no cost. But the process could take time. For experiment, you could insert a subset from original table (using `ORDER BY id LIMIT <number>` clause) and see time needed to do actual work.++- It's crucial to check that you haven't forgotten some range by checking default partition (that contains rows inserted into a parent table but not corresponding any partition ranges) meanwhile. Overgrown default partition slows down almost any operations with partitioned table.++- When insert is finished you can rename old and new "parent" table. Do it in one transaction, so that you have no time old name is already don't exist but not yet re-created for any query.++- Indices definition will be copied with INCLUDING ALL option. They will be propagated during insertion of data into a new parent table.++- Of course you can hold old table for your data to be safe indefinitely. Do not delete it outright! Your queries after renaming will not use it. Indexes etc. work behind the scenes, you are not required to know its names. (In reality indices for a "new" partitioned table will be new, but you won't see it if you have used LIKE..INCLUDING ALL option).++- Triggers will be needed to re-create for "parent" partitioned table using `CREATE TRIGGER` clause https://www.postgresql.org/docs/16/sql-createtrigger.html++- Functions will switch to work with a new table after renaming.++- There are certain limitations of declarative partitioning that you may need to be aware: https://www.postgresql.org/docs/current/ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVEdiff --git a/apps/docs/content/troubleshooting/resolving-42p01-relation-does-not-exist-error-W4_9-V.mdx b/apps/docs/content/troubleshooting/resolving-42p01-relation-does-not-exist-error-W4_9-V.mdxnew file mode 100644index 00000000000..c2f982faa0f--- /dev/null+++ b/apps/docs/content/troubleshooting/resolving-42p01-relation-does-not-exist-error-W4_9-V.mdx@@ -0,0 +1,73 @@+---+title = "Resolving 42P01: relation does not exist error"+github_url = "https://github.com/orgs/supabase/discussions/29577"+date_created = "2024-09-29T15:52:41+00:00"+topics = ["database"]+keywords = ["relation", "schema", "capitalization"]++[[errors]]+code = "42P01"+message = 'relation "<some table name>" does not exist'++[[errors]]+code = "42501"+message = "permission denied"+---++`42P01` is a[ Postgres level error](https://www.postgresql.org/docs/current/errcodes-appendix.html), that can also be found in the [PostgREST error docs](https://postgrest.org/en/v12/references/errors.html)++```sql+42P01: relation "<some table name>" does not exist+```++## There are a few possible causes++---++### Cause 1: Search path broken++When directly accessing a table that is not in the `public` schema, it's important to reference the external schema explicitly in your query. Below is an example from the [JS client:](https://supabase.com/docs/reference/javascript/select)++```js+const { data, error } = await supabase.schema('myschema').from('mytable').select()+```++If after calling the table directly, you get a `42501` permission denied error, then you must also [expose the custom schema to the API.](https://supabase.com/docs/guides/api/using-custom-schemas). For Supabase managed schemas, such as `vault` and `auth`, these cannot be directly accessed through the DB REST API for security reasons. If necessary, they can be strictly accessed through [security definer functions.](https://supabase.com/docs/guides/database/functions?queryGroups=language&language=sql&queryGroups=example-view&example-view=sql#security-definer-vs-invoker)++---++### Cause 2: Ignoring capitalization and other typos++The table could be defined as: CREATE TABLE “Hello”`. The double quotes make it case-sensitive, so it becomes essential to call the table with the appropriate title. It is possible to change the table name to be lowercase for convenience, either in the Table Editor, or with raw SQL:++```sql+alter table "Table_name"+rename to table_name;+```++---++### Cause 3: table or function actually does not exist++One may have never made the table or dropped it deliberately or accidentally. This can be quickly checked with the following query:++```sql+-- For tables+SELECT * FROM information_schema.tables+WHERE table_name ILIKE 'example_table'; --<------ Add relevant table name+```++```sql+-- For functions+select+ p.proname as function_name,+ n.nspname as schema_name,+ pg_get_functiondef(p.oid) as function_definition+from+ pg_proc as p+ join pg_namespace as n on p.pronamespace = n.oid+where n.nspname in ('public', 'your custom schema') -- <------ Add other relevant schemas+order by n.nspname, p.proname;+```++---diff --git a/apps/docs/content/troubleshooting/why-are-there-gaps-in-my-postgres-id-sequence-Frifus.mdx b/apps/docs/content/troubleshooting/why-are-there-gaps-in-my-postgres-id-sequence-Frifus.mdxnew file mode 100644index 00000000000..c6019993177--- /dev/null+++ b/apps/docs/content/troubleshooting/why-are-there-gaps-in-my-postgres-id-sequence-Frifus.mdx@@ -0,0 +1,78 @@+---+title = "Why are there gaps in my Postgres id sequence?"+github_url = "https://github.com/orgs/supabase/discussions/22299"+date_created = "2024-03-27T18:04:39+00:00"+topics = ["database"]+keywords = ["sequence", "id", "rollback", "deletions", "upserts"]++[[errors]]+message = 'Failed to run sql query: duplicate key value violates unique constraint "{table}_pkey"'+---++Most database tables have a primary key that is a made-up number, and that number is usually made by a sequence.++You might be surprised to know that gaps in sequence IDs are a normal aspect of working with relational databases in general. It's a common scenario that can seem puzzling at first, but there are several reasons why these gaps occur, and sometimes, this is typically expected and does not indicate a problem with your database.++It's also important to understand the distinction that sequences guarantee **uniqueness**, but not **consecutiveness**, and this should not imply any issues relating to data integrity with your database.++### How to check the name of your sequence++If you don't know the name of your sequence, it's often formed based on a standard naming convention: table_name_id_seq, where table_name is the name of your table and id is the name of your serial column.++### Common Reasons for Gaps in Sequences++1. Rollbacks+ One of the most common reasons for a gap is the rollback of a transaction. If you initiate a transaction that includes an insert operation, the sequence responsible for generating the ID for the new row increments. If, for any reason, the transaction doesn't complete successfully—perhaps due to a constraint violation or a deliberate decision to rollback—the insert operation is undone, but the sequence value used is not returned or reused. The documentation explains that as well:++> To avoid blocking concurrent transactions that obtain numbers from the same sequence, a nextval operation is never rolled back; that is, once a value has been fetched it is considered used and will not be returned again. This is true even if the surrounding transaction later aborts, or if the calling query ends up not using the value. For example an INSERT with an ON CONFLICT clause will compute the to-be-inserted tuple, including doing any required nextval calls, before detecting any conflict that would cause it to follow the ON CONFLICT rule instead. Such cases will leave unused “holes” in the sequence of assigned values.++2. Deletions+ Removing rows from your table also creates gaps in the sequence of primary key values. Although deletions do not affect the sequence directly, they contribute to the non-sequential appearance of IDs in your table.++3. Manual Sequence Adjustments+ Another source of gaps can be manual interventions, such as incrementing a sequence number directly or setting it to a new value.++4. Upserts+ When an upsert is executed, it can still increase the sequence even if it is set to do nothing on conflicts++### Checking for Gaps++To check for gaps in the sequence of IDs in a PostgreSQL table, you can use a SQL query that compares the sequence of IDs to a generated series of numbers that spans the same range:++```+SELECT+ s.id AS missing_id+FROM+ generate_series((SELECT MIN(id) FROM your_table), (SELECT MAX(id) FROM your_table)) s(id)+ LEFT JOIN your_table ON your_table.id = s.id+WHERE+ your_table.id IS NULL;+```++This query should help you pinpoint where gaps exist.++### Encountering Errors and Adjusting Sequences++In operations involving sequences, you might encounter an error message such as:++`Failed to run sql query: duplicate key value violates unique constraint "{table}_pkey"`++This error suggests a discrepancy between the sequence-generated IDs and the actual IDs in the table.++To address this, you can check the current value of your sequence or the highest ID in your table with:++```+postgres=# SELECT max(id) FROM <table_name>;++postgres=# SELECT nextval('{table}_{column}_seq');+```++And reset the sequence value to match the highest ID plus one:+`SELECT setval('{table}_{column}_seq', (SELECT max(id) FROM <table_name>) + 1);`++Or, alternatively, adjust the sequence to a specific new value:+`ALTER SEQUENCE '{table}_{column}_seq' RESTART WITH new_value;`++### Implementing a gapless ID sequence++If your application requires contiguous IDs and you decide to build a gapless sequence. Think twice about this, as it will serialize all transactions that use that “sequence” which will then deteriorate your data modification performance considerably. However, if your application truly requires gapless IDs, then you can use a Trigger: After a successful insert, use a database trigger to assign an ID based on a custom logic that finds the next available gapless ID.diff --git a/apps/docs/content/troubleshooting/why-is-my-select-returning-an-empty-data-array-and-i-have-data-in-the-table-xvOPgx.mdx b/apps/docs/content/troubleshooting/why-is-my-select-returning-an-empty-data-array-and-i-have-data-in-the-table-xvOPgx.mdxnew file mode 100644index 00000000000..daa5ac57e85--- /dev/null+++ b/apps/docs/content/troubleshooting/why-is-my-select-returning-an-empty-data-array-and-i-have-data-in-the-table-xvOPgx.mdx@@ -0,0 +1,40 @@+---+title = "Why is my select returning an empty data array and I have data in the table?"+github_url = "https://github.com/orgs/supabase/discussions/12895"+date_created = "2023-03-08T16:38:02+00:00"+topics = ["database", "auth"]+keywords = ["rls", "policy", "filter", "jwt"]+---++Usually this means you have RLS (row level security) enabled and no policy, or do not meet the policy. It can also mean you have a filter and have no rows matching that.++If you have RLS enabled you can test quickly by disable RLS on the table. If your query works then you have no policies or do not meet them.++If you have policies that depend on having a signed in user (TO set to `authenticated` or using `auth.uid()`) then you can check by setting TO as `anon` and setting your policy to just `TRUE`. If that works then you don't have a signed in user (with a jwt in the authorization header) when you make the call.++For more information on RLS please see https://supabase.com/docs/guides/auth/row-level-security++Adding:+To test if you have a user session at time of your call you can add this function with the SQL editor and an rpc call in your code at same place as your current database call.++```sql+create function test_authorization_header() returns json+ language SQL+ as+$$+ select auth.jwt();+$$;+```++```javascript+const { data: testData, error: testError } = await supabase.rpc('test_authorization_header')+console.log(`The user role is ${testData.role} and the user UUID is ${testData.sub}. `, testError)+```++If you have anon or service_role as the role then you do not have a user session at the time of your call.++To delete the test SQL function:++```sql+drop function test_authorization_header;+```diff --git a/apps/docs/public/img/troubleshooting/49e11154-296b-42b9-8036-8a7288e49b8a.png b/apps/docs/public/img/troubleshooting/49e11154-296b-42b9-8036-8a7288e49b8a.pngnew file mode 100644index 00000000000..d56aedf13e5Binary files /dev/null and b/apps/docs/public/img/troubleshooting/49e11154-296b-42b9-8036-8a7288e49b8a.png differdiff --git a/apps/docs/public/img/troubleshooting/c7ac0f69-6fb9-4ff2-8aab-5cda6bada3f6.png b/apps/docs/public/img/troubleshooting/c7ac0f69-6fb9-4ff2-8aab-5cda6bada3f6.pngnew file mode 100644index 00000000000..11ea0ee5608Binary files /dev/null and b/apps/docs/public/img/troubleshooting/c7ac0f69-6fb9-4ff2-8aab-5cda6bada3f6.png differ