<div dir="ltr">Thanks a lot Mathias,<div><br></div><div>It helps a lot. I didn't find anything weird about this, and it seems to work correctly.</div><div><br></div><div>After a bit of research I think it is a database/networking issue. The database is in a cloud provider and it seems that when the connection has no activity for some time, it becomes unresponsive, but remains idle in postgresql.</div><div><br></div><div>I'm playing with tcp_keepalives_* postgres settings, as the database is <14 and idle_session_timeout is not present. I'm monitoring it to see if it works.</div><div><br></div><div>Again, thank you.</div><div><br></div><div><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">El jue, 6 oct 2022 a las 12:16, Matthias Kuhn (<<a href="mailto:matthias@opengis.ch" target="_blank">matthias@opengis.ch</a>>) escribió:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Francisco,<div><br></div><div>QGIS does internally have connection pooling and uses up to 8 connections per database.</div><div>It detects common database connections based on the URI, where there may be different parameters used for layers even if they come from the same database (e.g. SSL parameters, authentication, ...). That could be an explanation for multiplication of connections.</div><div>You can inspect those with:</div><div><br></div><div>for _, l in QgsProject.instance().mapLayers().items():<br></div><div>    print(l.dataProvider().uri())</div><div><br></div><div>Cheers</div><div>Matthias</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Oct 6, 2022 at 11:13 AM Francisco Puga via QGIS-Developer <<a href="mailto:qgis-developer@lists.osgeo.org" target="_blank">qgis-developer@lists.osgeo.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi,<div><br></div><div>I'm hitting the default (100) `max_connection` PostgreSQL limit with ~4 users using a QGIS project with ~25 layers/tables. I know that I can raise the limit, but I'm trying to understand how QGIS handles database connections.</div><div><br></div><div>I didn't find so much information about it:</div><div><br></div><div><ol><li><a href="https://lists.osgeo.org/pipermail/qgis-developer/2008-March/003446.html" target="_blank">https://lists.osgeo.org/pipermail/qgis-developer/2008-March/003446.html</a>. Old post against, something like, an internal connection pool in QGIS</li><li><a href="https://github.com/qgis/QGIS/issues/30373" target="_blank">https://github.com/qgis/QGIS/issues/30373</a>. Feature request to share a single connection between different layers</li><li><a href="https://stackoverflow.com/a/71557537/930271" target="_blank">https://stackoverflow.com/a/71557537/930271</a> an ¿explanation? of how connection works in QGIS. I copy&paste here because this post may be hide for some users:</li></ol><div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">for what I know every single layer creates at least one, possibly two, connections (one is read only the other one is read-write ), these two connections have the same lifetime of the layer, the read-write connection is created on-demand if a read-write operation is requested (editing/adding/deleting features). </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Multiple layers using the same DB connection will not share these two connections. </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">In addition to the two connections above, temporary connections are created from a connection pool when the features are fetched from the layer(s), these connections are pooled (shared) and released when the fetching is finished, there is also a timeout to close them after 60 seconds idle. </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">It would be useful to check PG logs and see in which state these connections are, from my tests, the read-only connection is kept in ClientRead | idle.</blockquote><div><br></div><div>So,</div><div><ul><li>Can someone confirm the previous quote, explain how QGIS handles connections, or point me to relevant code or doc?</li><li>An invisible/unchecked layer consumes a connection?</li><li>Does make sense a feature request about this topic. Maybe</li><ul><li>Reuse connection</li><li>a kind of internal pooling</li><li>a configurable user limit, so next database access will wait for other connections to close to start operations</li><li>...</li></ul></ul></div></div><div>Many thanks.</div></div></div></blockquote></div></blockquote></div></div>