Is there a way to configure QGIS to prevent the st_extent call when adding a PostGIS table?
Have a look at the settings of PostGIS connection. There is a selection "Use estimated metadata" that is unselected by default. Check it and QGIS will use ST_Estimated_Extent http://postgis.net/docs/manual-2.0/ST_Estimated_Extent.html. Remember to run VACUUM ANALYZE before.
The "use estimated metadata" option on the connection works for newly created layers, but it doesn't change it on existing layers that connect to that database, nor did I find any way to change it in the layer properties (I'm using 3.8.3). I found it relatively quick to change all my existing layers by editing the QGS file to add estimatedmetadata=true
to all PostGIS data source descriptions.
Here are the steps I followed using the Linux terminal. Similar steps can be done in any OS, as long as you have a way to search and replace text.
Extract QGS file from the QGZ:
$ unzip map.qgz map.qgs
Add
estimatedmetadata=true
to all PostGIS data source descriptions in the QGS. In my particular file, I found these in XML<datasource>
and<layer-tree-layer>
tags.For my particular file, I checked and saw that all such lines had no
estimatedmetadata=
, so rather than switchingestimatedmetadata=false
toestimatedmetadata=true
, I had to add in a newestimatedmetadata=true
. I looked for a string that was in the middle of all of the data source descriptions, so I could do the equivalent of a search and insert by using a text replace. The string I found in my file wascheckPrimaryKeyUnicity=
, so I ran the following command:$ sed -i 's/checkPrimaryKeyUnicity/estimatedmetadata=true checkPrimaryKeyUnicity/g' map.qgs
You may have to adapt this to the details of your specific file, make the changes in a more manual way, or use a tool with XML-specific editing features.
Replace the QGS file in the QGZ:
$ zip -u map.qgz map.qgs