by Tim Hunt.
I agree with Petr's comment in the bug: https://tracker.moodle.org/browse/MDL-39725?focusedCommentId=228711&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-228711
"I already tried to invent some automatic analyse for pg temp tables and failed."
"My +1 to add $DB->analyze_temp_tables() and call it manually in affected areas."
The only quesiton I would ask is, should this be a single method ->analyze_temp_tables(), or a more specific ->analyze_temp_table($tablename)?