Skip to content

[TASK] Reuse sequence metadata in database snapshots - #757

Open
wouter90 wants to merge 1 commit into
TYPO3:mainfrom
wouter90:perf/snapshot-sequence-metadata
Open

wouter90 wants to merge 1 commit into
TYPO3:mainfrom
wouter90:perf/snapshot-sequence-metadata

Conversation

@wouter90

Copy link
Copy Markdown
Contributor

Record auto-increment column names during snapshot export and pass them to the existing sequence reset helper when restoring rows. Skip sequence resets for tables without an auto-increment column.

This avoids repeated PostgreSQL catalog queries and restores generated IDs correctly for quoted mixed-case columns. Add functional coverage for ordinary and custom ID columns, repeated restores and non-sequence tables.

A local PostgreSQL 17 benchmark of SlugLinkGeneratorTest drops from 46.93 s to 42.37 s with the same 194 tests and 202 assertions. The number of sequence catalog lookups drops from 2,123 to zero.

Record auto-increment column names during snapshot export and pass them
to the existing sequence reset helper when restoring rows. Skip sequence
resets for tables without an auto-increment column.

This avoids repeated PostgreSQL catalog queries and restores generated
IDs correctly for quoted mixed-case columns. Add functional coverage for
ordinary and custom ID columns, repeated restores and non-sequence tables.

A local PostgreSQL 17 benchmark of SlugLinkGeneratorTest drops from
46.93 s to 42.37 s with the same 194 tests and 202 assertions. The number
of sequence catalog lookups drops from 2,123 to zero.

WIP: prepared for CI validation.

@sbuerk sbuerk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Wouter, skipping the catalog lookup on every restore makes a lot of sense.

I reviewed the change and ran it against PostgreSQL 17, MariaDB 10.11 and SQLite. Summary:

What works

  • The mixed-case fix is real. On main the data set "quoted mixed-case column" fails on PostgreSQL with relation "public.tx_testingframework_sequence_entryid_seq" does not exist, because the old path passes an unquoted sequence name to setval(). pg_get_serial_sequence() takes the column name literally, so the PR fixes that.
  • Changing the export format is safe. DatabaseSnapshot keeps the export in memory for one test case, and SQLite snapshots are file copies, so an export without autoIncrementColumnName can never be imported.
  • The tests pass on all three platforms with the PR code.
  • A quick probe (4 tables with 20 rows each, 50 restores, PostgreSQL 17) shows the import going from 111.5 ms to 101.1 ms on average, which matches your ~10 %.

Regression: SERIAL columns on PostgreSQL (see inline comment)

DBAL 4 and TYPO3's own PostgreSQLSchemaManager only report autoincrement for identity columns (attidentity = 'd'). A table with a classic SERIAL column now gets no sequence reset after a restore, and the next generated insert collides with the restored IDs. main handles that through the catalog lookup.

Blocker: the new functional test is never executed

Same as in #758: this repository has no functional suite. Build/phpunit/ only has UnitTests.xml, runTests.sh only knows -s unit, CI runs unit tests only, and the Core does not pick up vendor tests. Running it directly stops in bootstrap with Unable to determine path to entry script ... 'TYPO3_PATH_ROOT'. So CI only shows that the file lints and passes PHPStan. I'd like to add a functional suite (phpunit config, runTests.sh -s functional with database containers and the Core's not-<dbms> exclude groups, CI job) in a separate PR first, and then rebase this one and #758 on top. Nice side note: this test is platform agnostic and needs no groups at all.

Further points

  • The commit body ends with WIP: prepared for CI validation., which is not in the PR description and should not land.
  • Two body lines are longer than 72 characters ("ordinary and custom ID columns, ..." and "46.93 s to 42.37 s with the same ..."). Footer Releases: main, 10 is missing.
  • Please run the Core functional suite on PostgreSQL with this change, as CONTRIBUTING.rst asks, and add the result (tests and assertions, same as without the change) to the description.

Not mergeable yet from my side because of the SERIAL regression, the commit message and the missing functional suite wiring.

}
$autoIncrementColumnName = null;
foreach ($table->getColumns() as $column) {
if ($column->getAutoincrement()) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On PostgreSQL getAutoincrement() is only true for identity columns: 'autoincrement' => $tableColumn['attidentity'] === 'd' in DBAL 4.4 and in TYPO3\CMS\Core\Database\Schema\SchemaManager\PostgreSQLSchemaManager. A SERIAL column is not detected, so importTable() skips the reset and the sequence keeps its old position.

Reproduced on PostgreSQL 17:

$connection->executeStatement('CREATE TABLE tx_probe_serial (uid SERIAL PRIMARY KEY, title VARCHAR(20))');
$connection->insert('tx_probe_serial', ['uid' => 1, 'title' => 'a']);
$connection->insert('tx_probe_serial', ['uid' => 2, 'title' => 'b']);
$snapshot = $subject->export();
$connection->executeStatement('TRUNCATE tx_probe_serial RESTART IDENTITY');
$subject->import($snapshot);
$connection->insert('tx_probe_serial', ['title' => 'generated']);
// PR:   SQLSTATE[23505] duplicate key value violates unique constraint "tx_probe_serial_pkey"
// main: uid 3

The TYPO3 schema itself uses identity columns since DBAL 4, so the default tables are fine. But tests and extensions creating tables with raw SQL still use SERIAL a lot (the tests in #758 do), and on main this works.

Suggestion that keeps the gain: on PostgreSQL resolve the owned sequence column once in export() (which runs once per test case) with one catalog query for all tables, for example pg_depend with deptype IN ('a', 'i') joined to pg_attribute. That covers SERIAL and identity columns, and import() stays free of catalog lookups. Please add a SERIAL data set to the test as well.

use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Snapshot\DatabaseAccessor;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

final class DatabaseAccessorSequenceTest extends FunctionalTestCase

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the summary: nothing in this repository executes Tests/Functional/. With a manual harness it passes for me on PostgreSQL 17, MariaDB 10.11 and SQLite, and on main it fails on PostgreSQL for the mixed-case data set, so the test is good. It just isn't run by CI yet.

$connection->insert($table->getName(), [$columnName => 7, 'title' => 'first']);
$connection->insert($table->getName(), [$columnName => 11, 'title' => 'last']);
$subject = new DatabaseAccessor($connection);
$snapshot = json_decode(json_encode($subject->export(), JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: DatabaseSnapshot keeps the export as a PHP array in memory and only uses json_encode() for the size check, so this round trip is not what happens in production and suggests that snapshots get serialized. Passing $subject->export() directly (same in line 82) mirrors DatabaseSnapshot::create() and restore().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants