SQLite Forum

sqlite3_preupdate_hook and vacuum
Login

sqlite3_preupdate_hook and vacuum

(1) By Sigma (sigma20) on 2021-02-17 17:02:12 [link] [source]

I created a simple hook like feature (to simulate notifications) using sqlite3_preupdate_hook and noticed that it triggers for every row changed during a vacuum operation. The documentation states that:

registers a callback function that is invoked prior to each INSERT, UPDATE, and DELETE operation on a database table

This doesn't imply that it should trigger for a vacuum operation. In my opinion, I think it shouldn't because there's no way to distinguish between a vacuum and a regular INSERT, UPDATE or DELETE that actually changes data. Can this be considered a bug?

(2) By Larry Brasfield (larrybr) on 2021-02-18 04:23:41 in reply to 1 [link] [source]

I agree that this behavior/doc combination is awry. It represents a library bug or a doc bug.

Hyper-technically, the doc does not say that the callback will not be invoked for other non-strictly-read operations, but it certainly implies as much. So this may be a doc bug because the doc should be clearer on this if it's not a code bug.

I suggest you use the post edit feature to prepend the word Bug: into the title. Somebody on the dev team is likely then to fix it, one way or the other.

(3) By Larry Brasfield (larrybr) on 2021-02-18 04:42:33 in reply to 1 [link] [source]

If you look at paragraph 3 of section 3 in VACUUM, you will see that it can change rowids of tables having no INTEGER PRIMARY KEY.

I think you would agree that library users may well want sqlite3_preupdate_hook()-installed callbacks to be called in such a case.

I believe the doc on sqlite3_preupdate_hook() should indicate this possibility. And if it applies to your case, you may want to check to be sure that changing rowid values is not going to cause a problem. As you likely noticed in the sqlite3_preupdate_hook() doc, it is possible to determine what is about to be changed.

Also, I retract my title change suggestion.

(5) By Sigma (sigma20) on 2021-02-18 16:31:24 in reply to 3 [source]

I wasn't implying that the sqlite3_preupdate_hook shouldn't be triggered in vacuum. I think it should exactly because it can change rowids, and depending on your situation, you might want to know about that.

But, one can also argue the opposite case, because if you don't deal with rowids (or simply create tables with INTEGER PRIMARY KEY), then vacuum doesn't change anything on the DB schema or its data and then doesn't make sense to receive all that traffic in sqlite3_preupdate_hook.

I managed to work around this by using the "zDb" parameter with just "main", because apparently, the vacuum runs on an entire different database. But this seems like an hack, and the reason why I think the documentation should explicitly mention this and offer a solution for when you want to ignore sqlite3_preupdate_hook events during a vacuum.

(4) By Dan Kennedy (dan) on 2021-02-18 16:27:14 in reply to 1 [link] [source]

Thanks for reporting this. Now fixed here:

https://sqlite.org/src/info/3c25cb4ab8885a50

This change means no pre-update or update hooks are invoked as part of a VACUUM operation, even if that VACUUM operation changes rowid values.