自定义 Postgres 配置
每个 Supabase 项目都是一个预配置的 Postgres 集群。你可以覆盖一些配置设置以满足你的需求。这是一个高级话题,除非必要,我们不建议随意更改这些设置。
🌐 Each Supabase project is a pre-configured Postgres cluster. You can override some configuration settings to suit your needs. This is an advanced topic, and we don't recommend touching these settings unless it is necessary.
自定义 Postgres 配置可以让你对数据库进行 高级 控制,但不当的设置可能会导致严重的性能下降或项目不稳定。
🌐 Customizing Postgres configurations provides advanced control over your database, but inappropriate settings can lead to severe performance degradation or project instability.
查看设置 #
🌐 Viewing settings
要列出所有 Postgres 设置及其说明,请运行:
🌐 To list all Postgres settings and their descriptions, run:
1select * from pg_settings;可配置设置 #
🌐 Configurable settings
用户上下文设置 #
🌐 User-context settings
pg_settings 表的 context 列说明了更改设置的要求。默认情况下,具有 user 上下文的设置可以在 role 或 database 级别通过 SQL 进行更改。
🌐 The pg_settings table's context column specifies the requirements for changing a setting. By default, those with a user context can be changed at the role or database level with SQL.
要列出所有用户上下文设置,请运行:
🌐 To list all user-context settings, run:
1select * from pg_settings where context = 'user';举个例子,可以修改 statement_timeout 设置:
🌐 As an example, the statement_timeout setting can be altered:
1alter database "postgres" set "statement_timeout" TO '60s';要验证更改,请执行:
🌐 To verify the change, execute:
1show "statement_timeout";超级用户设置 #
🌐 Superuser settings
有些设置只能由超级用户修改。Supabase 预先启用了 supautils 扩展,它允许 postgres 角色保留某些超级用户权限。它可以在 role 级别修改以下保留配置:
🌐 Some settings can only be modified by a superuser. Supabase pre-enables the supautils extension, which allows the postgres role to retain certain superuser privileges. It enables modification of the below reserved configurations at the role level:
| 设置 | 描述 |
|---|---|
auto_explain.* | 配置 auto_explain 模块。可以配置为记录预计执行时间超过 x 秒的查询的执行计划,包括函数查询。 |
deadlock_timeout | 设置在检查死锁之前等待锁的时间。 |
log_duration | 记录查询运行了多久,但不记录整个查询内容。 |
log_lock_waits | 控制在会话获取锁时如果等待时间超过 deadlock_timeout 是否生成日志消息。 |
log_min_duration_statement | 记录执行时间达到或超过指定阈值的已完成语句的持续时间和文本 |
log_min_error_statement | 判断触发日志的查询是否应根据事件的严重等级记录为子字段 |
log_min_messages | 要记录的消息的最低严重级别。 |
log_parameter_max_length | 设置在记录语句时绑定参数值的数据日志的最大字节长度。 |
log_replication_commands | 记录所有复制命令 |
log_statement | 控制记录哪些 SQL 语句。有效值为 none(关闭)、ddl、mod 和 all(所有语句)。 |
log_temp_files | 控制临时文件名和大小的日志记录。 |
pg_stat_statements.* | 配置 pg_stat_statements 扩展。 |
pgaudit.* | 配置 PGAudit 扩展。log_parameter 仍然受到限制以保护机密 |
pgrst.* | PostgREST 设置 |
plan_filter.* | 配置 pg_plan_filter 扩展 |
safeupdate.enabled | 启用 safeupdate 扩展,这要求在 UPDATE 和 DELETE 语句中使用 WHERE 子句。 |
session_replication_role | 设置会话中触发器和重写规则的行为。 |
track_functions | 控制是否跟踪函数调用次数和时间。有效值为 none、pl(仅限过程语言函数)以及 all。 |
track_io_timing | 收集数据库 I/O 活动的时间统计信息。 |
wal_compression | 这个参数可以使用指定的压缩方法对 WAL 进行压缩。 |
例如,要为 postgres 角色启用 log_nested_statements,执行:
🌐 For example, to enable log_nested_statements for the postgres role, execute:
1alter role "postgres" set "auto_explain.log_nested_statements" to 'on';查看更改:
🌐 To view the change:
1select2 rolname,3 rolconfig4from pg_roles5where rolname = 'postgres';CLI 可配置设置 #
🌐 CLI configurable settings
虽然许多 Postgres 参数可以直接配置,但有些配置可以通过 Supabase CLI 在 system 级别进行更改。
🌐 While many Postgres parameters are configurable directly, some configurations can be changed with the Supabase CLI at the system level.
CLI 的更改会永久覆盖默认设置,所以 reset all 和 set to default 命令不会恢复到原来的值。
🌐 CLI changes permanently overwrite default settings, so reset all and set to default commands won't revert to the original values.
为了覆盖默认设置,你必须在你的组织中拥有 Owner 或 Administrator 权限。
🌐 In order to overwrite the default settings, you must have Owner or Administrator privileges within your organizations.
支持的命令行参数 #
🌐 CLI supported parameters
如果你需要的某个设置还无法配置,请和我们分享你的使用案例!告诉我们你希望控制哪个设置,我们会考虑在未来的更新中增加支持。
🌐 If a setting you need is not yet configurable, share your use case with us! Let us know what setting you'd like to control, and we'll consider adding support in future updates.
以下参数可以被覆盖:
🌐 The following parameters are available for overrides:
标记为 Restart: Yes 的参数会导致 CLI 自动重启你的数据库(以及任何只读副本)以应用更改。这可能会短暂中断活跃连接。你可以使用 --no-restart 标志来推迟重启。
🌐 Parameters marked with Restart: Yes cause the CLI to automatically restart your database (and any read replicas) to apply the change. This may cause a brief interruption to active connections. You can use the --no-restart flag to defer the restart.
使用下面的例子和 supabase postgres-config update --project-ref <project-ref> --experimental:
| 参数 | 类型 | 重启 | 示例 |
|---|---|---|---|
| cron.log_statement | 仅限CLI | 是 | --config cron.log_statement=false |
| checkpoint_timeout | 仅限命令行 | 否 | --config checkpoint_timeout=15min |
| effective_cache_size | 命令行 + SQL | 否 | --config effective_cache_size=8GB |
| 热备反馈 | 仅限CLI | 否 | --config hot_standby_feedback=true |
| logical_decoding_work_mem | 命令行 + SQL | 否 | --config logical_decoding_work_mem=128MB |
| log_autovacuum_min_duration | 仅限命令行 | 否 | --config log_autovacuum_min_duration=10s |
| log_checkpoints | 仅限 CLI | 否 | --config log_checkpoints=true |
| log_connections | 仅限命令行 | 否 | --config log_connections=true |
| log_disconnections | 仅限CLI | 否 | --config log_disconnections=true |
| log_lock_waits | CLI + SQL | 否 | --config log_lock_waits=true |
| log_recovery_conflict_waits | 仅限命令行 | 否 | --config log_recovery_conflict_waits=true |
| 日志复制命令 | CLI + SQL | 否 | --config log_replication_commands=true |
| log_startup_progress_interval | 仅限命令行 | 否 | --config log_startup_progress_interval=10s |
| log_temp_files | 命令行 + SQL | 否 | --config log_temp_files=10MB |
| maintenance_work_mem | 命令行 + SQL | 否 | --config maintenance_work_mem=512MB |
| 最大连接数(注意这些注意事项) | 仅限命令行 | 是 | --config max_connections=200 |
| max_locks_per_transaction | 仅限命令行 | 是 | --config max_locks_per_transaction=128 |
| max_logical_replication_workers | 仅限命令行 | 是 | --config max_logical_replication_workers=10 |
| max_parallel_maintenance_workers | CLI + SQL | 否 | --config max_parallel_maintenance_workers=2 |
| max_parallel_workers_per_gather | 命令行接口 + SQL | 否 | --config max_parallel_workers_per_gather=2 |
| max_parallel_workers | 命令行 + SQL | 否 | --config max_parallel_workers=4 |
| max_replication_slots | 仅限命令行使用 | 是 | --config max_replication_slots=10 |
| max_slot_wal_keep_size | 仅限 CLI | 否 | --config max_slot_wal_keep_size=4GB |
| max_standby_archive_delay | 仅限 CLI | 否 | --config max_standby_archive_delay=30s |
| max_standby_streaming_delay | 仅限 CLI | 否 | --config max_standby_streaming_delay=30s |
| max_sync_workers_per_subscription | 仅限 CLI | 否 | --config max_sync_workers_per_subscription=10 |
| max_wal_size | 仅限命令行 | 否 | --config max_wal_size=2GB |
| max_wal_senders | 仅限命令行接口 | 是 | --config max_wal_senders=10 |
| max_worker_processes | 仅限命令行使用 | 是 | --config max_worker_processes=8 |
| session_replication_role | 仅限 CLI | 否 | --config session_replication_role=replica |
| shared_buffers | 仅限命令行 | 是 | --config shared_buffers=256MB |
| statement_timeout | CLI + SQL | 否 | --config statement_timeout=60s |
| track_activity_query_size | 仅限 CLI | 是 | --config track_activity_query_size=2048B |
| track_commit_timestamp | 仅限 CLI | 是 | --config track_commit_timestamp=true |
| wal_keep_size | 仅限 CLI | 否 | --config wal_keep_size=1GB |
| wal_sender_timeout | 仅限 CLI | 否 | --config wal_sender_timeout=60s |
| work_mem | CLI + SQL | 否 | --config work_mem=64MB |
使用命令行管理 Postgres 配置 #
🌐 Managing Postgres configuration with the CLI
开始吧:
🌐 To start:
要更新 Postgres 配置,请使用 postgres config 命令:
🌐 To update Postgres configurations, use the postgres config command:
1supabase postgres-config update --config shared_buffers=250MB \2--project-ref <project-ref> \3--experimental默认情况下,CLI 会将提供的任何配置覆盖与现有的配置合并。可以使用 --replace-existing-overrides 标志来强制用提供的覆盖完全替换现有的覆盖:
🌐 By default, the CLI will merge any provided config overrides with any existing ones. The --replace-existing-overrides flag can be used to instead force all existing overrides to be replaced with the ones being provided:
1supabase postgres-config update --config max_parallel_workers=3 \2--replace-existing-overrides \3--project-ref <project-ref> \4--experimental要删除特定的配置覆盖,使用 postgres-config delete 命令:
🌐 To delete specific configuration overrides, use the postgres-config delete command:
1supabase postgres-config delete --config shared_buffers,work_mem \2--project-ref <project-ref> \3--experimental默认情况下,CLI v2(≥ 2.0.0)会检查参数的上下文并请求正确的操作(重载或重启):
🌐 By default, CLI v2 (≥ 2.0.0) checks the parameter’s context and requests the correct action (reload or restart):
- 如果这个设置可以重新加载(
pg_settings.context = 'sighup'),那么管理 API 会检测到这一点并通过配置重载来应用更改。 - 如果该设置需要重启(
pg_settings.context = 'postmaster'),那么主节点和所有只读副本都会重启以应用更改。
要检查某个参数是否可以在不重启的情况下重新加载,请参阅 Postgres 文档。
🌐 To check whether a parameter can be reloaded without a restart, see the Postgres docs.
你可以通过以下检查来确认更改是否已应用:
🌐 You can verify whether changes have been applied with the following checks:
1supabase --version;1-- Check whether the parameters were updated (and if a restart is pending):2select name, setting, context, pending_restart3from pg_settings4where name in ('max_slot_wal_keep_size', 'shared_buffers', 'max_connections');1-- If the timestamp hasn’t changed, no restart occurred2select pg_postmaster_start_time();你也可以传入 --no-restart 标志来尝试只重新加载应用。如果参数无法重新加载,变化会保持待处理状态,直到下次重启。
🌐 You can also pass the --no-restart flag to attempt a reload-only apply. If the parameter cannot be reloaded, the change stays pending until the next restart.
只读副本和自定义配置
Postgres 需要在主集群和只读副本之间同步几个参数。
🌐 Postgres requires several parameters to be synchronized between the Primary cluster and Read Replicas.
默认情况下,Supabase 会确保这种传播正确执行。不过,如果 --no-restart 行为与那些在不重启的情况下无法重新加载的参数一起使用,那么用户需要负责确保主节点和只读副本及时重启,以保证稳定运行。如果在这种情况下更新了配置但没有通过重启使用的话,当主节点或只读副本孤立重启(比如因为内存不足或者硬件故障)时,可能会导致只读副本失败。
🌐 By default, Supabase ensures that this propagation is executed correctly. However, if the --no-restart behavior is used in conjunction with parameters that cannot be reloaded without a restart, the user is responsible for ensuring that both the primaries and the read replicas get restarted in a timely manner to ensure a stable running state. Leaving the configuration updated, but not used (via a restart) in such a case can result in read replica failure if the primary, or a read replica, restarts in isolation (e.g. due to an out-of-memory event, or hardware failure).
1supabase postgres-config delete --config shared_buffers --no-restart \2--project-ref <project-ref> \3--experimental重置为默认配置 #
🌐 Resetting to default config
要在数据库层将设置重置为默认值:
🌐 To reset a setting to its default value at the database level:
1-- reset a single setting at the database level2alter database "postgres" set "<setting_name>" to default;34-- reset all settings at the database level5alter database "postgres" reset all;对于 role 级别的配置,你可以运行:
🌐 For role level configurations, you can run:
1alter role "<role_name>" set "<setting_name>" to default;考虑事项 #
🌐 Considerations
- 通过 CLI 进行的更改可能会重启数据库,从而暂时中断现有的数据库连接;在大多数情况下,这不会超过几秒钟。不过,你可以使用 --no-restart 标志来跳过重启,保持连接不变。不过要记住,这取决于你正在进行的具体配置更改。如果更改需要重启,使用 --no-restart 标志会阻止重启,但这些更改在手动重启之前不会生效。此外,有些参数要求主库和只读副本保持一致;在这种情况下如果不重启,可能会导致只读副本在主库或只读副本单独重启时出现故障。
- 自定义 Postgres 配置总是会覆盖 Supabase 生成的默认优化。当更改计算附加组件时,你也应该查看并更新你的自定义 Postgres 配置,以确保它们在更新的计算环境下仍然兼容且有效。
- 有些参数(例如
wal_keep_size)可能会增加磁盘使用率,从而触发磁盘扩容,这也可能导致账单增加。