Skip to content

[TASK] Skip unchanged PostgreSQL tables during database reset - #758

Open
wouter90 wants to merge 1 commit into
TYPO3:mainfrom
wouter90:perf/postgres-selective-reset
Open

wouter90 wants to merge 1 commit into
TYPO3:mainfrom
wouter90:perf/postgres-selective-reset

Conversation

@wouter90

Copy link
Copy Markdown
Contributor

Only truncate tables containing rows or changed owned sequences. Preserve sequence starts for serial and identity columns, including empty tables whose IDs have already been consumed. Keep the full reset for databases with foreign keys or enabled truncate triggers.

Bound table checks to 100 SELECT branches and resolve sequence owners against the current schema search path.

Add functional coverage for untouched tables, sequence state, multiple identity columns, schema changes, query batches and both fallbacks.

Repeated PostgreSQL 17 benchmarks improve WorkspacesModify by 8.3% and RootlineUtility by 15.7%, with identical tests and assertions.

Only truncate tables containing rows or changed owned sequences.
Preserve sequence starts for serial and identity columns, including
empty tables whose IDs have already been consumed. Keep the full
reset for databases with foreign keys or enabled truncate triggers.

Bound table checks to 100 SELECT branches and resolve sequence owners
against the current schema search path.

Add functional coverage for untouched tables, sequence state, multiple
identity columns, schema changes, query batches and both fallbacks.

Repeated PostgreSQL 17 benchmarks improve WorkspacesModify by 8.3%
and RootlineUtility by 15.7%, with identical tests and assertions.

@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, nice follow-up to #742.

I reviewed the change and also ran it against real PostgreSQL. Summary:

Code (Testbase::truncateAllTablesForPostgres()): looks correct to me.

  • The table keys built by the sequence query match what DBAL's PostgreSQLSchemaManager returns from listTableNames() (quote_ident() table name, schema prefix only outside current_schema()), so the sequence checks line up with the table list.
  • is_called OR last_value <> seqstart is exactly the state RESTART IDENTITY leaves behind, so skipping a table is safe for owned serial and identity sequences.
  • The full reset fallback for foreign keys and enabled TRUNCATE triggers keeps the old semantics where it matters.
  • Works on PostgreSQL 17.12 and 10.21.
  • Quick measurement on a minimal instance (33 tables, workspaces loaded, three rows inserted per round, 40 rounds, PostgreSQL 17): average reset 26.7 ms on main vs. 10.4 ms with this change.

Blocker: the new functional test is never executed.

This repository has no functional test suite. Build/phpunit/ only contains UnitTests.xml, Build/Scripts/runTests.sh only knows -s unit, and ci.yml runs composerUpdate, cgl, lint, phpstan and unit. The Core does not pick it up either, its FunctionalTests.xml only globs typo3/sysext/*/Tests/Functional/. So the green CI only says that the file lints and passes PHPStan. PostgreSqlDatabaseResetTest itself has never run in CI.

Running it by hand does not work out of the box either:

$ .Build/bin/phpunit -c Resources/Core/Build/FunctionalTests.xml Tests/Functional/
Unable to determine path to entry script. Please check your path or set an environment variable 'TYPO3_PATH_ROOT' to your root path.

TYPO3_PATH_ROOT points to <repo>/public, which does not exist here, and there is no database service.

With a hand-made harness (stub web root with an index.php, TYPO3_PATH_ROOT pointing to it, a postgres:17 container, the typo3Database* env vars) all 13 tests pass. With main's Testbase.php only resetLeavesUnusedTablesUntouched fails, as expected. So the tests themselves are fine, they are just not wired.

#757 has the same issue with DatabaseAccessorSequenceTest. I'd like to add a proper 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 #757 on top. Until then please don't describe the tests as functional coverage in the commit message, since nothing executes them.

Further points

  • Please run the Core functional suite on PostgreSQL with this change, as CONTRIBUTING.rst asks, and add the result (number of tests and assertions, same as without the change) to the description. The two per-class benchmarks are nice but don't show that the whole suite still behaves identically. #741 did that for MariaDB.
  • The commit subject has 61 characters, the TYPO3 rules aim for 52 at most. Suggestion: [TASK] Skip untouched PostgreSQL tables on reset. Footer Releases: main, 10. Both can also be adjusted on merge.
  • A few small things inline.

Not mergeable yet from my side because of the missing suite wiring and the missing Core PostgreSQL run.

Comment thread Classes/Core/Testbase.php
}

$sequenceRows = $connection->executeQuery(
"SELECT CASE WHEN N.nspname = current_schema() THEN quote_ident(T.relname)

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: this key has to match the format listTableNames() returns (PostgreSQLSchemaManager::selectTableNames() + _getPortableTableDefinition(): quote_ident(table_name), raw schema name, prefix only outside current_schema()). It does today, but if the two ever drift apart the sequence checks drop out silently and an empty table with a consumed sequence is no longer reset. Could you add a short comment saying the key mirrors the DBAL format?

use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
use TYPO3\TestingFramework\Core\Testbase;

final class PostgreSqlDatabaseResetTest 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 review summary: nothing in this repository executes Tests/Functional/. Neither runTests.sh nor CI nor the Core suite run this class. It passes for me on PostgreSQL 17 with a manual harness, but as it stands it is only checked by lint and PHPStan.

{
protected function setUp(): void
{
parent::setUp();

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.

Please drop the manual platform check in setUp(). TYPO3 Core development handles database specific functional tests with phpunit groups instead: runTests.sh -s functional -d <dbms> calls phpunit with --exclude-group not-${DBMS}, and a PostgreSQL only test is tagged with the groups of the platforms it must not run on (see for example typo3/sysext/core/Tests/Functional/Database/ConnectionTest.php):

#[Group('not-mariadb')]
#[Group('not-mysql')]
#[Group('not-sqlite')]
final class PostgreSqlDatabaseResetTest extends FunctionalTestCase

Once a functional suite gets added to this repository, it should follow that same convention, so tests here look and behave like Core tests. As a side effect this also avoids the current cost: parent::setUp() builds the complete instance and database before the platform check, so on MySQL/MariaDB/SQLite each test pays the full setup only to be skipped.

With the check gone, the $this->get(ConnectionPool::class) vs $this->getConnectionPool() inconsistency disappears as well.

$connection->executeStatement('CREATE TABLE tx_reset_audit (message TEXT)');
$connection->executeStatement("CREATE FUNCTION tx_reset_log() RETURNS TRIGGER AS $$ BEGIN INSERT INTO tx_reset_audit VALUES ('fired'); RETURN NULL; END $$ LANGUAGE plpgsql");
try {
$connection->executeStatement('CREATE TRIGGER tx_reset_truncated AFTER TRUNCATE ON tx_reset_trigger FOR EACH STATEMENT EXECUTE FUNCTION tx_reset_log()');

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.

EXECUTE FUNCTION needs PostgreSQL 11+. On PostgreSQL 10 this test errors with syntax error at or near "FUNCTION" (checked with postgres:10, the production code itself works there). EXECUTE PROCEDURE is accepted by all versions.

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