1

I'm reading documentation for SPI https://www.postgresql.org/docs/13/spi.html And I have a few questions:

  1. is there any way to use multithreading in C to connect to Postgres (via SPI or Libpq) and read data from one table? (for example, one thread reads data from 1 to 100 rows, second from 101 to 200 etc).
  2. I know that Postgres can use workers (for SELECT, for example), but how SPI will react to that (if I send command "select * from table_name")
3
  • The SQL language and PostGreSQL support pagination using LIMIT and OFFSET... so it should be easy to route different subsets of a query to different threads. I'm not sure how (if) it will improve performance, that would depend on specific use cases. Commented Mar 24, 2021 at 6:39
  • Do you expect faster execution or there is other reason for MT? Commented Mar 24, 2021 at 8:43
  • Yes, I want to work with big tables (like 1mil rows) and I want to have faster execution. Commented Mar 26, 2021 at 11:02

1 Answer 1

1

Do not use multithreading in PostgreSQL backends. The system is not laid out for that, and interesting, yet undesirable, things are bound to happen if you do.

Using a custom background worker is the better idea. There is no problem with using SPI to process statements once you have attached to a database.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.