Postgresql 中文操作指南

33.1. Running the Tests #

可以针对已经安装并运行的服务器运行回归测试,也可以使用构建树中的临时安装运行回归测试。此外,有用于运行测试的“并行”和“顺序”模式。顺序方法单独运行每个测试脚本,而并行方法启动多个服务器进程以并行运行组测试。并行测试增加了进程间通信和锁定正常工作的信心。即使在“并行”模式中,某些测试也可能顺序运行,以防测试需要。

The regression tests can be run against an already installed and running server, or using a temporary installation within the build tree. Furthermore, there is a “parallel” and a “sequential” mode for running the tests. The sequential method runs each test script alone, while the parallel method starts up multiple server processes to run groups of tests in parallel. Parallel testing adds confidence that interprocess communication and locking are working correctly. Some tests may run sequentially even in the “parallel” mode in case this is required by the test.

33.1.1. Running the Tests Against a Temporary Installation #

要在构建后但在安装前运行并行回归测试,请键入:

To run the parallel regression tests after building but before installation, type:

make check

在顶层目录中。(或者,您可以切换到 src/test/regress,然后在此处运行命令。)并行运行的测试带有“+”前缀,顺序运行的测试带有“-”前缀。在最后,您应该看到以下内容:

in the top-level directory. (Or you can change to src/test/regress and run the command there.) Tests which are run in parallel are prefixed with “+”, and tests which run sequentially are prefixed with “-”. At the end you should see something like:

# All 213 tests passed.

或者别的关于哪个测试失败的注释。在假设“失败”表示严重问题之前,请参阅以下 Section 33.2

or otherwise a note about which tests failed. See Section 33.2 below before assuming that a “failure” represents a serious problem.

由于此测试方法运行了一个临时服务器,如果您以 root 用户身份进行构建,它将不起作用,因为服务器将不会以 root 身份启动。建议的步骤是不以 root 身份进行构建,或者在完成安装后进行测试。

Because this test method runs a temporary server, it will not work if you did the build as the root user, since the server will not start as root. Recommended procedure is not to do the build as root, or else to perform testing after completing the installation.

如果您已将 PostgreSQL 配置为安装到已有 PostgreSQL 旧版本安装位置,然后在安装新版本之前执行 make check,您可能会发现由于新程序尝试使用已安装的共享库而导致测试失败。(常见症状是有关未定义符号的抱怨。)如果您想在覆盖旧安装之前运行测试,您需要使用 configure --disable-rpath 进行构建。但是,不建议您在最终安装中使用此选项。

If you have configured PostgreSQL to install into a location where an older PostgreSQL installation already exists, and you perform make check before installing the new version, you might find that the tests fail because the new programs try to use the already-installed shared libraries. (Typical symptoms are complaints about undefined symbols.) If you wish to run the tests before overwriting the old installation, you’ll need to build with configure --disable-rpath. It is not recommended that you use this option for the final installation, however.

并行回归测试会以您的用户 ID 运行许多进程。目前,最大并行度是 20 个并行测试脚本,这意味着 40 个进程:每个测试脚本有一个服务器进程和一个 psql 进程。因此,如果您的系统针对每个用户强制限制进程数,请确保此限制至少为 50 左右,否则在并行测试中您可能会遇到看似随机的故障。如果您无法提高限制,您可以通过设置 MAX_CONNECTIONS 参数来降低并行度。例如:

The parallel regression test starts quite a few processes under your user ID. Presently, the maximum concurrency is twenty parallel test scripts, which means forty processes: there’s a server process and a psql process for each test script. So if your system enforces a per-user limit on the number of processes, make sure this limit is at least fifty or so, else you might get random-seeming failures in the parallel test. If you are not in a position to raise the limit, you can cut down the degree of parallelism by setting the MAX_CONNECTIONS parameter. For example:

make MAX_CONNECTIONS=10 check

运行不超过 10 个并发测试。

runs no more than ten tests concurrently.

33.1.2. Running the Tests Against an Existing Installation #

要在安装后运行测试(请参见 Chapter 17),请初始化数据目录并按照 Chapter 19 中的说明启动服务器,然后输入:

To run the tests after installation (see Chapter 17), initialize a data directory and start the server as explained in Chapter 19, then type:

make installcheck

或者对于并行测试:

or for a parallel test:

make installcheck-parallel

除非 PGHOSTPGPORT 环境变量另有指示,否则,测试预计在本地主机和默认端口号上与服务器联系。测试将在名为 regression 的数据库中运行;将删除此名称的任何现有数据库。

The tests will expect to contact the server at the local host and the default port number, unless directed otherwise by PGHOST and PGPORT environment variables. The tests will be run in a database named regression; any existing database by this name will be dropped.

