You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
@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.
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.
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.
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.
@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.
Activity
mday-io commentedon Nov 24, 2026
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 closederrors 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?
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, settingkeepalives_idlein 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?
wahmedswl commentedon Nov 24, 2026
@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.
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.
mday-io commentedon Nov 24, 2026
Thanks, that's the cause. With
idle_in_transaction_session_timeoutat 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 1resets 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:That only affects SQLMesh's sessions, and you can remove the heartbeat once it's in place.
The
cursor already closederrors 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.wahmedswl commentedon Nov 24, 2026
@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.
mday-io commentedon Nov 24, 2026
To be clear, the release won't fix the slow model. #6249 only stops the
cursor already closederrors on the models that run after it. The slow model will still hit the 10-minuteidle_in_transaction_session_timeoutuntil 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:
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.
wahmedswl commentedon Nov 24, 2026
@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
mday-io commentedon Nov 24, 2026
@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:
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
SETrun first, it survived. You can drop the heartbeat once it's in place.