Fails to insert to a PostgreSQL master-slave config due to SequenceGenerator(NEXTVAL) issues #5347

Closed
opened 2026-01-22 15:05:09 +01:00 by admin · 1 comment
Owner

Originally created by @mkurzeja on GitHub (Dec 9, 2016).

Originally assigned to: @Ocramius on GitHub.

Background: I have a master-slave connection and I'm using auto-generated id for my entity;

Doctrine ORM fails to insert data because it tries to get the sequence NEXTVAL value from a slave.

public function generate(EntityManager $em, $entity)
    {
        if ($this->_maxValue === null || $this->_nextValue == $this->_maxValue) {
            // Allocate new values
            $conn = $em->getConnection();
            $sql  = $conn->getDatabasePlatform()->getSequenceNextValSQL($this->_sequenceName);

            $this->_nextValue = (int)$conn->fetchColumn($sql); //this line fails
            $this->_maxValue  = $this->_nextValue + $this->_allocationSize;
        }

        return $this->_nextValue++;
    }

PostgreSQL allows to fetch nextval only from a master server, and using fetchColumn runs the query using a slave connection.

I could start a transaction before, but I assume this case should be handled by ORM.

In case someone tries to reproduce this issue, just start a PostgreSQL cluster (docker-compose and https://hub.docker.com/r/bitnami/postgresql/ might be helpful) and try to persist any entity with an auto-generated ID.

Originally created by @mkurzeja on GitHub (Dec 9, 2016). Originally assigned to: @Ocramius on GitHub. Background: I have a master-slave connection and I'm using auto-generated id for my entity; Doctrine ORM fails to insert data because it tries to get the sequence NEXTVAL value from a slave. ```php public function generate(EntityManager $em, $entity) { if ($this->_maxValue === null || $this->_nextValue == $this->_maxValue) { // Allocate new values $conn = $em->getConnection(); $sql = $conn->getDatabasePlatform()->getSequenceNextValSQL($this->_sequenceName); $this->_nextValue = (int)$conn->fetchColumn($sql); //this line fails $this->_maxValue = $this->_nextValue + $this->_allocationSize; } return $this->_nextValue++; } ``` PostgreSQL allows to fetch nextval only from a master server, and using fetchColumn runs the query using a slave connection. I could start a transaction before, but I assume this case should be handled by ORM. In case someone tries to reproduce this issue, just start a PostgreSQL cluster (docker-compose and https://hub.docker.com/r/bitnami/postgresql/ might be helpful) and try to persist any entity with an auto-generated ID.
admin added the Bug label 2026-01-22 15:05:09 +01:00
admin closed this issue 2026-01-22 15:05:09 +01:00
Author
Owner

@Ocramius commented on GitHub (Jun 21, 2017):

Handled in #6168

@Ocramius commented on GitHub (Jun 21, 2017): Handled in #6168
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5347