Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
189 views
in Technique[技术] by (71.8m points)

javascript - Can a third query execute in the middle of two queries inside the same db.any() call?

If I have the following code, executed in the middle of a request:

db.any('SET search_path TO user001; SELECT * FROM messages')

Could a second request execute a query in between the SET and the SELECT of the first query? For example:

  • First request arrives
  • SET search_path TO user001 (1st)
  • Second request arrives
  • SET search_path TO user002 (2nd)
  • SELECT * FROM messages (1st)
  • SELECT * FROM messages (2nd)

Also what's a good way of defining the search_path at run time? I thought of using db.task() but that would limit the number of requests per second to around 100, right?

question from:https://stackoverflow.com/questions/66064187/can-a-third-query-execute-in-the-middle-of-two-queries-inside-the-same-db-any

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Could a second request execute a query in between the SET and the SELECT of the first query?

No, such a query is processed as one, what's known as atomic operation.

Also what's a good way of defining the search_path at run time?

Within pg-promise, initialization option schema does that. It sets the schema(s) automatically for every fresh connection.

However, if you need to keep changing the current schema for every single request, i.e. if you actually have a separate database schema for every user, then it won't work, and you will need to use that approach you tried, i.e. prepend schema-changing query to every query.

However again, there is no point in doing it for each query while handling one request, you need to do it only once, when you start handling the request. And this is why automatic tasks/transactions are best suited for that. In fact, you can use event extend to add your own custom task/transaction that would execute schema change in the beginning.

I thought of using db.task() but that would limit the number of requests per second to around 100, right?

No, where did you find such information? :) Tasks do not add any such limitation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...