1 -- Adjust this setting to control where the objects get created.
2 SET search_path = public;
4 -- Define the functions and test data
7 -- Turn off echoing so that expected file does not depend on
8 -- contents of dblink.sql.
9 SET client_min_messages = warning;
11 RESET client_min_messages;
12 CREATE TABLE foo(f1 int, f2 text, f3 text[], primary key (f1,f2));
13 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "foo_pkey" for table "foo"
14 INSERT INTO foo VALUES (0,'a','{"a0","b0","c0"}');
15 INSERT INTO foo VALUES (1,'b','{"a1","b1","c1"}');
16 INSERT INTO foo VALUES (2,'c','{"a2","b2","c2"}');
17 INSERT INTO foo VALUES (3,'d','{"a3","b3","c3"}');
18 INSERT INTO foo VALUES (4,'e','{"a4","b4","c4"}');
19 INSERT INTO foo VALUES (5,'f','{"a5","b5","c5"}');
20 INSERT INTO foo VALUES (6,'g','{"a6","b6","c6"}');
21 INSERT INTO foo VALUES (7,'h','{"a7","b7","c7"}');
22 INSERT INTO foo VALUES (8,'i','{"a8","b8","c8"}');
23 INSERT INTO foo VALUES (9,'j','{"a9","b9","c9"}');
25 -- list the primary key fields
27 FROM dblink_get_pkey('foo');
34 -- build an insert statement based on a local tuple,
35 -- replacing the primary key values with new ones
36 SELECT dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
37 dblink_build_sql_insert
38 -----------------------------------------------------------
39 INSERT INTO foo(f1,f2,f3) VALUES('99','xyz','{a0,b0,c0}')
42 -- build an update statement based on a local tuple,
43 -- replacing the primary key values with new ones
44 SELECT dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
45 dblink_build_sql_update
46 ----------------------------------------------------------------------------------------
47 UPDATE foo SET f1 = '99', f2 = 'xyz', f3 = '{a0,b0,c0}' WHERE f1 = '99' AND f2 = 'xyz'
50 -- build a delete statement based on a local tuple,
51 SELECT dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}');
52 dblink_build_sql_delete
53 ---------------------------------------------
54 DELETE FROM foo WHERE f1 = '0' AND f2 = 'a'
57 -- retest using a quoted and schema qualified table
58 CREATE SCHEMA "MySchema";
59 CREATE TABLE "MySchema"."Foo"(f1 int, f2 text, f3 text[], primary key (f1,f2));
60 NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "Foo_pkey" for table "Foo"
61 INSERT INTO "MySchema"."Foo" VALUES (0,'a','{"a0","b0","c0"}');
62 -- list the primary key fields
64 FROM dblink_get_pkey('"MySchema"."Foo"');
71 -- build an insert statement based on a local tuple,
72 -- replacing the primary key values with new ones
73 SELECT dblink_build_sql_insert('"MySchema"."Foo"','1 2',2,'{"0", "a"}','{"99", "xyz"}');
74 dblink_build_sql_insert
75 ------------------------------------------------------------------------
76 INSERT INTO "MySchema"."Foo"(f1,f2,f3) VALUES('99','xyz','{a0,b0,c0}')
79 -- build an update statement based on a local tuple,
80 -- replacing the primary key values with new ones
81 SELECT dblink_build_sql_update('"MySchema"."Foo"','1 2',2,'{"0", "a"}','{"99", "xyz"}');
82 dblink_build_sql_update
83 -----------------------------------------------------------------------------------------------------
84 UPDATE "MySchema"."Foo" SET f1 = '99', f2 = 'xyz', f3 = '{a0,b0,c0}' WHERE f1 = '99' AND f2 = 'xyz'
87 -- build a delete statement based on a local tuple,
88 SELECT dblink_build_sql_delete('"MySchema"."Foo"','1 2',2,'{"0", "a"}');
89 dblink_build_sql_delete
90 ----------------------------------------------------------
91 DELETE FROM "MySchema"."Foo" WHERE f1 = '0' AND f2 = 'a'
96 FROM dblink('dbname=contrib_regression','SELECT * FROM foo') AS t(a int, b text, c text[])
104 -- should generate "connection not available" error
106 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
108 ERROR: connection not available
109 -- create a persistent connection
110 SELECT dblink_connect('dbname=contrib_regression');
116 -- use the persistent connection
118 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
126 -- open a cursor with bad SQL and fail_on_error set to false
127 SELECT dblink_open('rmt_foo_cursor','SELECT * FROM foobar',false);
128 NOTICE: relation "foobar" does not exist
129 CONTEXT: Error occurred on dblink connection named "unnamed": could not open cursor.
135 -- reset remote transaction state
136 SELECT dblink_exec('ABORT');
143 SELECT dblink_open('rmt_foo_cursor','SELECT * FROM foo');
150 SELECT dblink_close('rmt_foo_cursor',false);
156 -- open the cursor again
157 SELECT dblink_open('rmt_foo_cursor','SELECT * FROM foo');
165 FROM dblink_fetch('rmt_foo_cursor',4) AS t(a int, b text, c text[]);
175 FROM dblink_fetch('rmt_foo_cursor',4) AS t(a int, b text, c text[]);
184 -- this one only finds two rows left
186 FROM dblink_fetch('rmt_foo_cursor',4) AS t(a int, b text, c text[]);
193 -- intentionally botch a fetch
195 FROM dblink_fetch('rmt_foobar_cursor',4,false) AS t(a int, b text, c text[]);
196 NOTICE: cursor "rmt_foobar_cursor" does not exist
197 CONTEXT: Error occurred on dblink connection named "unnamed": could not fetch from cursor.
202 -- reset remote transaction state
203 SELECT dblink_exec('ABORT');
209 -- close the wrong cursor
210 SELECT dblink_close('rmt_foobar_cursor',false);
211 NOTICE: cursor "rmt_foobar_cursor" does not exist
212 CONTEXT: Error occurred on dblink connection named "unnamed": could not close cursor.
218 -- should generate 'cursor "rmt_foo_cursor" not found' error
220 FROM dblink_fetch('rmt_foo_cursor',4) AS t(a int, b text, c text[]);
221 ERROR: cursor "rmt_foo_cursor" does not exist
222 CONTEXT: Error occurred on dblink connection named "unnamed": could not fetch from cursor.
223 -- this time, 'cursor "rmt_foo_cursor" not found' as a notice
225 FROM dblink_fetch('rmt_foo_cursor',4,false) AS t(a int, b text, c text[]);
226 NOTICE: cursor "rmt_foo_cursor" does not exist
227 CONTEXT: Error occurred on dblink connection named "unnamed": could not fetch from cursor.
232 -- close the persistent connection
233 SELECT dblink_disconnect();
239 -- should generate "connection not available" error
241 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
243 ERROR: connection not available
244 -- put more data into our slave table, first using arbitrary connection syntax
245 -- but truncate the actual return value so we can use diff to check for success
246 SELECT substr(dblink_exec('dbname=contrib_regression','INSERT INTO foo VALUES(10,''k'',''{"a10","b10","c10"}'')'),1,6);
252 -- create a persistent connection
253 SELECT dblink_connect('dbname=contrib_regression');
259 -- put more data into our slave table, using persistent connection syntax
260 -- but truncate the actual return value so we can use diff to check for success
261 SELECT substr(dblink_exec('INSERT INTO foo VALUES(11,''l'',''{"a11","b11","c11"}'')'),1,6);
269 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[]);
271 ----+---+---------------
282 10 | k | {a10,b10,c10}
283 11 | l | {a11,b11,c11}
288 FROM dblink('SELECT * FROM foobar',false) AS t(a int, b text, c text[]);
289 NOTICE: relation "foobar" does not exist
290 CONTEXT: Error occurred on dblink connection named "unnamed": could not execute query.
296 SELECT dblink_exec('UPDATE foo SET f3[2] = ''b99'' WHERE f1 = 11');
304 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
307 ----+---+---------------
308 11 | l | {a11,b99,c11}
311 -- botch a change to some other data
312 SELECT dblink_exec('UPDATE foobar SET f3[2] = ''b99'' WHERE f1 = 11',false);
313 NOTICE: relation "foobar" does not exist
314 CONTEXT: Error occurred on dblink connection named "unnamed": could not execute command.
321 SELECT dblink_exec('DELETE FROM foo WHERE f1 = 11');
329 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
335 -- close the persistent connection
336 SELECT dblink_disconnect();
343 -- tests for the new named persistent connection syntax
345 -- should generate "missing "=" after "myconn" in connection info string" error
347 FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
349 ERROR: could not establish connection
350 DETAIL: missing "=" after "myconn" in connection info string
352 -- create a named persistent connection
353 SELECT dblink_connect('myconn','dbname=contrib_regression');
359 -- use the named persistent connection
361 FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
364 ----+---+---------------
367 10 | k | {a10,b10,c10}
370 -- use the named persistent connection, but get it wrong
372 FROM dblink('myconn','SELECT * FROM foobar',false) AS t(a int, b text, c text[])
374 NOTICE: relation "foobar" does not exist
375 CONTEXT: Error occurred on dblink connection named "unnamed": could not execute query.
380 -- create a second named persistent connection
381 -- should error with "duplicate connection name"
382 SELECT dblink_connect('myconn','dbname=contrib_regression');
383 ERROR: duplicate connection name
384 -- create a second named persistent connection with a new name
385 SELECT dblink_connect('myconn2','dbname=contrib_regression');
391 -- use the second named persistent connection
393 FROM dblink('myconn2','SELECT * FROM foo') AS t(a int, b text, c text[])
396 ----+---+---------------
399 10 | k | {a10,b10,c10}
402 -- close the second named persistent connection
403 SELECT dblink_disconnect('myconn2');
409 -- open a cursor incorrectly
410 SELECT dblink_open('myconn','rmt_foo_cursor','SELECT * FROM foobar',false);
411 NOTICE: relation "foobar" does not exist
412 CONTEXT: Error occurred on dblink connection named "myconn": could not open cursor.
418 -- reset remote transaction state
419 SELECT dblink_exec('myconn','ABORT');
425 -- test opening cursor in a transaction
426 SELECT dblink_exec('myconn','BEGIN');
432 -- an open transaction will prevent dblink_open() from opening its own
433 SELECT dblink_open('myconn','rmt_foo_cursor','SELECT * FROM foo');
439 -- this should not commit the transaction because the client opened it
440 SELECT dblink_close('myconn','rmt_foo_cursor');
446 -- this should succeed because we have an open transaction
447 SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo');
453 -- commit remote transaction
454 SELECT dblink_exec('myconn','COMMIT');
460 -- test automatic transactions for multiple cursor opens
461 SELECT dblink_open('myconn','rmt_foo_cursor','SELECT * FROM foo');
468 SELECT dblink_open('myconn','rmt_foo_cursor2','SELECT * FROM foo');
474 -- this should not commit the transaction
475 SELECT dblink_close('myconn','rmt_foo_cursor2');
481 -- this should succeed because we have an open transaction
482 SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo');
488 -- this should commit the transaction
489 SELECT dblink_close('myconn','rmt_foo_cursor');
495 -- this should fail because there is no open transaction
496 SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo');
497 ERROR: DECLARE CURSOR can only be used in transaction blocks
498 CONTEXT: Error occurred on dblink connection named "unnamed": could not execute command.
499 -- reset remote transaction state
500 SELECT dblink_exec('myconn','ABORT');
507 SELECT dblink_open('myconn','rmt_foo_cursor','SELECT * FROM foo');
515 FROM dblink_fetch('myconn','rmt_foo_cursor',4) AS t(a int, b text, c text[]);
525 FROM dblink_fetch('myconn','rmt_foo_cursor',4) AS t(a int, b text, c text[]);
534 -- this one only finds three rows left
536 FROM dblink_fetch('myconn','rmt_foo_cursor',4) AS t(a int, b text, c text[]);
538 ----+---+---------------
541 10 | k | {a10,b10,c10}
544 -- fetch some data incorrectly
546 FROM dblink_fetch('myconn','rmt_foobar_cursor',4,false) AS t(a int, b text, c text[]);
547 NOTICE: cursor "rmt_foobar_cursor" does not exist
548 CONTEXT: Error occurred on dblink connection named "myconn": could not fetch from cursor.
553 -- reset remote transaction state
554 SELECT dblink_exec('myconn','ABORT');
560 -- should generate 'cursor "rmt_foo_cursor" not found' error
562 FROM dblink_fetch('myconn','rmt_foo_cursor',4) AS t(a int, b text, c text[]);
563 ERROR: cursor "rmt_foo_cursor" does not exist
564 CONTEXT: Error occurred on dblink connection named "myconn": could not fetch from cursor.
565 -- close the named persistent connection
566 SELECT dblink_disconnect('myconn');
572 -- should generate "missing "=" after "myconn" in connection info string" error
574 FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
576 ERROR: could not establish connection
577 DETAIL: missing "=" after "myconn" in connection info string
579 -- create a named persistent connection
580 SELECT dblink_connect('myconn','dbname=contrib_regression');
586 -- put more data into our slave table, using named persistent connection syntax
587 -- but truncate the actual return value so we can use diff to check for success
588 SELECT substr(dblink_exec('myconn','INSERT INTO foo VALUES(11,''l'',''{"a11","b11","c11"}'')'),1,6);
596 FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]);
598 ----+---+---------------
609 10 | k | {a10,b10,c10}
610 11 | l | {a11,b11,c11}
614 SELECT dblink_exec('myconn','UPDATE foo SET f3[2] = ''b99'' WHERE f1 = 11');
622 FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
625 ----+---+---------------
626 11 | l | {a11,b99,c11}
630 SELECT dblink_exec('myconn','DELETE FROM foo WHERE f1 = 11');
638 FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
644 -- close the named persistent connection
645 SELECT dblink_disconnect('myconn');
651 -- close the named persistent connection again
652 -- should get 'connection "myconn" not available' error
653 SELECT dblink_disconnect('myconn');
654 ERROR: connection "myconn" not available
655 -- test asynchronous queries
656 SELECT dblink_connect('dtest1', 'dbname=contrib_regression');
663 dblink_send_query('dtest1', 'select * from foo where f1 < 3') as t1;
669 SELECT dblink_connect('dtest2', 'dbname=contrib_regression');
676 dblink_send_query('dtest2', 'select * from foo where f1 > 2 and f1 < 7') as t1;
682 SELECT dblink_connect('dtest3', 'dbname=contrib_regression');
689 dblink_send_query('dtest3', 'select * from foo where f1 > 6') as t1;
695 CREATE TEMPORARY TABLE result AS
696 (SELECT * from dblink_get_result('dtest1') as t1(f1 int, f2 text, f3 text[]))
698 (SELECT * from dblink_get_result('dtest2') as t2(f1 int, f2 text, f3 text[]))
700 (SELECT * from dblink_get_result('dtest3') as t3(f1 int, f2 text, f3 text[]))
702 -- dblink_get_connections returns an array with elements in a machine-dependent
703 -- ordering, so we must resort to unnesting and sorting for a stable result
704 create function unnest(anyarray) returns setof anyelement
705 language sql strict immutable as $$
706 select $1[i] from generate_series(array_lower($1,1), array_upper($1,1)) as i
708 SELECT * FROM unnest(dblink_get_connections()) ORDER BY 1;
716 SELECT dblink_is_busy('dtest1');
722 SELECT dblink_disconnect('dtest1');
728 SELECT dblink_disconnect('dtest2');
734 SELECT dblink_disconnect('dtest3');
740 SELECT * from result;
742 ----+----+---------------
753 10 | k | {a10,b10,c10}
756 SELECT dblink_connect('dtest1', 'dbname=contrib_regression');
763 dblink_send_query('dtest1', 'select * from foo where f1 < 3') as t1;
769 SELECT dblink_cancel_query('dtest1');
771 ---------------------
775 SELECT dblink_error_message('dtest1');
777 ----------------------
781 SELECT dblink_disconnect('dtest1');
787 -- test foreign data wrapper functionality
788 CREATE USER dblink_regression_test;
789 CREATE FOREIGN DATA WRAPPER postgresql;
790 CREATE SERVER fdtest FOREIGN DATA WRAPPER postgresql OPTIONS (dbname 'contrib_regression');
791 CREATE USER MAPPING FOR public SERVER fdtest;
792 GRANT USAGE ON FOREIGN SERVER fdtest TO dblink_regression_test;
793 GRANT EXECUTE ON FUNCTION dblink_connect_u(text, text) TO dblink_regression_test;
794 \set ORIGINAL_USER :USER
795 \c - dblink_regression_test
797 SELECT dblink_connect('myconn', 'fdtest');
798 ERROR: password is required
799 DETAIL: Non-superusers must provide a password in the connection string.
801 SELECT dblink_connect_u('myconn', 'fdtest');
807 SELECT * FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]);
809 ----+---+---------------
820 10 | k | {a10,b10,c10}
824 REVOKE USAGE ON FOREIGN SERVER fdtest FROM dblink_regression_test;
825 REVOKE EXECUTE ON FUNCTION dblink_connect_u(text, text) FROM dblink_regression_test;
826 DROP USER dblink_regression_test;
827 DROP USER MAPPING FOR public SERVER fdtest;
829 DROP FOREIGN DATA WRAPPER postgresql;