]> granicus.if.org Git - postgresql/blob - contrib/dblink/dblink.test.sql
Add missing dblink files.
[postgresql] / contrib / dblink / dblink.test.sql
1 \connect dblink_test_slave
2
3 create table foo(f1 int, f2 text, f3 text[], primary key (f1,f2));
4 insert into foo values(0,'a','{"a0","b0","c0"}');
5 insert into foo values(1,'b','{"a1","b1","c1"}');
6 insert into foo values(2,'c','{"a2","b2","c2"}');
7 insert into foo values(3,'d','{"a3","b3","c3"}');
8 insert into foo values(4,'e','{"a4","b4","c4"}');
9 insert into foo values(5,'f','{"a5","b5","c5"}');
10 insert into foo values(6,'g','{"a6","b6","c6"}');
11 insert into foo values(7,'h','{"a7","b7","c7"}');
12 insert into foo values(8,'i','{"a8","b8","c8"}');
13 insert into foo values(9,'j','{"a9","b9","c9"}');
14
15 \connect dblink_test_master
16
17 -- regular old dblink
18 select * from dblink('dbname=dblink_test_slave','select * from foo') as t(a int, b text, c text[]) where t.a > 7;
19
20 -- should generate "no connection available" error
21 select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7;
22
23 -- create a persistent connection
24 select dblink_connect('dbname=dblink_test_slave');
25
26 -- use the persistent connection
27 select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7;
28
29 -- open a cursor
30 select dblink_open('rmt_foo_cursor','select * from foo');
31
32 -- fetch some data
33 select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]);
34 select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]);
35
36 -- this one only finds two rows left
37 select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]);
38
39 -- close the cursor
40 select dblink_close('rmt_foo_cursor');
41
42 -- should generate "cursor rmt_foo_cursor does not exist" error
43 select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]);
44
45 -- close the persistent connection
46 select dblink_disconnect();
47
48 -- should generate "no connection available" error
49 select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7;
50
51 -- put more data into our slave table, first using arbitrary connection syntax
52 -- but truncate the actual return value so we can use diff to check for success
53 select substr(dblink_exec('dbname=dblink_test_slave','insert into foo values(10,''k'',''{"a10","b10","c10"}'')'),1,6);
54
55 -- create a persistent connection
56 select dblink_connect('dbname=dblink_test_slave');
57
58 -- put more data into our slave table, using persistent connection syntax
59 -- but truncate the actual return value so we can use diff to check for success
60 select substr(dblink_exec('insert into foo values(11,''l'',''{"a11","b11","c11"}'')'),1,6);
61
62 -- let's see it
63 select * from dblink('select * from foo') as t(a int, b text, c text[]);
64
65 -- change some data
66 select dblink_exec('update foo set f3[2] = ''b99'' where f1 = 11');
67
68 -- let's see it
69 select * from dblink('select * from foo') as t(a int, b text, c text[]) where a = 11;
70
71 -- delete some data
72 select dblink_exec('delete from foo where f1 = 11');
73
74 -- let's see it
75 select * from dblink('select * from foo') as t(a int, b text, c text[]) where a = 11;
76
77 -- misc utilities
78 \connect dblink_test_slave
79
80 -- show the currently executing query
81 select 'hello' as hello, dblink_current_query() as query;
82
83 -- list the primary key fields
84 select * from dblink_get_pkey('foo');
85
86 -- build an insert statement based on a local tuple,
87 -- replacing the primary key values with new ones
88 select dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
89
90 -- build an update statement based on a local tuple,
91 -- replacing the primary key values with new ones
92 select dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
93
94 -- build a delete statement based on a local tuple,
95 select dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}');