| 1 |
CREATE EXTENSION dblink; |
| 2 |
|
| 3 |
-- directory paths and dlsuffix are passed to us in environment variables |
| 4 |
\getenv abs_srcdir PG_ABS_SRCDIR |
| 5 |
\getenv libdir PG_LIBDIR |
| 6 |
\getenv dlsuffix PG_DLSUFFIX |
| 7 |
|
| 8 |
\set regresslib :libdir '/regress' :dlsuffix |
| 9 |
|
| 10 |
-- create some functions needed for tests |
| 11 |
CREATE FUNCTION setenv(text, text) |
| 12 |
RETURNS void |
| 13 |
AS :'regresslib', 'regress_setenv' |
| 14 |
LANGUAGE C STRICT; |
| 15 |
|
| 16 |
CREATE FUNCTION wait_pid(int) |
| 17 |
RETURNS void |
| 18 |
AS :'regresslib' |
| 19 |
LANGUAGE C STRICT; |
| 20 |
|
| 21 |
\set path :abs_srcdir '/' |
| 22 |
\set fnbody 'SELECT setenv(''PGSERVICEFILE'', ' :'path' ' || $1)' |
| 23 |
CREATE FUNCTION set_pgservicefile(text) RETURNS void LANGUAGE SQL |
| 24 |
AS :'fnbody'; |
| 25 |
|
| 26 |
-- want context for notices |
| 27 |
\set SHOW_CONTEXT always |
| 28 |
|
| 29 |
CREATE TABLE foo(f1 int, f2 text, f3 text[], primary key (f1,f2)); |
| 30 |
INSERT INTO foo VALUES (0,'a','{"a0","b0","c0"}'); |
| 31 |
INSERT INTO foo VALUES (1,'b','{"a1","b1","c1"}'); |
| 32 |
INSERT INTO foo VALUES (2,'c','{"a2","b2","c2"}'); |
| 33 |
INSERT INTO foo VALUES (3,'d','{"a3","b3","c3"}'); |
| 34 |
INSERT INTO foo VALUES (4,'e','{"a4","b4","c4"}'); |
| 35 |
INSERT INTO foo VALUES (5,'f','{"a5","b5","c5"}'); |
| 36 |
INSERT INTO foo VALUES (6,'g','{"a6","b6","c6"}'); |
| 37 |
INSERT INTO foo VALUES (7,'h','{"a7","b7","c7"}'); |
| 38 |
INSERT INTO foo VALUES (8,'i','{"a8","b8","c8"}'); |
| 39 |
INSERT INTO foo VALUES (9,'j','{"a9","b9","c9"}'); |
| 40 |
|
| 41 |
-- misc utilities |
| 42 |
|
| 43 |
-- list the primary key fields |
| 44 |
SELECT * |
| 45 |
FROM dblink_get_pkey('foo'); |
| 46 |
|
| 47 |
-- build an insert statement based on a local tuple, |
| 48 |
-- replacing the primary key values with new ones |
| 49 |
SELECT dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}'); |
| 50 |
-- too many pk fields, should fail |
| 51 |
SELECT dblink_build_sql_insert('foo','1 2 3 4',4,'{"0", "a", "{a0,b0,c0}"}','{"99", "xyz", "{za0,zb0,zc0}"}'); |
| 52 |
|
| 53 |
-- build an update statement based on a local tuple, |
| 54 |
-- replacing the primary key values with new ones |
| 55 |
SELECT dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}'); |
| 56 |
-- too many pk fields, should fail |
| 57 |
SELECT dblink_build_sql_update('foo','1 2 3 4',4,'{"0", "a", "{a0,b0,c0}"}','{"99", "xyz", "{za0,zb0,zc0}"}'); |
| 58 |
|
| 59 |
-- build a delete statement based on a local tuple, |
| 60 |
SELECT dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}'); |
| 61 |
-- too many pk fields, should fail |
| 62 |
SELECT dblink_build_sql_delete('foo','1 2 3 4',4,'{"0", "a", "{a0,b0,c0}"}'); |
| 63 |
|
| 64 |
-- repeat the test for table with primary key index with included columns |
| 65 |
CREATE TABLE foo_1(f1 int, f2 text, f3 text[], primary key (f1,f2) include (f3)); |
| 66 |
INSERT INTO foo_1 VALUES (0,'a','{"a0","b0","c0"}'); |
| 67 |
INSERT INTO foo_1 VALUES (1,'b','{"a1","b1","c1"}'); |
| 68 |
INSERT INTO foo_1 VALUES (2,'c','{"a2","b2","c2"}'); |
| 69 |
INSERT INTO foo_1 VALUES (3,'d','{"a3","b3","c3"}'); |
| 70 |
INSERT INTO foo_1 VALUES (4,'e','{"a4","b4","c4"}'); |
| 71 |
INSERT INTO foo_1 VALUES (5,'f','{"a5","b5","c5"}'); |
| 72 |
INSERT INTO foo_1 VALUES (6,'g','{"a6","b6","c6"}'); |
| 73 |
INSERT INTO foo_1 VALUES (7,'h','{"a7","b7","c7"}'); |
| 74 |
INSERT INTO foo_1 VALUES (8,'i','{"a8","b8","c8"}'); |
| 75 |
INSERT INTO foo_1 VALUES (9,'j','{"a9","b9","c9"}'); |
| 76 |
|
| 77 |
-- misc utilities |
| 78 |
|
| 79 |
-- list the primary key fields |
| 80 |
SELECT * |
| 81 |
FROM dblink_get_pkey('foo_1'); |
| 82 |
|
| 83 |
-- build an insert statement based on a local tuple, |
| 84 |
-- replacing the primary key values with new ones |
| 85 |
SELECT dblink_build_sql_insert('foo_1','1 2',2,'{"0", "a"}','{"99", "xyz"}'); |
| 86 |
-- too many pk fields, should fail |
| 87 |
SELECT dblink_build_sql_insert('foo_1','1 2 3 4',4,'{"0", "a", "{a0,b0,c0}"}','{"99", "xyz", "{za0,zb0,zc0}"}'); |
| 88 |
|
| 89 |
-- build an update statement based on a local tuple, |
| 90 |
-- replacing the primary key values with new ones |
| 91 |
SELECT dblink_build_sql_update('foo_1','1 2',2,'{"0", "a"}','{"99", "xyz"}'); |
| 92 |
-- too many pk fields, should fail |
| 93 |
SELECT dblink_build_sql_update('foo_1','1 2 3 4',4,'{"0", "a", "{a0,b0,c0}"}','{"99", "xyz", "{za0,zb0,zc0}"}'); |
| 94 |
|
| 95 |
-- build a delete statement based on a local tuple, |
| 96 |
SELECT dblink_build_sql_delete('foo_1','1 2',2,'{"0", "a"}'); |
| 97 |
-- too many pk fields, should fail |
| 98 |
SELECT dblink_build_sql_delete('foo_1','1 2 3 4',4,'{"0", "a", "{a0,b0,c0}"}'); |
| 99 |
|
| 100 |
DROP TABLE foo_1; |
| 101 |
|
| 102 |
-- retest using a quoted and schema qualified table |
| 103 |
CREATE SCHEMA "MySchema"; |
| 104 |
CREATE TABLE "MySchema"."Foo"(f1 int, f2 text, f3 text[], primary key (f1,f2)); |
| 105 |
INSERT INTO "MySchema"."Foo" VALUES (0,'a','{"a0","b0","c0"}'); |
| 106 |
|
| 107 |
-- list the primary key fields |
| 108 |
SELECT * |
| 109 |
FROM dblink_get_pkey('"MySchema"."Foo"'); |
| 110 |
|
| 111 |
-- build an insert statement based on a local tuple, |
| 112 |
-- replacing the primary key values with new ones |
| 113 |
SELECT dblink_build_sql_insert('"MySchema"."Foo"','1 2',2,'{"0", "a"}','{"99", "xyz"}'); |
| 114 |
|
| 115 |
-- build an update statement based on a local tuple, |
| 116 |
-- replacing the primary key values with new ones |
| 117 |
SELECT dblink_build_sql_update('"MySchema"."Foo"','1 2',2,'{"0", "a"}','{"99", "xyz"}'); |
| 118 |
|
| 119 |
-- build a delete statement based on a local tuple, |
| 120 |
SELECT dblink_build_sql_delete('"MySchema"."Foo"','1 2',2,'{"0", "a"}'); |
| 121 |
|
| 122 |
CREATE FUNCTION connection_parameters() RETURNS text LANGUAGE SQL AS $f$ |
| 123 |
SELECT $$dbname='$$||current_database()||$$' port=$$||current_setting('port'); |
| 124 |
$f$; |
| 125 |
|
| 126 |
-- regular old dblink |
| 127 |
SELECT * |
| 128 |
FROM dblink(connection_parameters(),'SELECT * FROM foo') AS t(a int, b text, c text[]) |
| 129 |
WHERE t.a > 7; |
| 130 |
|
| 131 |
-- should generate "connection not available" error |
| 132 |
SELECT * |
| 133 |
FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[]) |
| 134 |
WHERE t.a > 7; |
| 135 |
|
| 136 |
-- The first-level connection's backend will crash on exit given OpenLDAP |
| 137 |
-- [2.4.24, 2.4.31]. We won't see evidence of any crash until the victim |
| 138 |
-- process terminates and the postmaster responds. If process termination |
| 139 |
-- entails writing a core dump, that can take awhile. Wait for the process to |
| 140 |
-- vanish. At that point, the postmaster has called waitpid() on the crashed |
| 141 |
-- process, and it will accept no new connections until it has reinitialized |
| 142 |
-- the cluster. (We can't exploit pg_stat_activity, because the crash happens |
| 143 |
-- after the backend updates shared memory to reflect its impending exit.) |
| 144 |
DO $pl$ |
| 145 |
DECLARE |
| 146 |
detail text; |
| 147 |
BEGIN |
| 148 |
PERFORM wait_pid(crash_pid) |
| 149 |
FROM dblink(connection_parameters(), $$ |
| 150 |
SELECT pg_backend_pid() FROM dblink( |
| 151 |
'service=test_ldap '||connection_parameters(), |
| 152 |
-- This string concatenation is a hack to shoehorn a |
| 153 |
-- set_pgservicefile call into the SQL statement. |
| 154 |
'SELECT 1' || set_pgservicefile('pg_service.conf') |
| 155 |
) t(c int) |
| 156 |
$$) AS t(crash_pid int); |
| 157 |
EXCEPTION WHEN OTHERS THEN |
| 158 |
GET STACKED DIAGNOSTICS detail = PG_EXCEPTION_DETAIL; |
| 159 |
-- Expected error in a non-LDAP build. |
| 160 |
IF NOT detail LIKE 'syntax error in service file%' THEN RAISE; END IF; |
| 161 |
END |
| 162 |
$pl$; |
| 163 |
|
| 164 |
-- create a persistent connection |
| 165 |
SELECT dblink_connect(connection_parameters()); |
| 166 |
|
| 167 |
-- use the persistent connection |
| 168 |
SELECT * |
| 169 |
FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[]) |
| 170 |
WHERE t.a > 7; |
| 171 |
|
| 172 |
-- open a cursor with bad SQL and fail_on_error set to false |
| 173 |
SELECT dblink_open('rmt_foo_cursor','SELECT * FROM foobar',false); |
| 174 |
|
| 175 |
-- reset remote transaction state |
| 176 |
SELECT dblink_exec('ABORT'); |
| 177 |
|
| 178 |
-- open a cursor |
| 179 |
SELECT dblink_open('rmt_foo_cursor','SELECT * FROM foo'); |
| 180 |
|
| 181 |
-- close the cursor |
| 182 |
SELECT dblink_close('rmt_foo_cursor',false); |
| 183 |
|
| 184 |
-- open the cursor again |
| 185 |
SELECT dblink_open('rmt_foo_cursor','SELECT * FROM foo'); |
| 186 |
|
| 187 |
-- fetch some data |
| 188 |
SELECT * |
| 189 |
FROM dblink_fetch('rmt_foo_cursor',4) AS t(a int, b text, c text[]); |
| 190 |
|
| 191 |
SELECT * |
| 192 |
FROM dblink_fetch('rmt_foo_cursor',4) AS t(a int, b text, c text[]); |
| 193 |
|
| 194 |
-- this one only finds two rows left |
| 195 |
SELECT * |
| 196 |
FROM dblink_fetch('rmt_foo_cursor',4) AS t(a int, b text, c text[]); |
| 197 |
|
| 198 |
-- intentionally botch a fetch |
| 199 |
SELECT * |
| 200 |
FROM dblink_fetch('rmt_foobar_cursor',4,false) AS t(a int, b text, c text[]); |
| 201 |
|
| 202 |
-- reset remote transaction state |
| 203 |
SELECT dblink_exec('ABORT'); |
| 204 |
|
| 205 |
-- close the wrong cursor |
| 206 |
SELECT dblink_close('rmt_foobar_cursor',false); |
| 207 |
|
| 208 |
-- should generate 'cursor "rmt_foo_cursor" not found' error |
| 209 |
SELECT * |
| 210 |
FROM dblink_fetch('rmt_foo_cursor',4) AS t(a int, b text, c text[]); |
| 211 |
|
| 212 |
-- this time, 'cursor "rmt_foo_cursor" not found' as a notice |
| 213 |
SELECT * |
| 214 |
FROM dblink_fetch('rmt_foo_cursor',4,false) AS t(a int, b text, c text[]); |
| 215 |
|
| 216 |
-- close the persistent connection |
| 217 |
SELECT dblink_disconnect(); |
| 218 |
|
| 219 |
-- should generate "connection not available" error |
| 220 |
SELECT * |
| 221 |
FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[]) |
| 222 |
WHERE t.a > 7; |
| 223 |
|
| 224 |
-- put more data into our table, first using arbitrary connection syntax |
| 225 |
-- but truncate the actual return value so we can use diff to check for success |
| 226 |
SELECT substr(dblink_exec(connection_parameters(),'INSERT INTO foo VALUES(10,''k'',''{"a10","b10","c10"}'')'),1,6); |
| 227 |
|
| 228 |
-- create a persistent connection |
| 229 |
SELECT dblink_connect(connection_parameters()); |
| 230 |
|
| 231 |
-- put more data into our table, using persistent connection syntax |
| 232 |
-- but truncate the actual return value so we can use diff to check for success |
| 233 |
SELECT substr(dblink_exec('INSERT INTO foo VALUES(11,''l'',''{"a11","b11","c11"}'')'),1,6); |
| 234 |
|
| 235 |
-- let's see it |
| 236 |
SELECT * |
| 237 |
FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[]); |
| 238 |
|
| 239 |
-- bad remote select |
| 240 |
SELECT * |
| 241 |
FROM dblink('SELECT * FROM foobar',false) AS t(a int, b text, c text[]); |
| 242 |
|
| 243 |
-- change some data |
| 244 |
SELECT dblink_exec('UPDATE foo SET f3[2] = ''b99'' WHERE f1 = 11'); |
| 245 |
|
| 246 |
-- let's see it |
| 247 |
SELECT * |
| 248 |
FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[]) |
| 249 |
WHERE a = 11; |
| 250 |
|
| 251 |
-- botch a change to some other data |
| 252 |
SELECT dblink_exec('UPDATE foobar SET f3[2] = ''b99'' WHERE f1 = 11',false); |
| 253 |
|
| 254 |
-- delete some data |
| 255 |
SELECT dblink_exec('DELETE FROM foo WHERE f1 = 11'); |
| 256 |
|
| 257 |
-- let's see it |
| 258 |
SELECT * |
| 259 |
FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[]) |
| 260 |
WHERE a = 11; |
| 261 |
|
| 262 |
-- close the persistent connection |
| 263 |
SELECT dblink_disconnect(); |
| 264 |
|
| 265 |
-- |
| 266 |
-- tests for the new named persistent connection syntax |
| 267 |
-- |
| 268 |
|
| 269 |
-- should generate "missing "=" after "myconn" in connection info string" error |
| 270 |
SELECT * |
| 271 |
FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]) |
| 272 |
WHERE t.a > 7; |
| 273 |
|
| 274 |
-- create a named persistent connection |
| 275 |
SELECT dblink_connect('myconn',connection_parameters()); |
| 276 |
|
| 277 |
-- use the named persistent connection |
| 278 |
SELECT * |
| 279 |
FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]) |
| 280 |
WHERE t.a > 7; |
| 281 |
|
| 282 |
-- use the named persistent connection, but get it wrong |
| 283 |
SELECT * |
| 284 |
FROM dblink('myconn','SELECT * FROM foobar',false) AS t(a int, b text, c text[]) |
| 285 |
WHERE t.a > 7; |
| 286 |
|
| 287 |
-- create a second named persistent connection |
| 288 |
-- should error with "duplicate connection name" |
| 289 |
SELECT dblink_connect('myconn',connection_parameters()); |
| 290 |
|
| 291 |
-- create a second named persistent connection with a new name |
| 292 |
SELECT dblink_connect('myconn2',connection_parameters()); |
| 293 |
|
| 294 |
-- use the second named persistent connection |
| 295 |
SELECT * |
| 296 |
FROM dblink('myconn2','SELECT * FROM foo') AS t(a int, b text, c text[]) |
| 297 |
WHERE t.a > 7; |
| 298 |
|
| 299 |
-- close the second named persistent connection |
| 300 |
SELECT dblink_disconnect('myconn2'); |
| 301 |
|
| 302 |
-- open a cursor incorrectly |
| 303 |
SELECT dblink_open('myconn','rmt_foo_cursor','SELECT * FROM foobar',false); |
| 304 |
|
| 305 |
-- reset remote transaction state |
| 306 |
SELECT dblink_exec('myconn','ABORT'); |
| 307 |
|
| 308 |
-- test opening cursor in a transaction |
| 309 |
SELECT dblink_exec('myconn','BEGIN'); |
| 310 |
|
| 311 |
-- an open transaction will prevent dblink_open() from opening its own |
| 312 |
SELECT dblink_open('myconn','rmt_foo_cursor','SELECT * FROM foo'); |
| 313 |
|
| 314 |
-- this should not commit the transaction because the client opened it |
| 315 |
SELECT dblink_close('myconn','rmt_foo_cursor'); |
| 316 |
|
| 317 |
-- this should succeed because we have an open transaction |
| 318 |
SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo'); |
| 319 |
|
| 320 |
-- commit remote transaction |
| 321 |
SELECT dblink_exec('myconn','COMMIT'); |
| 322 |
|
| 323 |
-- test automatic transactions for multiple cursor opens |
| 324 |
SELECT dblink_open('myconn','rmt_foo_cursor','SELECT * FROM foo'); |
| 325 |
|
| 326 |
-- the second cursor |
| 327 |
SELECT dblink_open('myconn','rmt_foo_cursor2','SELECT * FROM foo'); |
| 328 |
|
| 329 |
-- this should not commit the transaction |
| 330 |
SELECT dblink_close('myconn','rmt_foo_cursor2'); |
| 331 |
|
| 332 |
-- this should succeed because we have an open transaction |
| 333 |
SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo'); |
| 334 |
|
| 335 |
-- this should commit the transaction |
| 336 |
SELECT dblink_close('myconn','rmt_foo_cursor'); |
| 337 |
|
| 338 |
-- this should fail because there is no open transaction |
| 339 |
SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo'); |
| 340 |
|
| 341 |
-- reset remote transaction state |
| 342 |
SELECT dblink_exec('myconn','ABORT'); |
| 343 |
|
| 344 |
-- open a cursor |
| 345 |
SELECT dblink_open('myconn','rmt_foo_cursor','SELECT * FROM foo'); |
| 346 |
|
| 347 |
-- fetch some data |
| 348 |
SELECT * |
| 349 |
FROM dblink_fetch('myconn','rmt_foo_cursor',4) AS t(a int, b text, c text[]); |
| 350 |
|
| 351 |
SELECT * |
| 352 |
FROM dblink_fetch('myconn','rmt_foo_cursor',4) AS t(a int, b text, c text[]); |
| 353 |
|
| 354 |
-- this one only finds three rows left |
| 355 |
SELECT * |
| 356 |
FROM dblink_fetch('myconn','rmt_foo_cursor',4) AS t(a int, b text, c text[]); |
| 357 |
|
| 358 |
-- fetch some data incorrectly |
| 359 |
SELECT * |
| 360 |
FROM dblink_fetch('myconn','rmt_foobar_cursor',4,false) AS t(a int, b text, c text[]); |
| 361 |
|
| 362 |
-- reset remote transaction state |
| 363 |
SELECT dblink_exec('myconn','ABORT'); |
| 364 |
|
| 365 |
-- should generate 'cursor "rmt_foo_cursor" not found' error |
| 366 |
SELECT * |
| 367 |
FROM dblink_fetch('myconn','rmt_foo_cursor',4) AS t(a int, b text, c text[]); |
| 368 |
|
| 369 |
-- close the named persistent connection |
| 370 |
SELECT dblink_disconnect('myconn'); |
| 371 |
|
| 372 |
-- should generate "missing "=" after "myconn" in connection info string" error |
| 373 |
SELECT * |
| 374 |
FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]) |
| 375 |
WHERE t.a > 7; |
| 376 |
|
| 377 |
-- create a named persistent connection |
| 378 |
SELECT dblink_connect('myconn',connection_parameters()); |
| 379 |
|
| 380 |
-- put more data into our table, using named persistent connection syntax |
| 381 |
-- but truncate the actual return value so we can use diff to check for success |
| 382 |
SELECT substr(dblink_exec('myconn','INSERT INTO foo VALUES(11,''l'',''{"a11","b11","c11"}'')'),1,6); |
| 383 |
|
| 384 |
-- let's see it |
| 385 |
SELECT * |
| 386 |
FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]); |
| 387 |
|
| 388 |
-- change some data |
| 389 |
SELECT dblink_exec('myconn','UPDATE foo SET f3[2] = ''b99'' WHERE f1 = 11'); |
| 390 |
|
| 391 |
-- let's see it |
| 392 |
SELECT * |
| 393 |
FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]) |
| 394 |
WHERE a = 11; |
| 395 |
|
| 396 |
-- delete some data |
| 397 |
SELECT dblink_exec('myconn','DELETE FROM foo WHERE f1 = 11'); |
| 398 |
|
| 399 |
-- let's see it |
| 400 |
SELECT * |
| 401 |
FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]) |
| 402 |
WHERE a = 11; |
| 403 |
|
| 404 |
-- close the named persistent connection |
| 405 |
SELECT dblink_disconnect('myconn'); |
| 406 |
|
| 407 |
-- close the named persistent connection again |
| 408 |
-- should get 'connection "myconn" not available' error |
| 409 |
SELECT dblink_disconnect('myconn'); |
| 410 |
|
| 411 |
-- test asynchronous queries |
| 412 |
SELECT dblink_connect('dtest1', connection_parameters()); |
| 413 |
SELECT * from |
| 414 |
dblink_send_query('dtest1', 'select * from foo where f1 < 3') as t1; |
| 415 |
|
| 416 |
SELECT dblink_connect('dtest2', connection_parameters()); |
| 417 |
SELECT * from |
| 418 |
dblink_send_query('dtest2', 'select * from foo where f1 > 2 and f1 < 7') as t1; |
| 419 |
|
| 420 |
SELECT dblink_connect('dtest3', connection_parameters()); |
| 421 |
SELECT * from |
| 422 |
dblink_send_query('dtest3', 'select * from foo where f1 > 6') as t1; |
| 423 |
|
| 424 |
CREATE TEMPORARY TABLE result AS |
| 425 |
(SELECT * from dblink_get_result('dtest1') as t1(f1 int, f2 text, f3 text[])) |
| 426 |
UNION |
| 427 |
(SELECT * from dblink_get_result('dtest2') as t2(f1 int, f2 text, f3 text[])) |
| 428 |
UNION |
| 429 |
(SELECT * from dblink_get_result('dtest3') as t3(f1 int, f2 text, f3 text[])) |
| 430 |
ORDER by f1; |
| 431 |
|
| 432 |
-- dblink_get_connections returns an array with elements in a machine-dependent |
| 433 |
-- ordering, so we must resort to unnesting and sorting for a stable result |
| 434 |
create function unnest(anyarray) returns setof anyelement |
| 435 |
language sql strict immutable as $$ |
| 436 |
select $1[i] from generate_series(array_lower($1,1), array_upper($1,1)) as i |
| 437 |
$$; |
| 438 |
|
| 439 |
SELECT * FROM unnest(dblink_get_connections()) ORDER BY 1; |
| 440 |
|
| 441 |
SELECT dblink_is_busy('dtest1'); |
| 442 |
|
| 443 |
SELECT dblink_disconnect('dtest1'); |
| 444 |
SELECT dblink_disconnect('dtest2'); |
| 445 |
SELECT dblink_disconnect('dtest3'); |
| 446 |
|
| 447 |
SELECT * from result; |
| 448 |
|
| 449 |
SELECT dblink_connect('dtest1', connection_parameters()); |
| 450 |
SELECT * from |
| 451 |
dblink_send_query('dtest1', 'select * from foo where f1 < 3') as t1; |
| 452 |
|
| 453 |
SELECT dblink_cancel_query('dtest1'); |
| 454 |
SELECT dblink_error_message('dtest1'); |
| 455 |
SELECT dblink_disconnect('dtest1'); |
| 456 |
|
| 457 |
-- test foreign data wrapper functionality |
| 458 |
CREATE ROLE regress_dblink_user; |
| 459 |
DO $d$ |
| 460 |
BEGIN |
| 461 |
EXECUTE $$CREATE SERVER fdtest FOREIGN DATA WRAPPER dblink_fdw |
| 462 |
OPTIONS (dbname '$$||current_database()||$$', |
| 463 |
port '$$||current_setting('port')||$$' |
| 464 |
)$$; |
| 465 |
END; |
| 466 |
$d$; |
| 467 |
|
| 468 |
CREATE USER MAPPING FOR public SERVER fdtest |
| 469 |
OPTIONS (server 'localhost'); -- fail, can't specify server here |
| 470 |
CREATE USER MAPPING FOR public SERVER fdtest OPTIONS (user :'USER'); |
| 471 |
|
| 472 |
-- OAuth options are not allowed in either context |
| 473 |
ALTER SERVER fdtest OPTIONS (ADD oauth_issuer 'https://example.com'); |
| 474 |
ALTER SERVER fdtest OPTIONS (ADD oauth_client_id 'myID'); |
| 475 |
ALTER USER MAPPING FOR public SERVER fdtest |
| 476 |
OPTIONS (ADD oauth_issuer 'https://example.com'); |
| 477 |
ALTER USER MAPPING FOR public SERVER fdtest |
| 478 |
OPTIONS (ADD oauth_client_id 'myID'); |
| 479 |
|
| 480 |
GRANT USAGE ON FOREIGN SERVER fdtest TO regress_dblink_user; |
| 481 |
GRANT EXECUTE ON FUNCTION dblink_connect_u(text, text) TO regress_dblink_user; |
| 482 |
|
| 483 |
SET SESSION AUTHORIZATION regress_dblink_user; |
| 484 |
-- should fail |
| 485 |
SELECT dblink_connect('myconn', 'fdtest'); |
| 486 |
-- should succeed |
| 487 |
SELECT dblink_connect_u('myconn', 'fdtest'); |
| 488 |
SELECT * FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]); |
| 489 |
|
| 490 |
\c - - |
| 491 |
REVOKE USAGE ON FOREIGN SERVER fdtest FROM regress_dblink_user; |
| 492 |
REVOKE EXECUTE ON FUNCTION dblink_connect_u(text, text) FROM regress_dblink_user; |
| 493 |
DROP USER regress_dblink_user; |
| 494 |
DROP USER MAPPING FOR public SERVER fdtest; |
| 495 |
DROP SERVER fdtest; |
| 496 |
|
| 497 |
-- should fail |
| 498 |
ALTER FOREIGN DATA WRAPPER dblink_fdw OPTIONS (nonexistent 'fdw'); |
| 499 |
|
| 500 |
-- test repeated calls to dblink_connect |
| 501 |
SELECT dblink_connect(connection_parameters()); |
| 502 |
SELECT dblink_connect(connection_parameters()); |
| 503 |
SELECT dblink_disconnect(); |
| 504 |
|
| 505 |
-- test asynchronous notifications |
| 506 |
SELECT dblink_connect(connection_parameters()); |
| 507 |
|
| 508 |
--should return listen |
| 509 |
SELECT dblink_exec('LISTEN regression'); |
| 510 |
--should return listen |
| 511 |
SELECT dblink_exec('LISTEN foobar'); |
| 512 |
|
| 513 |
SELECT dblink_exec('NOTIFY regression'); |
| 514 |
SELECT dblink_exec('NOTIFY foobar'); |
| 515 |
|
| 516 |
SELECT notify_name, be_pid = (select t.be_pid from dblink('select pg_backend_pid()') as t(be_pid int)) AS is_self_notify, extra from dblink_get_notify(); |
| 517 |
|
| 518 |
SELECT * from dblink_get_notify(); |
| 519 |
|
| 520 |
SELECT dblink_disconnect(); |
| 521 |
|
| 522 |
-- test dropped columns in dblink_build_sql_insert, dblink_build_sql_update |
| 523 |
CREATE TEMP TABLE test_dropped |
| 524 |
( |
| 525 |
col1 INT NOT NULL DEFAULT 111, |
| 526 |
id SERIAL PRIMARY KEY, |
| 527 |
col2 INT NOT NULL DEFAULT 112, |
| 528 |
col2b INT NOT NULL DEFAULT 113 |
| 529 |
); |
| 530 |
|
| 531 |
INSERT INTO test_dropped VALUES(default); |
| 532 |
|
| 533 |
ALTER TABLE test_dropped |
| 534 |
DROP COLUMN col1, |
| 535 |
DROP COLUMN col2, |
| 536 |
ADD COLUMN col3 VARCHAR(10) NOT NULL DEFAULT 'foo', |
| 537 |
ADD COLUMN col4 INT NOT NULL DEFAULT 42; |
| 538 |
|
| 539 |
SELECT dblink_build_sql_insert('test_dropped', '1', 1, |
| 540 |
ARRAY['1'::TEXT], ARRAY['2'::TEXT]); |
| 541 |
|
| 542 |
SELECT dblink_build_sql_update('test_dropped', '1', 1, |
| 543 |
ARRAY['1'::TEXT], ARRAY['2'::TEXT]); |
| 544 |
|
| 545 |
SELECT dblink_build_sql_delete('test_dropped', '1', 1, |
| 546 |
ARRAY['2'::TEXT]); |
| 547 |
|
| 548 |
-- test local mimicry of remote GUC values that affect datatype I/O |
| 549 |
SET datestyle = ISO, MDY; |
| 550 |
SET intervalstyle = postgres; |
| 551 |
SET timezone = UTC; |
| 552 |
SELECT dblink_connect('myconn',connection_parameters()); |
| 553 |
SELECT dblink_exec('myconn', 'SET datestyle = GERMAN, DMY;'); |
| 554 |
|
| 555 |
-- single row synchronous case |
| 556 |
SELECT * |
| 557 |
FROM dblink('myconn', |
| 558 |
'SELECT * FROM (VALUES (''12.03.2013 00:00:00+00'')) t') |
| 559 |
AS t(a timestamptz); |
| 560 |
|
| 561 |
-- multi-row synchronous case |
| 562 |
SELECT * |
| 563 |
FROM dblink('myconn', |
| 564 |
'SELECT * FROM |
| 565 |
(VALUES (''12.03.2013 00:00:00+00''), |
| 566 |
(''12.03.2013 00:00:00+00'')) t') |
| 567 |
AS t(a timestamptz); |
| 568 |
|
| 569 |
-- single-row asynchronous case |
| 570 |
SELECT * |
| 571 |
FROM dblink_send_query('myconn', |
| 572 |
'SELECT * FROM |
| 573 |
(VALUES (''12.03.2013 00:00:00+00'')) t'); |
| 574 |
CREATE TEMPORARY TABLE result AS |
| 575 |
(SELECT * from dblink_get_result('myconn') as t(t timestamptz)) |
| 576 |
UNION ALL |
| 577 |
(SELECT * from dblink_get_result('myconn') as t(t timestamptz)); |
| 578 |
SELECT * FROM result; |
| 579 |
DROP TABLE result; |
| 580 |
|
| 581 |
-- multi-row asynchronous case |
| 582 |
SELECT * |
| 583 |
FROM dblink_send_query('myconn', |
| 584 |
'SELECT * FROM |
| 585 |
(VALUES (''12.03.2013 00:00:00+00''), |
| 586 |
(''12.03.2013 00:00:00+00'')) t'); |
| 587 |
CREATE TEMPORARY TABLE result AS |
| 588 |
(SELECT * from dblink_get_result('myconn') as t(t timestamptz)) |
| 589 |
UNION ALL |
| 590 |
(SELECT * from dblink_get_result('myconn') as t(t timestamptz)) |
| 591 |
UNION ALL |
| 592 |
(SELECT * from dblink_get_result('myconn') as t(t timestamptz)); |
| 593 |
SELECT * FROM result; |
| 594 |
DROP TABLE result; |
| 595 |
|
| 596 |
-- Try an ambiguous interval |
| 597 |
SELECT dblink_exec('myconn', 'SET intervalstyle = sql_standard;'); |
| 598 |
SELECT * |
| 599 |
FROM dblink('myconn', |
| 600 |
'SELECT * FROM (VALUES (''-1 2:03:04'')) i') |
| 601 |
AS i(i interval); |
| 602 |
|
| 603 |
-- Try swapping to another format to ensure the GUCs are tracked |
| 604 |
-- properly through a change. |
| 605 |
CREATE TEMPORARY TABLE result (t timestamptz); |
| 606 |
|
| 607 |
SELECT dblink_exec('myconn', 'SET datestyle = ISO, MDY;'); |
| 608 |
INSERT INTO result |
| 609 |
SELECT * |
| 610 |
FROM dblink('myconn', |
| 611 |
'SELECT * FROM (VALUES (''03.12.2013 00:00:00+00'')) t') |
| 612 |
AS t(a timestamptz); |
| 613 |
|
| 614 |
SELECT dblink_exec('myconn', 'SET datestyle = GERMAN, DMY;'); |
| 615 |
INSERT INTO result |
| 616 |
SELECT * |
| 617 |
FROM dblink('myconn', |
| 618 |
'SELECT * FROM (VALUES (''12.03.2013 00:00:00+00'')) t') |
| 619 |
AS t(a timestamptz); |
| 620 |
|
| 621 |
SELECT * FROM result; |
| 622 |
|
| 623 |
DROP TABLE result; |
| 624 |
|
| 625 |
-- Check error throwing in dblink_fetch |
| 626 |
SELECT dblink_open('myconn','error_cursor', |
| 627 |
'SELECT * FROM (VALUES (''1''), (''not an int'')) AS t(text);'); |
| 628 |
SELECT * |
| 629 |
FROM dblink_fetch('myconn','error_cursor', 1) AS t(i int); |
| 630 |
SELECT * |
| 631 |
FROM dblink_fetch('myconn','error_cursor', 1) AS t(i int); |
| 632 |
|
| 633 |
-- Make sure that the local settings have retained their values in spite |
| 634 |
-- of shenanigans on the connection. |
| 635 |
SHOW datestyle; |
| 636 |
SHOW intervalstyle; |
| 637 |
|
| 638 |
-- Check that adding use_scram_passthrough option on an foreign data wrapper is |
| 639 |
-- not allowed |
| 640 |
ALTER FOREIGN DATA WRAPPER dblink_fdw OPTIONS(add use_scram_passthrough 'true'); |
| 641 |
|
| 642 |
-- Clean up GUC-setting tests |
| 643 |
SELECT dblink_disconnect('myconn'); |
| 644 |
RESET datestyle; |
| 645 |
RESET intervalstyle; |
| 646 |
RESET timezone; |