Postgresql 中文操作指南
Synopsis
SET [ SESSION | LOCAL ] configuration_parameter { TO | = } { value | 'value' | DEFAULT }
SET [ SESSION | LOCAL ] TIME ZONE { value | 'value' | LOCAL | DEFAULT }
Description
SET 命令更改运行时配置参数。 Chapter 20 中列出的许多运行时参数都可以使用 SET 即时更改。(有些参数只能由超级用户和在该参数上已授予 SET 权限的用户更改。服务器或会话启动后也不能更改的另有一些参数。) SET 仅影响当前会话所用的值。
The SET command changes run-time configuration parameters. Many of the run-time parameters listed in Chapter 20 can be changed on-the-fly with SET. (Some parameters can only be changed by superusers and users who have been granted SET privilege on that parameter. There are also parameters that cannot be changed after server or session start.) SET only affects the value used by the current session.
如果 SET (或等效的 SET SESSION )是在稍后中止的事务中发出的,那么 SET 命令的效果将在事务回滚时消失。一旦围绕事务提交,除非由另一个 SET 覆盖,否则这些效果将持续到会话结束。
If SET (or equivalently SET SESSION) is issued within a transaction that is later aborted, the effects of the SET command disappear when the transaction is rolled back. Once the surrounding transaction is committed, the effects will persist until the end of the session, unless overridden by another SET.
SET LOCAL 的效果仅持续到当前事务结束,无论事务是否已提交。一个特例是单个事务中 SET 后面跟 SET LOCAL : SET LOCAL 的值将在事务结束前可见,但此后(如果已提交事务), SET 值会生效。
The effects of SET LOCAL last only till the end of the current transaction, whether committed or not. A special case is SET followed by SET LOCAL within a single transaction: the SET LOCAL value will be seen until the end of the transaction, but afterwards (if the transaction is committed) the SET value will take effect.
SET 或 SET LOCAL 的效果也通过回滚到早于命令的保存点而取消。
The effects of SET or SET LOCAL are also canceled by rolling back to a savepoint that is earlier than the command.
如果 SET LOCAL 用于对同一变量具有 SET 选项的函数中(请参阅 CREATE FUNCTION ),那么 SET LOCAL 命令的效果将在函数退出时消失;也就是说,无论如何都会还原调用函数时有效的值。这允许 SET LOCAL 用于函数中参数的动态或重复更改,同时仍然可以使用 SET 选项保存和还原调用方的值。但是,一个常规 SET 命令会覆盖任何周围函数的 SET 选项;它的效果将持续有效,除非回滚。
If SET LOCAL is used within a function that has a SET option for the same variable (see CREATE FUNCTION), the effects of the SET LOCAL command disappear at function exit; that is, the value in effect when the function was called is restored anyway. This allows SET LOCAL to be used for dynamic or repeated changes of a parameter within a function, while still having the convenience of using the SET option to save and restore the caller’s value. However, a regular SET command overrides any surrounding function’s SET option; its effects will persist unless rolled back.
Note
在 PostgreSQL 版本 8.0 到 8.2 中,释放较早的保存点,或成功退出 PL/pgSQL 异常块会取消 SET LOCAL 的效果。这种行为已被更改,因为它被认为不直观。
In PostgreSQL versions 8.0 through 8.2, the effects of a SET LOCAL would be canceled by releasing an earlier savepoint, or by successful exit from a PL/pgSQL exception block. This behavior has been changed because it was deemed unintuitive.
Parameters
-
SESSION
-
Specifies that the command takes effect for the current session. (This is the default if neither SESSION nor LOCAL appears.)
-
-
LOCAL
-
Specifies that the command takes effect for only the current transaction. After COMMIT or ROLLBACK, the session-level setting takes effect again. Issuing this outside of a transaction block emits a warning and otherwise has no effect.
-
-
configuration_parameter
-
Name of a settable run-time parameter. Available parameters are documented in Chapter 20 and below.
-
-
value
-
New value of parameter. Values can be specified as string constants, identifiers, numbers, or comma-separated lists of these, as appropriate for the particular parameter. DEFAULT can be written to specify resetting the parameter to its default value (that is, whatever value it would have had if no SET had been executed in the current session).
-
除了在 Chapter 20 中记录的配置参数之外,还有一些参数只能使用 SET 命令调整或具有特殊语法:
Besides the configuration parameters documented in Chapter 20, there are a few that can only be adjusted using the SET command or that have a special syntax:
-
SCHEMA
-
SET SCHEMA '_value'_ is an alias for SET search_path TO _value_. Only one schema can be specified using this syntax.
-
-
NAMES
-
_SET NAMES _value is an alias for _SET client_encoding TO _value.
-
-
SEED
-
Sets the internal seed for the random number generator (the function random). Allowed values are floating-point numbers between -1 and 1 inclusive.
-
The seed can also be set by invoking the function setseed:
-
SELECT setseed(value);
-
TIME ZONE
-
SET TIME ZONE '_value'_ is an alias for SET timezone TO '_value'. The syntax _SET TIME ZONE allows special syntax for the time zone specification. Here are examples of valid values:
-
Timezone settings given as numbers or intervals are internally translated to POSIX timezone syntax. For example, after SET TIME ZONE -7, SHOW TIME ZONE would report ←07>+07.
-
Time zone abbreviations are not supported by SET; see Section 8.5.3 for more information about time zones.
-
-
'PST8PDT'
-
The time zone for Berkeley, California.
-
-
'Europe/Rome'
-
The time zone for Italy.
-
-
-7
-
The time zone 7 hours west from UTC (equivalent to PDT). Positive values are east from UTC.
-
-
INTERVAL '-08:00' HOUR TO MINUTE
-
The time zone 8 hours west from UTC (equivalent to PST).
-
-
LOCAL__DEFAULT
-
Set the time zone to your local time zone (that is, the server’s default value of timezone).
-
Notes
函数 set_config 提供同等的功能;请参见 Section 9.27.1 。此外,可以 UPDATE pg_settings 系统视图来执行相当于 SET 的操作。
The function set_config provides equivalent functionality; see Section 9.27.1. Also, it is possible to UPDATE the pg_settings system view to perform the equivalent of SET.
Examples
设置模式搜索路径:
Set the schema search path:
SET search_path TO my_schema, public;
将日期样式设置为 POSTGRES traditional,输入惯例为“日在前,月在后”:
Set the style of date to traditional POSTGRES with “day before month” input convention:
SET datestyle TO postgres, dmy;
设置加州伯克利时区:
Set the time zone for Berkeley, California:
SET TIME ZONE 'PST8PDT';
设置意大利时区:
Set the time zone for Italy:
SET TIME ZONE 'Europe/Rome';
Compatibility
SET TIME ZONE 扩展了 SQL 标准中定义的语法。该标准只允许数字时区偏移量,而 PostgreSQL 允许更为灵活的时区规范。所有其他 SET 特性都是 PostgreSQL 扩展。
SET TIME ZONE extends syntax defined in the SQL standard. The standard allows only numeric time zone offsets while PostgreSQL allows more flexible time-zone specifications. All other SET features are PostgreSQL extensions.