使用C函数扩展PostgreSQL时实现高性能事务 [英] Achieving high-performance transactions when extending PostgreSQL with C-functions
问题描述
我的目标是获得最高的性能,该性能可用于将数据库中的数据块复制到C函数以作为查询结果进行处理和返回。
My goal is to achieve the highest performance available for copying a block of data from the database into a C-function to be processed and returned as the result of a query.
我是PostgreSQL的新手,我目前正在研究移动数据的可能方法。具体来说,我正在寻找与PostgreSQL相关的细微差别或关键字来快速移动大数据。
I am new to PostgreSQL and I am currently researching possible ways to move the data. Specifically, I am looking for nuances or keywords related specifically to PostgreSQL to move big data fast.
注意:
我的最终目标是提高速度,因此我愿意接受我提出的确切问题以外的答案,只要它能带来出色的性能即可。例如,我遇到了COPY关键字(仅适用于PostgreSQL),它可以将数据从表快速移至文件。反之亦然。我试图远离数据库外部的处理,但是如果它提供的性能改进超过了外部处理的明显缺点,那就可以了。
推荐答案
听起来您可能想使用服务器编程接口(SPI)来将存储过程实现为 C语言函数在PostgreSQL后端中运行。
It sounds like you probably want to use the server programming interface (SPI) to implement a stored procedure as a C language function running inside the PostgreSQL back-end.
使用 SPI_connect
来设置SPI。
Use SPI_connect
to set up the SPI.
现在 SPI_prepare_cursor
查询,然后 SPI_cursor_open
。 SPI_cursor_fetch
行和 SPI_cursor_close
完成后。请注意, SPI_cursor_fetch
允许您获取一批行。
Now SPI_prepare_cursor
a query, then SPI_cursor_open
it. SPI_cursor_fetch
rows from it and SPI_cursor_close
it when done. Note that SPI_cursor_fetch
allows you to fetch batches of rows.
SPI_finish
完成后进行清理。
SPI_finish
to clean up when done.
您可以在生成结果行时将结果行返回到元组存储中,而无需在内存中构建整个表。请参见PostgreSQL源代码中任何返回集合的函数中的示例。您可能还需要查看 SPI_returntuple
辅助功能。
You can return the result rows into a tuplestore as you generate them, avoiding the need to build the whole table in memory. See examples in any of the set-returning functions in the PostgreSQL source code. You might also want to look at the SPI_returntuple
helper function.
如果需要最大速度,您的客户端可能希望通过 libpqtypes ,以便它以最小的开销接收服务器端SPI使用过程产生的数据。
If maximum speed is of interest, your client may want to use the libpq binary protocol via libpqtypes so it receives the data produced by your server-side SPI-using procedure with minimal overhead.
这篇关于使用C函数扩展PostgreSQL时实现高性能事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!