Postgresql 中文操作指南
Description
dblink_get_result 收集先前使用 dblink_send_query 发送的异步查询的结果。如果查询尚未完成, dblink_get_result 将等待直到它完成。
dblink_get_result collects the results of an asynchronous query previously sent with dblink_send_query. If the query is not already completed, dblink_get_result will wait until it is.
Arguments
-
connname
-
Name of the connection to use.
-
-
fail_on_error
-
If true (the default when omitted) then an error thrown on the remote side of the connection causes an error to also be thrown locally. If false, the remote error is locally reported as a NOTICE, and the function returns no rows.
-
Return Value
对于异步查询(即返回行的 SQL 语句),此函数将返回查询生成的行。若要使用此函数,你需要指定预期的列集,如先前针对 dblink 讨论的那样。
For an async query (that is, an SQL statement returning rows), the function returns the row(s) produced by the query. To use this function, you will need to specify the expected set of columns, as previously discussed for dblink.
对于一个异步命令(即未返回行的 SQL 语句),此函数返回一行,它包含一个文本列,其中包含该命令的状态字符串。在调用 FROM 子句时,仍然需要指定结果将在单个文本列中。
For an async command (that is, an SQL statement not returning rows), the function returns a single row with a single text column containing the command’s status string. It is still necessary to specify that the result will have a single text column in the calling FROM clause.
Notes
此函数只能在 dblink_send_query 返回 1 时 must 调用。对于发送的每个查询,必须调用此函数一次,在再次使用连接之前需额外调用一次以获取一个空集结果。
This function must be called if dblink_send_query returned 1. It must be called once for each query sent, and one additional time to obtain an empty set result, before the connection can be used again.
在使用 dblink_send_query 和 dblink_get_result 时,dblink 在将任何结果返回本地查询处理器之前提取整个远程查询结果。如果查询返回大量的行,则可能导致本地会话中的瞬态内存膨胀。可能最好用 dblink_open 打开这样一个查询作为游标,然后一次检索可管理的数量的行。或者,使用普通的 dblink() ,它通过将大量结果集暂存到磁盘来避免内存膨胀。
When using dblink_send_query and dblink_get_result, dblink fetches the entire remote query result before returning any of it to the local query processor. If the query returns a large number of rows, this can result in transient memory bloat in the local session. It may be better to open such a query as a cursor with dblink_open and then fetch a manageable number of rows at a time. Alternatively, use plain dblink(), which avoids memory bloat by spooling large result sets to disk.
Examples
contrib_regression=# SELECT dblink_connect('dtest1', 'dbname=contrib_regression');
dblink_connect
----------------
OK
(1 row)
contrib_regression=# SELECT * FROM
contrib_regression-# dblink_send_query('dtest1', 'select * from foo where f1 < 3') AS t1;
t1
1 (1 row)
contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]); f1 | f2 | f3---- 0 | a | {a0,b0,c0} 1 | b | {a1,b1,c1} 2 | c | {a2,b2,c2}(3 rows)----
contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]); f1 | f2 | f3
0 | a | {a0,b0,c0} 1 | b | {a1,b1,c1} 2 | c | {a2,b2,c2} (3 rows) [.iokays-translated-5dc00fa6e1172863db899a7b745142ef] contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]); f1 | f2 | f3----(0 rows)---- [.iokays-original-5dc00fa6e1172863db899a7b745142ef] contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]); f1 | f2 | f3 ------------ (0 rows) [.iokays-translated-8115f34b3b9c0db5fd149ad90c44202b] contrib_regression=# SELECT * FROMcontrib_regression-# dblink_send_query('dtest1', 'select * from foo where f1 < 3; select * from foo where f1 > 6') AS t1; t1 [.iokays-original-8115f34b3b9c0db5fd149ad90c44202b] contrib_regression=# SELECT * FROM contrib_regression-# dblink_send_query('dtest1', 'select * from foo where f1 < 3; select * from foo where f1 > 6') AS t1; t1 ---- 1 (1 row) contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]); f1 | f2 | f3 ----+----+------------ 0 | a | {a0,b0,c0} 1 | b | {a1,b1,c1} 2 | c | {a2,b2,c2} (3 rows) contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]); f1 | f2 | f3 ----+----+--------------- 7 | h | {a7,b7,c7} 8 | i | {a8,b8,c8} 9 | j | {a9,b9,c9} 10 | k | {a10,b10,c10} (4 rows) contrib_regression=# SELECT * FROM dblink_get_result('dtest1') AS t1(f1 int, f2 text, f3 text[]); f1 | f2 | f3 ----+----+---- (0 rows) ----