Debian Codegraph @

contrib/dblink/expected/dblink.out

raw
1 CREATE EXTENSION dblink;
2 -- directory paths and dlsuffix are passed to us in environment variables
3 \getenv abs_srcdir PG_ABS_SRCDIR
4 \getenv libdir PG_LIBDIR
5 \getenv dlsuffix PG_DLSUFFIX
6 \set regresslib :libdir '/regress' :dlsuffix
7 -- create some functions needed for tests
8 CREATE FUNCTION setenv(text, text)
9 RETURNS void
10 AS :'regresslib', 'regress_setenv'
11 LANGUAGE C STRICT;
12 CREATE FUNCTION wait_pid(int)
13 RETURNS void
14 AS :'regresslib'
15 LANGUAGE C STRICT;
16 \set path :abs_srcdir '/'
17 \set fnbody 'SELECT setenv(''PGSERVICEFILE'', ' :'path' ' || $1)'
18 CREATE FUNCTION set_pgservicefile(text) RETURNS void LANGUAGE SQL
19 AS :'fnbody';
20 -- want context for notices
21 \set SHOW_CONTEXT always
22 CREATE TABLE foo(f1 int, f2 text, f3 text[], primary key (f1,f2));
23 INSERT INTO foo VALUES (0,'a','{"a0","b0","c0"}');
24 INSERT INTO foo VALUES (1,'b','{"a1","b1","c1"}');
25 INSERT INTO foo VALUES (2,'c','{"a2","b2","c2"}');
26 INSERT INTO foo VALUES (3,'d','{"a3","b3","c3"}');
27 INSERT INTO foo VALUES (4,'e','{"a4","b4","c4"}');
28 INSERT INTO foo VALUES (5,'f','{"a5","b5","c5"}');
29 INSERT INTO foo VALUES (6,'g','{"a6","b6","c6"}');
30 INSERT INTO foo VALUES (7,'h','{"a7","b7","c7"}');
31 INSERT INTO foo VALUES (8,'i','{"a8","b8","c8"}');
32 INSERT INTO foo VALUES (9,'j','{"a9","b9","c9"}');
33 -- misc utilities
34 -- list the primary key fields
35 SELECT *
36 FROM dblink_get_pkey('foo');
37 position | colname
38 ----------+---------
39 1 | f1
40 2 | f2
41 (2 rows)
42
43 -- build an insert statement based on a local tuple,
44 -- replacing the primary key values with new ones
45 SELECT dblink_build_sql_insert('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
46 dblink_build_sql_insert
47 -----------------------------------------------------------
48 INSERT INTO foo(f1,f2,f3) VALUES('99','xyz','{a0,b0,c0}')
49 (1 row)
50
51 -- too many pk fields, should fail
52 SELECT dblink_build_sql_insert('foo','1 2 3 4',4,'{"0", "a", "{a0,b0,c0}"}','{"99", "xyz", "{za0,zb0,zc0}"}');
53 ERROR: invalid attribute number 4
54 -- build an update statement based on a local tuple,
55 -- replacing the primary key values with new ones
56 SELECT dblink_build_sql_update('foo','1 2',2,'{"0", "a"}','{"99", "xyz"}');
57 dblink_build_sql_update
58 ----------------------------------------------------------------------------------------
59 UPDATE foo SET f1 = '99', f2 = 'xyz', f3 = '{a0,b0,c0}' WHERE f1 = '99' AND f2 = 'xyz'
60 (1 row)
61
62 -- too many pk fields, should fail
63 SELECT dblink_build_sql_update('foo','1 2 3 4',4,'{"0", "a", "{a0,b0,c0}"}','{"99", "xyz", "{za0,zb0,zc0}"}');
64 ERROR: invalid attribute number 4
65 -- build a delete statement based on a local tuple,
66 SELECT dblink_build_sql_delete('foo','1 2',2,'{"0", "a"}');
67 dblink_build_sql_delete
68 ---------------------------------------------
69 DELETE FROM foo WHERE f1 = '0' AND f2 = 'a'
70 (1 row)
71
72 -- too many pk fields, should fail
73 SELECT dblink_build_sql_delete('foo','1 2 3 4',4,'{"0", "a", "{a0,b0,c0}"}');
74 ERROR: invalid attribute number 4
75 -- repeat the test for table with primary key index with included columns
76 CREATE TABLE foo_1(f1 int, f2 text, f3 text[], primary key (f1,f2) include (f3));
77 INSERT INTO foo_1 VALUES (0,'a','{"a0","b0","c0"}');
78 INSERT INTO foo_1 VALUES (1,'b','{"a1","b1","c1"}');
79 INSERT INTO foo_1 VALUES (2,'c','{"a2","b2","c2"}');
80 INSERT INTO foo_1 VALUES (3,'d','{"a3","b3","c3"}');
81 INSERT INTO foo_1 VALUES (4,'e','{"a4","b4","c4"}');
82 INSERT INTO foo_1 VALUES (5,'f','{"a5","b5","c5"}');
83 INSERT INTO foo_1 VALUES (6,'g','{"a6","b6","c6"}');
84 INSERT INTO foo_1 VALUES (7,'h','{"a7","b7","c7"}');
85 INSERT INTO foo_1 VALUES (8,'i','{"a8","b8","c8"}');
86 INSERT INTO foo_1 VALUES (9,'j','{"a9","b9","c9"}');
87 -- misc utilities
88 -- list the primary key fields
89 SELECT *
90 FROM dblink_get_pkey('foo_1');
91 position | colname
92 ----------+---------
93 1 | f1
94 2 | f2
95 (2 rows)
96
97 -- build an insert statement based on a local tuple,
98 -- replacing the primary key values with new ones
99 SELECT dblink_build_sql_insert('foo_1','1 2',2,'{"0", "a"}','{"99", "xyz"}');
100 dblink_build_sql_insert
101 -------------------------------------------------------------
102 INSERT INTO foo_1(f1,f2,f3) VALUES('99','xyz','{a0,b0,c0}')
103 (1 row)
104
105 -- too many pk fields, should fail
106 SELECT dblink_build_sql_insert('foo_1','1 2 3 4',4,'{"0", "a", "{a0,b0,c0}"}','{"99", "xyz", "{za0,zb0,zc0}"}');
107 ERROR: invalid attribute number 4
108 -- build an update statement based on a local tuple,
109 -- replacing the primary key values with new ones
110 SELECT dblink_build_sql_update('foo_1','1 2',2,'{"0", "a"}','{"99", "xyz"}');
111 dblink_build_sql_update
112 ------------------------------------------------------------------------------------------
113 UPDATE foo_1 SET f1 = '99', f2 = 'xyz', f3 = '{a0,b0,c0}' WHERE f1 = '99' AND f2 = 'xyz'
114 (1 row)
115
116 -- too many pk fields, should fail
117 SELECT dblink_build_sql_update('foo_1','1 2 3 4',4,'{"0", "a", "{a0,b0,c0}"}','{"99", "xyz", "{za0,zb0,zc0}"}');
118 ERROR: invalid attribute number 4
119 -- build a delete statement based on a local tuple,
120 SELECT dblink_build_sql_delete('foo_1','1 2',2,'{"0", "a"}');
121 dblink_build_sql_delete
122 -----------------------------------------------
123 DELETE FROM foo_1 WHERE f1 = '0' AND f2 = 'a'
124 (1 row)
125
126 -- too many pk fields, should fail
127 SELECT dblink_build_sql_delete('foo_1','1 2 3 4',4,'{"0", "a", "{a0,b0,c0}"}');
128 ERROR: invalid attribute number 4
129 DROP TABLE foo_1;
130 -- retest using a quoted and schema qualified table
131 CREATE SCHEMA "MySchema";
132 CREATE TABLE "MySchema"."Foo"(f1 int, f2 text, f3 text[], primary key (f1,f2));
133 INSERT INTO "MySchema"."Foo" VALUES (0,'a','{"a0","b0","c0"}');
134 -- list the primary key fields
135 SELECT *
136 FROM dblink_get_pkey('"MySchema"."Foo"');
137 position | colname
138 ----------+---------
139 1 | f1
140 2 | f2
141 (2 rows)
142
143 -- build an insert statement based on a local tuple,
144 -- replacing the primary key values with new ones
145 SELECT dblink_build_sql_insert('"MySchema"."Foo"','1 2',2,'{"0", "a"}','{"99", "xyz"}');
146 dblink_build_sql_insert
147 ------------------------------------------------------------------------
148 INSERT INTO "MySchema"."Foo"(f1,f2,f3) VALUES('99','xyz','{a0,b0,c0}')
149 (1 row)
150
151 -- build an update statement based on a local tuple,
152 -- replacing the primary key values with new ones
153 SELECT dblink_build_sql_update('"MySchema"."Foo"','1 2',2,'{"0", "a"}','{"99", "xyz"}');
154 dblink_build_sql_update
155 -----------------------------------------------------------------------------------------------------
156 UPDATE "MySchema"."Foo" SET f1 = '99', f2 = 'xyz', f3 = '{a0,b0,c0}' WHERE f1 = '99' AND f2 = 'xyz'
157 (1 row)
158
159 -- build a delete statement based on a local tuple,
160 SELECT dblink_build_sql_delete('"MySchema"."Foo"','1 2',2,'{"0", "a"}');
161 dblink_build_sql_delete
162 ----------------------------------------------------------
163 DELETE FROM "MySchema"."Foo" WHERE f1 = '0' AND f2 = 'a'
164 (1 row)
165
166 CREATE FUNCTION connection_parameters() RETURNS text LANGUAGE SQL AS $f$
167 SELECT $$dbname='$$||current_database()||$$' port=$$||current_setting('port');
168 $f$;
169 -- regular old dblink
170 SELECT *
171 FROM dblink(connection_parameters(),'SELECT * FROM foo') AS t(a int, b text, c text[])
172 WHERE t.a > 7;
173 a | b | c
174 ---+---+------------
175 8 | i | {a8,b8,c8}
176 9 | j | {a9,b9,c9}
177 (2 rows)
178
179 -- should generate "connection not available" error
180 SELECT *
181 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
182 WHERE t.a > 7;
183 ERROR: connection not available
184 -- The first-level connection's backend will crash on exit given OpenLDAP
185 -- [2.4.24, 2.4.31]. We won't see evidence of any crash until the victim
186 -- process terminates and the postmaster responds. If process termination
187 -- entails writing a core dump, that can take awhile. Wait for the process to
188 -- vanish. At that point, the postmaster has called waitpid() on the crashed
189 -- process, and it will accept no new connections until it has reinitialized
190 -- the cluster. (We can't exploit pg_stat_activity, because the crash happens
191 -- after the backend updates shared memory to reflect its impending exit.)
192 DO $pl$
193 DECLARE
194 detail text;
195 BEGIN
196 PERFORM wait_pid(crash_pid)
197 FROM dblink(connection_parameters(), $$
198 SELECT pg_backend_pid() FROM dblink(
199 'service=test_ldap '||connection_parameters(),
200 -- This string concatenation is a hack to shoehorn a
201 -- set_pgservicefile call into the SQL statement.
202 'SELECT 1' || set_pgservicefile('pg_service.conf')
203 ) t(c int)
204 $$) AS t(crash_pid int);
205 EXCEPTION WHEN OTHERS THEN
206 GET STACKED DIAGNOSTICS detail = PG_EXCEPTION_DETAIL;
207 -- Expected error in a non-LDAP build.
208 IF NOT detail LIKE 'syntax error in service file%' THEN RAISE; END IF;
209 END
210 $pl$;
211 -- create a persistent connection
212 SELECT dblink_connect(connection_parameters());
213 dblink_connect
214 ----------------
215 OK
216 (1 row)
217
218 -- use the persistent connection
219 SELECT *
220 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
221 WHERE t.a > 7;
222 a | b | c
223 ---+---+------------
224 8 | i | {a8,b8,c8}
225 9 | j | {a9,b9,c9}
226 (2 rows)
227
228 -- open a cursor with bad SQL and fail_on_error set to false
229 SELECT dblink_open('rmt_foo_cursor','SELECT * FROM foobar',false);
230 NOTICE: relation "foobar" does not exist
231 CONTEXT: while opening cursor "rmt_foo_cursor" on unnamed dblink connection
232 dblink_open
233 -------------
234 ERROR
235 (1 row)
236
237 -- reset remote transaction state
238 SELECT dblink_exec('ABORT');
239 dblink_exec
240 -------------
241 ROLLBACK
242 (1 row)
243
244 -- open a cursor
245 SELECT dblink_open('rmt_foo_cursor','SELECT * FROM foo');
246 dblink_open
247 -------------
248 OK
249 (1 row)
250
251 -- close the cursor
252 SELECT dblink_close('rmt_foo_cursor',false);
253 dblink_close
254 --------------
255 OK
256 (1 row)
257
258 -- open the cursor again
259 SELECT dblink_open('rmt_foo_cursor','SELECT * FROM foo');
260 dblink_open
261 -------------
262 OK
263 (1 row)
264
265 -- fetch some data
266 SELECT *
267 FROM dblink_fetch('rmt_foo_cursor',4) AS t(a int, b text, c text[]);
268 a | b | c
269 ---+---+------------
270 0 | a | {a0,b0,c0}
271 1 | b | {a1,b1,c1}
272 2 | c | {a2,b2,c2}
273 3 | d | {a3,b3,c3}
274 (4 rows)
275
276 SELECT *
277 FROM dblink_fetch('rmt_foo_cursor',4) AS t(a int, b text, c text[]);
278 a | b | c
279 ---+---+------------
280 4 | e | {a4,b4,c4}
281 5 | f | {a5,b5,c5}
282 6 | g | {a6,b6,c6}
283 7 | h | {a7,b7,c7}
284 (4 rows)
285
286 -- this one only finds two rows left
287 SELECT *
288 FROM dblink_fetch('rmt_foo_cursor',4) AS t(a int, b text, c text[]);
289 a | b | c
290 ---+---+------------
291 8 | i | {a8,b8,c8}
292 9 | j | {a9,b9,c9}
293 (2 rows)
294
295 -- intentionally botch a fetch
296 SELECT *
297 FROM dblink_fetch('rmt_foobar_cursor',4,false) AS t(a int, b text, c text[]);
298 NOTICE: cursor "rmt_foobar_cursor" does not exist
299 CONTEXT: while fetching from cursor "rmt_foobar_cursor" on unnamed dblink connection
300 a | b | c
301 ---+---+---
302 (0 rows)
303
304 -- reset remote transaction state
305 SELECT dblink_exec('ABORT');
306 dblink_exec
307 -------------
308 ROLLBACK
309 (1 row)
310
311 -- close the wrong cursor
312 SELECT dblink_close('rmt_foobar_cursor',false);
313 NOTICE: cursor "rmt_foobar_cursor" does not exist
314 CONTEXT: while closing cursor "rmt_foobar_cursor" on unnamed dblink connection
315 dblink_close
316 --------------
317 ERROR
318 (1 row)
319
320 -- should generate 'cursor "rmt_foo_cursor" not found' error
321 SELECT *
322 FROM dblink_fetch('rmt_foo_cursor',4) AS t(a int, b text, c text[]);
323 ERROR: cursor "rmt_foo_cursor" does not exist
324 CONTEXT: while fetching from cursor "rmt_foo_cursor" on unnamed dblink connection
325 -- this time, 'cursor "rmt_foo_cursor" not found' as a notice
326 SELECT *
327 FROM dblink_fetch('rmt_foo_cursor',4,false) AS t(a int, b text, c text[]);
328 NOTICE: cursor "rmt_foo_cursor" does not exist
329 CONTEXT: while fetching from cursor "rmt_foo_cursor" on unnamed dblink connection
330 a | b | c
331 ---+---+---
332 (0 rows)
333
334 -- close the persistent connection
335 SELECT dblink_disconnect();
336 dblink_disconnect
337 -------------------
338 OK
339 (1 row)
340
341 -- should generate "connection not available" error
342 SELECT *
343 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
344 WHERE t.a > 7;
345 ERROR: connection not available
346 -- put more data into our table, first using arbitrary connection syntax
347 -- but truncate the actual return value so we can use diff to check for success
348 SELECT substr(dblink_exec(connection_parameters(),'INSERT INTO foo VALUES(10,''k'',''{"a10","b10","c10"}'')'),1,6);
349 substr
350 --------
351 INSERT
352 (1 row)
353
354 -- create a persistent connection
355 SELECT dblink_connect(connection_parameters());
356 dblink_connect
357 ----------------
358 OK
359 (1 row)
360
361 -- put more data into our table, using persistent connection syntax
362 -- but truncate the actual return value so we can use diff to check for success
363 SELECT substr(dblink_exec('INSERT INTO foo VALUES(11,''l'',''{"a11","b11","c11"}'')'),1,6);
364 substr
365 --------
366 INSERT
367 (1 row)
368
369 -- let's see it
370 SELECT *
371 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[]);
372 a | b | c
373 ----+---+---------------
374 0 | a | {a0,b0,c0}
375 1 | b | {a1,b1,c1}
376 2 | c | {a2,b2,c2}
377 3 | d | {a3,b3,c3}
378 4 | e | {a4,b4,c4}
379 5 | f | {a5,b5,c5}
380 6 | g | {a6,b6,c6}
381 7 | h | {a7,b7,c7}
382 8 | i | {a8,b8,c8}
383 9 | j | {a9,b9,c9}
384 10 | k | {a10,b10,c10}
385 11 | l | {a11,b11,c11}
386 (12 rows)
387
388 -- bad remote select
389 SELECT *
390 FROM dblink('SELECT * FROM foobar',false) AS t(a int, b text, c text[]);
391 NOTICE: relation "foobar" does not exist
392 CONTEXT: while executing query on unnamed dblink connection
393 a | b | c
394 ---+---+---
395 (0 rows)
396
397 -- change some data
398 SELECT dblink_exec('UPDATE foo SET f3[2] = ''b99'' WHERE f1 = 11');
399 dblink_exec
400 -------------
401 UPDATE 1
402 (1 row)
403
404 -- let's see it
405 SELECT *
406 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
407 WHERE a = 11;
408 a | b | c
409 ----+---+---------------
410 11 | l | {a11,b99,c11}
411 (1 row)
412
413 -- botch a change to some other data
414 SELECT dblink_exec('UPDATE foobar SET f3[2] = ''b99'' WHERE f1 = 11',false);
415 NOTICE: relation "foobar" does not exist
416 CONTEXT: while executing command on unnamed dblink connection
417 dblink_exec
418 -------------
419 ERROR
420 (1 row)
421
422 -- delete some data
423 SELECT dblink_exec('DELETE FROM foo WHERE f1 = 11');
424 dblink_exec
425 -------------
426 DELETE 1
427 (1 row)
428
429 -- let's see it
430 SELECT *
431 FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[])
432 WHERE a = 11;
433 a | b | c
434 ---+---+---
435 (0 rows)
436
437 -- close the persistent connection
438 SELECT dblink_disconnect();
439 dblink_disconnect
440 -------------------
441 OK
442 (1 row)
443
444 --
445 -- tests for the new named persistent connection syntax
446 --
447 -- should generate "missing "=" after "myconn" in connection info string" error
448 SELECT *
449 FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
450 WHERE t.a > 7;
451 ERROR: could not establish connection
452 DETAIL: missing "=" after "myconn" in connection info string
453 -- create a named persistent connection
454 SELECT dblink_connect('myconn',connection_parameters());
455 dblink_connect
456 ----------------
457 OK
458 (1 row)
459
460 -- use the named persistent connection
461 SELECT *
462 FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
463 WHERE t.a > 7;
464 a | b | c
465 ----+---+---------------
466 8 | i | {a8,b8,c8}
467 9 | j | {a9,b9,c9}
468 10 | k | {a10,b10,c10}
469 (3 rows)
470
471 -- use the named persistent connection, but get it wrong
472 SELECT *
473 FROM dblink('myconn','SELECT * FROM foobar',false) AS t(a int, b text, c text[])
474 WHERE t.a > 7;
475 NOTICE: relation "foobar" does not exist
476 CONTEXT: while executing query on dblink connection named "myconn"
477 a | b | c
478 ---+---+---
479 (0 rows)
480
481 -- create a second named persistent connection
482 -- should error with "duplicate connection name"
483 SELECT dblink_connect('myconn',connection_parameters());
484 ERROR: duplicate connection name
485 -- create a second named persistent connection with a new name
486 SELECT dblink_connect('myconn2',connection_parameters());
487 dblink_connect
488 ----------------
489 OK
490 (1 row)
491
492 -- use the second named persistent connection
493 SELECT *
494 FROM dblink('myconn2','SELECT * FROM foo') AS t(a int, b text, c text[])
495 WHERE t.a > 7;
496 a | b | c
497 ----+---+---------------
498 8 | i | {a8,b8,c8}
499 9 | j | {a9,b9,c9}
500 10 | k | {a10,b10,c10}
501 (3 rows)
502
503 -- close the second named persistent connection
504 SELECT dblink_disconnect('myconn2');
505 dblink_disconnect
506 -------------------
507 OK
508 (1 row)
509
510 -- open a cursor incorrectly
511 SELECT dblink_open('myconn','rmt_foo_cursor','SELECT * FROM foobar',false);
512 NOTICE: relation "foobar" does not exist
513 CONTEXT: while opening cursor "rmt_foo_cursor" on dblink connection named "myconn"
514 dblink_open
515 -------------
516 ERROR
517 (1 row)
518
519 -- reset remote transaction state
520 SELECT dblink_exec('myconn','ABORT');
521 dblink_exec
522 -------------
523 ROLLBACK
524 (1 row)
525
526 -- test opening cursor in a transaction
527 SELECT dblink_exec('myconn','BEGIN');
528 dblink_exec
529 -------------
530 BEGIN
531 (1 row)
532
533 -- an open transaction will prevent dblink_open() from opening its own
534 SELECT dblink_open('myconn','rmt_foo_cursor','SELECT * FROM foo');
535 dblink_open
536 -------------
537 OK
538 (1 row)
539
540 -- this should not commit the transaction because the client opened it
541 SELECT dblink_close('myconn','rmt_foo_cursor');
542 dblink_close
543 --------------
544 OK
545 (1 row)
546
547 -- this should succeed because we have an open transaction
548 SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo');
549 dblink_exec
550 ----------------
551 DECLARE CURSOR
552 (1 row)
553
554 -- commit remote transaction
555 SELECT dblink_exec('myconn','COMMIT');
556 dblink_exec
557 -------------
558 COMMIT
559 (1 row)
560
561 -- test automatic transactions for multiple cursor opens
562 SELECT dblink_open('myconn','rmt_foo_cursor','SELECT * FROM foo');
563 dblink_open
564 -------------
565 OK
566 (1 row)
567
568 -- the second cursor
569 SELECT dblink_open('myconn','rmt_foo_cursor2','SELECT * FROM foo');
570 dblink_open
571 -------------
572 OK
573 (1 row)
574
575 -- this should not commit the transaction
576 SELECT dblink_close('myconn','rmt_foo_cursor2');
577 dblink_close
578 --------------
579 OK
580 (1 row)
581
582 -- this should succeed because we have an open transaction
583 SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo');
584 dblink_exec
585 ----------------
586 DECLARE CURSOR
587 (1 row)
588
589 -- this should commit the transaction
590 SELECT dblink_close('myconn','rmt_foo_cursor');
591 dblink_close
592 --------------
593 OK
594 (1 row)
595
596 -- this should fail because there is no open transaction
597 SELECT dblink_exec('myconn','DECLARE xact_test CURSOR FOR SELECT * FROM foo');
598 ERROR: DECLARE CURSOR can only be used in transaction blocks
599 CONTEXT: while executing command on dblink connection named "myconn"
600 -- reset remote transaction state
601 SELECT dblink_exec('myconn','ABORT');
602 dblink_exec
603 -------------
604 ROLLBACK
605 (1 row)
606
607 -- open a cursor
608 SELECT dblink_open('myconn','rmt_foo_cursor','SELECT * FROM foo');
609 dblink_open
610 -------------
611 OK
612 (1 row)
613
614 -- fetch some data
615 SELECT *
616 FROM dblink_fetch('myconn','rmt_foo_cursor',4) AS t(a int, b text, c text[]);
617 a | b | c
618 ---+---+------------
619 0 | a | {a0,b0,c0}
620 1 | b | {a1,b1,c1}
621 2 | c | {a2,b2,c2}
622 3 | d | {a3,b3,c3}
623 (4 rows)
624
625 SELECT *
626 FROM dblink_fetch('myconn','rmt_foo_cursor',4) AS t(a int, b text, c text[]);
627 a | b | c
628 ---+---+------------
629 4 | e | {a4,b4,c4}
630 5 | f | {a5,b5,c5}
631 6 | g | {a6,b6,c6}
632 7 | h | {a7,b7,c7}
633 (4 rows)
634
635 -- this one only finds three rows left
636 SELECT *
637 FROM dblink_fetch('myconn','rmt_foo_cursor',4) AS t(a int, b text, c text[]);
638 a | b | c
639 ----+---+---------------
640 8 | i | {a8,b8,c8}
641 9 | j | {a9,b9,c9}
642 10 | k | {a10,b10,c10}
643 (3 rows)
644
645 -- fetch some data incorrectly
646 SELECT *
647 FROM dblink_fetch('myconn','rmt_foobar_cursor',4,false) AS t(a int, b text, c text[]);
648 NOTICE: cursor "rmt_foobar_cursor" does not exist
649 CONTEXT: while fetching from cursor "rmt_foobar_cursor" on dblink connection named "myconn"
650 a | b | c
651 ---+---+---
652 (0 rows)
653
654 -- reset remote transaction state
655 SELECT dblink_exec('myconn','ABORT');
656 dblink_exec
657 -------------
658 ROLLBACK
659 (1 row)
660
661 -- should generate 'cursor "rmt_foo_cursor" not found' error
662 SELECT *
663 FROM dblink_fetch('myconn','rmt_foo_cursor',4) AS t(a int, b text, c text[]);
664 ERROR: cursor "rmt_foo_cursor" does not exist
665 CONTEXT: while fetching from cursor "rmt_foo_cursor" on dblink connection named "myconn"
666 -- close the named persistent connection
667 SELECT dblink_disconnect('myconn');
668 dblink_disconnect
669 -------------------
670 OK
671 (1 row)
672
673 -- should generate "missing "=" after "myconn" in connection info string" error
674 SELECT *
675 FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
676 WHERE t.a > 7;
677 ERROR: could not establish connection
678 DETAIL: missing "=" after "myconn" in connection info string
679 -- create a named persistent connection
680 SELECT dblink_connect('myconn',connection_parameters());
681 dblink_connect
682 ----------------
683 OK
684 (1 row)
685
686 -- put more data into our table, using named persistent connection syntax
687 -- but truncate the actual return value so we can use diff to check for success
688 SELECT substr(dblink_exec('myconn','INSERT INTO foo VALUES(11,''l'',''{"a11","b11","c11"}'')'),1,6);
689 substr
690 --------
691 INSERT
692 (1 row)
693
694 -- let's see it
695 SELECT *
696 FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]);
697 a | b | c
698 ----+---+---------------
699 0 | a | {a0,b0,c0}
700 1 | b | {a1,b1,c1}
701 2 | c | {a2,b2,c2}
702 3 | d | {a3,b3,c3}
703 4 | e | {a4,b4,c4}
704 5 | f | {a5,b5,c5}
705 6 | g | {a6,b6,c6}
706 7 | h | {a7,b7,c7}
707 8 | i | {a8,b8,c8}
708 9 | j | {a9,b9,c9}
709 10 | k | {a10,b10,c10}
710 11 | l | {a11,b11,c11}
711 (12 rows)
712
713 -- change some data
714 SELECT dblink_exec('myconn','UPDATE foo SET f3[2] = ''b99'' WHERE f1 = 11');
715 dblink_exec
716 -------------
717 UPDATE 1
718 (1 row)
719
720 -- let's see it
721 SELECT *
722 FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
723 WHERE a = 11;
724 a | b | c
725 ----+---+---------------
726 11 | l | {a11,b99,c11}
727 (1 row)
728
729 -- delete some data
730 SELECT dblink_exec('myconn','DELETE FROM foo WHERE f1 = 11');
731 dblink_exec
732 -------------
733 DELETE 1
734 (1 row)
735
736 -- let's see it
737 SELECT *
738 FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[])
739 WHERE a = 11;
740 a | b | c
741 ---+---+---
742 (0 rows)
743
744 -- close the named persistent connection
745 SELECT dblink_disconnect('myconn');
746 dblink_disconnect
747 -------------------
748 OK
749 (1 row)
750
751 -- close the named persistent connection again
752 -- should get 'connection "myconn" not available' error
753 SELECT dblink_disconnect('myconn');
754 ERROR: connection "myconn" not available
755 -- test asynchronous queries
756 SELECT dblink_connect('dtest1', connection_parameters());
757 dblink_connect
758 ----------------
759 OK
760 (1 row)
761
762 SELECT * from
763 dblink_send_query('dtest1', 'select * from foo where f1 < 3') as t1;
764 t1
765 ----
766 1
767 (1 row)
768
769 SELECT dblink_connect('dtest2', connection_parameters());
770 dblink_connect
771 ----------------
772 OK
773 (1 row)
774
775 SELECT * from
776 dblink_send_query('dtest2', 'select * from foo where f1 > 2 and f1 < 7') as t1;
777 t1
778 ----
779 1
780 (1 row)
781
782 SELECT dblink_connect('dtest3', connection_parameters());
783 dblink_connect
784 ----------------
785 OK
786 (1 row)
787
788 SELECT * from
789 dblink_send_query('dtest3', 'select * from foo where f1 > 6') as t1;
790 t1
791 ----
792 1
793 (1 row)
794
795 CREATE TEMPORARY TABLE result AS
796 (SELECT * from dblink_get_result('dtest1') as t1(f1 int, f2 text, f3 text[]))
797 UNION
798 (SELECT * from dblink_get_result('dtest2') as t2(f1 int, f2 text, f3 text[]))
799 UNION
800 (SELECT * from dblink_get_result('dtest3') as t3(f1 int, f2 text, f3 text[]))
801 ORDER by f1;
802 -- dblink_get_connections returns an array with elements in a machine-dependent
803 -- ordering, so we must resort to unnesting and sorting for a stable result
804 create function unnest(anyarray) returns setof anyelement
805 language sql strict immutable as $$
806 select $1[i] from generate_series(array_lower($1,1), array_upper($1,1)) as i
807 $$;
808 SELECT * FROM unnest(dblink_get_connections()) ORDER BY 1;
809 unnest
810 --------
811 dtest1
812 dtest2
813 dtest3
814 (3 rows)
815
816 SELECT dblink_is_busy('dtest1');
817 dblink_is_busy
818 ----------------
819 0
820 (1 row)
821
822 SELECT dblink_disconnect('dtest1');
823 dblink_disconnect
824 -------------------
825 OK
826 (1 row)
827
828 SELECT dblink_disconnect('dtest2');
829 dblink_disconnect
830 -------------------
831 OK
832 (1 row)
833
834 SELECT dblink_disconnect('dtest3');
835 dblink_disconnect
836 -------------------
837 OK
838 (1 row)
839
840 SELECT * from result;
841 f1 | f2 | f3
842 ----+----+---------------
843 0 | a | {a0,b0,c0}
844 1 | b | {a1,b1,c1}
845 2 | c | {a2,b2,c2}
846 3 | d | {a3,b3,c3}
847 4 | e | {a4,b4,c4}
848 5 | f | {a5,b5,c5}
849 6 | g | {a6,b6,c6}
850 7 | h | {a7,b7,c7}
851 8 | i | {a8,b8,c8}
852 9 | j | {a9,b9,c9}
853 10 | k | {a10,b10,c10}
854 (11 rows)
855
856 SELECT dblink_connect('dtest1', connection_parameters());
857 dblink_connect
858 ----------------
859 OK
860 (1 row)
861
862 SELECT * from
863 dblink_send_query('dtest1', 'select * from foo where f1 < 3') as t1;
864 t1
865 ----
866 1
867 (1 row)
868
869 SELECT dblink_cancel_query('dtest1');
870 dblink_cancel_query
871 ---------------------
872 OK
873 (1 row)
874
875 SELECT dblink_error_message('dtest1');
876 dblink_error_message
877 ----------------------
878 OK
879 (1 row)
880
881 SELECT dblink_disconnect('dtest1');
882 dblink_disconnect
883 -------------------
884 OK
885 (1 row)
886
887 -- test foreign data wrapper functionality
888 CREATE ROLE regress_dblink_user;
889 DO $d$
890 BEGIN
891 EXECUTE $$CREATE SERVER fdtest FOREIGN DATA WRAPPER dblink_fdw
892 OPTIONS (dbname '$$||current_database()||$$',
893 port '$$||current_setting('port')||$$'
894 )$$;
895 END;
896 $d$;
897 CREATE USER MAPPING FOR public SERVER fdtest
898 OPTIONS (server 'localhost'); -- fail, can't specify server here
899 ERROR: invalid option "server"
900 CREATE USER MAPPING FOR public SERVER fdtest OPTIONS (user :'USER');
901 -- OAuth options are not allowed in either context
902 ALTER SERVER fdtest OPTIONS (ADD oauth_issuer 'https://example.com');
903 ERROR: invalid option "oauth_issuer"
904 ALTER SERVER fdtest OPTIONS (ADD oauth_client_id 'myID');
905 ERROR: invalid option "oauth_client_id"
906 ALTER USER MAPPING FOR public SERVER fdtest
907 OPTIONS (ADD oauth_issuer 'https://example.com');
908 ERROR: invalid option "oauth_issuer"
909 ALTER USER MAPPING FOR public SERVER fdtest
910 OPTIONS (ADD oauth_client_id 'myID');
911 ERROR: invalid option "oauth_client_id"
912 GRANT USAGE ON FOREIGN SERVER fdtest TO regress_dblink_user;
913 GRANT EXECUTE ON FUNCTION dblink_connect_u(text, text) TO regress_dblink_user;
914 SET SESSION AUTHORIZATION regress_dblink_user;
915 -- should fail
916 SELECT dblink_connect('myconn', 'fdtest');
917 ERROR: password or GSSAPI delegated credentials required
918 DETAIL: Non-superusers must provide a password in the connection string or send delegated GSSAPI credentials.
919 -- should succeed
920 SELECT dblink_connect_u('myconn', 'fdtest');
921 dblink_connect_u
922 ------------------
923 OK
924 (1 row)
925
926 SELECT * FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]);
927 a | b | c
928 ----+---+---------------
929 0 | a | {a0,b0,c0}
930 1 | b | {a1,b1,c1}
931 2 | c | {a2,b2,c2}
932 3 | d | {a3,b3,c3}
933 4 | e | {a4,b4,c4}
934 5 | f | {a5,b5,c5}
935 6 | g | {a6,b6,c6}
936 7 | h | {a7,b7,c7}
937 8 | i | {a8,b8,c8}
938 9 | j | {a9,b9,c9}
939 10 | k | {a10,b10,c10}
940 (11 rows)
941
942 \c - -
943 REVOKE USAGE ON FOREIGN SERVER fdtest FROM regress_dblink_user;
944 REVOKE EXECUTE ON FUNCTION dblink_connect_u(text, text) FROM regress_dblink_user;
945 DROP USER regress_dblink_user;
946 DROP USER MAPPING FOR public SERVER fdtest;
947 DROP SERVER fdtest;
948 -- should fail
949 ALTER FOREIGN DATA WRAPPER dblink_fdw OPTIONS (nonexistent 'fdw');
950 ERROR: invalid option "nonexistent"
951 HINT: There are no valid options in this context.
952 -- test repeated calls to dblink_connect
953 SELECT dblink_connect(connection_parameters());
954 dblink_connect
955 ----------------
956 OK
957 (1 row)
958
959 SELECT dblink_connect(connection_parameters());
960 dblink_connect
961 ----------------
962 OK
963 (1 row)
964
965 SELECT dblink_disconnect();
966 dblink_disconnect
967 -------------------
968 OK
969 (1 row)
970
971 -- test asynchronous notifications
972 SELECT dblink_connect(connection_parameters());
973 dblink_connect
974 ----------------
975 OK
976 (1 row)
977
978 --should return listen
979 SELECT dblink_exec('LISTEN regression');
980 dblink_exec
981 -------------
982 LISTEN
983 (1 row)
984
985 --should return listen
986 SELECT dblink_exec('LISTEN foobar');
987 dblink_exec
988 -------------
989 LISTEN
990 (1 row)
991
992 SELECT dblink_exec('NOTIFY regression');
993 dblink_exec
994 -------------
995 NOTIFY
996 (1 row)
997
998 SELECT dblink_exec('NOTIFY foobar');
999 dblink_exec
1000 -------------
1001 NOTIFY
1002 (1 row)
1003
1004 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();
1005 notify_name | is_self_notify | extra
1006 -------------+----------------+-------
1007 regression | t |
1008 foobar | t |
1009 (2 rows)
1010
1011 SELECT * from dblink_get_notify();
1012 notify_name | be_pid | extra
1013 -------------+--------+-------
1014 (0 rows)
1015
1016 SELECT dblink_disconnect();
1017 dblink_disconnect
1018 -------------------
1019 OK
1020 (1 row)
1021
1022 -- test dropped columns in dblink_build_sql_insert, dblink_build_sql_update
1023 CREATE TEMP TABLE test_dropped
1024 (
1025 col1 INT NOT NULL DEFAULT 111,
1026 id SERIAL PRIMARY KEY,
1027 col2 INT NOT NULL DEFAULT 112,
1028 col2b INT NOT NULL DEFAULT 113
1029 );
1030 INSERT INTO test_dropped VALUES(default);
1031 ALTER TABLE test_dropped
1032 DROP COLUMN col1,
1033 DROP COLUMN col2,
1034 ADD COLUMN col3 VARCHAR(10) NOT NULL DEFAULT 'foo',
1035 ADD COLUMN col4 INT NOT NULL DEFAULT 42;
1036 SELECT dblink_build_sql_insert('test_dropped', '1', 1,
1037 ARRAY['1'::TEXT], ARRAY['2'::TEXT]);
1038 dblink_build_sql_insert
1039 ---------------------------------------------------------------------------
1040 INSERT INTO test_dropped(id,col2b,col3,col4) VALUES('2','113','foo','42')
1041 (1 row)
1042
1043 SELECT dblink_build_sql_update('test_dropped', '1', 1,
1044 ARRAY['1'::TEXT], ARRAY['2'::TEXT]);
1045 dblink_build_sql_update
1046 -------------------------------------------------------------------------------------------
1047 UPDATE test_dropped SET id = '2', col2b = '113', col3 = 'foo', col4 = '42' WHERE id = '2'
1048 (1 row)
1049
1050 SELECT dblink_build_sql_delete('test_dropped', '1', 1,
1051 ARRAY['2'::TEXT]);
1052 dblink_build_sql_delete
1053 -----------------------------------------
1054 DELETE FROM test_dropped WHERE id = '2'
1055 (1 row)
1056
1057 -- test local mimicry of remote GUC values that affect datatype I/O
1058 SET datestyle = ISO, MDY;
1059 SET intervalstyle = postgres;
1060 SET timezone = UTC;
1061 SELECT dblink_connect('myconn',connection_parameters());
1062 dblink_connect
1063 ----------------
1064 OK
1065 (1 row)
1066
1067 SELECT dblink_exec('myconn', 'SET datestyle = GERMAN, DMY;');
1068 dblink_exec
1069 -------------
1070 SET
1071 (1 row)
1072
1073 -- single row synchronous case
1074 SELECT *
1075 FROM dblink('myconn',
1076 'SELECT * FROM (VALUES (''12.03.2013 00:00:00+00'')) t')
1077 AS t(a timestamptz);
1078 a
1079 ------------------------
1080 2013-03-12 00:00:00+00
1081 (1 row)
1082
1083 -- multi-row synchronous case
1084 SELECT *
1085 FROM dblink('myconn',
1086 'SELECT * FROM
1087 (VALUES (''12.03.2013 00:00:00+00''),
1088 (''12.03.2013 00:00:00+00'')) t')
1089 AS t(a timestamptz);
1090 a
1091 ------------------------
1092 2013-03-12 00:00:00+00
1093 2013-03-12 00:00:00+00
1094 (2 rows)
1095
1096 -- single-row asynchronous case
1097 SELECT *
1098 FROM dblink_send_query('myconn',
1099 'SELECT * FROM
1100 (VALUES (''12.03.2013 00:00:00+00'')) t');
1101 dblink_send_query
1102 -------------------
1103 1
1104 (1 row)
1105
1106 CREATE TEMPORARY TABLE result AS
1107 (SELECT * from dblink_get_result('myconn') as t(t timestamptz))
1108 UNION ALL
1109 (SELECT * from dblink_get_result('myconn') as t(t timestamptz));
1110 SELECT * FROM result;
1111 t
1112 ------------------------
1113 2013-03-12 00:00:00+00
1114 (1 row)
1115
1116 DROP TABLE result;
1117 -- multi-row asynchronous case
1118 SELECT *
1119 FROM dblink_send_query('myconn',
1120 'SELECT * FROM
1121 (VALUES (''12.03.2013 00:00:00+00''),
1122 (''12.03.2013 00:00:00+00'')) t');
1123 dblink_send_query
1124 -------------------
1125 1
1126 (1 row)
1127
1128 CREATE TEMPORARY TABLE result AS
1129 (SELECT * from dblink_get_result('myconn') as t(t timestamptz))
1130 UNION ALL
1131 (SELECT * from dblink_get_result('myconn') as t(t timestamptz))
1132 UNION ALL
1133 (SELECT * from dblink_get_result('myconn') as t(t timestamptz));
1134 SELECT * FROM result;
1135 t
1136 ------------------------
1137 2013-03-12 00:00:00+00
1138 2013-03-12 00:00:00+00
1139 (2 rows)
1140
1141 DROP TABLE result;
1142 -- Try an ambiguous interval
1143 SELECT dblink_exec('myconn', 'SET intervalstyle = sql_standard;');
1144 dblink_exec
1145 -------------
1146 SET
1147 (1 row)
1148
1149 SELECT *
1150 FROM dblink('myconn',
1151 'SELECT * FROM (VALUES (''-1 2:03:04'')) i')
1152 AS i(i interval);
1153 i
1154 -------------------
1155 -1 days -02:03:04
1156 (1 row)
1157
1158 -- Try swapping to another format to ensure the GUCs are tracked
1159 -- properly through a change.
1160 CREATE TEMPORARY TABLE result (t timestamptz);
1161 SELECT dblink_exec('myconn', 'SET datestyle = ISO, MDY;');
1162 dblink_exec
1163 -------------
1164 SET
1165 (1 row)
1166
1167 INSERT INTO result
1168 SELECT *
1169 FROM dblink('myconn',
1170 'SELECT * FROM (VALUES (''03.12.2013 00:00:00+00'')) t')
1171 AS t(a timestamptz);
1172 SELECT dblink_exec('myconn', 'SET datestyle = GERMAN, DMY;');
1173 dblink_exec
1174 -------------
1175 SET
1176 (1 row)
1177
1178 INSERT INTO result
1179 SELECT *
1180 FROM dblink('myconn',
1181 'SELECT * FROM (VALUES (''12.03.2013 00:00:00+00'')) t')
1182 AS t(a timestamptz);
1183 SELECT * FROM result;
1184 t
1185 ------------------------
1186 2013-03-12 00:00:00+00
1187 2013-03-12 00:00:00+00
1188 (2 rows)
1189
1190 DROP TABLE result;
1191 -- Check error throwing in dblink_fetch
1192 SELECT dblink_open('myconn','error_cursor',
1193 'SELECT * FROM (VALUES (''1''), (''not an int'')) AS t(text);');
1194 dblink_open
1195 -------------
1196 OK
1197 (1 row)
1198
1199 SELECT *
1200 FROM dblink_fetch('myconn','error_cursor', 1) AS t(i int);
1201 i
1202 ---
1203 1
1204 (1 row)
1205
1206 SELECT *
1207 FROM dblink_fetch('myconn','error_cursor', 1) AS t(i int);
1208 ERROR: invalid input syntax for type integer: "not an int"
1209 -- Make sure that the local settings have retained their values in spite
1210 -- of shenanigans on the connection.
1211 SHOW datestyle;
1212 DateStyle
1213 -----------
1214 ISO, MDY
1215 (1 row)
1216
1217 SHOW intervalstyle;
1218 IntervalStyle
1219 ---------------
1220 postgres
1221 (1 row)
1222
1223 -- Check that adding use_scram_passthrough option on an foreign data wrapper is
1224 -- not allowed
1225 ALTER FOREIGN DATA WRAPPER dblink_fdw OPTIONS(add use_scram_passthrough 'true');
1226 ERROR: invalid option "use_scram_passthrough"
1227 HINT: There are no valid options in this context.
1228 -- Clean up GUC-setting tests
1229 SELECT dblink_disconnect('myconn');
1230 dblink_disconnect
1231 -------------------
1232 OK
1233 (1 row)
1234
1235 RESET datestyle;
1236 RESET intervalstyle;
1237 RESET timezone;