Skip to content

Connection/Cursor already closed #6228

Open

Description

@wahmedswl

Hi!

We have python-based models which pull data in pre-statements via duckdb; it seems like if it takes > 30m, it started to throw this error.

image

Is there anyway we can avoid this because as dataset growing; time to pull is increasing.

Thanks

Activity

  1. mday-io commented on Nov 24, 2026

    @mday-io
    Collaborator

    Thanks for the report. From the screenshot, your target is Postgres, and the errors come from psycopg2. SQLMesh keeps the model's Postgres transaction open while its pre-statements and Python code run. When your pull takes 30+ minutes, that connection sits idle the whole time, and something closes it around the 30-minute mark. That's why the slow model fails with connection already closed.

    The cursor already closed errors on your other models were a separate bug, fixed in #6249. After one failed transaction, SQLMesh kept reusing the dead connection instead of reconnecting. Once that's released, only the slow model should fail.

    To fix the slow model, we need to find what's closing the connection. Could you run this as the user SQLMesh connects with?

    SHOW idle_in_transaction_session_timeout;

    If it's 30 minutes, you can raise it for that user, for example ALTER ROLE <user> SET idle_in_transaction_session_timeout = '2h';. If it's 0, something between SQLMesh and the database is more likely dropping idle connections, such as PgBouncer, a proxy, or a firewall. In that case, setting keepalives_idle in your SQLMesh Postgres connection config, for example to 60, may keep it alive.

    Which Postgres are you running (self-hosted, RDS, Azure, or something else), and is there a pooler in front of it?

  2. wahmedswl commented on Nov 24, 2026

    @wahmedswl
    Author

    @mday-io its 10m; we have self-hosted pigsty installation on azure. I guess we had models taking more than 10m, but its possible that they were not taking more than 10m in pre-statement.

    image

    We use duckdb in pre-statement to push from mssql to parquet; and then in post-statement parquet to pg; right now we are using this to avoid this, not sure if it works or not because we have improved speed of the source.

    image
  3. mday-io commented on Nov 24, 2026

    @mday-io
    Collaborator

    Thanks, that's the cause. With idle_in_transaction_session_timeout at 10 minutes, Postgres closes any session that sits inside an open transaction for 10 minutes without running a statement. SQLMesh opens the model's transaction before the pre-statements run, so your MSSQL → Parquet copy in DuckDB leaves that transaction idle until Postgres closes it. Only the gaps between statements count, which is why models that take a long time overall don't hit it.

    Your heartbeat should work, since each SELECT 1 resets the timer. One catch: if a beat fails, the thread stops quietly and the model will fail again at 10 minutes. A simpler fix is to raise the timeout for the user SQLMesh connects as:

    ALTER ROLE <sqlmesh_user> SET idle_in_transaction_session_timeout = '2h';

    That only affects SQLMesh's sessions, and you can remove the heartbeat once it's in place.

    The cursor already closed errors on your other models were a separate SQLMesh bug, fixed in #6249. They'll go away once that's released. I'll close this once you've confirmed the slow model runs, unless you're still seeing failures after that.

  4. wahmedswl commented on Nov 24, 2026

    @wahmedswl
    Author

    @mday-io Is there any ETA when new version will be released with the fix? That's great, fix on the pg side is better.

  5. mday-io commented on Nov 24, 2026

    @mday-io
    Collaborator

    To be clear, the release won't fix the slow model. #6249 only stops the cursor already closed errors on the models that run after it. The slow model will still hit the 10-minute idle_in_transaction_session_timeout until something changes.

    If you'd rather not change the role setting, you can raise it only for SQLMesh's session by adding this as the model's first pre-statement:

    SET idle_in_transaction_session_timeout = '2h';

    Another option is to run the MSSQL → Parquet copy before sqlmesh run, so no transaction is open while it runs.

    If you'd like SQLMesh to avoid holding the transaction open during long pre-statements, feel free to open a feature request for that. #6249 is merged and will ship in the next release. I don't have a date yet.

  6. wahmedswl commented on Nov 24, 2026

    @wahmedswl
    Author

    @mday-io what does mean by

    Another option is to run the MSSQL → Parquet copy before sqlmesh run, so no transaction is open while it runs.

    @tobi mentioned this pattern so we adopted it. Because we want model to be maintained by sqlmesh

  7. mday-io commented on Nov 24, 2026

    @mday-io
    Collaborator

    @wahmedswl I meant running the copy as a separate job before calling sqlmesh run, outside SQLMesh. You're right that it gives up having SQLMesh manage the model, so skip that option.

    Keeping the copy in the pre-statement is fine. Add this as the model's first pre-statement, before the DuckDB copy:

    SET idle_in_transaction_session_timeout = '2h';

    It only changes the limit for SQLMesh's own Postgres session, so the 10-minute limit stays in place for everyone else. I checked it against Postgres 18. With the setting at a few seconds, an idle transaction got closed, and with this SET run first, it survived. You can drop the heartbeat once it's in place.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions