Skip to content

Commit cd7a3b6

Browse files
committed
Simplify handling of second database for dblink tests.
1 parent b66ba36 commit cd7a3b6

File tree

2 files changed

+43
-79
lines changed

2 files changed

+43
-79
lines changed

contrib/dblink/expected/dblink.out

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
--
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.
2+
-- First, create a slave database and define the functions and test data
3+
-- therein.
4+
--
5+
-- This initial hackery is to allow successive runs without failures.
56
--
7+
CREATE OR REPLACE FUNCTION conditional_drop() RETURNS text AS '
8+
DECLARE
9+
dbname text;
10+
BEGIN
11+
SELECT INTO dbname datname FROM pg_database WHERE datname = ''regression_slave'';
12+
IF FOUND THEN
13+
DROP DATABASE regression_slave;
14+
END IF;
15+
RETURN ''OK'';
16+
END;
17+
' LANGUAGE 'plpgsql';
18+
SELECT conditional_drop();
19+
conditional_drop
20+
------------------
21+
OK
22+
(1 row)
23+
624
CREATE DATABASE regression_slave;
725
\connect regression_slave
26+
-- Turn off echoing so that expected file does not depend on
27+
-- contents of dblink.sql.
828
\set ECHO none
929
create table foo(f1 int, f2 text, f3 text[], primary key (f1,f2));
1030
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'foo_pkey' for table 'foo'
@@ -219,41 +239,3 @@ select dblink_disconnect();
219239
OK
220240
(1 row)
221241

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;
230-
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'';
235-
236-
WHILE FOUND LOOP
237-
cntr = cntr + 1;
238-
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
256-
(1 row)
257-
258-
-- OK, safe to drop the slave
259-
DROP DATABASE regression_slave;

contrib/dblink/sql/dblink.sql

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
--
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.
2+
-- First, create a slave database and define the functions and test data
3+
-- therein.
4+
--
5+
-- This initial hackery is to allow successive runs without failures.
56
--
7+
CREATE OR REPLACE FUNCTION conditional_drop() RETURNS text AS '
8+
DECLARE
9+
dbname text;
10+
BEGIN
11+
SELECT INTO dbname datname FROM pg_database WHERE datname = ''regression_slave'';
12+
IF FOUND THEN
13+
DROP DATABASE regression_slave;
14+
END IF;
15+
RETURN ''OK'';
16+
END;
17+
' LANGUAGE 'plpgsql';
18+
SELECT conditional_drop();
19+
620
CREATE DATABASE regression_slave;
721
\connect regression_slave
22+
23+
-- Turn off echoing so that expected file does not depend on
24+
-- contents of dblink.sql.
825
\set ECHO none
926
\i dblink.sql
1027
\set ECHO all
@@ -112,38 +129,3 @@ select * from dblink('select * from foo') as t(a int, b text, c text[]) where a
112129

113130
-- close the persistent connection
114131
select dblink_disconnect();
115-
116-
-- now wait for the connection to the slave to be cleared before
117-
-- we try to drop the database
118-
CREATE FUNCTION wait() RETURNS TEXT AS '
119-
DECLARE
120-
rec record;
121-
cntr int;
122-
BEGIN
123-
cntr = 0;
124-
125-
select into rec d.datname
126-
from pg_database d,
127-
(select pg_stat_get_backend_dbid(pg_stat_get_backend_idset()) AS dbid) b
128-
where d.oid = b.dbid and d.datname = ''regression_slave'';
129-
130-
WHILE FOUND LOOP
131-
cntr = cntr + 1;
132-
133-
select into rec d.datname
134-
from pg_database d,
135-
(select pg_stat_get_backend_dbid(pg_stat_get_backend_idset()) AS dbid) b
136-
where d.oid = b.dbid and d.datname = ''regression_slave'';
137-
138-
-- safety valve
139-
if cntr > 1000 THEN
140-
EXIT;
141-
end if;
142-
END LOOP;
143-
RETURN ''OK'';
144-
END;
145-
' LANGUAGE 'plpgsql';
146-
SELECT wait();
147-
148-
-- OK, safe to drop the slave
149-
DROP DATABASE regression_slave;

0 commit comments

Comments
 (0)