Problem/Motivation

Truncate::__toString() in core/lib/Drupal/Core/Database/Query/Truncate.php emits DELETE FROM instead of TRUNCATE when a transaction is active. On MySQL, TRUNCATE is a DDL statement and causes an implicit commit, so the fallback is needed there. On PostgreSQL, TRUNCATE is fully transactional and can be rolled back. The pgsql driver inherits the fallback anyway.

DELETE FROM is slow on large tables. Each deleted row also becomes a dead tuple that autovacuum must clean up. Frequent cache table clears create this vacuum load on PostgreSQL sites.

Proposed resolution

Override __toString() in the pgsql driver's Truncate class to always emit TRUNCATE, also inside transactions.

Caveats: TRUNCATE takes an ACCESS EXCLUSIVE lock on the table, held until the transaction ends. It is not MVCC-safe for concurrent transactions using a snapshot taken before the truncation. The core docblock already describes the lock.

Steps to reproduce

On PostgreSQL, run a truncate query inside a transaction and log the queries. A DELETE FROM statement runs.

Remaining tasks

Review.

API changes

None.

Data model changes

None.

For the committer

The changes to the .gitlab-ci.yml file need to be removed before merging!

Issue fork drupal-3615225

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

daffie created an issue. See original summary.

daffie’s picture

Issue summary: View changes

Updated the IS.

daffie’s picture

Status: Active » Needs review

Ready for a review.

daffie’s picture

Disclosure: I have used AI for the PR, the IS and the CR.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Note: Gitlab file should not be converted.

Reviewing the changes I actually have no feedback. Lets give it a shot.

  • catch committed 7bc7faf1 on 11.x
    task: #3615225 Truncate queries on PostgreSQL do not need to fallback to...

  • catch committed d54d3fe0 on main
    task: #3615225 Truncate queries on PostgreSQL do not need to fallback to...
catch’s picture

Version: main » 11.x-dev
Status: Reviewed & tested by the community » Fixed

I didn't know this about PostgreSQL, but the logic looks sound and the code comments explain it well. Committed/pushed to main and 11.x, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.