Page 1 of 1

Does DatabaseQuery Release Previous Query's Resources?

Posted: Thu Feb 29, 2024 5:44 pm
by swhite
Hi

The documentation mentions that FinishDatabaseQuery() must be called to free the query resources. However, what happens in the following case with multiple queries?

Code: Select all

    DatabaseQuery(2,"select * from mytable1")
    
    ... process the data
    
    DatabaseQuery(2,"select * from mytable2")
    
    ... process the data
    
    DatabaseQuery(2,"select * from mytable3")
    
    ... process the data
    
   FinishDatabaseQuery(2)    
In the above pseudo code do the subsequent queries release the resources of the previous query? Does the FinishDatabaseQuery(2) clear all the resources from all queries or should a FinishDatabaseQuery(2) be called before the subsequent queries?

Thanks,
Simon

Re: Does DatabaseQuery Release Previous Query's Resources?

Posted: Thu Feb 29, 2024 7:41 pm
by spikey
I'm reasonably confident in saying the scenario you describe will leak and you should 'Finish' every query (but I haven't tested it).
SQLite Documentation wrote: The application must finalize every prepared statement in order to avoid resource leaks.
See https://www.sqlite.org/c3ref/finalize.html. PostgreSQL is even more emphatic in https://www.postgresql.org/docs/current/libpq-exec.html. I don't have references to hand for the other databases but I don't imagine they say something very different.

A query will result in a memory allocation of an indeterminate (to PB) size to handle the results; the size being dependent on the backend database and the particulars of the query. PB, therefore, can't garbage collect it successfully on its own and must rely on the database library to do so.

Re: Does DatabaseQuery Release Previous Query's Resources?

Posted: Thu Feb 29, 2024 9:11 pm
by infratec
You have to use FinishDatabaseQuery() for every query and before you start an other one.
This has nothing to do with the used database.

If you mean you have to do more queries after an other ,you have 2 possibilies:
1. Use an other DB handle
2. Pack your selects into one. (Hint: With ...)

Re: Does DatabaseQuery Release Previous Query's Resources?

Posted: Thu Feb 29, 2024 9:34 pm
by swhite
Hi

I was assuming that I would need to use the FinishDatabaseQuery() but was not absolutely sure. So thank-you for the clarification.

Simon