From d724c314da727d21cd66c219a62d601e10693789 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Sat, 14 Sep 2002 20:28:54 +0000 Subject: [PATCH] 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_check.sh | 15 -- .../dblink.out} | 139 ++++++++++++------ .../{dblink.test.sql => sql/dblink.sql} | 94 +++++++++--- 5 files changed, 196 insertions(+), 83 deletions(-) delete mode 100644 contrib/dblink/dblink_check.sh rename contrib/dblink/{dblink.test.expected.out => expected/dblink.out} (76%) rename contrib/dblink/{dblink.test.sql => sql/dblink.sql} (64%) diff --git a/contrib/dblink/Makefile b/contrib/dblink/Makefile index 932dd8e6de..dc511d0fa3 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 b92801a5c5..9a2e05a20e 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_check.sh b/contrib/dblink/dblink_check.sh deleted file mode 100644 index a3893c5314..0000000000 --- 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/dblink.test.expected.out b/contrib/dblink/expected/dblink.out similarity index 76% rename from contrib/dblink/dblink.test.expected.out rename to contrib/dblink/expected/dblink.out index 9ede34da5e..d00056677c 100644 --- a/contrib/dblink/dblink.test.expected.out +++ b/contrib/dblink/expected/dblink.out @@ -1,4 +1,11 @@ -\connect dblink_test_slave +-- +-- 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"}'); @@ -11,9 +18,54 @@ 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 +-- 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=dblink_test_slave','select * from foo') as t(a int, b text, c text[]) where t.a > 7; +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} @@ -24,7 +76,7 @@ select * from dblink('dbname=dblink_test_slave','select * from foo') as t(a int, 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'); +select dblink_connect('dbname=regression_slave'); dblink_connect ---------------- OK @@ -94,14 +146,14 @@ select * from dblink('select * from foo') as t(a int, b text, c text[]) where t. 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); +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=dblink_test_slave'); +select dblink_connect('dbname=regression_slave'); dblink_connect ---------------- OK @@ -160,43 +212,48 @@ select * from dblink('select * from foo') as t(a int, b text, c text[]) where a ---+---+--- (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; +-- close the persistent connection +select dblink_disconnect(); + dblink_disconnect +------------------- + OK (1 row) --- list the primary key fields -select * from dblink_get_pkey('foo'); - position | colname -----------+--------- - 1 | f1 - 2 | f2 -(2 rows) +-- 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; --- 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' + 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/dblink.test.sql b/contrib/dblink/sql/dblink.sql similarity index 64% rename from contrib/dblink/dblink.test.sql rename to contrib/dblink/sql/dblink.sql index 29a31c6b53..cfc2684348 100644 --- a/contrib/dblink/dblink.test.sql +++ b/contrib/dblink/sql/dblink.sql @@ -1,4 +1,13 @@ -\connect dblink_test_slave +-- +-- 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"}'); @@ -12,16 +21,43 @@ 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 +-- 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=dblink_test_slave','select * from foo') as t(a int, b text, c text[]) where t.a > 7; +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=dblink_test_slave'); +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; @@ -50,10 +86,10 @@ select * from dblink('select * from foo') as t(a int, b text, c text[]) where t. -- 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); +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=dblink_test_slave'); +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 @@ -74,22 +110,40 @@ 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 +-- close the persistent connection +select dblink_disconnect(); --- show the currently executing query -select 'hello' as hello, dblink_current_query() as query; +-- 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; --- list the primary key fields -select * from dblink_get_pkey('foo'); + 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''; --- 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"}'); + WHILE FOUND LOOP + cntr = cntr + 1; --- 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"}'); + 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''; --- build a delete statement based on a local tuple, -select dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}'); + -- 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;