<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">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><br></div>-- <br><div dir="ltr"><div dir="ltr"><div>Francisco Puga</div><div>iCarto | Innovación, Cooperación, Cartografía y Territorio S.L.</div><div><a href="https://icarto.es/" target="_blank">https://icarto.es</a></div><div><br></div></div></div></div></div>
_______________________________________________<br>
QGIS-Developer mailing list<br>
<a href="mailto:QGIS-Developer@lists.osgeo.org" target="_blank">QGIS-Developer@lists.osgeo.org</a><br>
List info: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
Unsubscribe: <a href="https://lists.osgeo.org/mailman/listinfo/qgis-developer" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/qgis-developer</a><br>
</blockquote></div>

<br>
<div style="text-align:left"><a href="https://qfield.org/get/" target="_blank"><img src="https://www.opengis.ch/wp-content/uploads/2022/04/Qfield_Banner_email.png" alt="https://qfield.org/get/"></a><span style="font-family:Arial"><a><br></a></span></div><div style="text-align:left"><span style="font-family:Arial">QFIELD 2.0 IS HERE!<span style="font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif"><font size="3"> </font></span>- Hold the power of QGIS in your hand</span><span style="font-family:Arial"> - <a href="https://www.opengis.ch/2022/04/05/qfield-2-0-is-here/" target="_blank">learn more</a> - <a href="https://qfield.org/get" target="_blank">get it now</a><br></span></div>