Repository navigation
Add opt-in database creation strategies #1487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
cc5a2d3
185c397
e438c48
9eeb73a
6b1652a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -272,6 +272,95 @@ For async tests against an external server, create a client fixture with | |
|
|
||
| By default, it connects to ``127.0.0.1:5432``. | ||
|
|
||
| Choosing how test databases are cloned | ||
| -------------------------------------- | ||
|
|
||
| Use ``create_strategy`` on ``postgresql_proc`` or ``postgresql_noproc`` to choose | ||
| how synchronous and asynchronous client fixtures clone the session's template | ||
| database for each test. The setting only affects client database creation; | ||
| initial and layered template databases keep PostgreSQL's default behavior. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| postgresql_my_proc = factories.postgresql_proc(create_strategy="clone") | ||
| postgresql_my = factories.postgresql("postgresql_my_proc") | ||
|
|
||
| For an existing server, including PostgreSQL in Docker, configure the noproc | ||
| factory instead. No server configuration change is needed: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| postgresql_external = factories.postgresql_noproc(create_strategy="clone") | ||
| postgresql_my = factories.postgresql("postgresql_external") | ||
| postgresql_my_async = factories.postgresql_async("postgresql_external") | ||
|
|
||
| The same setting is available on the command line and in ``pytest.ini``: | ||
|
|
||
| .. code-block:: sh | ||
|
|
||
| pytest --postgresql-create-strategy=clone | ||
|
|
||
| .. code-block:: ini | ||
|
|
||
| [pytest] | ||
| postgresql_create_strategy = clone | ||
|
|
||
| The available values are: | ||
|
|
||
| * Unset (the default): no ``STRATEGY`` clause or session setting is sent, leaving | ||
| the choice to PostgreSQL. | ||
| * ``wal_log``: ``CREATE DATABASE ... STRATEGY WAL_LOG``. | ||
| * ``file_copy``: ``SET file_copy_method = copy``, followed by | ||
| ``CREATE DATABASE ... STRATEGY FILE_COPY`` on the same connection. | ||
| * ``clone``: ``SET file_copy_method = clone``, followed by | ||
| ``CREATE DATABASE ... STRATEGY FILE_COPY`` on the same connection. | ||
|
|
||
| The session setting applies immediately before database creation and does not | ||
| require a server restart. Explicit strategies require PostgreSQL support: | ||
| ``WAL_LOG`` requires PostgreSQL 15 or newer, while ``file_copy`` and ``clone`` | ||
| require PostgreSQL 18 or newer for ``file_copy_method``. PostgreSQL reports unsupported | ||
| settings or strategies directly; the plugin does not silently fall back to another | ||
| mode. The ``clone`` method must also be supported by the server's operating system. | ||
|
|
||
| Factory arguments take precedence over command line and ini options. With chained | ||
| fixtures, configure the ``postgresql_noproc`` factory used by the client: its | ||
| strategy comes from its own argument or the global configuration, rather than | ||
| inheriting the strategy of the fixture named by ``depends_on``. | ||
|
|
||
| Filesystem and performance considerations | ||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd trim this section and mostly defer to the official documentation where we can. This summary though useful belongs to some additonal pages of a documentation (we don't have, apart from README) or official documentation for the postgresql/filesystems.... :/ I'm just afraid it'll clutter the README
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Addressed in 9eeb73a. Removed the extended filesystem, Docker and benchmarking discussion. The README now keeps a short reflink/performance caveat and links to the official PostgreSQL CREATE DATABASE and file_copy_method documentation for the details. Applicable README pre-commit hooks (including rstcheck), source/wheel builds and PyPI README rendering pass. |
||
|
|
||
| On PostgreSQL 15 and newer, the default ``WAL_LOG`` strategy ignores | ||
| ``file_copy_method``. Setting that parameter on the server alone therefore does | ||
| not enable file-copy cloning; choose ``file_copy`` or ``clone`` explicitly. | ||
|
|
||
| ``clone`` can share file blocks when the PostgreSQL data directory's filesystem | ||
| supports reflinks, such as suitable XFS, Btrfs, or APFS configurations. Without | ||
| reflinks, the operating system may copy the data instead or reject the operation. | ||
| A successful ``clone`` request does not prove that disk blocks were shared. | ||
| Even with reflink support, template size and concurrent work can make another | ||
| strategy faster. This is why the option is opt-in and the default is unchanged. | ||
|
|
||
| Check the filesystem backing the server's actual data directory, which can be | ||
| found with ``SHOW data_directory``. In Docker, a volume, bind mount, and tmpfs can | ||
| have different behavior. Docker on macOS runs PostgreSQL in a Linux VM; a bind | ||
| mount of an APFS directory through file sharing is not equivalent to running | ||
| PostgreSQL directly on APFS. tmpfs is memory-backed and does not provide disk | ||
| reflinks; a fast ``clone`` result there is not evidence of reflink support. | ||
|
|
||
| ``FILE_COPY`` forces checkpoints before and after database creation, which can | ||
| cause contention between concurrent pytest-xdist workers. Compare the complete | ||
| test suite at its usual worker count, including database creation and cleanup, | ||
| and repeat each strategy on the same storage with the same template and server | ||
| settings. Keep failed or timed-out runs separate from successful timings. | ||
| Faster isolated database creation does not guarantee a faster test suite, and | ||
| results from one filesystem or worker count need not transfer to another. | ||
|
|
||
| For details, see PostgreSQL's `CREATE DATABASE strategy documentation | ||
| <https://www.postgresql.org/docs/18/sql-createdatabase.html>`_ and | ||
| `file_copy_method documentation | ||
| <https://www.postgresql.org/docs/18/runtime-config-resource.html#GUC-FILE-COPY-METHOD>`_. | ||
|
|
||
| Using a maintenance database other than ``postgres`` | ||
| ---------------------------------------------------- | ||
|
|
||
|
|
@@ -416,6 +505,12 @@ You can define settings via fixture factory arguments, command line options, or | |
| - --postgresql-load-autocommit | ||
| - postgresql_load_autocommit | ||
| - False | ||
| * - Test database cloning strategy | ||
| - create_strategy | ||
| - create_strategy | ||
| - --postgresql-create-strategy | ||
| - postgresql_create_strategy | ||
| - PostgreSQL's default | ||
| * - PostgreSQL connection options | ||
| - options | ||
| - options | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Add ``create_strategy`` to the process and noproc factories, with matching CLI | ||
| and ini options, to select PostgreSQL's default, ``wal_log``, ``file_copy``, or | ||
| the operating-system ``clone`` method when client fixtures copy the template | ||
| database. The default remains unchanged; cloning does not guarantee reflinks. |
Uh oh!
There was an error while loading. Please reload this page.