## Problem/Motivation
On Drupal 11.4 with PostgreSQL, saving a group membership throws
Drupal\Core\Database\TransactionNameNonUniqueException. The root cause is in the core pgsql driver
(core issue #2920527), but Group is the most common trigger because it issues database queries
from inside hook_query_*_alter().
## Environment
- Drupal 11.4.12
- PHP 8.5.12
- PostgreSQL
- group 4.0.0-alpha3
- group relationship plugin group_easy_entity:plan installed, with an existing relationship between a group and the content entity
## Steps to reproduce
$group = \Drupal::entityTypeManager()->getStorage('group')->load(1);
$user = \Drupal\user\Entity\User::create(['name' => 'probe', 'status' => 1]);
$user->save();
$group->addMember($user); // no explicit transaction needed
## Expected result
The membership is created.
## Actual result
Drupal\Core\Database\TransactionNameNonUniqueException: A transaction named mimic_implicit_commit is already in use.
MySQL is not affected.
## Root cause
1. SqlContentEntityStorage::save() opens a root transaction (Drupal 11.x).
2. GroupMembershipTrait::preSave() calls $this->validate().
3. ValidReferenceConstraintValidator -> DefaultSelection::validateReferenceableEntities() runs a group entity query tagged group_access.
4. pgsql Select::execute() creates the savepoint mimic_implicit_commit before preExecute() runs the query alters.
5. QueryHooks::entityQueryAlter() -> GroupQueryAlter::doAlter() -> GroupPermissionCalculator::calculateFullPermissions() -> IndividualGroupRoleAccessPolicy::calculatePermissions() -> GroupMembership::loadByUser() issues another entity query.
6. That nested query requests the same savepoint name again, and TransactionManagerBase::push() rejects duplicates.
A second, unconditional path: EntityQueryAlter::doAlter() runs a raw database select from inside the alter, which fails the same way regardless of cache state.
## Proposed resolution
The complete fix must land in core: the pgsql query classes are missing the !has('mimic_implicit_commit') guard that Connection::query() already has. See core #2920527, comment #115 for the 11.4 stable reproduction.
Group-side hardening while core is unfixed: in EntityQueryAlter::doAlter() use Connection::query() instead of a Select object, because Connection::query() skips the savepoint when one is already active:
$plugin_ids_in_use = $this->database->query(
'SELECT DISTINCT plugin_id FROM {' . $group_relationship_data_table . '} WHERE plugin_id IN (:plugin_ids[])',
[':plugin_ids[]' => $plugin_ids]
)->fetchCol();
Note: this only removes the EntityQueryAlter path. The GroupMembership::loadByUser() path (cold permission cache) still fails until core is fixed.
## Remaining tasks
- Add a kernel test on PostgreSQL: $group->addMember($user) must succeed.
- Consider avoiding queries from inside query alters where feasible.
## Related
- Drupal core #2920527
- Drupal core #3523409
Comments
Comment #2
qiutuo commented