Avoid SQL failure or crash
There has been a thread on the devel mailing list (Failure to save as postresql database) and some related bugs (I'll try to list them later) about errors and crashes with the SQL back end. I have an initial fix to post shortly, but I'm opening this issue for a slightly wider discussion.
The immediate cause of the failure is that there is a query to count the rows in a table which does not exist. The quick fix is to test for the existence of the table before querying it. In this case, it does not matter whether the table exists, because if it does, all rows found would be dropped and the table (re)created before (re)filling it with appropriate data.
The issue for discussion is whether this fix is sufficient for now, or if we want to find places to add query.finish() to avoid other possible invalidations of query objects (db transactions.)