1
- \connect dblink_test_slave
1
+ --
2
+ -- First, create a slave database and define the functions.
3
+ -- Turn off echoing so that expected file does not depend on
4
+ -- contents of dblink.sql.
5
+ --
6
+ CREATE DATABASE regression_slave;
7
+ \connect regression_slave
8
+ \set ECHO none
2
9
create table foo(f1 int, f2 text, f3 text[], primary key (f1,f2));
3
10
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'foo_pkey' for table 'foo'
4
11
insert into foo values(0,'a','{"a0","b0","c0"}');
@@ -11,9 +18,54 @@ insert into foo values(6,'g','{"a6","b6","c6"}');
11
18
insert into foo values(7,'h','{"a7","b7","c7"}');
12
19
insert into foo values(8,'i','{"a8","b8","c8"}');
13
20
insert into foo values(9,'j','{"a9","b9","c9"}');
14
- \connect dblink_test_master
21
+ -- misc utilities
22
+ -- show the currently executing query
23
+ select 'hello' as hello, dblink_current_query() as query;
24
+ hello | query
25
+ -------+-----------------------------------------------------------
26
+ hello | select 'hello' as hello, dblink_current_query() as query;
27
+ (1 row)
28
+
29
+ -- list the primary key fields
30
+ select * from dblink_get_pkey('foo');
31
+ position | colname
32
+ ----------+---------
33
+ 1 | f1
34
+ 2 | f2
35
+ (2 rows)
36
+
37
+ -- build an insert statement based on a local tuple,
38
+ -- replacing the primary key values with new ones
39
+ select dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
40
+ dblink_build_sql_insert
41
+ -----------------------------------------------------------
42
+ INSERT INTO foo(f1,f2,f3) VALUES('99','xyz','{a0,b0,c0}')
43
+ (1 row)
44
+
45
+ -- build an update statement based on a local tuple,
46
+ -- replacing the primary key values with new ones
47
+ select dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
48
+ dblink_build_sql_update
49
+ ----------------------------------------------------------------------------------------
50
+ UPDATE foo SET f1 = '99', f2 = 'xyz', f3 = '{a0,b0,c0}' WHERE f1 = '99' AND f2 = 'xyz'
51
+ (1 row)
52
+
53
+ -- build a delete statement based on a local tuple,
54
+ select dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}');
55
+ dblink_build_sql_delete
56
+ ---------------------------------------------
57
+ DELETE FROM foo WHERE f1 = '0' AND f2 = 'a'
58
+ (1 row)
59
+
60
+ --
61
+ -- Connect back to the regression database and define the functions.
62
+ -- Turn off echoing so that expected file does not depend on
63
+ -- contents of dblink.sql.
64
+ --
65
+ \connect regression
66
+ \set ECHO none
15
67
-- regular old dblink
16
- select * from dblink('dbname=dblink_test_slave ','select * from foo') as t(a int, b text, c text[]) where t.a > 7;
68
+ select * from dblink('dbname=regression_slave ','select * from foo') as t(a int, b text, c text[]) where t.a > 7;
17
69
a | b | c
18
70
---+---+------------
19
71
8 | i | {a8,b8,c8}
@@ -24,7 +76,7 @@ select * from dblink('dbname=dblink_test_slave','select * from foo') as t(a int,
24
76
select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7;
25
77
ERROR: dblink: no connection available
26
78
-- create a persistent connection
27
- select dblink_connect('dbname=dblink_test_slave ');
79
+ select dblink_connect('dbname=regression_slave ');
28
80
dblink_connect
29
81
----------------
30
82
OK
@@ -94,14 +146,14 @@ select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.
94
146
ERROR: dblink: no connection available
95
147
-- put more data into our slave table, first using arbitrary connection syntax
96
148
-- but truncate the actual return value so we can use diff to check for success
97
- select substr(dblink_exec('dbname=dblink_test_slave ','insert into foo values(10,''k'',''{"a10","b10","c10"}'')'),1,6);
149
+ select substr(dblink_exec('dbname=regression_slave ','insert into foo values(10,''k'',''{"a10","b10","c10"}'')'),1,6);
98
150
substr
99
151
--------
100
152
INSERT
101
153
(1 row)
102
154
103
155
-- create a persistent connection
104
- select dblink_connect('dbname=dblink_test_slave ');
156
+ select dblink_connect('dbname=regression_slave ');
105
157
dblink_connect
106
158
----------------
107
159
OK
@@ -160,43 +212,48 @@ select * from dblink('select * from foo') as t(a int, b text, c text[]) where a
160
212
---+---+---
161
213
(0 rows)
162
214
163
- -- misc utilities
164
- \connect dblink_test_slave
165
- -- show the currently executing query
166
- select 'hello' as hello, dblink_current_query() as query;
167
- hello | query
168
- -------+-----------------------------------------------------------
169
- hello | select 'hello' as hello, dblink_current_query() as query;
215
+ -- close the persistent connection
216
+ select dblink_disconnect();
217
+ dblink_disconnect
218
+ -------------------
219
+ OK
170
220
(1 row)
171
221
172
- -- list the primary key fields
173
- select * from dblink_get_pkey('foo');
174
- position | colname
175
- ----------+---------
176
- 1 | f1
177
- 2 | f2
178
- (2 rows)
222
+ -- now wait for the connection to the slave to be cleared before
223
+ -- we try to drop the database
224
+ CREATE FUNCTION wait() RETURNS TEXT AS '
225
+ DECLARE
226
+ rec record;
227
+ cntr int;
228
+ BEGIN
229
+ cntr = 0;
179
230
180
- -- build an insert statement based on a local tuple,
181
- -- replacing the primary key values with new ones
182
- select dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
183
- dblink_build_sql_insert
184
- -----------------------------------------------------------
185
- INSERT INTO foo(f1,f2,f3) VALUES('99','xyz','{a0,b0,c0}')
186
- (1 row)
231
+ select into rec d.datname
232
+ from pg_database d,
233
+ (select pg_stat_get_backend_dbid(pg_stat_get_backend_idset()) AS dbid) b
234
+ where d.oid = b.dbid and d.datname = ''regression_slave'';
187
235
188
- -- build an update statement based on a local tuple,
189
- -- replacing the primary key values with new ones
190
- select dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
191
- dblink_build_sql_update
192
- ----------------------------------------------------------------------------------------
193
- UPDATE foo SET f1 = '99', f2 = 'xyz', f3 = '{a0,b0,c0}' WHERE f1 = '99' AND f2 = 'xyz'
194
- (1 row)
236
+ WHILE FOUND LOOP
237
+ cntr = cntr + 1;
195
238
196
- -- build a delete statement based on a local tuple,
197
- select dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}');
198
- dblink_build_sql_delete
199
- ---------------------------------------------
200
- DELETE FROM foo WHERE f1 = '0' AND f2 = 'a'
239
+ select into rec d.datname
240
+ from pg_database d,
241
+ (select pg_stat_get_backend_dbid(pg_stat_get_backend_idset()) AS dbid) b
242
+ where d.oid = b.dbid and d.datname = ''regression_slave'';
243
+
244
+ -- safety valve
245
+ if cntr > 1000 THEN
246
+ EXIT;
247
+ end if;
248
+ END LOOP;
249
+ RETURN ''OK'';
250
+ END;
251
+ ' LANGUAGE 'plpgsql';
252
+ SELECT wait();
253
+ wait
254
+ ------
255
+ OK
201
256
(1 row)
202
257
258
+ -- OK, safe to drop the slave
259
+ DROP DATABASE regression_slave;
0 commit comments