Limit or killing ecto fetch records operation

I am running long queries(ecto version > 3) based on users params but I want to kill the query if query takes longer than 10 seconds and return error response.

One way I find (if I understand it correctly) is to set-statement-timeout on each query before running (if I can set it for a dedicated user that would be great). What is the best workaround/solution to kill the query/function which takes longer than 10 seconds.

Any help/workaround is appreciated. Thanks

Try this:

Repo.all(query, timeout: 10_000)

Thanks. it worked. with try/rescue I can send any custom response back as well in case of timeout