测试还将临时创建一些集群范围的对象,如角色、表空间和订阅。这些对象的名称将以 regress_ 开头。当使用 installcheck 模式而安装中有任何这样命名的实际全局对象时,请小心。

The tests will also transiently create some cluster-wide objects, such as roles, tablespaces, and subscriptions. These objects will have names beginning with regress_. Beware of using installcheck mode with an installation that has any actual global objects named that way.

33.1.3. Additional Test Suites #

make checkmake installcheck 命令仅运行“核心”回归测试,它测试 PostgreSQL 服务器的内置功能。源代码发行版包含许多其他测试套件,其中大多数与附加功能(如可选过程语言)相关。

The make check and make installcheck commands run only the “core” regression tests, which test built-in functionality of the PostgreSQL server. The source distribution contains many additional test suites, most of them having to do with add-on functionality such as optional procedural languages.

要运行适用于已选择构建的模块的所有测试套件(包括核心测试),请在构建树的顶部键入以下命令之一:

To run all test suites applicable to the modules that have been selected to be built, including the core tests, type one of these commands at the top of the build tree:

make check-world
make installcheck-world

这些命令分别使用临时服务器或已安装的服务器运行测试,就像之前针对 make checkmake installcheck 说明的那样。其他注意事项与之前为每种方法说明的相同。请注意,make check-world 为每个测试模块构建一个单独实例(临时数据目录),因此它需要比 make installcheck-world 更多的时间和磁盘空间。

These commands run the tests using temporary servers or an already-installed server, respectively, just as previously explained for make check and make installcheck. Other considerations are the same as previously explained for each method. Note that make check-world builds a separate instance (temporary data directory) for each tested module, so it requires more time and disk space than make installcheck-world.

在具有多核 CPU 且无严格操作系统限制的现代机器上,您可以使用并行性使事情进行得更快。大多数 PostgreSQL 开发人员为运行所有测试实际使用的配方类似于

On a modern machine with multiple CPU cores and no tight operating-system limits, you can make things go substantially faster with parallelism. The recipe that most PostgreSQL developers actually use for running all tests is something like

make check-world -j8 >/dev/null

-j 限制接近于或略高于可用内核的数量。丢弃 stdout 即可消除在您只想验证成功时不感兴趣的闲聊。(在失败的情况下,stderr 消息通常足以确定在何处进行更仔细的检查。)

with a -j limit near to or a bit more than the number of available cores. Discarding stdout eliminates chatter that’s not interesting when you just want to verify success. (In case of failure, the stderr messages are usually enough to determine where to look closer.)

或者,您也可以在构建树的相应子目录中输入 make checkmake installcheck 来运行单个测试套件。请牢记,make installcheck 假设您已安装相关模块,而不仅安装了核心服务器。

Alternatively, you can run individual test suites by typing make check or make installcheck in the appropriate subdirectory of the build tree. Keep in mind that make installcheck assumes you’ve installed the relevant module(s), not only the core server.

可以通过这种方式调用的其他测试包括:

The additional tests that can be invoked this way include:

在使用 installcheck 模式时,这些测试会创建并销毁名称包含 regression 的测试数据库,例如 pl_regressioncontrib_regression。当使用 installcheck 模式而安装中有任何此类命名的非测试数据库时,请小心。

When using installcheck mode, these tests will create and destroy test databases whose names include regression, for example pl_regression or contrib_regression. Beware of using installcheck mode with an installation that has any non-test databases named that way.

其中一些辅助测试套件使用 Section 33.4 中说明的 TAP 基础设施。仅当 PostgreSQL 使用 —​enable-tap-tests 选项进行配置时,才运行基于 TAP 的测试。建议进行开发,但如果安装不适合,则可以忽略。

Some of these auxiliary test suites use the TAP infrastructure explained in Section 33.4. The TAP-based tests are run only when PostgreSQL was configured with the option —​enable-tap-tests. This is recommended for development, but can be omitted if there is no suitable Perl installation.

默认情况下并不运行一些测试套件,它们之所以没有默认运行,要么是因为在多用户系统中运行不安全,要么是因为需要特殊软件,要么是因为它们消耗资源。您可以通过将 make 或环境变量 PG_TEST_EXTRA 设置为以空格分隔的列表来决定另外运行哪些测试套件,例如:

Some test suites are not run by default, either because they are not secure to run on a multiuser system, because they require special software or because they are resource intensive. You can decide which test suites to run additionally by setting the make or environment variable PG_TEST_EXTRA to a whitespace-separated list, for example:

make check-world PG_TEST_EXTRA='kerberos ldap ssl load_balance'

当前支持以下值:

