From d724c314da727d21cd66c219a62d601e10693789 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 14 Sep 2002 20:28:54 +0000 Subject: The attached adds GRANTs to PUBLIC for dblink functions, removes the non-standard regression test, and adds standard installcheck regression test support. The test creates a second database (regression_slave) and drops it again, in order to avoid the cheesy-ness of connecting back to the same database ;-) Joe Conway --- contrib/dblink/Makefile | 5 +- contrib/dblink/dblink.sql.in | 26 +++- contrib/dblink/dblink.test.expected.out | 202 ------------------------- contrib/dblink/dblink.test.sql | 95 ------------ contrib/dblink/dblink_check.sh | 15 -- contrib/dblink/expected/dblink.out | 259 ++++++++++++++++++++++++++++++++ contrib/dblink/sql/dblink.sql | 149 ++++++++++++++++++ 7 files changed, 432 insertions(+), 319 deletions(-) delete mode 100644 contrib/dblink/dblink.test.expected.out delete mode 100644 contrib/dblink/dblink.test.sql delete mode 100644 contrib/dblink/dblink_check.sh create mode 100644 contrib/dblink/expected/dblink.out create mode 100644 contrib/dblink/sql/dblink.sql (limited to 'contrib') diff --git a/contrib/dblink/Makefile b/contrib/dblink/Makefile index 932dd8e6de3..dc511d0fa31 100644 --- a/contrib/dblink/Makefile +++ b/contrib/dblink/Makefile @@ -1,4 +1,4 @@ -# $Header: /cvsroot/pgsql/contrib/dblink/Makefile,v 1.6 2002/09/03 04:01:05 tgl Exp $ +# $Header: /cvsroot/pgsql/contrib/dblink/Makefile,v 1.7 2002/09/14 20:28:54 tgl Exp $ subdir = contrib/dblink top_builddir = ../.. @@ -11,7 +11,6 @@ SHLIB_LINK = $(libpq) DATA_built = dblink.sql DOCS = README.dblink - -EXTRA_CLEAN = dblink.test.out dblink.test.diff +REGRESS = dblink include $(top_srcdir)/contrib/contrib-global.mk diff --git a/contrib/dblink/dblink.sql.in b/contrib/dblink/dblink.sql.in index b92801a5c51..9a2e05a20e9 100644 --- a/contrib/dblink/dblink.sql.in +++ b/contrib/dblink/dblink.sql.in @@ -29,10 +29,12 @@ CREATE OR REPLACE FUNCTION dblink_close (text) RETURNS text AS 'MODULE_PATHNAME','dblink_close' LANGUAGE 'c' WITH (isstrict); --- Note: if this is a first time install of dblink, the following DROP --- FUNCTION line is expected to fail. --- Comment out the following 4 lines if the DEPRECATED functions are used. -DROP FUNCTION dblink (text,text); +-- Note: if this is not a first time install of dblink, uncomment the +-- following DROP which prepares the database for the new, non-deprecated +-- version. +--DROP FUNCTION dblink (text,text); + +-- Comment out the following 3 lines if the DEPRECATED functions are used. CREATE OR REPLACE FUNCTION dblink (text,text) RETURNS setof record AS 'MODULE_PATHNAME','dblink_record' LANGUAGE 'c' WITH (isstrict); @@ -69,3 +71,19 @@ CREATE OR REPLACE FUNCTION dblink_build_sql_update (text, int2vector, int2, _tex CREATE OR REPLACE FUNCTION dblink_current_query () RETURNS text AS 'MODULE_PATHNAME','dblink_current_query' LANGUAGE 'c'; + +GRANT EXECUTE ON FUNCTION dblink_connect (text) TO PUBLIC; +GRANT EXECUTE ON FUNCTION dblink_disconnect () TO PUBLIC; +GRANT EXECUTE ON FUNCTION dblink_open (text,text) TO PUBLIC; +GRANT EXECUTE ON FUNCTION dblink_fetch (text,int) TO PUBLIC; +GRANT EXECUTE ON FUNCTION dblink_close (text) TO PUBLIC; +GRANT EXECUTE ON FUNCTION dblink (text,text) TO PUBLIC; +GRANT EXECUTE ON FUNCTION dblink (text) TO PUBLIC; +GRANT EXECUTE ON FUNCTION dblink_exec (text,text) TO PUBLIC; +GRANT EXECUTE ON FUNCTION dblink_exec (text) TO PUBLIC; +GRANT EXECUTE ON FUNCTION dblink_get_pkey (text) TO PUBLIC; +GRANT EXECUTE ON FUNCTION dblink_build_sql_insert (text, int2vector, int2, _text, _text) TO PUBLIC; +GRANT EXECUTE ON FUNCTION dblink_build_sql_delete (text, int2vector, int2, _text) TO PUBLIC; +GRANT EXECUTE ON FUNCTION dblink_build_sql_update (text, int2vector, int2, _text, _text) TO PUBLIC; +GRANT EXECUTE ON FUNCTION dblink_current_query () TO PUBLIC; + diff --git a/contrib/dblink/dblink.test.expected.out b/contrib/dblink/dblink.test.expected.out deleted file mode 100644 index 9ede34da5e8..00000000000 --- a/contrib/dblink/dblink.test.expected.out +++ /dev/null @@ -1,202 +0,0 @@ -\connect dblink_test_slave -create table foo(f1 int, f2 text, f3 text[], primary key (f1,f2)); -NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'foo_pkey' for table 'foo' -insert into foo values(0,'a','{"a0","b0","c0"}'); -insert into foo values(1,'b','{"a1","b1","c1"}'); -insert into foo values(2,'c','{"a2","b2","c2"}'); -insert into foo values(3,'d','{"a3","b3","c3"}'); -insert into foo values(4,'e','{"a4","b4","c4"}'); -insert into foo values(5,'f','{"a5","b5","c5"}'); -insert into foo values(6,'g','{"a6","b6","c6"}'); -insert into foo values(7,'h','{"a7","b7","c7"}'); -insert into foo values(8,'i','{"a8","b8","c8"}'); -insert into foo values(9,'j','{"a9","b9","c9"}'); -\connect dblink_test_master --- regular old dblink -select * from dblink('dbname=dblink_test_slave','select * from foo') as t(a int, b text, c text[]) where t.a > 7; - a | b | c ----+---+------------ - 8 | i | {a8,b8,c8} - 9 | j | {a9,b9,c9} -(2 rows) - --- should generate "no connection available" error -select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7; -ERROR: dblink: no connection available --- create a persistent connection -select dblink_connect('dbname=dblink_test_slave'); - dblink_connect ----------------- - OK -(1 row) - --- use the persistent connection -select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7; - a | b | c ----+---+------------ - 8 | i | {a8,b8,c8} - 9 | j | {a9,b9,c9} -(2 rows) - --- open a cursor -select dblink_open('rmt_foo_cursor','select * from foo'); - dblink_open -------------- - OK -(1 row) - --- fetch some data -select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); - a | b | c ----+---+------------ - 0 | a | {a0,b0,c0} - 1 | b | {a1,b1,c1} - 2 | c | {a2,b2,c2} - 3 | d | {a3,b3,c3} -(4 rows) - -select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); - a | b | c ----+---+------------ - 4 | e | {a4,b4,c4} - 5 | f | {a5,b5,c5} - 6 | g | {a6,b6,c6} - 7 | h | {a7,b7,c7} -(4 rows) - --- this one only finds two rows left -select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); - a | b | c ----+---+------------ - 8 | i | {a8,b8,c8} - 9 | j | {a9,b9,c9} -(2 rows) - --- close the cursor -select dblink_close('rmt_foo_cursor'); - dblink_close --------------- - OK -(1 row) - --- should generate "cursor rmt_foo_cursor does not exist" error -select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); -ERROR: dblink_fetch: cursor rmt_foo_cursor does not exist --- close the persistent connection -select dblink_disconnect(); - dblink_disconnect -------------------- - OK -(1 row) - --- should generate "no connection available" error -select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7; -ERROR: dblink: no connection available --- put more data into our slave table, first using arbitrary connection syntax --- but truncate the actual return value so we can use diff to check for success -select substr(dblink_exec('dbname=dblink_test_slave','insert into foo values(10,''k'',''{"a10","b10","c10"}'')'),1,6); - substr --------- - INSERT -(1 row) - --- create a persistent connection -select dblink_connect('dbname=dblink_test_slave'); - dblink_connect ----------------- - OK -(1 row) - --- put more data into our slave table, using persistent connection syntax --- but truncate the actual return value so we can use diff to check for success -select substr(dblink_exec('insert into foo values(11,''l'',''{"a11","b11","c11"}'')'),1,6); - substr --------- - INSERT -(1 row) - --- let's see it -select * from dblink('select * from foo') as t(a int, b text, c text[]); - a | b | c -----+---+--------------- - 0 | a | {a0,b0,c0} - 1 | b | {a1,b1,c1} - 2 | c | {a2,b2,c2} - 3 | d | {a3,b3,c3} - 4 | e | {a4,b4,c4} - 5 | f | {a5,b5,c5} - 6 | g | {a6,b6,c6} - 7 | h | {a7,b7,c7} - 8 | i | {a8,b8,c8} - 9 | j | {a9,b9,c9} - 10 | k | {a10,b10,c10} - 11 | l | {a11,b11,c11} -(12 rows) - --- change some data -select dblink_exec('update foo set f3[2] = ''b99'' where f1 = 11'); - dblink_exec -------------- - UPDATE 1 -(1 row) - --- let's see it -select * from dblink('select * from foo') as t(a int, b text, c text[]) where a = 11; - a | b | c -----+---+--------------- - 11 | l | {a11,b99,c11} -(1 row) - --- delete some data -select dblink_exec('delete from foo where f1 = 11'); - dblink_exec -------------- - DELETE 1 -(1 row) - --- let's see it -select * from dblink('select * from foo') as t(a int, b text, c text[]) where a = 11; - a | b | c ----+---+--- -(0 rows) - --- misc utilities -\connect dblink_test_slave --- show the currently executing query -select 'hello' as hello, dblink_current_query() as query; - hello | query --------+----------------------------------------------------------- - hello | select 'hello' as hello, dblink_current_query() as query; -(1 row) - --- list the primary key fields -select * from dblink_get_pkey('foo'); - position | colname -----------+--------- - 1 | f1 - 2 | f2 -(2 rows) - --- build an insert statement based on a local tuple, --- replacing the primary key values with new ones -select dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}'); - dblink_build_sql_insert ------------------------------------------------------------ - INSERT INTO foo(f1,f2,f3) VALUES('99','xyz','{a0,b0,c0}') -(1 row) - --- build an update statement based on a local tuple, --- replacing the primary key values with new ones -select dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}'); - dblink_build_sql_update ----------------------------------------------------------------------------------------- - UPDATE foo SET f1 = '99', f2 = 'xyz', f3 = '{a0,b0,c0}' WHERE f1 = '99' AND f2 = 'xyz' -(1 row) - --- build a delete statement based on a local tuple, -select dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}'); - dblink_build_sql_delete ---------------------------------------------- - DELETE FROM foo WHERE f1 = '0' AND f2 = 'a' -(1 row) - diff --git a/contrib/dblink/dblink.test.sql b/contrib/dblink/dblink.test.sql deleted file mode 100644 index 29a31c6b534..00000000000 --- a/contrib/dblink/dblink.test.sql +++ /dev/null @@ -1,95 +0,0 @@ -\connect dblink_test_slave - -create table foo(f1 int, f2 text, f3 text[], primary key (f1,f2)); -insert into foo values(0,'a','{"a0","b0","c0"}'); -insert into foo values(1,'b','{"a1","b1","c1"}'); -insert into foo values(2,'c','{"a2","b2","c2"}'); -insert into foo values(3,'d','{"a3","b3","c3"}'); -insert into foo values(4,'e','{"a4","b4","c4"}'); -insert into foo values(5,'f','{"a5","b5","c5"}'); -insert into foo values(6,'g','{"a6","b6","c6"}'); -insert into foo values(7,'h','{"a7","b7","c7"}'); -insert into foo values(8,'i','{"a8","b8","c8"}'); -insert into foo values(9,'j','{"a9","b9","c9"}'); - -\connect dblink_test_master - --- regular old dblink -select * from dblink('dbname=dblink_test_slave','select * from foo') as t(a int, b text, c text[]) where t.a > 7; - --- should generate "no connection available" error -select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7; - --- create a persistent connection -select dblink_connect('dbname=dblink_test_slave'); - --- use the persistent connection -select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7; - --- open a cursor -select dblink_open('rmt_foo_cursor','select * from foo'); - --- fetch some data -select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); -select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); - --- this one only finds two rows left -select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); - --- close the cursor -select dblink_close('rmt_foo_cursor'); - --- should generate "cursor rmt_foo_cursor does not exist" error -select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); - --- close the persistent connection -select dblink_disconnect(); - --- should generate "no connection available" error -select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7; - --- put more data into our slave table, first using arbitrary connection syntax --- but truncate the actual return value so we can use diff to check for success -select substr(dblink_exec('dbname=dblink_test_slave','insert into foo values(10,''k'',''{"a10","b10","c10"}'')'),1,6); - --- create a persistent connection -select dblink_connect('dbname=dblink_test_slave'); - --- put more data into our slave table, using persistent connection syntax --- but truncate the actual return value so we can use diff to check for success -select substr(dblink_exec('insert into foo values(11,''l'',''{"a11","b11","c11"}'')'),1,6); - --- let's see it -select * from dblink('select * from foo') as t(a int, b text, c text[]); - --- change some data -select dblink_exec('update foo set f3[2] = ''b99'' where f1 = 11'); - --- let's see it -select * from dblink('select * from foo') as t(a int, b text, c text[]) where a = 11; - --- delete some data -select dblink_exec('delete from foo where f1 = 11'); - --- let's see it -select * from dblink('select * from foo') as t(a int, b text, c text[]) where a = 11; - --- misc utilities -\connect dblink_test_slave - --- show the currently executing query -select 'hello' as hello, dblink_current_query() as query; - --- list the primary key fields -select * from dblink_get_pkey('foo'); - --- build an insert statement based on a local tuple, --- replacing the primary key values with new ones -select dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}'); - --- build an update statement based on a local tuple, --- replacing the primary key values with new ones -select dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}'); - --- build a delete statement based on a local tuple, -select dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}'); diff --git a/contrib/dblink/dblink_check.sh b/contrib/dblink/dblink_check.sh deleted file mode 100644 index a3893c5314a..00000000000 --- a/contrib/dblink/dblink_check.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh - -dropdb -U postgres dblink_test_master -createdb -U postgres dblink_test_master -psql -U postgres dblink_test_master < `pwd`/dblink.sql - -dropdb -U postgres dblink_test_slave -createdb -U postgres dblink_test_slave -psql -U postgres dblink_test_slave < `pwd`/dblink.sql - -psql -eaq -U postgres template1 < `pwd`/dblink.test.sql > dblink.test.out 2>&1 -diff -c ./dblink.test.expected.out `pwd`/dblink.test.out > dblink.test.diff -ls -l dblink.test.diff - - diff --git a/contrib/dblink/expected/dblink.out b/contrib/dblink/expected/dblink.out new file mode 100644 index 00000000000..d00056677ce --- /dev/null +++ b/contrib/dblink/expected/dblink.out @@ -0,0 +1,259 @@ +-- +-- First, create a slave database and define the functions. +-- Turn off echoing so that expected file does not depend on +-- contents of dblink.sql. +-- +CREATE DATABASE regression_slave; +\connect regression_slave +\set ECHO none +create table foo(f1 int, f2 text, f3 text[], primary key (f1,f2)); +NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'foo_pkey' for table 'foo' +insert into foo values(0,'a','{"a0","b0","c0"}'); +insert into foo values(1,'b','{"a1","b1","c1"}'); +insert into foo values(2,'c','{"a2","b2","c2"}'); +insert into foo values(3,'d','{"a3","b3","c3"}'); +insert into foo values(4,'e','{"a4","b4","c4"}'); +insert into foo values(5,'f','{"a5","b5","c5"}'); +insert into foo values(6,'g','{"a6","b6","c6"}'); +insert into foo values(7,'h','{"a7","b7","c7"}'); +insert into foo values(8,'i','{"a8","b8","c8"}'); +insert into foo values(9,'j','{"a9","b9","c9"}'); +-- misc utilities +-- show the currently executing query +select 'hello' as hello, dblink_current_query() as query; + hello | query +-------+----------------------------------------------------------- + hello | select 'hello' as hello, dblink_current_query() as query; +(1 row) + +-- list the primary key fields +select * from dblink_get_pkey('foo'); + position | colname +----------+--------- + 1 | f1 + 2 | f2 +(2 rows) + +-- build an insert statement based on a local tuple, +-- replacing the primary key values with new ones +select dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}'); + dblink_build_sql_insert +----------------------------------------------------------- + INSERT INTO foo(f1,f2,f3) VALUES('99','xyz','{a0,b0,c0}') +(1 row) + +-- build an update statement based on a local tuple, +-- replacing the primary key values with new ones +select dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}'); + dblink_build_sql_update +---------------------------------------------------------------------------------------- + UPDATE foo SET f1 = '99', f2 = 'xyz', f3 = '{a0,b0,c0}' WHERE f1 = '99' AND f2 = 'xyz' +(1 row) + +-- build a delete statement based on a local tuple, +select dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}'); + dblink_build_sql_delete +--------------------------------------------- + DELETE FROM foo WHERE f1 = '0' AND f2 = 'a' +(1 row) + +-- +-- Connect back to the regression database and define the functions. +-- Turn off echoing so that expected file does not depend on +-- contents of dblink.sql. +-- +\connect regression +\set ECHO none +-- regular old dblink +select * from dblink('dbname=regression_slave','select * from foo') as t(a int, b text, c text[]) where t.a > 7; + a | b | c +---+---+------------ + 8 | i | {a8,b8,c8} + 9 | j | {a9,b9,c9} +(2 rows) + +-- should generate "no connection available" error +select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7; +ERROR: dblink: no connection available +-- create a persistent connection +select dblink_connect('dbname=regression_slave'); + dblink_connect +---------------- + OK +(1 row) + +-- use the persistent connection +select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7; + a | b | c +---+---+------------ + 8 | i | {a8,b8,c8} + 9 | j | {a9,b9,c9} +(2 rows) + +-- open a cursor +select dblink_open('rmt_foo_cursor','select * from foo'); + dblink_open +------------- + OK +(1 row) + +-- fetch some data +select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); + a | b | c +---+---+------------ + 0 | a | {a0,b0,c0} + 1 | b | {a1,b1,c1} + 2 | c | {a2,b2,c2} + 3 | d | {a3,b3,c3} +(4 rows) + +select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); + a | b | c +---+---+------------ + 4 | e | {a4,b4,c4} + 5 | f | {a5,b5,c5} + 6 | g | {a6,b6,c6} + 7 | h | {a7,b7,c7} +(4 rows) + +-- this one only finds two rows left +select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); + a | b | c +---+---+------------ + 8 | i | {a8,b8,c8} + 9 | j | {a9,b9,c9} +(2 rows) + +-- close the cursor +select dblink_close('rmt_foo_cursor'); + dblink_close +-------------- + OK +(1 row) + +-- should generate "cursor rmt_foo_cursor does not exist" error +select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); +ERROR: dblink_fetch: cursor rmt_foo_cursor does not exist +-- close the persistent connection +select dblink_disconnect(); + dblink_disconnect +------------------- + OK +(1 row) + +-- should generate "no connection available" error +select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7; +ERROR: dblink: no connection available +-- put more data into our slave table, first using arbitrary connection syntax +-- but truncate the actual return value so we can use diff to check for success +select substr(dblink_exec('dbname=regression_slave','insert into foo values(10,''k'',''{"a10","b10","c10"}'')'),1,6); + substr +-------- + INSERT +(1 row) + +-- create a persistent connection +select dblink_connect('dbname=regression_slave'); + dblink_connect +---------------- + OK +(1 row) + +-- put more data into our slave table, using persistent connection syntax +-- but truncate the actual return value so we can use diff to check for success +select substr(dblink_exec('insert into foo values(11,''l'',''{"a11","b11","c11"}'')'),1,6); + substr +-------- + INSERT +(1 row) + +-- let's see it +select * from dblink('select * from foo') as t(a int, b text, c text[]); + a | b | c +----+---+--------------- + 0 | a | {a0,b0,c0} + 1 | b | {a1,b1,c1} + 2 | c | {a2,b2,c2} + 3 | d | {a3,b3,c3} + 4 | e | {a4,b4,c4} + 5 | f | {a5,b5,c5} + 6 | g | {a6,b6,c6} + 7 | h | {a7,b7,c7} + 8 | i | {a8,b8,c8} + 9 | j | {a9,b9,c9} + 10 | k | {a10,b10,c10} + 11 | l | {a11,b11,c11} +(12 rows) + +-- change some data +select dblink_exec('update foo set f3[2] = ''b99'' where f1 = 11'); + dblink_exec +------------- + UPDATE 1 +(1 row) + +-- let's see it +select * from dblink('select * from foo') as t(a int, b text, c text[]) where a = 11; + a | b | c +----+---+--------------- + 11 | l | {a11,b99,c11} +(1 row) + +-- delete some data +select dblink_exec('delete from foo where f1 = 11'); + dblink_exec +------------- + DELETE 1 +(1 row) + +-- let's see it +select * from dblink('select * from foo') as t(a int, b text, c text[]) where a = 11; + a | b | c +---+---+--- +(0 rows) + +-- close the persistent connection +select dblink_disconnect(); + dblink_disconnect +------------------- + OK +(1 row) + +-- now wait for the connection to the slave to be cleared before +-- we try to drop the database +CREATE FUNCTION wait() RETURNS TEXT AS ' +DECLARE + rec record; + cntr int; +BEGIN + cntr = 0; + + select into rec d.datname + from pg_database d, + (select pg_stat_get_backend_dbid(pg_stat_get_backend_idset()) AS dbid) b + where d.oid = b.dbid and d.datname = ''regression_slave''; + + WHILE FOUND LOOP + cntr = cntr + 1; + + select into rec d.datname + from pg_database d, + (select pg_stat_get_backend_dbid(pg_stat_get_backend_idset()) AS dbid) b + where d.oid = b.dbid and d.datname = ''regression_slave''; + + -- safety valve + if cntr > 1000 THEN + EXIT; + end if; + END LOOP; + RETURN ''OK''; +END; +' LANGUAGE 'plpgsql'; +SELECT wait(); + wait +------ + OK +(1 row) + +-- OK, safe to drop the slave +DROP DATABASE regression_slave; diff --git a/contrib/dblink/sql/dblink.sql b/contrib/dblink/sql/dblink.sql new file mode 100644 index 00000000000..cfc2684348c --- /dev/null +++ b/contrib/dblink/sql/dblink.sql @@ -0,0 +1,149 @@ +-- +-- First, create a slave database and define the functions. +-- Turn off echoing so that expected file does not depend on +-- contents of dblink.sql. +-- +CREATE DATABASE regression_slave; +\connect regression_slave +\set ECHO none +\i dblink.sql +\set ECHO all + +create table foo(f1 int, f2 text, f3 text[], primary key (f1,f2)); +insert into foo values(0,'a','{"a0","b0","c0"}'); +insert into foo values(1,'b','{"a1","b1","c1"}'); +insert into foo values(2,'c','{"a2","b2","c2"}'); +insert into foo values(3,'d','{"a3","b3","c3"}'); +insert into foo values(4,'e','{"a4","b4","c4"}'); +insert into foo values(5,'f','{"a5","b5","c5"}'); +insert into foo values(6,'g','{"a6","b6","c6"}'); +insert into foo values(7,'h','{"a7","b7","c7"}'); +insert into foo values(8,'i','{"a8","b8","c8"}'); +insert into foo values(9,'j','{"a9","b9","c9"}'); + +-- misc utilities + +-- show the currently executing query +select 'hello' as hello, dblink_current_query() as query; + +-- list the primary key fields +select * from dblink_get_pkey('foo'); + +-- build an insert statement based on a local tuple, +-- replacing the primary key values with new ones +select dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}'); + +-- build an update statement based on a local tuple, +-- replacing the primary key values with new ones +select dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}'); + +-- build a delete statement based on a local tuple, +select dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}'); + +-- +-- Connect back to the regression database and define the functions. +-- Turn off echoing so that expected file does not depend on +-- contents of dblink.sql. +-- +\connect regression +\set ECHO none +\i dblink.sql +\set ECHO all + +-- regular old dblink +select * from dblink('dbname=regression_slave','select * from foo') as t(a int, b text, c text[]) where t.a > 7; + +-- should generate "no connection available" error +select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7; + +-- create a persistent connection +select dblink_connect('dbname=regression_slave'); + +-- use the persistent connection +select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7; + +-- open a cursor +select dblink_open('rmt_foo_cursor','select * from foo'); + +-- fetch some data +select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); +select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); + +-- this one only finds two rows left +select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); + +-- close the cursor +select dblink_close('rmt_foo_cursor'); + +-- should generate "cursor rmt_foo_cursor does not exist" error +select * from dblink_fetch('rmt_foo_cursor',4) as t(a int, b text, c text[]); + +-- close the persistent connection +select dblink_disconnect(); + +-- should generate "no connection available" error +select * from dblink('select * from foo') as t(a int, b text, c text[]) where t.a > 7; + +-- put more data into our slave table, first using arbitrary connection syntax +-- but truncate the actual return value so we can use diff to check for success +select substr(dblink_exec('dbname=regression_slave','insert into foo values(10,''k'',''{"a10","b10","c10"}'')'),1,6); + +-- create a persistent connection +select dblink_connect('dbname=regression_slave'); + +-- put more data into our slave table, using persistent connection syntax +-- but truncate the actual return value so we can use diff to check for success +select substr(dblink_exec('insert into foo values(11,''l'',''{"a11","b11","c11"}'')'),1,6); + +-- let's see it +select * from dblink('select * from foo') as t(a int, b text, c text[]); + +-- change some data +select dblink_exec('update foo set f3[2] = ''b99'' where f1 = 11'); + +-- let's see it +select * from dblink('select * from foo') as t(a int, b text, c text[]) where a = 11; + +-- delete some data +select dblink_exec('delete from foo where f1 = 11'); + +-- let's see it +select * from dblink('select * from foo') as t(a int, b text, c text[]) where a = 11; + +-- close the persistent connection +select dblink_disconnect(); + +-- now wait for the connection to the slave to be cleared before +-- we try to drop the database +CREATE FUNCTION wait() RETURNS TEXT AS ' +DECLARE + rec record; + cntr int; +BEGIN + cntr = 0; + + select into rec d.datname + from pg_database d, + (select pg_stat_get_backend_dbid(pg_stat_get_backend_idset()) AS dbid) b + where d.oid = b.dbid and d.datname = ''regression_slave''; + + WHILE FOUND LOOP + cntr = cntr + 1; + + select into rec d.datname + from pg_database d, + (select pg_stat_get_backend_dbid(pg_stat_get_backend_idset()) AS dbid) b + where d.oid = b.dbid and d.datname = ''regression_slave''; + + -- safety valve + if cntr > 1000 THEN + EXIT; + end if; + END LOOP; + RETURN ''OK''; +END; +' LANGUAGE 'plpgsql'; +SELECT wait(); + +-- OK, safe to drop the slave +DROP DATABASE regression_slave; -- cgit v1.2.3