The following values are currently supported:

  • kerberos

    • Runs the test suite under src/test/kerberos. This requires an MIT Kerberos installation and opens TCP/IP listen sockets.

  • ldap

    • Runs the test suite under src/test/ldap. This requires an OpenLDAP installation and opens TCP/IP listen sockets.

  • ssl

    • Runs the test suite under src/test/ssl. This opens TCP/IP listen sockets.

  • load_balance

    • Runs the test src/interfaces/libpq/t/004_load_balance_dns.pl. This requires editing the system hosts file and opens TCP/IP listen sockets.

  • wal_consistency_checking

    • Uses wal_consistency_checking=all while running certain tests under src/test/recovery. Not enabled by default because it is resource intensive.

不会运行当前构建配置不支持的特性测试,即使它们在 PG_TEST_EXTRA 中被提及。

Tests for features that are not supported by the current build configuration are not run even if they are mentioned in PG_TEST_EXTRA.

此外,还有 src/test/modules 中的测试将由 make check-world 运行,但不会由 make installcheck-world 运行。这是因为它们安装非生成扩展或有其他被认为对生成安装不希望的副作用。如果你愿意,你可以在那些子目录中的一个中使用 make installmake installcheck,但建议不要在非测试服务器上这样做。

In addition, there are tests in src/test/modules which will be run by make check-world but not by make installcheck-world. This is because they install non-production extensions or have other side-effects that are considered undesirable for a production installation. You can use make install and make installcheck in one of those subdirectories if you wish, but it’s not recommended to do so with a non-test server.

33.1.4. Locale and Encoding #

默认情况下,使用临时安装的测试使用当前环境中定义的区域设置和由 initdb 确定的相应数据库编码。通过设置适当的环境变量,测试不同的区域设置很有用,例如:

By default, tests using a temporary installation use the locale defined in the current environment and the corresponding database encoding as determined by initdb. It can be useful to test different locales by setting the appropriate environment variables, for example:

make check LANG=C
make check LC_COLLATE=en_US.utf8 LC_CTYPE=fr_CA.utf8

出于实现的原因,为此目的设置 LC_ALL 无法起作用;所有其他与区域设置相关的环境变量都可起作用。

For implementation reasons, setting LC_ALL does not work for this purpose; all the other locale-related environment variables do work.

针对现有安装进行测试时,区域设置由现有数据库集群确定,并且不能为测试运行另外设置。

When testing against an existing installation, the locale is determined by the existing database cluster and cannot be set separately for the test run.

你还可以通过设置变量 ENCODING 显式选择数据库编码,例如:

You can also choose the database encoding explicitly by setting the variable ENCODING, for example:

make check LANG=C ENCODING=EUC_JP

通常只有当区域设置为 C 时,通过这种方式设置数据库编码才有意义;否则,将自动从区域设置中选择编码,而指定与区域设置不匹配的编码将导致错误。

Setting the database encoding this way typically only makes sense if the locale is C; otherwise the encoding is chosen automatically from the locale, and specifying an encoding that does not match the locale will result in an error.

尽管在后一种情况下它必须与安装的区域设置兼容,但可以针对临时或现有安装设置数据库编码。

The database encoding can be set for tests against either a temporary or an existing installation, though in the latter case it must be compatible with the installation’s locale.

33.1.5. Custom Server Settings #

在运行回归测试套件时使用的自定义服务器设置可以在 PGOPTIONS 环境变量中设置(对于允许此设置的设置):

Custom server settings to use when running a regression test suite can be set in the PGOPTIONS environment variable (for settings that allow this):

make check PGOPTIONS="-c debug_parallel_query=regress -c work_mem=50MB"

在针对临时安装运行时,还可以通过提供一个预先编写的 postgresql.conf 设置自定义设置:

When running against a temporary installation, custom settings can also be set by supplying a pre-written postgresql.conf:

echo 'log_checkpoints = on' > test_postgresql.conf
echo 'work_mem = 50MB' >> test_postgresql.conf
make check EXTRA_REGRESS_OPTS="--temp-config=test_postgresql.conf"

这样做非常有用,可以启用额外的日志记录,调整资源限制,或启用额外的运行时检查,如 debug_discard_caches

This can be useful to enable additional logging, adjust resource limits, or enable extra run-time checks such as debug_discard_caches.

33.1.6. Extra Tests #

核心回归测试套件包含一些默认情况下不会运行的测试文件,因为它们可能是平台相关的或运行时间非常长。你可以通过设置变量 EXTRA_TESTS 运行这些或其他额外的测试文件。例如,运行 numeric_big 测试:

The core regression test suite contains a few test files that are not run by default, because they might be platform-dependent or take a very long time to run. You can run these or other extra test files by setting the variable EXTRA_TESTS. For example, to run the numeric_big test:

make check EXTRA_TESTS=numeric_big