Debian Codegraph @

contrib/postgres_fdw/sql/postgres_fdw.sql

raw
1 -- ===================================================================
2 -- create FDW objects
3 -- ===================================================================
4
5 CREATE EXTENSION postgres_fdw;
6
7 CREATE SERVER testserver1 FOREIGN DATA WRAPPER postgres_fdw;
8 DO $d$
9 BEGIN
10 EXECUTE $$CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw
11 OPTIONS (dbname '$$||current_database()||$$',
12 port '$$||current_setting('port')||$$'
13 )$$;
14 EXECUTE $$CREATE SERVER loopback2 FOREIGN DATA WRAPPER postgres_fdw
15 OPTIONS (dbname '$$||current_database()||$$',
16 port '$$||current_setting('port')||$$'
17 )$$;
18 EXECUTE $$CREATE SERVER loopback3 FOREIGN DATA WRAPPER postgres_fdw
19 OPTIONS (dbname '$$||current_database()||$$',
20 port '$$||current_setting('port')||$$'
21 )$$;
22 END;
23 $d$;
24
25 CREATE USER MAPPING FOR public SERVER testserver1
26 OPTIONS (user 'value', password 'value');
27 CREATE USER MAPPING FOR CURRENT_USER SERVER loopback;
28 CREATE USER MAPPING FOR CURRENT_USER SERVER loopback2;
29 CREATE USER MAPPING FOR public SERVER loopback3;
30
31 -- ===================================================================
32 -- create objects used through FDW loopback server
33 -- ===================================================================
34 CREATE TYPE user_enum AS ENUM ('foo', 'bar', 'buz');
35 CREATE SCHEMA "S 1";
36 CREATE TABLE "S 1"."T 1" (
37 "C 1" int NOT NULL,
38 c2 int NOT NULL,
39 c3 text,
40 c4 timestamptz,
41 c5 timestamp,
42 c6 varchar(10),
43 c7 char(10),
44 c8 user_enum,
45 CONSTRAINT t1_pkey PRIMARY KEY ("C 1")
46 );
47 CREATE TABLE "S 1"."T 2" (
48 c1 int NOT NULL,
49 c2 text,
50 CONSTRAINT t2_pkey PRIMARY KEY (c1)
51 );
52 CREATE TABLE "S 1"."T 3" (
53 c1 int NOT NULL,
54 c2 int NOT NULL,
55 c3 text,
56 CONSTRAINT t3_pkey PRIMARY KEY (c1)
57 );
58 CREATE TABLE "S 1"."T 4" (
59 c1 int NOT NULL,
60 c2 int NOT NULL,
61 c3 text,
62 CONSTRAINT t4_pkey PRIMARY KEY (c1)
63 );
64
65 -- Disable autovacuum for these tables to avoid unexpected effects of that
66 ALTER TABLE "S 1"."T 1" SET (autovacuum_enabled = 'false');
67 ALTER TABLE "S 1"."T 2" SET (autovacuum_enabled = 'false');
68 ALTER TABLE "S 1"."T 3" SET (autovacuum_enabled = 'false');
69 ALTER TABLE "S 1"."T 4" SET (autovacuum_enabled = 'false');
70
71 INSERT INTO "S 1"."T 1"
72 SELECT id,
73 id % 10,
74 to_char(id, 'FM00000'),
75 '1970-01-01'::timestamptz + ((id % 100) || ' days')::interval,
76 '1970-01-01'::timestamp + ((id % 100) || ' days')::interval,
77 id % 10,
78 id % 10,
79 'foo'::user_enum
80 FROM generate_series(1, 1000) id;
81 INSERT INTO "S 1"."T 2"
82 SELECT id,
83 'AAA' || to_char(id, 'FM000')
84 FROM generate_series(1, 100) id;
85 INSERT INTO "S 1"."T 3"
86 SELECT id,
87 id + 1,
88 'AAA' || to_char(id, 'FM000')
89 FROM generate_series(1, 100) id;
90 DELETE FROM "S 1"."T 3" WHERE c1 % 2 != 0; -- delete for outer join tests
91 INSERT INTO "S 1"."T 4"
92 SELECT id,
93 id + 1,
94 'AAA' || to_char(id, 'FM000')
95 FROM generate_series(1, 100) id;
96 DELETE FROM "S 1"."T 4" WHERE c1 % 3 != 0; -- delete for outer join tests
97
98 ANALYZE "S 1"."T 1";
99 ANALYZE "S 1"."T 2";
100 ANALYZE "S 1"."T 3";
101 ANALYZE "S 1"."T 4";
102
103 -- ===================================================================
104 -- create foreign tables
105 -- ===================================================================
106 CREATE FOREIGN TABLE ft1 (
107 c0 int,
108 c1 int NOT NULL,
109 c2 int NOT NULL,
110 c3 text,
111 c4 timestamptz,
112 c5 timestamp,
113 c6 varchar(10),
114 c7 char(10) default 'ft1',
115 c8 user_enum
116 ) SERVER loopback;
117 ALTER FOREIGN TABLE ft1 DROP COLUMN c0;
118
119 CREATE FOREIGN TABLE ft2 (
120 c1 int NOT NULL,
121 c2 int NOT NULL,
122 cx int,
123 c3 text,
124 c4 timestamptz,
125 c5 timestamp,
126 c6 varchar(10),
127 c7 char(10) default 'ft2',
128 c8 user_enum
129 ) SERVER loopback;
130 ALTER FOREIGN TABLE ft2 DROP COLUMN cx;
131
132 CREATE FOREIGN TABLE ft4 (
133 c1 int NOT NULL,
134 c2 int NOT NULL,
135 c3 text
136 ) SERVER loopback OPTIONS (schema_name 'S 1', table_name 'T 3');
137
138 CREATE FOREIGN TABLE ft5 (
139 c1 int NOT NULL,
140 c2 int NOT NULL,
141 c3 text
142 ) SERVER loopback OPTIONS (schema_name 'S 1', table_name 'T 4');
143
144 CREATE FOREIGN TABLE ft6 (
145 c1 int NOT NULL,
146 c2 int NOT NULL,
147 c3 text
148 ) SERVER loopback2 OPTIONS (schema_name 'S 1', table_name 'T 4');
149
150 CREATE FOREIGN TABLE ft7 (
151 c1 int NOT NULL,
152 c2 int NOT NULL,
153 c3 text
154 ) SERVER loopback3 OPTIONS (schema_name 'S 1', table_name 'T 4');
155
156 -- ===================================================================
157 -- tests for validator
158 -- ===================================================================
159 -- requiressl and some other parameters are omitted because
160 -- valid values for them depend on configure options
161 ALTER SERVER testserver1 OPTIONS (
162 use_remote_estimate 'false',
163 updatable 'true',
164 fdw_startup_cost '123.456',
165 fdw_tuple_cost '0.123',
166 service 'value',
167 connect_timeout 'value',
168 dbname 'value',
169 host 'value',
170 hostaddr 'value',
171 port 'value',
172 --client_encoding 'value',
173 application_name 'value',
174 --fallback_application_name 'value',
175 keepalives 'value',
176 keepalives_idle 'value',
177 keepalives_interval 'value',
178 tcp_user_timeout 'value',
179 -- requiressl 'value',
180 sslcompression 'value',
181 sslmode 'value',
182 sslcert 'value',
183 sslkey 'value',
184 sslrootcert 'value',
185 sslcrl 'value',
186 --requirepeer 'value',
187 krbsrvname 'value',
188 gsslib 'value',
189 gssdelegation 'value'
190 --replication 'value'
191 );
192
193 -- Error, invalid list syntax
194 ALTER SERVER testserver1 OPTIONS (ADD extensions 'foo; bar');
195
196 -- OK but gets a warning
197 ALTER SERVER testserver1 OPTIONS (ADD extensions 'foo, bar');
198 ALTER SERVER testserver1 OPTIONS (DROP extensions);
199
200 ALTER USER MAPPING FOR public SERVER testserver1
201 OPTIONS (DROP user, DROP password);
202
203 -- Attempt to add a valid option that's not allowed in a user mapping
204 ALTER USER MAPPING FOR public SERVER testserver1
205 OPTIONS (ADD sslmode 'require');
206
207 -- But we can add valid ones fine
208 ALTER USER MAPPING FOR public SERVER testserver1
209 OPTIONS (ADD sslpassword 'dummy');
210
211 -- Ensure valid options we haven't used in a user mapping yet are
212 -- permitted to check validation.
213 ALTER USER MAPPING FOR public SERVER testserver1
214 OPTIONS (ADD sslkey 'value', ADD sslcert 'value');
215
216 -- OAuth options are not allowed in either context
217 ALTER SERVER testserver1 OPTIONS (ADD oauth_issuer 'https://example.com');
218 ALTER SERVER testserver1 OPTIONS (ADD oauth_client_id 'myID');
219 ALTER USER MAPPING FOR public SERVER testserver1
220 OPTIONS (ADD oauth_issuer 'https://example.com');
221 ALTER USER MAPPING FOR public SERVER testserver1
222 OPTIONS (ADD oauth_client_id 'myID');
223
224 ALTER FOREIGN TABLE ft1 OPTIONS (schema_name 'S 1', table_name 'T 1');
225 ALTER FOREIGN TABLE ft2 OPTIONS (schema_name 'S 1', table_name 'T 1');
226 ALTER FOREIGN TABLE ft1 ALTER COLUMN c1 OPTIONS (column_name 'C 1');
227 ALTER FOREIGN TABLE ft2 ALTER COLUMN c1 OPTIONS (column_name 'C 1');
228 \det+
229
230 -- Test that alteration of server options causes reconnection
231 -- Remote's errors might be non-English, so hide them to ensure stable results
232 \set VERBOSITY terse
233 SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should work
234 ALTER SERVER loopback OPTIONS (SET dbname 'no such database');
235 SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should fail
236 DO $d$
237 BEGIN
238 EXECUTE $$ALTER SERVER loopback
239 OPTIONS (SET dbname '$$||current_database()||$$')$$;
240 END;
241 $d$;
242 SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should work again
243
244 -- Test that alteration of user mapping options causes reconnection
245 ALTER USER MAPPING FOR CURRENT_USER SERVER loopback
246 OPTIONS (ADD user 'no such user');
247 SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should fail
248 ALTER USER MAPPING FOR CURRENT_USER SERVER loopback
249 OPTIONS (DROP user);
250 SELECT c3, c4 FROM ft1 ORDER BY c3, c1 LIMIT 1; -- should work again
251 \set VERBOSITY default
252
253 -- Now we should be able to run ANALYZE.
254 -- To exercise multiple code paths, we use local stats on ft1
255 -- and remote-estimate mode on ft2.
256 ANALYZE ft1;
257 ALTER FOREIGN TABLE ft2 OPTIONS (use_remote_estimate 'true');
258
259 -- ===================================================================
260 -- test error case for create publication on foreign table
261 -- ===================================================================
262 CREATE PUBLICATION testpub_ftbl FOR TABLE ft1; -- should fail
263
264 -- ===================================================================
265 -- simple queries
266 -- ===================================================================
267 -- single table without alias
268 EXPLAIN (COSTS OFF) SELECT * FROM ft1 ORDER BY c3, c1 OFFSET 100 LIMIT 10;
269 SELECT * FROM ft1 ORDER BY c3, c1 OFFSET 100 LIMIT 10;
270 -- single table with alias - also test that tableoid sort is not pushed to remote side
271 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 ORDER BY t1.c3, t1.c1, t1.tableoid OFFSET 100 LIMIT 10;
272 SELECT * FROM ft1 t1 ORDER BY t1.c3, t1.c1, t1.tableoid OFFSET 100 LIMIT 10;
273 -- whole-row reference
274 EXPLAIN (VERBOSE, COSTS OFF) SELECT t1 FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
275 SELECT t1 FROM ft1 t1 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
276 -- empty result
277 SELECT * FROM ft1 WHERE false;
278 -- with WHERE clause
279 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE t1.c1 = 101 AND t1.c6 = '1' AND t1.c7 >= '1';
280 SELECT * FROM ft1 t1 WHERE t1.c1 = 101 AND t1.c6 = '1' AND t1.c7 >= '1';
281 -- with FOR UPDATE/SHARE
282 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE c1 = 101 FOR UPDATE;
283 SELECT * FROM ft1 t1 WHERE c1 = 101 FOR UPDATE;
284 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE c1 = 102 FOR SHARE;
285 SELECT * FROM ft1 t1 WHERE c1 = 102 FOR SHARE;
286 -- aggregate
287 SELECT COUNT(*) FROM ft1 t1;
288 -- subquery
289 SELECT * FROM ft1 t1 WHERE t1.c3 IN (SELECT c3 FROM ft2 t2 WHERE c1 <= 10) ORDER BY c1;
290 -- subquery+MAX
291 SELECT * FROM ft1 t1 WHERE t1.c3 = (SELECT MAX(c3) FROM ft2 t2) ORDER BY c1;
292 -- used in CTE
293 WITH t1 AS (SELECT * FROM ft1 WHERE c1 <= 10) SELECT t2.c1, t2.c2, t2.c3, t2.c4 FROM t1, ft2 t2 WHERE t1.c1 = t2.c1 ORDER BY t1.c1;
294 -- fixed values
295 SELECT 'fixed', NULL FROM ft1 t1 WHERE c1 = 1;
296 -- Test forcing the remote server to produce sorted data for a merge join.
297 SET enable_hashjoin TO false;
298 SET enable_nestloop TO false;
299 -- inner join; expressions in the clauses appear in the equivalence class list
300 EXPLAIN (VERBOSE, COSTS OFF)
301 SELECT t1.c1, t2."C 1" FROM ft2 t1 JOIN "S 1"."T 1" t2 ON (t1.c1 = t2."C 1") OFFSET 100 LIMIT 10;
302 SELECT t1.c1, t2."C 1" FROM ft2 t1 JOIN "S 1"."T 1" t2 ON (t1.c1 = t2."C 1") OFFSET 100 LIMIT 10;
303 -- outer join; expressions in the clauses do not appear in equivalence class
304 -- list but no output change as compared to the previous query
305 EXPLAIN (VERBOSE, COSTS OFF)
306 SELECT t1.c1, t2."C 1" FROM ft2 t1 LEFT JOIN "S 1"."T 1" t2 ON (t1.c1 = t2."C 1") OFFSET 100 LIMIT 10;
307 SELECT t1.c1, t2."C 1" FROM ft2 t1 LEFT JOIN "S 1"."T 1" t2 ON (t1.c1 = t2."C 1") OFFSET 100 LIMIT 10;
308 -- A join between local table and foreign join. ORDER BY clause is added to the
309 -- foreign join so that the local table can be joined using merge join strategy.
310 EXPLAIN (VERBOSE, COSTS OFF)
311 SELECT t1."C 1" FROM "S 1"."T 1" t1 left join ft1 t2 join ft2 t3 on (t2.c1 = t3.c1) on (t3.c1 = t1."C 1") OFFSET 100 LIMIT 10;
312 SELECT t1."C 1" FROM "S 1"."T 1" t1 left join ft1 t2 join ft2 t3 on (t2.c1 = t3.c1) on (t3.c1 = t1."C 1") OFFSET 100 LIMIT 10;
313 -- Test similar to above, except that the full join prevents any equivalence
314 -- classes from being merged. This produces single relation equivalence classes
315 -- included in join restrictions.
316 EXPLAIN (VERBOSE, COSTS OFF)
317 SELECT t1."C 1", t2.c1, t3.c1 FROM "S 1"."T 1" t1 left join ft1 t2 full join ft2 t3 on (t2.c1 = t3.c1) on (t3.c1 = t1."C 1") OFFSET 100 LIMIT 10;
318 SELECT t1."C 1", t2.c1, t3.c1 FROM "S 1"."T 1" t1 left join ft1 t2 full join ft2 t3 on (t2.c1 = t3.c1) on (t3.c1 = t1."C 1") OFFSET 100 LIMIT 10;
319 -- Test similar to above with all full outer joins
320 EXPLAIN (VERBOSE, COSTS OFF)
321 SELECT t1."C 1", t2.c1, t3.c1 FROM "S 1"."T 1" t1 full join ft1 t2 full join ft2 t3 on (t2.c1 = t3.c1) on (t3.c1 = t1."C 1") OFFSET 100 LIMIT 10;
322 SELECT t1."C 1", t2.c1, t3.c1 FROM "S 1"."T 1" t1 full join ft1 t2 full join ft2 t3 on (t2.c1 = t3.c1) on (t3.c1 = t1."C 1") OFFSET 100 LIMIT 10;
323 RESET enable_hashjoin;
324 RESET enable_nestloop;
325
326 -- Test executing assertion in estimate_path_cost_size() that makes sure that
327 -- retrieved_rows for foreign rel re-used to cost pre-sorted foreign paths is
328 -- a sensible value even when the rel has tuples=0
329 CREATE TABLE loct_empty (c1 int NOT NULL, c2 text);
330 CREATE FOREIGN TABLE ft_empty (c1 int NOT NULL, c2 text)
331 SERVER loopback OPTIONS (table_name 'loct_empty');
332 INSERT INTO loct_empty
333 SELECT id, 'AAA' || to_char(id, 'FM000') FROM generate_series(1, 100) id;
334 DELETE FROM loct_empty;
335 ANALYZE ft_empty;
336 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft_empty ORDER BY c1;
337
338 -- test restriction on non-system foreign tables.
339 SET restrict_nonsystem_relation_kind TO 'foreign-table';
340 SELECT * from ft1 where c1 < 1; -- ERROR
341 INSERT INTO ft1 (c1) VALUES (1); -- ERROR
342 DELETE FROM ft1 WHERE c1 = 1; -- ERROR
343 TRUNCATE ft1; -- ERROR
344 RESET restrict_nonsystem_relation_kind;
345
346 -- ===================================================================
347 -- WHERE with remotely-executable conditions
348 -- ===================================================================
349 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE t1.c1 = 1; -- Var, OpExpr(b), Const
350 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE t1.c1 = 100 AND t1.c2 = 0; -- BoolExpr
351 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE c3 IS NULL; -- NullTest
352 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE c3 IS NOT NULL; -- NullTest
353 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE round(abs(c1), 0) = 1; -- FuncExpr
354 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE c1 = -c1; -- OpExpr(l)
355 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE (c1 IS NOT NULL) IS DISTINCT FROM (c1 IS NOT NULL); -- DistinctExpr
356 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE c1 = ANY(ARRAY[c2, 1, c1 + 0]); -- ScalarArrayOpExpr
357 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE c1 = (ARRAY[c1,c2,3])[1]; -- SubscriptingRef
358 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE c6 = E'foo''s\\bar'; -- check special chars
359 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 t1 WHERE c8 = 'foo'; -- can't be sent to remote
360 -- parameterized remote path for foreign table
361 EXPLAIN (VERBOSE, COSTS OFF)
362 SELECT * FROM "S 1"."T 1" a, ft2 b WHERE a."C 1" = 47 AND b.c1 = a.c2;
363 SELECT * FROM "S 1"."T 1" a, ft2 b WHERE a."C 1" = 47 AND b.c1 = a.c2;
364
365 -- check both safe and unsafe join conditions
366 EXPLAIN (VERBOSE, COSTS OFF)
367 SELECT * FROM ft2 a, ft2 b
368 WHERE a.c2 = 6 AND b.c1 = a.c1 AND a.c8 = 'foo' AND b.c7 = upper(a.c7);
369 SELECT * FROM ft2 a, ft2 b
370 WHERE a.c2 = 6 AND b.c1 = a.c1 AND a.c8 = 'foo' AND b.c7 = upper(a.c7);
371 -- bug before 9.3.5 due to sloppy handling of remote-estimate parameters
372 SELECT * FROM ft1 WHERE c1 = ANY (ARRAY(SELECT c1 FROM ft2 WHERE c1 < 5));
373 SELECT * FROM ft2 WHERE c1 = ANY (ARRAY(SELECT c1 FROM ft1 WHERE c1 < 5));
374
375 -- user-defined operator/function
376 CREATE FUNCTION postgres_fdw_abs(int) RETURNS int AS $$
377 BEGIN
378 RETURN abs($1);
379 END
380 $$ LANGUAGE plpgsql IMMUTABLE;
381 CREATE OPERATOR === (
382 LEFTARG = int,
383 RIGHTARG = int,
384 PROCEDURE = int4eq,
385 COMMUTATOR = ===
386 );
387
388 -- built-in operators and functions can be shipped for remote execution
389 EXPLAIN (VERBOSE, COSTS OFF)
390 SELECT count(c3) FROM ft1 t1 WHERE t1.c1 = abs(t1.c2);
391 SELECT count(c3) FROM ft1 t1 WHERE t1.c1 = abs(t1.c2);
392 EXPLAIN (VERBOSE, COSTS OFF)
393 SELECT count(c3) FROM ft1 t1 WHERE t1.c1 = t1.c2;
394 SELECT count(c3) FROM ft1 t1 WHERE t1.c1 = t1.c2;
395
396 -- by default, user-defined ones cannot
397 EXPLAIN (VERBOSE, COSTS OFF)
398 SELECT count(c3) FROM ft1 t1 WHERE t1.c1 = postgres_fdw_abs(t1.c2);
399 SELECT count(c3) FROM ft1 t1 WHERE t1.c1 = postgres_fdw_abs(t1.c2);
400 EXPLAIN (VERBOSE, COSTS OFF)
401 SELECT count(c3) FROM ft1 t1 WHERE t1.c1 === t1.c2;
402 SELECT count(c3) FROM ft1 t1 WHERE t1.c1 === t1.c2;
403
404 -- ORDER BY can be shipped, though
405 EXPLAIN (VERBOSE, COSTS OFF)
406 SELECT * FROM ft1 t1 WHERE t1.c1 === t1.c2 order by t1.c2 limit 1;
407 SELECT * FROM ft1 t1 WHERE t1.c1 === t1.c2 order by t1.c2 limit 1;
408
409 -- but let's put them in an extension ...
410 ALTER EXTENSION postgres_fdw ADD FUNCTION postgres_fdw_abs(int);
411 ALTER EXTENSION postgres_fdw ADD OPERATOR === (int, int);
412 ALTER SERVER loopback OPTIONS (ADD extensions 'postgres_fdw');
413
414 -- ... now they can be shipped
415 EXPLAIN (VERBOSE, COSTS OFF)
416 SELECT count(c3) FROM ft1 t1 WHERE t1.c1 = postgres_fdw_abs(t1.c2);
417 SELECT count(c3) FROM ft1 t1 WHERE t1.c1 = postgres_fdw_abs(t1.c2);
418 EXPLAIN (VERBOSE, COSTS OFF)
419 SELECT count(c3) FROM ft1 t1 WHERE t1.c1 === t1.c2;
420 SELECT count(c3) FROM ft1 t1 WHERE t1.c1 === t1.c2;
421
422 -- and both ORDER BY and LIMIT can be shipped
423 EXPLAIN (VERBOSE, COSTS OFF)
424 SELECT * FROM ft1 t1 WHERE t1.c1 === t1.c2 order by t1.c2 limit 1;
425 SELECT * FROM ft1 t1 WHERE t1.c1 === t1.c2 order by t1.c2 limit 1;
426
427 -- Ensure we don't ship FETCH FIRST .. WITH TIES
428 EXPLAIN (VERBOSE, COSTS OFF)
429 SELECT t1.c2 FROM ft1 t1 WHERE t1.c1 > 960 ORDER BY t1.c2 FETCH FIRST 2 ROWS WITH TIES;
430 SELECT t1.c2 FROM ft1 t1 WHERE t1.c1 > 960 ORDER BY t1.c2 FETCH FIRST 2 ROWS WITH TIES;
431
432 -- Test CASE pushdown
433 EXPLAIN (VERBOSE, COSTS OFF)
434 SELECT c1,c2,c3 FROM ft2 WHERE CASE WHEN c1 > 990 THEN c1 END < 1000 ORDER BY c1;
435 SELECT c1,c2,c3 FROM ft2 WHERE CASE WHEN c1 > 990 THEN c1 END < 1000 ORDER BY c1;
436
437 -- Nested CASE
438 EXPLAIN (VERBOSE, COSTS OFF)
439 SELECT c1,c2,c3 FROM ft2 WHERE CASE CASE WHEN c2 > 0 THEN c2 END WHEN 100 THEN 601 WHEN c2 THEN c2 ELSE 0 END > 600 ORDER BY c1;
440
441 SELECT c1,c2,c3 FROM ft2 WHERE CASE CASE WHEN c2 > 0 THEN c2 END WHEN 100 THEN 601 WHEN c2 THEN c2 ELSE 0 END > 600 ORDER BY c1;
442
443 -- CASE arg WHEN
444 EXPLAIN (VERBOSE, COSTS OFF)
445 SELECT * FROM ft1 WHERE c1 > (CASE mod(c1, 4) WHEN 0 THEN 1 WHEN 2 THEN 50 ELSE 100 END);
446
447 -- CASE cannot be pushed down because of unshippable arg clause
448 EXPLAIN (VERBOSE, COSTS OFF)
449 SELECT * FROM ft1 WHERE c1 > (CASE random()::integer WHEN 0 THEN 1 WHEN 2 THEN 50 ELSE 100 END);
450
451 -- these are shippable
452 EXPLAIN (VERBOSE, COSTS OFF)
453 SELECT * FROM ft1 WHERE CASE c6 WHEN 'foo' THEN true ELSE c3 < 'bar' END;
454 EXPLAIN (VERBOSE, COSTS OFF)
455 SELECT * FROM ft1 WHERE CASE c3 WHEN c6 THEN true ELSE c3 < 'bar' END;
456
457 -- but this is not because of collation
458 EXPLAIN (VERBOSE, COSTS OFF)
459 SELECT * FROM ft1 WHERE CASE c3 COLLATE "C" WHEN c6 THEN true ELSE c3 < 'bar' END;
460
461 -- a regconfig constant referring to this text search configuration
462 -- is initially unshippable
463 CREATE TEXT SEARCH CONFIGURATION public.custom_search
464 (COPY = pg_catalog.english);
465 EXPLAIN (VERBOSE, COSTS OFF)
466 SELECT c1, to_tsvector('custom_search'::regconfig, c3) FROM ft1
467 WHERE c1 = 642 AND length(to_tsvector('custom_search'::regconfig, c3)) > 0;
468 SELECT c1, to_tsvector('custom_search'::regconfig, c3) FROM ft1
469 WHERE c1 = 642 AND length(to_tsvector('custom_search'::regconfig, c3)) > 0;
470 -- but if it's in a shippable extension, it can be shipped
471 ALTER EXTENSION postgres_fdw ADD TEXT SEARCH CONFIGURATION public.custom_search;
472 -- however, that doesn't flush the shippability cache, so do a quick reconnect
473 \c -
474 EXPLAIN (VERBOSE, COSTS OFF)
475 SELECT c1, to_tsvector('custom_search'::regconfig, c3) FROM ft1
476 WHERE c1 = 642 AND length(to_tsvector('custom_search'::regconfig, c3)) > 0;
477 SELECT c1, to_tsvector('custom_search'::regconfig, c3) FROM ft1
478 WHERE c1 = 642 AND length(to_tsvector('custom_search'::regconfig, c3)) > 0;
479
480 -- ===================================================================
481 -- ORDER BY queries
482 -- ===================================================================
483 -- we should not push order by clause with volatile expressions or unsafe
484 -- collations
485 EXPLAIN (VERBOSE, COSTS OFF)
486 SELECT * FROM ft2 ORDER BY ft2.c1, random();
487 EXPLAIN (VERBOSE, COSTS OFF)
488 SELECT * FROM ft2 ORDER BY ft2.c1, ft2.c3 collate "C";
489
490 -- Ensure we don't push ORDER BY expressions which are Consts at the UNION
491 -- child level to the foreign server.
492 EXPLAIN (VERBOSE, COSTS OFF)
493 SELECT * FROM (
494 SELECT 1 AS type,c1 FROM ft1
495 UNION ALL
496 SELECT 2 AS type,c1 FROM ft2
497 ) a ORDER BY type,c1;
498
499 EXPLAIN (VERBOSE, COSTS OFF)
500 SELECT * FROM (
501 SELECT 1 AS type,c1 FROM ft1
502 UNION ALL
503 SELECT 2 AS type,c1 FROM ft2
504 ) a ORDER BY type;
505
506 -- ===================================================================
507 -- JOIN queries
508 -- ===================================================================
509 -- Analyze ft4 and ft5 so that we have better statistics. These tables do not
510 -- have use_remote_estimate set.
511 ANALYZE ft4;
512 ANALYZE ft5;
513
514 -- join two tables
515 EXPLAIN (VERBOSE, COSTS OFF)
516 SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
517 SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
518 -- join three tables
519 EXPLAIN (VERBOSE, COSTS OFF)
520 SELECT t1.c1, t2.c2, t3.c3 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) JOIN ft4 t3 ON (t3.c1 = t1.c1) ORDER BY t1.c3, t1.c1 OFFSET 10 LIMIT 10;
521 SELECT t1.c1, t2.c2, t3.c3 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) JOIN ft4 t3 ON (t3.c1 = t1.c1) ORDER BY t1.c3, t1.c1 OFFSET 10 LIMIT 10;
522 -- left outer join
523 EXPLAIN (VERBOSE, COSTS OFF)
524 SELECT t1.c1, t2.c1 FROM ft4 t1 LEFT JOIN ft5 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 10 LIMIT 10;
525 SELECT t1.c1, t2.c1 FROM ft4 t1 LEFT JOIN ft5 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 10 LIMIT 10;
526 -- left outer join three tables
527 EXPLAIN (VERBOSE, COSTS OFF)
528 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 LEFT JOIN ft2 t2 ON (t1.c1 = t2.c1) LEFT JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
529 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 LEFT JOIN ft2 t2 ON (t1.c1 = t2.c1) LEFT JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
530 -- left outer join + placement of clauses.
531 -- clauses within the nullable side are not pulled up, but top level clause on
532 -- non-nullable side is pushed into non-nullable side
533 EXPLAIN (VERBOSE, COSTS OFF)
534 SELECT t1.c1, t1.c2, t2.c1, t2.c2 FROM ft4 t1 LEFT JOIN (SELECT * FROM ft5 WHERE c1 < 10) t2 ON (t1.c1 = t2.c1) WHERE t1.c1 < 10;
535 SELECT t1.c1, t1.c2, t2.c1, t2.c2 FROM ft4 t1 LEFT JOIN (SELECT * FROM ft5 WHERE c1 < 10) t2 ON (t1.c1 = t2.c1) WHERE t1.c1 < 10;
536 -- clauses within the nullable side are not pulled up, but the top level clause
537 -- on nullable side is not pushed down into nullable side
538 EXPLAIN (VERBOSE, COSTS OFF)
539 SELECT t1.c1, t1.c2, t2.c1, t2.c2 FROM ft4 t1 LEFT JOIN (SELECT * FROM ft5 WHERE c1 < 10) t2 ON (t1.c1 = t2.c1)
540 WHERE (t2.c1 < 10 OR t2.c1 IS NULL) AND t1.c1 < 10;
541 SELECT t1.c1, t1.c2, t2.c1, t2.c2 FROM ft4 t1 LEFT JOIN (SELECT * FROM ft5 WHERE c1 < 10) t2 ON (t1.c1 = t2.c1)
542 WHERE (t2.c1 < 10 OR t2.c1 IS NULL) AND t1.c1 < 10;
543 -- right outer join
544 EXPLAIN (VERBOSE, COSTS OFF)
545 SELECT t1.c1, t2.c1 FROM ft5 t1 RIGHT JOIN ft4 t2 ON (t1.c1 = t2.c1) ORDER BY t2.c1, t1.c1 OFFSET 10 LIMIT 10;
546 SELECT t1.c1, t2.c1 FROM ft5 t1 RIGHT JOIN ft4 t2 ON (t1.c1 = t2.c1) ORDER BY t2.c1, t1.c1 OFFSET 10 LIMIT 10;
547 -- right outer join three tables
548 EXPLAIN (VERBOSE, COSTS OFF)
549 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 RIGHT JOIN ft2 t2 ON (t1.c1 = t2.c1) RIGHT JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
550 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 RIGHT JOIN ft2 t2 ON (t1.c1 = t2.c1) RIGHT JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
551 -- full outer join
552 EXPLAIN (VERBOSE, COSTS OFF)
553 SELECT t1.c1, t2.c1 FROM ft4 t1 FULL JOIN ft5 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 45 LIMIT 10;
554 SELECT t1.c1, t2.c1 FROM ft4 t1 FULL JOIN ft5 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 45 LIMIT 10;
555 -- full outer join with restrictions on the joining relations
556 -- a. the joining relations are both base relations
557 EXPLAIN (VERBOSE, COSTS OFF)
558 SELECT t1.c1, t2.c1 FROM (SELECT c1 FROM ft4 WHERE c1 between 50 and 60) t1 FULL JOIN (SELECT c1 FROM ft5 WHERE c1 between 50 and 60) t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1;
559 SELECT t1.c1, t2.c1 FROM (SELECT c1 FROM ft4 WHERE c1 between 50 and 60) t1 FULL JOIN (SELECT c1 FROM ft5 WHERE c1 between 50 and 60) t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1;
560 EXPLAIN (VERBOSE, COSTS OFF)
561 SELECT 1 FROM (SELECT c1 FROM ft4 WHERE c1 between 50 and 60) t1 FULL JOIN (SELECT c1 FROM ft5 WHERE c1 between 50 and 60) t2 ON (TRUE) OFFSET 10 LIMIT 10;
562 SELECT 1 FROM (SELECT c1 FROM ft4 WHERE c1 between 50 and 60) t1 FULL JOIN (SELECT c1 FROM ft5 WHERE c1 between 50 and 60) t2 ON (TRUE) OFFSET 10 LIMIT 10;
563 -- b. one of the joining relations is a base relation and the other is a join
564 -- relation
565 EXPLAIN (VERBOSE, COSTS OFF)
566 SELECT t1.c1, ss.a, ss.b FROM (SELECT c1 FROM ft4 WHERE c1 between 50 and 60) t1 FULL JOIN (SELECT t2.c1, t3.c1 FROM ft4 t2 LEFT JOIN ft5 t3 ON (t2.c1 = t3.c1) WHERE (t2.c1 between 50 and 60)) ss(a, b) ON (t1.c1 = ss.a) ORDER BY t1.c1, ss.a, ss.b;
567 SELECT t1.c1, ss.a, ss.b FROM (SELECT c1 FROM ft4 WHERE c1 between 50 and 60) t1 FULL JOIN (SELECT t2.c1, t3.c1 FROM ft4 t2 LEFT JOIN ft5 t3 ON (t2.c1 = t3.c1) WHERE (t2.c1 between 50 and 60)) ss(a, b) ON (t1.c1 = ss.a) ORDER BY t1.c1, ss.a, ss.b;
568 -- c. test deparsing the remote query as nested subqueries
569 EXPLAIN (VERBOSE, COSTS OFF)
570 SELECT t1.c1, ss.a, ss.b FROM (SELECT c1 FROM ft4 WHERE c1 between 50 and 60) t1 FULL JOIN (SELECT t2.c1, t3.c1 FROM (SELECT c1 FROM ft4 WHERE c1 between 50 and 60) t2 FULL JOIN (SELECT c1 FROM ft5 WHERE c1 between 50 and 60) t3 ON (t2.c1 = t3.c1) WHERE t2.c1 IS NULL OR t2.c1 IS NOT NULL) ss(a, b) ON (t1.c1 = ss.a) ORDER BY t1.c1, ss.a, ss.b;
571 SELECT t1.c1, ss.a, ss.b FROM (SELECT c1 FROM ft4 WHERE c1 between 50 and 60) t1 FULL JOIN (SELECT t2.c1, t3.c1 FROM (SELECT c1 FROM ft4 WHERE c1 between 50 and 60) t2 FULL JOIN (SELECT c1 FROM ft5 WHERE c1 between 50 and 60) t3 ON (t2.c1 = t3.c1) WHERE t2.c1 IS NULL OR t2.c1 IS NOT NULL) ss(a, b) ON (t1.c1 = ss.a) ORDER BY t1.c1, ss.a, ss.b;
572 -- d. test deparsing rowmarked relations as subqueries
573 EXPLAIN (VERBOSE, COSTS OFF)
574 SELECT t1.c1, ss.a, ss.b FROM (SELECT c1 FROM "S 1"."T 3" WHERE c1 = 50) t1 INNER JOIN (SELECT t2.c1, t3.c1 FROM (SELECT c1 FROM ft4 WHERE c1 between 50 and 60) t2 FULL JOIN (SELECT c1 FROM ft5 WHERE c1 between 50 and 60) t3 ON (t2.c1 = t3.c1) WHERE t2.c1 IS NULL OR t2.c1 IS NOT NULL) ss(a, b) ON (TRUE) ORDER BY t1.c1, ss.a, ss.b FOR UPDATE OF t1;
575 SELECT t1.c1, ss.a, ss.b FROM (SELECT c1 FROM "S 1"."T 3" WHERE c1 = 50) t1 INNER JOIN (SELECT t2.c1, t3.c1 FROM (SELECT c1 FROM ft4 WHERE c1 between 50 and 60) t2 FULL JOIN (SELECT c1 FROM ft5 WHERE c1 between 50 and 60) t3 ON (t2.c1 = t3.c1) WHERE t2.c1 IS NULL OR t2.c1 IS NOT NULL) ss(a, b) ON (TRUE) ORDER BY t1.c1, ss.a, ss.b FOR UPDATE OF t1;
576 -- full outer join + inner join
577 EXPLAIN (VERBOSE, COSTS OFF)
578 SELECT t1.c1, t2.c1, t3.c1 FROM ft4 t1 INNER JOIN ft5 t2 ON (t1.c1 = t2.c1 + 1 and t1.c1 between 50 and 60) FULL JOIN ft4 t3 ON (t2.c1 = t3.c1) ORDER BY t1.c1, t2.c1, t3.c1 LIMIT 10;
579 SELECT t1.c1, t2.c1, t3.c1 FROM ft4 t1 INNER JOIN ft5 t2 ON (t1.c1 = t2.c1 + 1 and t1.c1 between 50 and 60) FULL JOIN ft4 t3 ON (t2.c1 = t3.c1) ORDER BY t1.c1, t2.c1, t3.c1 LIMIT 10;
580 -- full outer join three tables
581 EXPLAIN (VERBOSE, COSTS OFF)
582 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 FULL JOIN ft2 t2 ON (t1.c1 = t2.c1) FULL JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
583 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 FULL JOIN ft2 t2 ON (t1.c1 = t2.c1) FULL JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
584 -- full outer join + right outer join
585 EXPLAIN (VERBOSE, COSTS OFF)
586 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 FULL JOIN ft2 t2 ON (t1.c1 = t2.c1) RIGHT JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
587 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 FULL JOIN ft2 t2 ON (t1.c1 = t2.c1) RIGHT JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
588 -- right outer join + full outer join
589 EXPLAIN (VERBOSE, COSTS OFF)
590 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 RIGHT JOIN ft2 t2 ON (t1.c1 = t2.c1) FULL JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
591 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 RIGHT JOIN ft2 t2 ON (t1.c1 = t2.c1) FULL JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
592 -- full outer join + left outer join
593 EXPLAIN (VERBOSE, COSTS OFF)
594 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 FULL JOIN ft2 t2 ON (t1.c1 = t2.c1) LEFT JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
595 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 FULL JOIN ft2 t2 ON (t1.c1 = t2.c1) LEFT JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
596 -- left outer join + full outer join
597 EXPLAIN (VERBOSE, COSTS OFF)
598 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 LEFT JOIN ft2 t2 ON (t1.c1 = t2.c1) FULL JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
599 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 LEFT JOIN ft2 t2 ON (t1.c1 = t2.c1) FULL JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
600 SET enable_memoize TO off;
601 -- right outer join + left outer join
602 EXPLAIN (VERBOSE, COSTS OFF)
603 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 RIGHT JOIN ft2 t2 ON (t1.c1 = t2.c1) LEFT JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
604 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 RIGHT JOIN ft2 t2 ON (t1.c1 = t2.c1) LEFT JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
605 RESET enable_memoize;
606 -- left outer join + right outer join
607 EXPLAIN (VERBOSE, COSTS OFF)
608 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 LEFT JOIN ft2 t2 ON (t1.c1 = t2.c1) RIGHT JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
609 SELECT t1.c1, t2.c2, t3.c3 FROM ft2 t1 LEFT JOIN ft2 t2 ON (t1.c1 = t2.c1) RIGHT JOIN ft4 t3 ON (t2.c1 = t3.c1) OFFSET 10 LIMIT 10;
610 -- full outer join + WHERE clause, only matched rows
611 EXPLAIN (VERBOSE, COSTS OFF)
612 SELECT t1.c1, t2.c1 FROM ft4 t1 FULL JOIN ft5 t2 ON (t1.c1 = t2.c1) WHERE (t1.c1 = t2.c1 OR t1.c1 IS NULL) ORDER BY t1.c1, t2.c1 OFFSET 10 LIMIT 10;
613 SELECT t1.c1, t2.c1 FROM ft4 t1 FULL JOIN ft5 t2 ON (t1.c1 = t2.c1) WHERE (t1.c1 = t2.c1 OR t1.c1 IS NULL) ORDER BY t1.c1, t2.c1 OFFSET 10 LIMIT 10;
614 -- full outer join + WHERE clause with shippable extensions set
615 EXPLAIN (VERBOSE, COSTS OFF)
616 SELECT t1.c1, t2.c2, t1.c3 FROM ft1 t1 FULL JOIN ft2 t2 ON (t1.c1 = t2.c1) WHERE postgres_fdw_abs(t1.c1) > 0 OFFSET 10 LIMIT 10;
617 ALTER SERVER loopback OPTIONS (DROP extensions);
618 -- full outer join + WHERE clause with shippable extensions not set
619 EXPLAIN (VERBOSE, COSTS OFF)
620 SELECT t1.c1, t2.c2, t1.c3 FROM ft1 t1 FULL JOIN ft2 t2 ON (t1.c1 = t2.c1) WHERE postgres_fdw_abs(t1.c1) > 0 OFFSET 10 LIMIT 10;
621 ALTER SERVER loopback OPTIONS (ADD extensions 'postgres_fdw');
622 -- join two tables with FOR UPDATE clause
623 -- tests whole-row reference for row marks
624 EXPLAIN (VERBOSE, COSTS OFF)
625 SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10 FOR UPDATE OF t1;
626 SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10 FOR UPDATE OF t1;
627 EXPLAIN (VERBOSE, COSTS OFF)
628 SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10 FOR UPDATE;
629 SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10 FOR UPDATE;
630 -- join two tables with FOR SHARE clause
631 EXPLAIN (VERBOSE, COSTS OFF)
632 SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10 FOR SHARE OF t1;
633 SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10 FOR SHARE OF t1;
634 EXPLAIN (VERBOSE, COSTS OFF)
635 SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10 FOR SHARE;
636 SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10 FOR SHARE;
637 -- join in CTE
638 EXPLAIN (VERBOSE, COSTS OFF)
639 WITH t (c1_1, c1_3, c2_1) AS MATERIALIZED (SELECT t1.c1, t1.c3, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1)) SELECT c1_1, c2_1 FROM t ORDER BY c1_3, c1_1 OFFSET 100 LIMIT 10;
640 WITH t (c1_1, c1_3, c2_1) AS MATERIALIZED (SELECT t1.c1, t1.c3, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1)) SELECT c1_1, c2_1 FROM t ORDER BY c1_3, c1_1 OFFSET 100 LIMIT 10;
641 -- ctid with whole-row reference
642 EXPLAIN (VERBOSE, COSTS OFF)
643 SELECT t1.ctid, t1, t2, t1.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
644 -- SEMI JOIN
645 EXPLAIN (VERBOSE, COSTS OFF)
646 SELECT t1.c1 FROM ft1 t1 WHERE EXISTS (SELECT 1 FROM ft2 t2 WHERE t1.c1 = t2.c1) ORDER BY t1.c1 OFFSET 100 LIMIT 10;
647 SELECT t1.c1 FROM ft1 t1 WHERE EXISTS (SELECT 1 FROM ft2 t2 WHERE t1.c1 = t2.c1) ORDER BY t1.c1 OFFSET 100 LIMIT 10;
648 -- ANTI JOIN, not pushed down
649 EXPLAIN (VERBOSE, COSTS OFF)
650 SELECT t1.c1 FROM ft1 t1 WHERE NOT EXISTS (SELECT 1 FROM ft2 t2 WHERE t1.c1 = t2.c2) ORDER BY t1.c1 OFFSET 100 LIMIT 10;
651 SELECT t1.c1 FROM ft1 t1 WHERE NOT EXISTS (SELECT 1 FROM ft2 t2 WHERE t1.c1 = t2.c2) ORDER BY t1.c1 OFFSET 100 LIMIT 10;
652 -- CROSS JOIN can be pushed down
653 EXPLAIN (VERBOSE, COSTS OFF)
654 SELECT t1.c1, t2.c1 FROM ft1 t1 CROSS JOIN ft2 t2 ORDER BY t1.c1, t2.c1 OFFSET 100 LIMIT 10;
655 SELECT t1.c1, t2.c1 FROM ft1 t1 CROSS JOIN ft2 t2 ORDER BY t1.c1, t2.c1 OFFSET 100 LIMIT 10;
656 -- different server, not pushed down. No result expected.
657 EXPLAIN (VERBOSE, COSTS OFF)
658 SELECT t1.c1, t2.c1 FROM ft5 t1 JOIN ft6 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 100 LIMIT 10;
659 SELECT t1.c1, t2.c1 FROM ft5 t1 JOIN ft6 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 100 LIMIT 10;
660 -- unsafe join conditions (c8 has a UDT), not pushed down. Practically a CROSS
661 -- JOIN since c8 in both tables has same value.
662 EXPLAIN (VERBOSE, COSTS OFF)
663 SELECT t1.c1, t2.c1 FROM ft1 t1 LEFT JOIN ft2 t2 ON (t1.c8 = t2.c8) ORDER BY t1.c1, t2.c1 OFFSET 100 LIMIT 10;
664 SELECT t1.c1, t2.c1 FROM ft1 t1 LEFT JOIN ft2 t2 ON (t1.c8 = t2.c8) ORDER BY t1.c1, t2.c1 OFFSET 100 LIMIT 10;
665 -- unsafe conditions on one side (c8 has a UDT), not pushed down.
666 EXPLAIN (VERBOSE, COSTS OFF)
667 SELECT t1.c1, t2.c1 FROM ft1 t1 LEFT JOIN ft2 t2 ON (t1.c1 = t2.c1) WHERE t1.c8 = 'foo' ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
668 SELECT t1.c1, t2.c1 FROM ft1 t1 LEFT JOIN ft2 t2 ON (t1.c1 = t2.c1) WHERE t1.c8 = 'foo' ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
669 -- join where unsafe to pushdown condition in WHERE clause has a column not
670 -- in the SELECT clause. In this test unsafe clause needs to have column
671 -- references from both joining sides so that the clause is not pushed down
672 -- into one of the joining sides.
673 EXPLAIN (VERBOSE, COSTS OFF)
674 SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) WHERE t1.c8 = t2.c8 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
675 SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) WHERE t1.c8 = t2.c8 ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
676 -- Aggregate after UNION, for testing setrefs
677 EXPLAIN (VERBOSE, COSTS OFF)
678 SELECT t1c1, avg(t1c1 + t2c1) FROM (SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) UNION SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1)) AS t (t1c1, t2c1) GROUP BY t1c1 ORDER BY t1c1 OFFSET 100 LIMIT 10;
679 SELECT t1c1, avg(t1c1 + t2c1) FROM (SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1) UNION SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1)) AS t (t1c1, t2c1) GROUP BY t1c1 ORDER BY t1c1 OFFSET 100 LIMIT 10;
680 -- join with lateral reference
681 EXPLAIN (VERBOSE, COSTS OFF)
682 SELECT t1."C 1" FROM "S 1"."T 1" t1, LATERAL (SELECT DISTINCT t2.c1, t3.c1 FROM ft1 t2, ft2 t3 WHERE t2.c1 = t3.c1 AND t2.c2 = t1.c2) q ORDER BY t1."C 1" OFFSET 10 LIMIT 10;
683 SELECT t1."C 1" FROM "S 1"."T 1" t1, LATERAL (SELECT DISTINCT t2.c1, t3.c1 FROM ft1 t2, ft2 t3 WHERE t2.c1 = t3.c1 AND t2.c2 = t1.c2) q ORDER BY t1."C 1" OFFSET 10 LIMIT 10;
684 -- join with pseudoconstant quals
685 EXPLAIN (VERBOSE, COSTS OFF)
686 SELECT t1.c1, t2.c1 FROM ft1 t1 JOIN ft2 t2 ON (t1.c1 = t2.c1 AND CURRENT_USER = SESSION_USER) ORDER BY t1.c3, t1.c1 OFFSET 100 LIMIT 10;
687
688 -- non-Var items in targetlist of the nullable rel of a join preventing
689 -- push-down in some cases
690 -- unable to push {ft1, ft2}
691 EXPLAIN (VERBOSE, COSTS OFF)
692 SELECT q.a, ft2.c1 FROM (SELECT 13 FROM ft1 WHERE c1 = 13) q(a) RIGHT JOIN ft2 ON (q.a = ft2.c1) WHERE ft2.c1 BETWEEN 10 AND 15;
693 SELECT q.a, ft2.c1 FROM (SELECT 13 FROM ft1 WHERE c1 = 13) q(a) RIGHT JOIN ft2 ON (q.a = ft2.c1) WHERE ft2.c1 BETWEEN 10 AND 15;
694
695 -- ok to push {ft1, ft2} but not {ft1, ft2, ft4}
696 EXPLAIN (VERBOSE, COSTS OFF)
697 SELECT ft4.c1, q.* FROM ft4 LEFT JOIN (SELECT 13, ft1.c1, ft2.c1 FROM ft1 RIGHT JOIN ft2 ON (ft1.c1 = ft2.c1) WHERE ft1.c1 = 12) q(a, b, c) ON (ft4.c1 = q.b) WHERE ft4.c1 BETWEEN 10 AND 15;
698 SELECT ft4.c1, q.* FROM ft4 LEFT JOIN (SELECT 13, ft1.c1, ft2.c1 FROM ft1 RIGHT JOIN ft2 ON (ft1.c1 = ft2.c1) WHERE ft1.c1 = 12) q(a, b, c) ON (ft4.c1 = q.b) WHERE ft4.c1 BETWEEN 10 AND 15;
699
700 -- join with nullable side with some columns with null values
701 UPDATE ft5 SET c3 = null where c1 % 9 = 0;
702 EXPLAIN (VERBOSE, COSTS OFF)
703 SELECT ft5, ft5.c1, ft5.c2, ft5.c3, ft4.c1, ft4.c2 FROM ft5 left join ft4 on ft5.c1 = ft4.c1 WHERE ft4.c1 BETWEEN 10 and 30 ORDER BY ft5.c1, ft4.c1;
704 SELECT ft5, ft5.c1, ft5.c2, ft5.c3, ft4.c1, ft4.c2 FROM ft5 left join ft4 on ft5.c1 = ft4.c1 WHERE ft4.c1 BETWEEN 10 and 30 ORDER BY ft5.c1, ft4.c1;
705
706 -- multi-way join involving multiple merge joins
707 -- (this case used to have EPQ-related planning problems)
708 CREATE TABLE local_tbl (c1 int NOT NULL, c2 int NOT NULL, c3 text, CONSTRAINT local_tbl_pkey PRIMARY KEY (c1));
709 INSERT INTO local_tbl SELECT id, id % 10, to_char(id, 'FM0000') FROM generate_series(1, 1000) id;
710 ANALYZE local_tbl;
711 SET enable_nestloop TO false;
712 SET enable_hashjoin TO false;
713 EXPLAIN (VERBOSE, COSTS OFF)
714 SELECT * FROM ft1, ft2, ft4, ft5, local_tbl WHERE ft1.c1 = ft2.c1 AND ft1.c2 = ft4.c1
715 AND ft1.c2 = ft5.c1 AND ft1.c2 = local_tbl.c1 AND ft1.c1 < 100 AND ft2.c1 < 100 FOR UPDATE;
716 SELECT * FROM ft1, ft2, ft4, ft5, local_tbl WHERE ft1.c1 = ft2.c1 AND ft1.c2 = ft4.c1
717 AND ft1.c2 = ft5.c1 AND ft1.c2 = local_tbl.c1 AND ft1.c1 < 100 AND ft2.c1 < 100 FOR UPDATE;
718 EXPLAIN (VERBOSE, COSTS OFF)
719 SELECT * FROM ft1, ft4, ft5, local_tbl WHERE ft1.c1 = ft4.c1 AND ft1.c1 = ft5.c1
720 AND ft1.c2 = local_tbl.c1 AND ft1.c1 < 100 AND ft5.c1 < 100 FOR UPDATE;
721 SELECT * FROM ft1, ft4, ft5, local_tbl WHERE ft1.c1 = ft4.c1 AND ft1.c1 = ft5.c1
722 AND ft1.c2 = local_tbl.c1 AND ft1.c1 < 100 AND ft5.c1 < 100 FOR UPDATE;
723 RESET enable_nestloop;
724 RESET enable_hashjoin;
725
726 -- test that add_paths_with_pathkeys_for_rel() arranges for the epq_path to
727 -- return columns needed by the parent ForeignScan node
728 EXPLAIN (VERBOSE, COSTS OFF)
729 SELECT * FROM local_tbl LEFT JOIN (SELECT ft1.*, COALESCE(ft1.c3 || ft2.c3, 'foobar') FROM ft1 INNER JOIN ft2 ON (ft1.c1 = ft2.c1 AND ft1.c1 < 100)) ss ON (local_tbl.c1 = ss.c1) ORDER BY local_tbl.c1 FOR UPDATE OF local_tbl;
730
731 ALTER SERVER loopback OPTIONS (DROP extensions);
732 ALTER SERVER loopback OPTIONS (ADD fdw_startup_cost '10000.0');
733 EXPLAIN (VERBOSE, COSTS OFF)
734 SELECT * FROM local_tbl LEFT JOIN (SELECT ft1.* FROM ft1 INNER JOIN ft2 ON (ft1.c1 = ft2.c1 AND ft1.c1 < 100 AND (ft1.c1 - postgres_fdw_abs(ft2.c2)) = 0)) ss ON (local_tbl.c3 = ss.c3) ORDER BY local_tbl.c1 FOR UPDATE OF local_tbl;
735 ALTER SERVER loopback OPTIONS (DROP fdw_startup_cost);
736 ALTER SERVER loopback OPTIONS (ADD extensions 'postgres_fdw');
737
738 DROP TABLE local_tbl;
739
740 -- check join pushdown in situations where multiple userids are involved
741 CREATE ROLE regress_view_owner SUPERUSER;
742 CREATE USER MAPPING FOR regress_view_owner SERVER loopback;
743 GRANT SELECT ON ft4 TO regress_view_owner;
744 GRANT SELECT ON ft5 TO regress_view_owner;
745
746 CREATE VIEW v4 AS SELECT * FROM ft4;
747 CREATE VIEW v5 AS SELECT * FROM ft5;
748 ALTER VIEW v5 OWNER TO regress_view_owner;
749 EXPLAIN (VERBOSE, COSTS OFF)
750 SELECT t1.c1, t2.c2 FROM v4 t1 LEFT JOIN v5 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 10 LIMIT 10; -- can't be pushed down, different view owners
751 SELECT t1.c1, t2.c2 FROM v4 t1 LEFT JOIN v5 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 10 LIMIT 10;
752 ALTER VIEW v4 OWNER TO regress_view_owner;
753 EXPLAIN (VERBOSE, COSTS OFF)
754 SELECT t1.c1, t2.c2 FROM v4 t1 LEFT JOIN v5 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 10 LIMIT 10; -- can be pushed down
755 SELECT t1.c1, t2.c2 FROM v4 t1 LEFT JOIN v5 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 10 LIMIT 10;
756
757 EXPLAIN (VERBOSE, COSTS OFF)
758 SELECT t1.c1, t2.c2 FROM v4 t1 LEFT JOIN ft5 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 10 LIMIT 10; -- can't be pushed down, view owner not current user
759 SELECT t1.c1, t2.c2 FROM v4 t1 LEFT JOIN ft5 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 10 LIMIT 10;
760 ALTER VIEW v4 OWNER TO CURRENT_USER;
761 EXPLAIN (VERBOSE, COSTS OFF)
762 SELECT t1.c1, t2.c2 FROM v4 t1 LEFT JOIN ft5 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 10 LIMIT 10; -- can be pushed down
763 SELECT t1.c1, t2.c2 FROM v4 t1 LEFT JOIN ft5 t2 ON (t1.c1 = t2.c1) ORDER BY t1.c1, t2.c1 OFFSET 10 LIMIT 10;
764 ALTER VIEW v4 OWNER TO regress_view_owner;
765
766 -- ====================================================================
767 -- Check that userid to use when querying the remote table is correctly
768 -- propagated into foreign rels present in subqueries under an UNION ALL
769 -- ====================================================================
770 CREATE ROLE regress_view_owner_another;
771 ALTER VIEW v4 OWNER TO regress_view_owner_another;
772 GRANT SELECT ON ft4 TO regress_view_owner_another;
773 ALTER FOREIGN TABLE ft4 OPTIONS (ADD use_remote_estimate 'true');
774 -- The following should query the remote backing table of ft4 as user
775 -- regress_view_owner_another, the view owner, though it fails as expected
776 -- due to the lack of a user mapping for that user.
777 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM v4;
778 -- Likewise, but with the query under an UNION ALL
779 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM (SELECT * FROM v4 UNION ALL SELECT * FROM v4);
780 -- Should not get that error once a user mapping is created
781 CREATE USER MAPPING FOR regress_view_owner_another SERVER loopback OPTIONS (password_required 'false');
782 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM v4;
783 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM (SELECT * FROM v4 UNION ALL SELECT * FROM v4);
784 DROP USER MAPPING FOR regress_view_owner_another SERVER loopback;
785 DROP OWNED BY regress_view_owner_another;
786 DROP ROLE regress_view_owner_another;
787 ALTER FOREIGN TABLE ft4 OPTIONS (SET use_remote_estimate 'false');
788
789 -- cleanup
790 DROP OWNED BY regress_view_owner;
791 DROP ROLE regress_view_owner;
792
793
794 -- ===================================================================
795 -- Aggregate and grouping queries
796 -- ===================================================================
797
798 -- Simple aggregates
799 explain (verbose, costs off)
800 select count(c6), sum(c1), avg(c1), min(c2), max(c1), stddev(c2), sum(c1) * (random() <= 1)::int as sum2 from ft1 where c2 < 5 group by c2 order by 1, 2;
801 select count(c6), sum(c1), avg(c1), min(c2), max(c1), stddev(c2), sum(c1) * (random() <= 1)::int as sum2 from ft1 where c2 < 5 group by c2 order by 1, 2;
802
803 explain (verbose, costs off)
804 select count(c6), sum(c1), avg(c1), min(c2), max(c1), stddev(c2), sum(c1) * (random() <= 1)::int as sum2 from ft1 where c2 < 5 group by c2 order by 1, 2 limit 1;
805 select count(c6), sum(c1), avg(c1), min(c2), max(c1), stddev(c2), sum(c1) * (random() <= 1)::int as sum2 from ft1 where c2 < 5 group by c2 order by 1, 2 limit 1;
806
807 -- Aggregate is not pushed down as aggregation contains random()
808 explain (verbose, costs off)
809 select sum(c1 * (random() <= 1)::int) as sum, avg(c1) from ft1;
810
811 -- Aggregate over join query
812 explain (verbose, costs off)
813 select count(*), sum(t1.c1), avg(t2.c1) from ft1 t1 inner join ft1 t2 on (t1.c2 = t2.c2) where t1.c2 = 6;
814 select count(*), sum(t1.c1), avg(t2.c1) from ft1 t1 inner join ft1 t2 on (t1.c2 = t2.c2) where t1.c2 = 6;
815
816 -- Not pushed down due to local conditions present in underneath input rel
817 explain (verbose, costs off)
818 select sum(t1.c1), count(t2.c1) from ft1 t1 inner join ft2 t2 on (t1.c1 = t2.c1) where ((t1.c1 * t2.c1)/(t1.c1 * t2.c1)) * random() <= 1;
819
820 -- GROUP BY clause having expressions
821 explain (verbose, costs off)
822 select c2/2, sum(c2) * (c2/2) from ft1 group by c2/2 order by c2/2;
823 select c2/2, sum(c2) * (c2/2) from ft1 group by c2/2 order by c2/2;
824
825 -- Aggregates in subquery are pushed down.
826 set enable_incremental_sort = off;
827 explain (verbose, costs off)
828 select count(x.a), sum(x.a) from (select c2 a, sum(c1) b from ft1 group by c2, sqrt(c1) order by 1, 2) x;
829 select count(x.a), sum(x.a) from (select c2 a, sum(c1) b from ft1 group by c2, sqrt(c1) order by 1, 2) x;
830 reset enable_incremental_sort;
831
832 -- Aggregate is still pushed down by taking unshippable expression out
833 explain (verbose, costs off)
834 select c2 * (random() <= 1)::int as sum1, sum(c1) * c2 as sum2 from ft1 group by c2 order by 1, 2;
835 select c2 * (random() <= 1)::int as sum1, sum(c1) * c2 as sum2 from ft1 group by c2 order by 1, 2;
836
837 -- Aggregate with unshippable GROUP BY clause are not pushed
838 explain (verbose, costs off)
839 select c2 * (random() <= 1)::int as c2 from ft2 group by c2 * (random() <= 1)::int order by 1;
840
841 -- GROUP BY clause in various forms, cardinal, alias and constant expression
842 explain (verbose, costs off)
843 select count(c2) w, c2 x, 5 y, 7.0 z from ft1 group by 2, y, 9.0::int order by 2;
844 select count(c2) w, c2 x, 5 y, 7.0 z from ft1 group by 2, y, 9.0::int order by 2;
845
846 -- GROUP BY clause referring to same column multiple times
847 -- Also, ORDER BY contains an aggregate function
848 explain (verbose, costs off)
849 select c2, c2 from ft1 where c2 > 6 group by 1, 2 order by sum(c1);
850 select c2, c2 from ft1 where c2 > 6 group by 1, 2 order by sum(c1);
851
852 -- Testing HAVING clause shippability
853 explain (verbose, costs off)
854 select c2, sum(c1) from ft2 group by c2 having avg(c1) < 500 and sum(c1) < 49800 order by c2;
855 select c2, sum(c1) from ft2 group by c2 having avg(c1) < 500 and sum(c1) < 49800 order by c2;
856
857 -- Unshippable HAVING clause will be evaluated locally, and other qual in HAVING clause is pushed down
858 explain (verbose, costs off)
859 select count(*) from (select c5, count(c1) from ft1 group by c5, sqrt(c2) having (avg(c1) / avg(c1)) * random() <= 1 and avg(c1) < 500) x;
860 select count(*) from (select c5, count(c1) from ft1 group by c5, sqrt(c2) having (avg(c1) / avg(c1)) * random() <= 1 and avg(c1) < 500) x;
861
862 -- Aggregate in HAVING clause is not pushable, and thus aggregation is not pushed down
863 explain (verbose, costs off)
864 select sum(c1) from ft1 group by c2 having avg(c1 * (random() <= 1)::int) > 100 order by 1;
865
866 -- Remote aggregate in combination with a local Param (for the output
867 -- of an initplan) can be trouble, per bug #15781
868 explain (verbose, costs off)
869 select exists(select 1 from pg_enum), sum(c1) from ft1;
870 select exists(select 1 from pg_enum), sum(c1) from ft1;
871
872 explain (verbose, costs off)
873 select exists(select 1 from pg_enum), sum(c1) from ft1 group by 1;
874 select exists(select 1 from pg_enum), sum(c1) from ft1 group by 1;
875
876
877 -- Testing ORDER BY, DISTINCT, FILTER, Ordered-sets and VARIADIC within aggregates
878
879 -- ORDER BY within aggregate, same column used to order
880 explain (verbose, costs off)
881 select array_agg(c1 order by c1) from ft1 where c1 < 100 group by c2 order by 1;
882 select array_agg(c1 order by c1) from ft1 where c1 < 100 group by c2 order by 1;
883
884 -- ORDER BY within aggregate, different column used to order also using DESC
885 explain (verbose, costs off)
886 select array_agg(c5 order by c1 desc) from ft2 where c2 = 6 and c1 < 50;
887 select array_agg(c5 order by c1 desc) from ft2 where c2 = 6 and c1 < 50;
888
889 -- DISTINCT within aggregate
890 explain (verbose, costs off)
891 select array_agg(distinct (t1.c1)%5) from ft4 t1 full join ft5 t2 on (t1.c1 = t2.c1) where t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) group by (t2.c1)%3 order by 1;
892 select array_agg(distinct (t1.c1)%5) from ft4 t1 full join ft5 t2 on (t1.c1 = t2.c1) where t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) group by (t2.c1)%3 order by 1;
893
894 -- DISTINCT combined with ORDER BY within aggregate
895 explain (verbose, costs off)
896 select array_agg(distinct (t1.c1)%5 order by (t1.c1)%5) from ft4 t1 full join ft5 t2 on (t1.c1 = t2.c1) where t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) group by (t2.c1)%3 order by 1;
897 select array_agg(distinct (t1.c1)%5 order by (t1.c1)%5) from ft4 t1 full join ft5 t2 on (t1.c1 = t2.c1) where t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) group by (t2.c1)%3 order by 1;
898
899 explain (verbose, costs off)
900 select array_agg(distinct (t1.c1)%5 order by (t1.c1)%5 desc nulls last) from ft4 t1 full join ft5 t2 on (t1.c1 = t2.c1) where t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) group by (t2.c1)%3 order by 1;
901 select array_agg(distinct (t1.c1)%5 order by (t1.c1)%5 desc nulls last) from ft4 t1 full join ft5 t2 on (t1.c1 = t2.c1) where t1.c1 < 20 or (t1.c1 is null and t2.c1 < 5) group by (t2.c1)%3 order by 1;
902
903 -- FILTER within aggregate
904 explain (verbose, costs off)
905 select sum(c1) filter (where c1 < 100 and c2 > 5) from ft1 group by c2 order by 1 nulls last;
906 select sum(c1) filter (where c1 < 100 and c2 > 5) from ft1 group by c2 order by 1 nulls last;
907
908 -- DISTINCT, ORDER BY and FILTER within aggregate
909 explain (verbose, costs off)
910 select sum(c1%3), sum(distinct c1%3 order by c1%3) filter (where c1%3 < 2), c2 from ft1 where c2 = 6 group by c2;
911 select sum(c1%3), sum(distinct c1%3 order by c1%3) filter (where c1%3 < 2), c2 from ft1 where c2 = 6 group by c2;
912
913 -- Outer query is aggregation query
914 explain (verbose, costs off)
915 select distinct (select count(*) filter (where t2.c2 = 6 and t2.c1 < 10) from ft1 t1 where t1.c1 = 6) from ft2 t2 where t2.c2 % 6 = 0 order by 1;
916 select distinct (select count(*) filter (where t2.c2 = 6 and t2.c1 < 10) from ft1 t1 where t1.c1 = 6) from ft2 t2 where t2.c2 % 6 = 0 order by 1;
917 -- Inner query is aggregation query
918 explain (verbose, costs off)
919 select distinct (select count(t1.c1) filter (where t2.c2 = 6 and t2.c1 < 10) from ft1 t1 where t1.c1 = 6) from ft2 t2 where t2.c2 % 6 = 0 order by 1;
920 select distinct (select count(t1.c1) filter (where t2.c2 = 6 and t2.c1 < 10) from ft1 t1 where t1.c1 = 6) from ft2 t2 where t2.c2 % 6 = 0 order by 1;
921
922 -- Aggregate not pushed down as FILTER condition is not pushable
923 explain (verbose, costs off)
924 select sum(c1) filter (where (c1 / c1) * random() <= 1) from ft1 group by c2 order by 1;
925 explain (verbose, costs off)
926 select sum(c2) filter (where c2 in (select c2 from ft1 where c2 < 5)) from ft1;
927
928 -- Ordered-sets within aggregate
929 explain (verbose, costs off)
930 select c2, rank('10'::varchar) within group (order by c6), percentile_cont(c2/10::numeric) within group (order by c1) from ft1 where c2 < 10 group by c2 having percentile_cont(c2/10::numeric) within group (order by c1) < 500 order by c2;
931 select c2, rank('10'::varchar) within group (order by c6), percentile_cont(c2/10::numeric) within group (order by c1) from ft1 where c2 < 10 group by c2 having percentile_cont(c2/10::numeric) within group (order by c1) < 500 order by c2;
932
933 -- Using multiple arguments within aggregates
934 explain (verbose, costs off)
935 select c1, rank(c1, c2) within group (order by c1, c2) from ft1 group by c1, c2 having c1 = 6 order by 1;
936 select c1, rank(c1, c2) within group (order by c1, c2) from ft1 group by c1, c2 having c1 = 6 order by 1;
937
938 -- User defined function for user defined aggregate, VARIADIC
939 create function least_accum(anyelement, variadic anyarray)
940 returns anyelement language sql as
941 'select least($1, min($2[i])) from generate_subscripts($2,1) g(i)';
942 create aggregate least_agg(variadic items anyarray) (
943 stype = anyelement, sfunc = least_accum
944 );
945
946 -- Disable hash aggregation for plan stability.
947 set enable_hashagg to false;
948
949 -- Not pushed down due to user defined aggregate
950 explain (verbose, costs off)
951 select c2, least_agg(c1) from ft1 group by c2 order by c2;
952
953 -- Add function and aggregate into extension
954 alter extension postgres_fdw add function least_accum(anyelement, variadic anyarray);
955 alter extension postgres_fdw add aggregate least_agg(variadic items anyarray);
956 alter server loopback options (set extensions 'postgres_fdw');
957
958 -- Now aggregate will be pushed. Aggregate will display VARIADIC argument.
959 explain (verbose, costs off)
960 select c2, least_agg(c1) from ft1 where c2 < 100 group by c2 order by c2;
961 select c2, least_agg(c1) from ft1 where c2 < 100 group by c2 order by c2;
962
963 -- Remove function and aggregate from extension
964 alter extension postgres_fdw drop function least_accum(anyelement, variadic anyarray);
965 alter extension postgres_fdw drop aggregate least_agg(variadic items anyarray);
966 alter server loopback options (set extensions 'postgres_fdw');
967
968 -- Not pushed down as we have dropped objects from extension.
969 explain (verbose, costs off)
970 select c2, least_agg(c1) from ft1 group by c2 order by c2;
971
972 -- Cleanup
973 reset enable_hashagg;
974 drop aggregate least_agg(variadic items anyarray);
975 drop function least_accum(anyelement, variadic anyarray);
976
977
978 -- Testing USING OPERATOR() in ORDER BY within aggregate.
979 -- For this, we need user defined operators along with operator family and
980 -- operator class. Create those and then add them in extension. Note that
981 -- user defined objects are considered unshippable unless they are part of
982 -- the extension.
983 create operator public.<^ (
984 leftarg = int4,
985 rightarg = int4,
986 procedure = int4eq
987 );
988
989 create operator public.=^ (
990 leftarg = int4,
991 rightarg = int4,
992 procedure = int4lt
993 );
994
995 create operator public.>^ (
996 leftarg = int4,
997 rightarg = int4,
998 procedure = int4gt
999 );
1000
1001 create operator family my_op_family using btree;
1002
1003 create function my_op_cmp(a int, b int) returns int as
1004 $$begin return btint4cmp(a, b); end $$ language plpgsql;
1005
1006 create operator class my_op_class for type int using btree family my_op_family as
1007 operator 1 public.<^,
1008 operator 3 public.=^,
1009 operator 5 public.>^,
1010 function 1 my_op_cmp(int, int);
1011
1012 -- This will not be pushed as user defined sort operator is not part of the
1013 -- extension yet.
1014 explain (verbose, costs off)
1015 select array_agg(c1 order by c1 using operator(public.<^)) from ft2 where c2 = 6 and c1 < 100 group by c2;
1016
1017 -- This should not be pushed either.
1018 explain (verbose, costs off)
1019 select * from ft2 order by c1 using operator(public.<^);
1020
1021 -- Update local stats on ft2
1022 ANALYZE ft2;
1023
1024 -- Add into extension
1025 alter extension postgres_fdw add operator class my_op_class using btree;
1026 alter extension postgres_fdw add function my_op_cmp(a int, b int);
1027 alter extension postgres_fdw add operator family my_op_family using btree;
1028 alter extension postgres_fdw add operator public.<^(int, int);
1029 alter extension postgres_fdw add operator public.=^(int, int);
1030 alter extension postgres_fdw add operator public.>^(int, int);
1031 alter server loopback options (set extensions 'postgres_fdw');
1032
1033 -- Now this will be pushed as sort operator is part of the extension.
1034 alter server loopback options (add fdw_tuple_cost '0.5');
1035 explain (verbose, costs off)
1036 select array_agg(c1 order by c1 using operator(public.<^)) from ft2 where c2 = 6 and c1 < 100 group by c2;
1037 select array_agg(c1 order by c1 using operator(public.<^)) from ft2 where c2 = 6 and c1 < 100 group by c2;
1038 alter server loopback options (drop fdw_tuple_cost);
1039
1040 -- This should be pushed too.
1041 explain (verbose, costs off)
1042 select * from ft2 order by c1 using operator(public.<^);
1043
1044 -- Remove from extension
1045 alter extension postgres_fdw drop operator class my_op_class using btree;
1046 alter extension postgres_fdw drop function my_op_cmp(a int, b int);
1047 alter extension postgres_fdw drop operator family my_op_family using btree;
1048 alter extension postgres_fdw drop operator public.<^(int, int);
1049 alter extension postgres_fdw drop operator public.=^(int, int);
1050 alter extension postgres_fdw drop operator public.>^(int, int);
1051 alter server loopback options (set extensions 'postgres_fdw');
1052
1053 -- This will not be pushed as sort operator is now removed from the extension.
1054 explain (verbose, costs off)
1055 select array_agg(c1 order by c1 using operator(public.<^)) from ft2 where c2 = 6 and c1 < 100 group by c2;
1056
1057 -- Cleanup
1058 drop operator class my_op_class using btree;
1059 drop function my_op_cmp(a int, b int);
1060 drop operator family my_op_family using btree;
1061 drop operator public.>^(int, int);
1062 drop operator public.=^(int, int);
1063 drop operator public.<^(int, int);
1064
1065 -- Input relation to aggregate push down hook is not safe to pushdown and thus
1066 -- the aggregate cannot be pushed down to foreign server.
1067 explain (verbose, costs off)
1068 select count(t1.c3) from ft2 t1 left join ft2 t2 on (t1.c1 = random() * t2.c2);
1069
1070 -- Subquery in FROM clause having aggregate
1071 explain (verbose, costs off)
1072 select count(*), x.b from ft1, (select c2 a, sum(c1) b from ft1 group by c2) x where ft1.c2 = x.a group by x.b order by 1, 2;
1073 select count(*), x.b from ft1, (select c2 a, sum(c1) b from ft1 group by c2) x where ft1.c2 = x.a group by x.b order by 1, 2;
1074
1075 -- FULL join with IS NULL check in HAVING
1076 explain (verbose, costs off)
1077 select avg(t1.c1), sum(t2.c1) from ft4 t1 full join ft5 t2 on (t1.c1 = t2.c1) group by t2.c1 having (avg(t1.c1) is null and sum(t2.c1) < 10) or sum(t2.c1) is null order by 1 nulls last, 2;
1078 select avg(t1.c1), sum(t2.c1) from ft4 t1 full join ft5 t2 on (t1.c1 = t2.c1) group by t2.c1 having (avg(t1.c1) is null and sum(t2.c1) < 10) or sum(t2.c1) is null order by 1 nulls last, 2;
1079
1080 -- Aggregate over FULL join needing to deparse the joining relations as
1081 -- subqueries.
1082 explain (verbose, costs off)
1083 select count(*), sum(t1.c1), avg(t2.c1) from (select c1 from ft4 where c1 between 50 and 60) t1 full join (select c1 from ft5 where c1 between 50 and 60) t2 on (t1.c1 = t2.c1);
1084 select count(*), sum(t1.c1), avg(t2.c1) from (select c1 from ft4 where c1 between 50 and 60) t1 full join (select c1 from ft5 where c1 between 50 and 60) t2 on (t1.c1 = t2.c1);
1085
1086 -- ORDER BY expression is part of the target list but not pushed down to
1087 -- foreign server.
1088 explain (verbose, costs off)
1089 select sum(c2) * (random() <= 1)::int as sum from ft1 order by 1;
1090 select sum(c2) * (random() <= 1)::int as sum from ft1 order by 1;
1091
1092 -- LATERAL join, with parameterization
1093 set enable_hashagg to false;
1094 explain (verbose, costs off)
1095 select c2, sum from "S 1"."T 1" t1, lateral (select sum(t2.c1 + t1."C 1") sum from ft2 t2 group by t2.c1) qry where t1.c2 * 2 = qry.sum and t1.c2 < 3 and t1."C 1" < 100 order by 1;
1096 select c2, sum from "S 1"."T 1" t1, lateral (select sum(t2.c1 + t1."C 1") sum from ft2 t2 group by t2.c1) qry where t1.c2 * 2 = qry.sum and t1.c2 < 3 and t1."C 1" < 100 order by 1;
1097 reset enable_hashagg;
1098
1099 -- bug #15613: bad plan for foreign table scan with lateral reference
1100 EXPLAIN (VERBOSE, COSTS OFF)
1101 SELECT ref_0.c2, subq_1.*
1102 FROM
1103 "S 1"."T 1" AS ref_0,
1104 LATERAL (
1105 SELECT ref_0."C 1" c1, subq_0.*
1106 FROM (SELECT ref_0.c2, ref_1.c3
1107 FROM ft1 AS ref_1) AS subq_0
1108 RIGHT JOIN ft2 AS ref_3 ON (subq_0.c3 = ref_3.c3)
1109 ) AS subq_1
1110 WHERE ref_0."C 1" < 10 AND subq_1.c3 = '00001'
1111 ORDER BY ref_0."C 1";
1112
1113 SELECT ref_0.c2, subq_1.*
1114 FROM
1115 "S 1"."T 1" AS ref_0,
1116 LATERAL (
1117 SELECT ref_0."C 1" c1, subq_0.*
1118 FROM (SELECT ref_0.c2, ref_1.c3
1119 FROM ft1 AS ref_1) AS subq_0
1120 RIGHT JOIN ft2 AS ref_3 ON (subq_0.c3 = ref_3.c3)
1121 ) AS subq_1
1122 WHERE ref_0."C 1" < 10 AND subq_1.c3 = '00001'
1123 ORDER BY ref_0."C 1";
1124
1125 -- Check with placeHolderVars
1126 explain (verbose, costs off)
1127 select sum(q.a), count(q.b) from ft4 left join (select 13, avg(ft1.c1), sum(ft2.c1) from ft1 right join ft2 on (ft1.c1 = ft2.c1)) q(a, b, c) on (ft4.c1 <= q.b);
1128 select sum(q.a), count(q.b) from ft4 left join (select 13, avg(ft1.c1), sum(ft2.c1) from ft1 right join ft2 on (ft1.c1 = ft2.c1)) q(a, b, c) on (ft4.c1 <= q.b);
1129
1130
1131 -- Not supported cases
1132 -- Grouping sets
1133 explain (verbose, costs off)
1134 select c2, sum(c1) from ft1 where c2 < 3 group by rollup(c2) order by 1 nulls last;
1135 select c2, sum(c1) from ft1 where c2 < 3 group by rollup(c2) order by 1 nulls last;
1136 explain (verbose, costs off)
1137 select c2, sum(c1) from ft1 where c2 < 3 group by cube(c2) order by 1 nulls last;
1138 select c2, sum(c1) from ft1 where c2 < 3 group by cube(c2) order by 1 nulls last;
1139 explain (verbose, costs off)
1140 select c2, c6, sum(c1) from ft1 where c2 < 3 group by grouping sets(c2, c6) order by 1 nulls last, 2 nulls last;
1141 select c2, c6, sum(c1) from ft1 where c2 < 3 group by grouping sets(c2, c6) order by 1 nulls last, 2 nulls last;
1142 explain (verbose, costs off)
1143 select c2, sum(c1), grouping(c2) from ft1 where c2 < 3 group by c2 order by 1 nulls last;
1144 select c2, sum(c1), grouping(c2) from ft1 where c2 < 3 group by c2 order by 1 nulls last;
1145
1146 -- DISTINCT itself is not pushed down, whereas underneath aggregate is pushed
1147 explain (verbose, costs off)
1148 select distinct sum(c1)/1000 s from ft2 where c2 < 6 group by c2 order by 1;
1149 select distinct sum(c1)/1000 s from ft2 where c2 < 6 group by c2 order by 1;
1150
1151 -- WindowAgg
1152 explain (verbose, costs off)
1153 select c2, sum(c2), count(c2) over (partition by c2%2) from ft2 where c2 < 10 group by c2 order by 1;
1154 select c2, sum(c2), count(c2) over (partition by c2%2) from ft2 where c2 < 10 group by c2 order by 1;
1155 explain (verbose, costs off)
1156 select c2, array_agg(c2) over (partition by c2%2 order by c2 desc) from ft1 where c2 < 10 group by c2 order by 1;
1157 select c2, array_agg(c2) over (partition by c2%2 order by c2 desc) from ft1 where c2 < 10 group by c2 order by 1;
1158 explain (verbose, costs off)
1159 select c2, array_agg(c2) over (partition by c2%2 order by c2 range between current row and unbounded following) from ft1 where c2 < 10 group by c2 order by 1;
1160 select c2, array_agg(c2) over (partition by c2%2 order by c2 range between current row and unbounded following) from ft1 where c2 < 10 group by c2 order by 1;
1161
1162
1163 -- ===================================================================
1164 -- parameterized queries
1165 -- ===================================================================
1166 -- simple join
1167 PREPARE st1(int, int) AS SELECT t1.c3, t2.c3 FROM ft1 t1, ft2 t2 WHERE t1.c1 = $1 AND t2.c1 = $2;
1168 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st1(1, 2);
1169 EXECUTE st1(1, 1);
1170 EXECUTE st1(101, 101);
1171 SET enable_hashjoin TO off;
1172 SET enable_sort TO off;
1173 -- subquery using stable function (can't be sent to remote)
1174 PREPARE st2(int) AS SELECT * FROM ft1 t1 WHERE t1.c1 < $2 AND t1.c3 IN (SELECT c3 FROM ft2 t2 WHERE c1 > $1 AND date(c4) = '1970-01-17'::date) ORDER BY c1;
1175 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st2(10, 20);
1176 EXECUTE st2(10, 20);
1177 EXECUTE st2(101, 121);
1178 RESET enable_hashjoin;
1179 RESET enable_sort;
1180 -- subquery using immutable function (can be sent to remote)
1181 PREPARE st3(int) AS SELECT * FROM ft1 t1 WHERE t1.c1 < $2 AND t1.c3 IN (SELECT c3 FROM ft2 t2 WHERE c1 > $1 AND date(c5) = '1970-01-17'::date) ORDER BY c1;
1182 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st3(10, 20);
1183 EXECUTE st3(10, 20);
1184 EXECUTE st3(20, 30);
1185 -- custom plan should be chosen initially
1186 PREPARE st4(int) AS SELECT * FROM ft1 t1 WHERE t1.c1 = $1;
1187 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st4(1);
1188 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st4(1);
1189 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st4(1);
1190 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st4(1);
1191 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st4(1);
1192 -- once we try it enough times, should switch to generic plan
1193 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st4(1);
1194 -- value of $1 should not be sent to remote
1195 PREPARE st5(user_enum,int) AS SELECT * FROM ft1 t1 WHERE c8 = $1 and c1 = $2;
1196 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st5('foo', 1);
1197 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st5('foo', 1);
1198 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st5('foo', 1);
1199 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st5('foo', 1);
1200 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st5('foo', 1);
1201 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st5('foo', 1);
1202 EXECUTE st5('foo', 1);
1203
1204 -- altering FDW options requires replanning
1205 PREPARE st6 AS SELECT * FROM ft1 t1 WHERE t1.c1 = t1.c2;
1206 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st6;
1207 PREPARE st7 AS INSERT INTO ft1 (c1,c2,c3) VALUES (1001,101,'foo');
1208 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st7;
1209 ALTER TABLE "S 1"."T 1" RENAME TO "T 0";
1210 ALTER FOREIGN TABLE ft1 OPTIONS (SET table_name 'T 0');
1211 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st6;
1212 EXECUTE st6;
1213 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st7;
1214 ALTER TABLE "S 1"."T 0" RENAME TO "T 1";
1215 ALTER FOREIGN TABLE ft1 OPTIONS (SET table_name 'T 1');
1216
1217 PREPARE st8 AS SELECT count(c3) FROM ft1 t1 WHERE t1.c1 === t1.c2;
1218 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st8;
1219 ALTER SERVER loopback OPTIONS (DROP extensions);
1220 EXPLAIN (VERBOSE, COSTS OFF) EXECUTE st8;
1221 EXECUTE st8;
1222 ALTER SERVER loopback OPTIONS (ADD extensions 'postgres_fdw');
1223
1224 -- cleanup
1225 DEALLOCATE st1;
1226 DEALLOCATE st2;
1227 DEALLOCATE st3;
1228 DEALLOCATE st4;
1229 DEALLOCATE st5;
1230 DEALLOCATE st6;
1231 DEALLOCATE st7;
1232 DEALLOCATE st8;
1233
1234 -- System columns, except ctid and oid, should not be sent to remote
1235 EXPLAIN (VERBOSE, COSTS OFF)
1236 SELECT * FROM ft1 t1 WHERE t1.tableoid = 'pg_class'::regclass LIMIT 1;
1237 SELECT * FROM ft1 t1 WHERE t1.tableoid = 'ft1'::regclass LIMIT 1;
1238 EXPLAIN (VERBOSE, COSTS OFF)
1239 SELECT tableoid::regclass, * FROM ft1 t1 LIMIT 1;
1240 SELECT tableoid::regclass, * FROM ft1 t1 LIMIT 1;
1241 EXPLAIN (VERBOSE, COSTS OFF)
1242 SELECT * FROM ft1 t1 WHERE t1.ctid = '(0,2)';
1243 SELECT * FROM ft1 t1 WHERE t1.ctid = '(0,2)';
1244 EXPLAIN (VERBOSE, COSTS OFF)
1245 SELECT ctid, * FROM ft1 t1 LIMIT 1;
1246 SELECT ctid, * FROM ft1 t1 LIMIT 1;
1247
1248 -- ===================================================================
1249 -- used in PL/pgSQL function
1250 -- ===================================================================
1251 CREATE OR REPLACE FUNCTION f_test(p_c1 int) RETURNS int AS $$
1252 DECLARE
1253 v_c1 int;
1254 BEGIN
1255 SELECT c1 INTO v_c1 FROM ft1 WHERE c1 = p_c1 LIMIT 1;
1256 PERFORM c1 FROM ft1 WHERE c1 = p_c1 AND p_c1 = v_c1 LIMIT 1;
1257 RETURN v_c1;
1258 END;
1259 $$ LANGUAGE plpgsql;
1260 SELECT f_test(100);
1261 DROP FUNCTION f_test(int);
1262
1263 -- ===================================================================
1264 -- REINDEX
1265 -- ===================================================================
1266 -- remote table is not created here
1267 CREATE FOREIGN TABLE reindex_foreign (c1 int, c2 int)
1268 SERVER loopback2 OPTIONS (table_name 'reindex_local');
1269 REINDEX TABLE reindex_foreign; -- error
1270 REINDEX TABLE CONCURRENTLY reindex_foreign; -- error
1271 DROP FOREIGN TABLE reindex_foreign;
1272 -- partitions and foreign tables
1273 CREATE TABLE reind_fdw_parent (c1 int) PARTITION BY RANGE (c1);
1274 CREATE TABLE reind_fdw_0_10 PARTITION OF reind_fdw_parent
1275 FOR VALUES FROM (0) TO (10);
1276 CREATE FOREIGN TABLE reind_fdw_10_20 PARTITION OF reind_fdw_parent
1277 FOR VALUES FROM (10) TO (20)
1278 SERVER loopback OPTIONS (table_name 'reind_local_10_20');
1279 REINDEX TABLE reind_fdw_parent; -- ok
1280 REINDEX TABLE CONCURRENTLY reind_fdw_parent; -- ok
1281 DROP TABLE reind_fdw_parent;
1282
1283 -- ===================================================================
1284 -- conversion error
1285 -- ===================================================================
1286 ALTER FOREIGN TABLE ft1 ALTER COLUMN c8 TYPE int;
1287 SELECT * FROM ft1 ftx(x1,x2,x3,x4,x5,x6,x7,x8) WHERE x1 = 1; -- ERROR
1288 SELECT ftx.x1, ft2.c2, ftx.x8 FROM ft1 ftx(x1,x2,x3,x4,x5,x6,x7,x8), ft2
1289 WHERE ftx.x1 = ft2.c1 AND ftx.x1 = 1; -- ERROR
1290 SELECT ftx.x1, ft2.c2, ftx FROM ft1 ftx(x1,x2,x3,x4,x5,x6,x7,x8), ft2
1291 WHERE ftx.x1 = ft2.c1 AND ftx.x1 = 1; -- ERROR
1292 SELECT sum(c2), array_agg(c8) FROM ft1 GROUP BY c8; -- ERROR
1293 ANALYZE ft1; -- ERROR
1294 ALTER FOREIGN TABLE ft1 ALTER COLUMN c8 TYPE user_enum;
1295
1296 -- ===================================================================
1297 -- local type can be different from remote type in some cases,
1298 -- in particular if similarly-named operators do equivalent things
1299 -- ===================================================================
1300 ALTER FOREIGN TABLE ft1 ALTER COLUMN c8 TYPE text;
1301 EXPLAIN (VERBOSE, COSTS OFF)
1302 SELECT * FROM ft1 WHERE c8 = 'foo' LIMIT 1;
1303 SELECT * FROM ft1 WHERE c8 = 'foo' LIMIT 1;
1304 EXPLAIN (VERBOSE, COSTS OFF)
1305 SELECT * FROM ft1 WHERE 'foo' = c8 LIMIT 1;
1306 SELECT * FROM ft1 WHERE 'foo' = c8 LIMIT 1;
1307 -- we declared c8 to be text locally, but it's still the same type on
1308 -- the remote which will balk if we try to do anything incompatible
1309 -- with that remote type
1310 SELECT * FROM ft1 WHERE c8 LIKE 'foo' LIMIT 1; -- ERROR
1311 SELECT * FROM ft1 WHERE c8::text LIKE 'foo' LIMIT 1; -- ERROR; cast not pushed down
1312 ALTER FOREIGN TABLE ft1 ALTER COLUMN c8 TYPE user_enum;
1313
1314 -- ===================================================================
1315 -- subtransaction
1316 -- + local/remote error doesn't break cursor
1317 -- ===================================================================
1318 BEGIN;
1319 DECLARE c CURSOR FOR SELECT * FROM ft1 ORDER BY c1;
1320 FETCH c;
1321 SAVEPOINT s;
1322 ERROR OUT; -- ERROR
1323 ROLLBACK TO s;
1324 FETCH c;
1325 SAVEPOINT s;
1326 SELECT * FROM ft1 WHERE 1 / (c1 - 1) > 0; -- ERROR
1327 ROLLBACK TO s;
1328 FETCH c;
1329 SELECT * FROM ft1 ORDER BY c1 LIMIT 1;
1330 COMMIT;
1331
1332 -- ===================================================================
1333 -- test handling of collations
1334 -- ===================================================================
1335 create table loct3 (f1 text collate "C" unique, f2 text, f3 varchar(10) unique);
1336 create foreign table ft3 (f1 text collate "C", f2 text, f3 varchar(10))
1337 server loopback options (table_name 'loct3', use_remote_estimate 'true');
1338
1339 -- can be sent to remote
1340 explain (verbose, costs off) select * from ft3 where f1 = 'foo';
1341 explain (verbose, costs off) select * from ft3 where f1 COLLATE "C" = 'foo';
1342 explain (verbose, costs off) select * from ft3 where f2 = 'foo';
1343 explain (verbose, costs off) select * from ft3 where f3 = 'foo';
1344 explain (verbose, costs off) select * from ft3 f, loct3 l
1345 where f.f3 = l.f3 and l.f1 = 'foo';
1346 -- can't be sent to remote
1347 explain (verbose, costs off) select * from ft3 where f1 COLLATE "POSIX" = 'foo';
1348 explain (verbose, costs off) select * from ft3 where f1 = 'foo' COLLATE "C";
1349 explain (verbose, costs off) select * from ft3 where f2 COLLATE "C" = 'foo';
1350 explain (verbose, costs off) select * from ft3 where f2 = 'foo' COLLATE "C";
1351 explain (verbose, costs off) select * from ft3 f, loct3 l
1352 where f.f3 = l.f3 COLLATE "POSIX" and l.f1 = 'foo';
1353
1354 -- ===================================================================
1355 -- test SEMI-JOIN pushdown
1356 -- ===================================================================
1357 EXPLAIN (verbose, costs off)
1358 SELECT ft2.*, ft4.* FROM ft2 INNER JOIN ft4 ON ft2.c2 = ft4.c1
1359 WHERE ft2.c1 > 900
1360 AND EXISTS (SELECT 1 FROM ft5 WHERE ft4.c1 = ft5.c1)
1361 ORDER BY ft2.c1;
1362 SELECT ft2.*, ft4.* FROM ft2 INNER JOIN ft4 ON ft2.c2 = ft4.c1
1363 WHERE ft2.c1 > 900
1364 AND EXISTS (SELECT 1 FROM ft5 WHERE ft4.c1 = ft5.c1)
1365 ORDER BY ft2.c1;
1366
1367 -- The same query, different join order
1368 EXPLAIN (verbose, costs off)
1369 SELECT ft2.*, ft4.* FROM ft2 INNER JOIN
1370 (SELECT * FROM ft4 WHERE
1371 EXISTS (SELECT 1 FROM ft5 WHERE ft4.c1 = ft5.c1)) ft4
1372 ON ft2.c2 = ft4.c1
1373 WHERE ft2.c1 > 900
1374 ORDER BY ft2.c1;
1375 SELECT ft2.*, ft4.* FROM ft2 INNER JOIN
1376 (SELECT * FROM ft4 WHERE
1377 EXISTS (SELECT 1 FROM ft5 WHERE ft4.c1 = ft5.c1)) ft4
1378 ON ft2.c2 = ft4.c1
1379 WHERE ft2.c1 > 900
1380 ORDER BY ft2.c1;
1381
1382 -- Left join
1383 EXPLAIN (verbose, costs off)
1384 SELECT ft2.*, ft4.* FROM ft2 LEFT JOIN
1385 (SELECT * FROM ft4 WHERE
1386 EXISTS (SELECT 1 FROM ft5 WHERE ft4.c1 = ft5.c1)) ft4
1387 ON ft2.c2 = ft4.c1
1388 WHERE ft2.c1 > 900
1389 ORDER BY ft2.c1 LIMIT 10;
1390 SELECT ft2.*, ft4.* FROM ft2 LEFT JOIN
1391 (SELECT * FROM ft4 WHERE
1392 EXISTS (SELECT 1 FROM ft5 WHERE ft4.c1 = ft5.c1)) ft4
1393 ON ft2.c2 = ft4.c1
1394 WHERE ft2.c1 > 900
1395 ORDER BY ft2.c1 LIMIT 10;
1396
1397 -- Several semi-joins per upper level join
1398 EXPLAIN (verbose, costs off)
1399 SELECT ft2.*, ft4.* FROM ft2 INNER JOIN
1400 (SELECT * FROM ft4 WHERE
1401 EXISTS (SELECT 1 FROM ft5 WHERE ft4.c1 = ft5.c1)) ft4
1402 ON ft2.c2 = ft4.c1
1403 INNER JOIN (SELECT * FROM ft5 WHERE
1404 EXISTS (SELECT 1 FROM ft4 WHERE ft4.c1 = ft5.c1)) ft5
1405 ON ft2.c2 <= ft5.c1
1406 WHERE ft2.c1 > 900
1407 ORDER BY ft2.c1 LIMIT 10;
1408 SELECT ft2.*, ft4.* FROM ft2 INNER JOIN
1409 (SELECT * FROM ft4 WHERE
1410 EXISTS (SELECT 1 FROM ft5 WHERE ft4.c1 = ft5.c1)) ft4
1411 ON ft2.c2 = ft4.c1
1412 INNER JOIN (SELECT * FROM ft5 WHERE
1413 EXISTS (SELECT 1 FROM ft4 WHERE ft4.c1 = ft5.c1)) ft5
1414 ON ft2.c2 <= ft5.c1
1415 WHERE ft2.c1 > 900
1416 ORDER BY ft2.c1 LIMIT 10;
1417
1418 -- Semi-join below Semi-join
1419 EXPLAIN (verbose, costs off)
1420 SELECT ft2.* FROM ft2 WHERE
1421 c1 = ANY (
1422 SELECT c1 FROM ft2 WHERE
1423 EXISTS (SELECT 1 FROM ft4 WHERE ft4.c2 = ft2.c2))
1424 AND ft2.c1 > 900
1425 ORDER BY ft2.c1 LIMIT 10;
1426 SELECT ft2.* FROM ft2 WHERE
1427 c1 = ANY (
1428 SELECT c1 FROM ft2 WHERE
1429 EXISTS (SELECT 1 FROM ft4 WHERE ft4.c2 = ft2.c2))
1430 AND ft2.c1 > 900
1431 ORDER BY ft2.c1 LIMIT 10;
1432
1433 -- Upper level relations shouldn't refer EXISTS() subqueries
1434 EXPLAIN (verbose, costs off)
1435 SELECT * FROM ft2 ftupper WHERE
1436 EXISTS (
1437 SELECT c1 FROM ft2 WHERE
1438 EXISTS (SELECT 1 FROM ft4 WHERE ft4.c2 = ft2.c2) AND c1 = ftupper.c1 )
1439 AND ftupper.c1 > 900
1440 ORDER BY ftupper.c1 LIMIT 10;
1441 SELECT * FROM ft2 ftupper WHERE
1442 EXISTS (
1443 SELECT c1 FROM ft2 WHERE
1444 EXISTS (SELECT 1 FROM ft4 WHERE ft4.c2 = ft2.c2) AND c1 = ftupper.c1 )
1445 AND ftupper.c1 > 900
1446 ORDER BY ftupper.c1 LIMIT 10;
1447
1448 -- EXISTS should be propagated to the highest upper inner join
1449 EXPLAIN (verbose, costs off)
1450 SELECT ft2.*, ft4.* FROM ft2 INNER JOIN
1451 (SELECT * FROM ft4 WHERE EXISTS (
1452 SELECT 1 FROM ft2 WHERE ft2.c2 = ft4.c2)) ft4
1453 ON ft2.c2 = ft4.c1
1454 INNER JOIN
1455 (SELECT * FROM ft2 WHERE EXISTS (
1456 SELECT 1 FROM ft4 WHERE ft2.c2 = ft4.c2)) ft21
1457 ON ft2.c2 = ft21.c2
1458 WHERE ft2.c1 > 900
1459 ORDER BY ft2.c1 LIMIT 10;
1460 SELECT ft2.*, ft4.* FROM ft2 INNER JOIN
1461 (SELECT * FROM ft4 WHERE EXISTS (
1462 SELECT 1 FROM ft2 WHERE ft2.c2 = ft4.c2)) ft4
1463 ON ft2.c2 = ft4.c1
1464 INNER JOIN
1465 (SELECT * FROM ft2 WHERE EXISTS (
1466 SELECT 1 FROM ft4 WHERE ft2.c2 = ft4.c2)) ft21
1467 ON ft2.c2 = ft21.c2
1468 WHERE ft2.c1 > 900
1469 ORDER BY ft2.c1 LIMIT 10;
1470
1471 -- Semi-join conditions shouldn't pop up as left/right join clauses.
1472 SET enable_material TO off;
1473 EXPLAIN (verbose, costs off)
1474 SELECT x1.c1 FROM
1475 (SELECT * FROM ft2 WHERE EXISTS (SELECT 1 FROM ft4 WHERE ft4.c1 = ft2.c1 AND ft2.c2 < 10)) x1
1476 RIGHT JOIN
1477 (SELECT * FROM ft2 WHERE EXISTS (SELECT 1 FROM ft4 WHERE ft4.c1 = ft2.c1 AND ft2.c2 < 10)) x2
1478 ON (x1.c1 = x2.c1)
1479 ORDER BY x1.c1 LIMIT 10;
1480 SELECT x1.c1 FROM
1481 (SELECT * FROM ft2 WHERE EXISTS (SELECT 1 FROM ft4 WHERE ft4.c1 = ft2.c1 AND ft2.c2 < 10)) x1
1482 RIGHT JOIN
1483 (SELECT * FROM ft2 WHERE EXISTS (SELECT 1 FROM ft4 WHERE ft4.c1 = ft2.c1 AND ft2.c2 < 10)) x2
1484 ON (x1.c1 = x2.c1)
1485 ORDER BY x1.c1 LIMIT 10;
1486 RESET enable_material;
1487
1488 -- Can't push down semi-join with inner rel vars in targetlist
1489 EXPLAIN (verbose, costs off)
1490 SELECT ft1.c1 FROM ft1 JOIN ft2 on ft1.c1 = ft2.c1 WHERE
1491 ft1.c1 IN (
1492 SELECT ft2.c1 FROM ft2 JOIN ft4 ON ft2.c1 = ft4.c1)
1493 ORDER BY ft1.c1 LIMIT 5;
1494
1495 -- ===================================================================
1496 -- test writable foreign table stuff
1497 -- ===================================================================
1498 EXPLAIN (verbose, costs off)
1499 INSERT INTO ft2 (c1,c2,c3) SELECT c1+1000,c2+100, c3 || c3 FROM ft2 LIMIT 20;
1500 INSERT INTO ft2 (c1,c2,c3) SELECT c1+1000,c2+100, c3 || c3 FROM ft2 LIMIT 20;
1501 INSERT INTO ft2 (c1,c2,c3)
1502 VALUES (1101,201,'aaa'), (1102,202,'bbb'), (1103,203,'ccc') RETURNING old, new, old.*, new.*;
1503 INSERT INTO ft2 (c1,c2,c3) VALUES (1104,204,'ddd'), (1105,205,'eee');
1504 EXPLAIN (verbose, costs off)
1505 UPDATE ft2 SET c2 = c2 + 300, c3 = c3 || '_update3' WHERE c1 % 10 = 3; -- can be pushed down
1506 UPDATE ft2 SET c2 = c2 + 300, c3 = c3 || '_update3' WHERE c1 % 10 = 3;
1507 EXPLAIN (verbose, costs off)
1508 UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7' WHERE c1 % 10 = 7 RETURNING *; -- can be pushed down
1509 UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7' WHERE c1 % 10 = 7 RETURNING *;
1510 BEGIN;
1511 EXPLAIN (verbose, costs off)
1512 UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7b' WHERE c1 % 10 = 7 AND c1 < 40
1513 RETURNING old.*, new.*; -- can't be pushed down
1514 UPDATE ft2 SET c2 = c2 + 400, c3 = c3 || '_update7b' WHERE c1 % 10 = 7 AND c1 < 40
1515 RETURNING old.*, new.*;
1516 ROLLBACK;
1517 EXPLAIN (verbose, costs off)
1518 UPDATE ft2 SET c2 = ft2.c2 + 500, c3 = ft2.c3 || '_update9', c7 = DEFAULT
1519 FROM ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 9; -- can be pushed down
1520 UPDATE ft2 SET c2 = ft2.c2 + 500, c3 = ft2.c3 || '_update9', c7 = DEFAULT
1521 FROM ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 9;
1522 EXPLAIN (verbose, costs off)
1523 DELETE FROM ft2 WHERE c1 % 10 = 5 RETURNING c1, c4; -- can be pushed down
1524 DELETE FROM ft2 WHERE c1 % 10 = 5 RETURNING c1, c4;
1525 BEGIN;
1526 EXPLAIN (verbose, costs off)
1527 DELETE FROM ft2 WHERE c1 % 10 = 6 AND c1 < 40 RETURNING old.c1, c4; -- can't be pushed down
1528 DELETE FROM ft2 WHERE c1 % 10 = 6 AND c1 < 40 RETURNING old.c1, c4;
1529 ROLLBACK;
1530 EXPLAIN (verbose, costs off)
1531 DELETE FROM ft2 USING ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 2; -- can be pushed down
1532 DELETE FROM ft2 USING ft1 WHERE ft1.c1 = ft2.c2 AND ft1.c1 % 10 = 2;
1533 SELECT c1,c2,c3,c4 FROM ft2 ORDER BY c1;
1534 EXPLAIN (verbose, costs off)
1535 INSERT INTO ft2 (c1,c2,c3) VALUES (1200,999,'foo') RETURNING tableoid::regclass;
1536 INSERT INTO ft2 (c1,c2,c3) VALUES (1200,999,'foo') RETURNING tableoid::regclass;
1537 EXPLAIN (verbose, costs off)
1538 UPDATE ft2 SET c3 = 'bar' WHERE c1 = 1200 RETURNING tableoid::regclass; -- can be pushed down
1539 UPDATE ft2 SET c3 = 'bar' WHERE c1 = 1200 RETURNING tableoid::regclass;
1540 EXPLAIN (verbose, costs off)
1541 DELETE FROM ft2 WHERE c1 = 1200 RETURNING tableoid::regclass; -- can be pushed down
1542 DELETE FROM ft2 WHERE c1 = 1200 RETURNING tableoid::regclass;
1543
1544 -- Test UPDATE/DELETE with RETURNING on a three-table join
1545 INSERT INTO ft2 (c1,c2,c3)
1546 SELECT id, id - 1200, to_char(id, 'FM00000') FROM generate_series(1201, 1300) id;
1547 EXPLAIN (verbose, costs off)
1548 UPDATE ft2 SET c3 = 'foo'
1549 FROM ft4 INNER JOIN ft5 ON (ft4.c1 = ft5.c1)
1550 WHERE ft2.c1 > 1200 AND ft2.c2 = ft4.c1
1551 RETURNING ft2, ft2.*, ft4, ft4.*; -- can be pushed down
1552 UPDATE ft2 SET c3 = 'foo'
1553 FROM ft4 INNER JOIN ft5 ON (ft4.c1 = ft5.c1)
1554 WHERE ft2.c1 > 1200 AND ft2.c2 = ft4.c1
1555 RETURNING ft2, ft2.*, ft4, ft4.*;
1556 BEGIN;
1557 EXPLAIN (verbose, costs off)
1558 UPDATE ft2 SET c3 = 'bar'
1559 FROM ft4 INNER JOIN ft5 ON (ft4.c1 = ft5.c1)
1560 WHERE ft2.c1 > 1200 AND ft2.c2 = ft4.c1
1561 RETURNING old, new, ft2, ft2.*, ft4, ft4.*; -- can't be pushed down
1562 UPDATE ft2 SET c3 = 'bar'
1563 FROM ft4 INNER JOIN ft5 ON (ft4.c1 = ft5.c1)
1564 WHERE ft2.c1 > 1200 AND ft2.c2 = ft4.c1
1565 RETURNING old, new, ft2, ft2.*, ft4, ft4.*;
1566 ROLLBACK;
1567 EXPLAIN (verbose, costs off)
1568 DELETE FROM ft2
1569 USING ft4 LEFT JOIN ft5 ON (ft4.c1 = ft5.c1)
1570 WHERE ft2.c1 > 1200 AND ft2.c1 % 10 = 0 AND ft2.c2 = ft4.c1
1571 RETURNING 100; -- can be pushed down
1572 DELETE FROM ft2
1573 USING ft4 LEFT JOIN ft5 ON (ft4.c1 = ft5.c1)
1574 WHERE ft2.c1 > 1200 AND ft2.c1 % 10 = 0 AND ft2.c2 = ft4.c1
1575 RETURNING 100;
1576 DELETE FROM ft2 WHERE ft2.c1 > 1200;
1577
1578 -- Test UPDATE with a MULTIEXPR sub-select
1579 -- (maybe someday this'll be remotely executable, but not today)
1580 EXPLAIN (verbose, costs off)
1581 UPDATE ft2 AS target SET (c2, c7) = (
1582 SELECT c2 * 10, c7
1583 FROM ft2 AS src
1584 WHERE target.c1 = src.c1
1585 ) WHERE c1 > 1100;
1586 UPDATE ft2 AS target SET (c2, c7) = (
1587 SELECT c2 * 10, c7
1588 FROM ft2 AS src
1589 WHERE target.c1 = src.c1
1590 ) WHERE c1 > 1100;
1591
1592 UPDATE ft2 AS target SET (c2) = (
1593 SELECT c2 / 10
1594 FROM ft2 AS src
1595 WHERE target.c1 = src.c1
1596 ) WHERE c1 > 1100;
1597
1598 -- Test UPDATE involving a join that can be pushed down,
1599 -- but a SET clause that can't be
1600 EXPLAIN (VERBOSE, COSTS OFF)
1601 UPDATE ft2 d SET c2 = CASE WHEN random() >= 0 THEN d.c2 ELSE 0 END
1602 FROM ft2 AS t WHERE d.c1 = t.c1 AND d.c1 > 1000;
1603 UPDATE ft2 d SET c2 = CASE WHEN random() >= 0 THEN d.c2 ELSE 0 END
1604 FROM ft2 AS t WHERE d.c1 = t.c1 AND d.c1 > 1000;
1605
1606 -- Test UPDATE/DELETE with WHERE or JOIN/ON conditions containing
1607 -- user-defined operators/functions
1608 ALTER SERVER loopback OPTIONS (DROP extensions);
1609 INSERT INTO ft2 (c1,c2,c3)
1610 SELECT id, id % 10, to_char(id, 'FM00000') FROM generate_series(2001, 2010) id;
1611
1612 -- this will do a remote seqscan, causing unstable result order, so sort
1613 EXPLAIN (verbose, costs off)
1614 WITH cte AS (
1615 UPDATE ft2 SET c3 = 'bar' WHERE postgres_fdw_abs(c1) > 2000 RETURNING *
1616 ) SELECT * FROM cte ORDER BY c1; -- can't be pushed down
1617 WITH cte AS (
1618 UPDATE ft2 SET c3 = 'bar' WHERE postgres_fdw_abs(c1) > 2000 RETURNING *
1619 ) SELECT * FROM cte ORDER BY c1;
1620
1621 EXPLAIN (verbose, costs off)
1622 UPDATE ft2 SET c3 = 'baz'
1623 FROM ft4 INNER JOIN ft5 ON (ft4.c1 = ft5.c1)
1624 WHERE ft2.c1 > 2000 AND ft2.c2 === ft4.c1
1625 RETURNING ft2.*, ft4.*, ft5.*; -- can't be pushed down
1626 UPDATE ft2 SET c3 = 'baz'
1627 FROM ft4 INNER JOIN ft5 ON (ft4.c1 = ft5.c1)
1628 WHERE ft2.c1 > 2000 AND ft2.c2 === ft4.c1
1629 RETURNING ft2.*, ft4.*, ft5.*;
1630 EXPLAIN (verbose, costs off)
1631 DELETE FROM ft2
1632 USING ft4 INNER JOIN ft5 ON (ft4.c1 === ft5.c1)
1633 WHERE ft2.c1 > 2000 AND ft2.c2 = ft4.c1
1634 RETURNING ft2.c1, ft2.c2, ft2.c3; -- can't be pushed down
1635 DELETE FROM ft2
1636 USING ft4 INNER JOIN ft5 ON (ft4.c1 === ft5.c1)
1637 WHERE ft2.c1 > 2000 AND ft2.c2 = ft4.c1
1638 RETURNING ft2.c1, ft2.c2, ft2.c3;
1639 DELETE FROM ft2 WHERE ft2.c1 > 2000;
1640 ALTER SERVER loopback OPTIONS (ADD extensions 'postgres_fdw');
1641
1642 -- Test that trigger on remote table works as expected
1643 CREATE OR REPLACE FUNCTION "S 1".F_BRTRIG() RETURNS trigger AS $$
1644 BEGIN
1645 NEW.c3 = NEW.c3 || '_trig_update';
1646 RETURN NEW;
1647 END;
1648 $$ LANGUAGE plpgsql;
1649 CREATE TRIGGER t1_br_insert BEFORE INSERT OR UPDATE
1650 ON "S 1"."T 1" FOR EACH ROW EXECUTE PROCEDURE "S 1".F_BRTRIG();
1651
1652 INSERT INTO ft2 (c1,c2,c3) VALUES (1208, 818, 'fff') RETURNING *;
1653 INSERT INTO ft2 (c1,c2,c3,c6) VALUES (1218, 818, 'ggg', '(--;') RETURNING *;
1654 UPDATE ft2 SET c2 = c2 + 600 WHERE c1 % 10 = 8 AND c1 < 1200 RETURNING *;
1655
1656 -- Test errors thrown on remote side during update
1657 ALTER TABLE "S 1"."T 1" ADD CONSTRAINT c2positive CHECK (c2 >= 0);
1658
1659 INSERT INTO ft1(c1, c2) VALUES(11, 12); -- duplicate key
1660 INSERT INTO ft1(c1, c2) VALUES(11, 12) ON CONFLICT DO NOTHING; -- works
1661 INSERT INTO ft1(c1, c2) VALUES(11, 12) ON CONFLICT (c1, c2) DO NOTHING; -- unsupported
1662 INSERT INTO ft1(c1, c2) VALUES(11, 12) ON CONFLICT (c1, c2) DO UPDATE SET c3 = 'ffg'; -- unsupported
1663 INSERT INTO ft1(c1, c2) VALUES(1111, -2); -- c2positive
1664 UPDATE ft1 SET c2 = -c2 WHERE c1 = 1; -- c2positive
1665
1666 -- Test savepoint/rollback behavior
1667 select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
1668 select c2, count(*) from "S 1"."T 1" where c2 < 500 group by 1 order by 1;
1669 begin;
1670 update ft2 set c2 = 42 where c2 = 0;
1671 select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
1672 savepoint s1;
1673 update ft2 set c2 = 44 where c2 = 4;
1674 select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
1675 release savepoint s1;
1676 select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
1677 savepoint s2;
1678 update ft2 set c2 = 46 where c2 = 6;
1679 select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
1680 rollback to savepoint s2;
1681 select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
1682 release savepoint s2;
1683 select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
1684 savepoint s3;
1685 update ft2 set c2 = -2 where c2 = 42 and c1 = 10; -- fail on remote side
1686 rollback to savepoint s3;
1687 select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
1688 release savepoint s3;
1689 select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
1690 -- none of the above is committed yet remotely
1691 select c2, count(*) from "S 1"."T 1" where c2 < 500 group by 1 order by 1;
1692 commit;
1693 select c2, count(*) from ft2 where c2 < 500 group by 1 order by 1;
1694 select c2, count(*) from "S 1"."T 1" where c2 < 500 group by 1 order by 1;
1695
1696 VACUUM ANALYZE "S 1"."T 1";
1697
1698 -- Above DMLs add data with c6 as NULL in ft1, so test ORDER BY NULLS LAST and NULLs
1699 -- FIRST behavior here.
1700 -- ORDER BY DESC NULLS LAST options
1701 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 ORDER BY c6 DESC NULLS LAST, c1 OFFSET 795 LIMIT 10;
1702 SELECT * FROM ft1 ORDER BY c6 DESC NULLS LAST, c1 OFFSET 795 LIMIT 10;
1703 -- ORDER BY DESC NULLS FIRST options
1704 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 ORDER BY c6 DESC NULLS FIRST, c1 OFFSET 15 LIMIT 10;
1705 SELECT * FROM ft1 ORDER BY c6 DESC NULLS FIRST, c1 OFFSET 15 LIMIT 10;
1706 -- ORDER BY ASC NULLS FIRST options
1707 EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM ft1 ORDER BY c6 ASC NULLS FIRST, c1 OFFSET 15 LIMIT 10;
1708 SELECT * FROM ft1 ORDER BY c6 ASC NULLS FIRST, c1 OFFSET 15 LIMIT 10;
1709
1710 -- Test ReScan code path that recreates the cursor even when no parameters
1711 -- change (bug #17889)
1712 CREATE TABLE loct1 (c1 int);
1713 CREATE TABLE loct2 (c1 int, c2 text);
1714 INSERT INTO loct1 VALUES (1001);
1715 INSERT INTO loct1 VALUES (1002);
1716 INSERT INTO loct2 SELECT id, to_char(id, 'FM0000') FROM generate_series(1, 1000) id;
1717 INSERT INTO loct2 VALUES (1001, 'foo');
1718 INSERT INTO loct2 VALUES (1002, 'bar');
1719 CREATE FOREIGN TABLE remt2 (c1 int, c2 text) SERVER loopback OPTIONS (table_name 'loct2');
1720 ANALYZE loct1;
1721 ANALYZE remt2;
1722 SET enable_mergejoin TO false;
1723 SET enable_hashjoin TO false;
1724 SET enable_material TO false;
1725 EXPLAIN (VERBOSE, COSTS OFF)
1726 UPDATE remt2 SET c2 = remt2.c2 || remt2.c2 FROM loct1 WHERE loct1.c1 = remt2.c1 RETURNING remt2.*;
1727 UPDATE remt2 SET c2 = remt2.c2 || remt2.c2 FROM loct1 WHERE loct1.c1 = remt2.c1 RETURNING remt2.*;
1728 RESET enable_mergejoin;
1729 RESET enable_hashjoin;
1730 RESET enable_material;
1731 DROP FOREIGN TABLE remt2;
1732 DROP TABLE loct1;
1733 DROP TABLE loct2;
1734
1735 -- Test that direct modify and foreign modify work with runtime pruning of
1736 -- result relations (bug #19484)
1737 create table fdw_part_update (a int not null, b int) partition by list (a);
1738 create table fdw_part_update_p1 partition of fdw_part_update for values in (1);
1739 create table fdw_part_update_remote (a int not null, b int);
1740 create foreign table fdw_part_update_p2 partition of fdw_part_update
1741 for values in (2)
1742 server loopback options (table_name 'fdw_part_update_remote');
1743 insert into fdw_part_update_p1 values (1, 10);
1744 insert into fdw_part_update_remote values (2, 20);
1745 set plan_cache_mode = force_generic_plan;
1746
1747 -- Check DirectModify case
1748 prepare fdw_part_upd(int) as
1749 update fdw_part_update set b = b + 1 where a = $1
1750 returning tableoid::regclass, a, b;
1751 explain (verbose, costs off)
1752 execute fdw_part_upd(2);
1753 execute fdw_part_upd(2);
1754 deallocate fdw_part_upd;
1755
1756 -- Check ForeignModify case
1757 prepare fdw_part_upd2(int) as
1758 update fdw_part_update set b = b + random()::int * 0 + 1 where a = $1
1759 returning tableoid::regclass, a, b;
1760 explain (verbose, costs off)
1761 execute fdw_part_upd2(2);
1762 execute fdw_part_upd2(2);
1763 deallocate fdw_part_upd2;
1764
1765 reset plan_cache_mode;
1766 drop table fdw_part_update;
1767 drop table fdw_part_update_remote;
1768
1769 -- ===================================================================
1770 -- test check constraints
1771 -- ===================================================================
1772
1773 -- Consistent check constraints provide consistent results
1774 ALTER FOREIGN TABLE ft1 ADD CONSTRAINT ft1_c2positive CHECK (c2 >= 0);
1775 EXPLAIN (VERBOSE, COSTS OFF) SELECT count(*) FROM ft1 WHERE c2 < 0;
1776 SELECT count(*) FROM ft1 WHERE c2 < 0;
1777 SET constraint_exclusion = 'on';
1778 EXPLAIN (VERBOSE, COSTS OFF) SELECT count(*) FROM ft1 WHERE c2 < 0;
1779 SELECT count(*) FROM ft1 WHERE c2 < 0;
1780 RESET constraint_exclusion;
1781 -- check constraint is enforced on the remote side, not locally
1782 INSERT INTO ft1(c1, c2) VALUES(1111, -2); -- c2positive
1783 UPDATE ft1 SET c2 = -c2 WHERE c1 = 1; -- c2positive
1784 ALTER FOREIGN TABLE ft1 DROP CONSTRAINT ft1_c2positive;
1785
1786 -- But inconsistent check constraints provide inconsistent results
1787 ALTER FOREIGN TABLE ft1 ADD CONSTRAINT ft1_c2negative CHECK (c2 < 0);
1788 EXPLAIN (VERBOSE, COSTS OFF) SELECT count(*) FROM ft1 WHERE c2 >= 0;
1789 SELECT count(*) FROM ft1 WHERE c2 >= 0;
1790 SET constraint_exclusion = 'on';
1791 EXPLAIN (VERBOSE, COSTS OFF) SELECT count(*) FROM ft1 WHERE c2 >= 0;
1792 SELECT count(*) FROM ft1 WHERE c2 >= 0;
1793 RESET constraint_exclusion;
1794 -- local check constraint is not actually enforced
1795 INSERT INTO ft1(c1, c2) VALUES(1111, 2);
1796 UPDATE ft1 SET c2 = c2 + 1 WHERE c1 = 1;
1797 ALTER FOREIGN TABLE ft1 DROP CONSTRAINT ft1_c2negative;
1798
1799 -- ===================================================================
1800 -- test WITH CHECK OPTION constraints
1801 -- ===================================================================
1802
1803 CREATE FUNCTION row_before_insupd_trigfunc() RETURNS trigger AS $$BEGIN NEW.a := NEW.a + 10; RETURN NEW; END$$ LANGUAGE plpgsql;
1804
1805 CREATE TABLE base_tbl (a int, b int);
1806 ALTER TABLE base_tbl SET (autovacuum_enabled = 'false');
1807 CREATE TRIGGER row_before_insupd_trigger BEFORE INSERT OR UPDATE ON base_tbl FOR EACH ROW EXECUTE PROCEDURE row_before_insupd_trigfunc();
1808 CREATE FOREIGN TABLE foreign_tbl (a int, b int)
1809 SERVER loopback OPTIONS (table_name 'base_tbl');
1810 CREATE VIEW rw_view AS SELECT * FROM foreign_tbl
1811 WHERE a < b WITH CHECK OPTION;
1812 \d+ rw_view
1813
1814 EXPLAIN (VERBOSE, COSTS OFF)
1815 INSERT INTO rw_view VALUES (0, 5);
1816 INSERT INTO rw_view VALUES (0, 5); -- should fail
1817 EXPLAIN (VERBOSE, COSTS OFF)
1818 INSERT INTO rw_view VALUES (0, 15);
1819 INSERT INTO rw_view VALUES (0, 15); -- ok
1820 SELECT * FROM foreign_tbl;
1821
1822 EXPLAIN (VERBOSE, COSTS OFF)
1823 UPDATE rw_view SET b = b + 5;
1824 UPDATE rw_view SET b = b + 5; -- should fail
1825 EXPLAIN (VERBOSE, COSTS OFF)
1826 UPDATE rw_view SET b = b + 15;
1827 UPDATE rw_view SET b = b + 15; -- ok
1828 SELECT * FROM foreign_tbl;
1829
1830 -- We don't allow batch insert when there are any WCO constraints
1831 ALTER SERVER loopback OPTIONS (ADD batch_size '10');
1832 EXPLAIN (VERBOSE, COSTS OFF)
1833 INSERT INTO rw_view VALUES (0, 15), (0, 5);
1834 INSERT INTO rw_view VALUES (0, 15), (0, 5); -- should fail
1835 SELECT * FROM foreign_tbl;
1836 ALTER SERVER loopback OPTIONS (DROP batch_size);
1837
1838 DROP FOREIGN TABLE foreign_tbl CASCADE;
1839 DROP TRIGGER row_before_insupd_trigger ON base_tbl;
1840 DROP TABLE base_tbl;
1841
1842 -- test WCO for partitions
1843
1844 CREATE TABLE child_tbl (a int, b int);
1845 ALTER TABLE child_tbl SET (autovacuum_enabled = 'false');
1846 CREATE TRIGGER row_before_insupd_trigger BEFORE INSERT OR UPDATE ON child_tbl FOR EACH ROW EXECUTE PROCEDURE row_before_insupd_trigfunc();
1847 CREATE FOREIGN TABLE foreign_tbl (a int, b int)
1848 SERVER loopback OPTIONS (table_name 'child_tbl');
1849
1850 CREATE TABLE parent_tbl (a int, b int) PARTITION BY RANGE(a);
1851 ALTER TABLE parent_tbl ATTACH PARTITION foreign_tbl FOR VALUES FROM (0) TO (100);
1852 -- Detach and re-attach once, to stress the concurrent detach case.
1853 ALTER TABLE parent_tbl DETACH PARTITION foreign_tbl CONCURRENTLY;
1854 ALTER TABLE parent_tbl ATTACH PARTITION foreign_tbl FOR VALUES FROM (0) TO (100);
1855
1856 CREATE VIEW rw_view AS SELECT * FROM parent_tbl
1857 WHERE a < b WITH CHECK OPTION;
1858 \d+ rw_view
1859
1860 EXPLAIN (VERBOSE, COSTS OFF)
1861 INSERT INTO rw_view VALUES (0, 5);
1862 INSERT INTO rw_view VALUES (0, 5); -- should fail
1863 EXPLAIN (VERBOSE, COSTS OFF)
1864 INSERT INTO rw_view VALUES (0, 15);
1865 INSERT INTO rw_view VALUES (0, 15); -- ok
1866 SELECT * FROM foreign_tbl;
1867
1868 EXPLAIN (VERBOSE, COSTS OFF)
1869 UPDATE rw_view SET b = b + 5;
1870 UPDATE rw_view SET b = b + 5; -- should fail
1871 EXPLAIN (VERBOSE, COSTS OFF)
1872 UPDATE rw_view SET b = b + 15;
1873 UPDATE rw_view SET b = b + 15; -- ok
1874 SELECT * FROM foreign_tbl;
1875
1876 -- We don't allow batch insert when there are any WCO constraints
1877 ALTER SERVER loopback OPTIONS (ADD batch_size '10');
1878 EXPLAIN (VERBOSE, COSTS OFF)
1879 INSERT INTO rw_view VALUES (0, 15), (0, 5);
1880 INSERT INTO rw_view VALUES (0, 15), (0, 5); -- should fail
1881 SELECT * FROM foreign_tbl;
1882 ALTER SERVER loopback OPTIONS (DROP batch_size);
1883
1884 DROP FOREIGN TABLE foreign_tbl CASCADE;
1885 DROP TRIGGER row_before_insupd_trigger ON child_tbl;
1886 DROP TABLE parent_tbl CASCADE;
1887
1888 DROP FUNCTION row_before_insupd_trigfunc;
1889
1890 -- Try a more complex permutation of WCO where there are multiple levels of
1891 -- partitioned tables with columns not all in the same order
1892 CREATE TABLE parent_tbl (a int, b text, c numeric) PARTITION BY RANGE(a);
1893 CREATE TABLE sub_parent (c numeric, a int, b text) PARTITION BY RANGE(a);
1894 ALTER TABLE parent_tbl ATTACH PARTITION sub_parent FOR VALUES FROM (1) TO (10);
1895 CREATE TABLE child_local (b text, c numeric, a int);
1896 CREATE FOREIGN TABLE child_foreign (b text, c numeric, a int)
1897 SERVER loopback OPTIONS (table_name 'child_local');
1898 ALTER TABLE sub_parent ATTACH PARTITION child_foreign FOR VALUES FROM (1) TO (10);
1899 CREATE VIEW rw_view AS SELECT * FROM parent_tbl WHERE a < 5 WITH CHECK OPTION;
1900
1901 INSERT INTO parent_tbl (a) VALUES(1),(5);
1902 EXPLAIN (VERBOSE, COSTS OFF)
1903 UPDATE rw_view SET b = 'text', c = 123.456;
1904 UPDATE rw_view SET b = 'text', c = 123.456;
1905 SELECT * FROM parent_tbl ORDER BY a;
1906
1907 DROP VIEW rw_view;
1908 DROP TABLE child_local;
1909 DROP FOREIGN TABLE child_foreign;
1910 DROP TABLE sub_parent;
1911 DROP TABLE parent_tbl;
1912
1913 -- ===================================================================
1914 -- test serial columns (ie, sequence-based defaults)
1915 -- ===================================================================
1916 create table loc1 (f1 serial, f2 text);
1917 alter table loc1 set (autovacuum_enabled = 'false');
1918 create foreign table rem1 (f1 serial, f2 text)
1919 server loopback options(table_name 'loc1');
1920 select pg_catalog.setval('rem1_f1_seq', 10, false);
1921 insert into loc1(f2) values('hi');
1922 insert into rem1(f2) values('hi remote');
1923 insert into loc1(f2) values('bye');
1924 insert into rem1(f2) values('bye remote');
1925 select * from loc1;
1926 select * from rem1;
1927
1928 -- ===================================================================
1929 -- test generated columns
1930 -- ===================================================================
1931 create table gloc1 (
1932 a int,
1933 b int generated always as (a * 2) stored,
1934 c int
1935 );
1936 alter table gloc1 set (autovacuum_enabled = 'false');
1937 create foreign table grem1 (
1938 a int,
1939 b int generated always as (a * 2) stored,
1940 c int generated always as (a * 3) virtual
1941 ) server loopback options(table_name 'gloc1');
1942 explain (verbose, costs off)
1943 insert into grem1 (a) values (1), (2);
1944 insert into grem1 (a) values (1), (2);
1945 explain (verbose, costs off)
1946 update grem1 set a = 22 where a = 2;
1947 update grem1 set a = 22 where a = 2;
1948 select * from gloc1;
1949 select * from grem1;
1950 delete from grem1;
1951
1952 -- test copy from
1953 copy grem1 from stdin;
1954 1
1955 2
1956 \.
1957 select * from gloc1;
1958 select * from grem1;
1959 delete from grem1;
1960
1961 -- test batch insert
1962 alter server loopback options (add batch_size '10');
1963 explain (verbose, costs off)
1964 insert into grem1 (a) values (1), (2);
1965 insert into grem1 (a) values (1), (2);
1966 select * from gloc1;
1967 select * from grem1;
1968 delete from grem1;
1969 -- batch insert with foreign partitions.
1970 -- This schema uses two partitions, one local and one remote with a modulo
1971 -- to loop across all of them in batches.
1972 create table tab_batch_local (id int, data text);
1973 insert into tab_batch_local select i, 'test'|| i from generate_series(1, 45) i;
1974 create table tab_batch_sharded (id int, data text) partition by hash(id);
1975 create table tab_batch_sharded_p0 partition of tab_batch_sharded
1976 for values with (modulus 2, remainder 0);
1977 create table tab_batch_sharded_p1_remote (id int, data text);
1978 create foreign table tab_batch_sharded_p1 partition of tab_batch_sharded
1979 for values with (modulus 2, remainder 1)
1980 server loopback options (table_name 'tab_batch_sharded_p1_remote');
1981 insert into tab_batch_sharded select * from tab_batch_local;
1982 select count(*) from tab_batch_sharded;
1983 drop table tab_batch_local;
1984 drop table tab_batch_sharded;
1985 drop table tab_batch_sharded_p1_remote;
1986
1987 alter server loopback options (drop batch_size);
1988
1989 -- ===================================================================
1990 -- test local triggers
1991 -- ===================================================================
1992
1993 -- Trigger functions "borrowed" from triggers regress test.
1994 CREATE FUNCTION trigger_func() RETURNS trigger LANGUAGE plpgsql AS $$
1995 BEGIN
1996 RAISE NOTICE 'trigger_func(%) called: action = %, when = %, level = %',
1997 TG_ARGV[0], TG_OP, TG_WHEN, TG_LEVEL;
1998 RETURN NULL;
1999 END;$$;
2000
2001 CREATE TRIGGER trig_stmt_before BEFORE DELETE OR INSERT OR UPDATE OR TRUNCATE ON rem1
2002 FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func();
2003 CREATE TRIGGER trig_stmt_after AFTER DELETE OR INSERT OR UPDATE OR TRUNCATE ON rem1
2004 FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func();
2005
2006 CREATE OR REPLACE FUNCTION trigger_data() RETURNS trigger
2007 LANGUAGE plpgsql AS $$
2008
2009 declare
2010 oldnew text[];
2011 relid text;
2012 argstr text;
2013 begin
2014
2015 relid := TG_relid::regclass;
2016 argstr := '';
2017 for i in 0 .. TG_nargs - 1 loop
2018 if i > 0 then
2019 argstr := argstr || ', ';
2020 end if;
2021 argstr := argstr || TG_argv[i];
2022 end loop;
2023
2024 RAISE NOTICE '%(%) % % % ON %',
2025 tg_name, argstr, TG_when, TG_level, TG_OP, relid;
2026 oldnew := '{}'::text[];
2027 if TG_OP != 'INSERT' then
2028 oldnew := array_append(oldnew, format('OLD: %s', OLD));
2029 end if;
2030
2031 if TG_OP != 'DELETE' then
2032 oldnew := array_append(oldnew, format('NEW: %s', NEW));
2033 end if;
2034
2035 RAISE NOTICE '%', array_to_string(oldnew, ',');
2036
2037 if TG_OP = 'DELETE' then
2038 return OLD;
2039 else
2040 return NEW;
2041 end if;
2042 end;
2043 $$;
2044
2045 -- Test basic functionality
2046 CREATE TRIGGER trig_row_before
2047 BEFORE INSERT OR UPDATE OR DELETE ON rem1
2048 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2049
2050 CREATE TRIGGER trig_row_after
2051 AFTER INSERT OR UPDATE OR DELETE ON rem1
2052 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2053
2054 delete from rem1;
2055 insert into rem1 values(1,'insert');
2056 update rem1 set f2 = 'update' where f1 = 1;
2057 update rem1 set f2 = f2 || f2;
2058 truncate rem1;
2059
2060
2061 -- cleanup
2062 DROP TRIGGER trig_row_before ON rem1;
2063 DROP TRIGGER trig_row_after ON rem1;
2064 DROP TRIGGER trig_stmt_before ON rem1;
2065 DROP TRIGGER trig_stmt_after ON rem1;
2066
2067 DELETE from rem1;
2068
2069 -- Test multiple AFTER ROW triggers on a foreign table
2070 CREATE TRIGGER trig_row_after1
2071 AFTER INSERT OR UPDATE OR DELETE ON rem1
2072 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2073
2074 CREATE TRIGGER trig_row_after2
2075 AFTER INSERT OR UPDATE OR DELETE ON rem1
2076 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2077
2078 insert into rem1 values(1,'insert');
2079 update rem1 set f2 = 'update' where f1 = 1;
2080 update rem1 set f2 = f2 || f2;
2081 delete from rem1;
2082
2083 -- cleanup
2084 DROP TRIGGER trig_row_after1 ON rem1;
2085 DROP TRIGGER trig_row_after2 ON rem1;
2086
2087 -- Test WHEN conditions
2088
2089 CREATE TRIGGER trig_row_before_insupd
2090 BEFORE INSERT OR UPDATE ON rem1
2091 FOR EACH ROW
2092 WHEN (NEW.f2 like '%update%')
2093 EXECUTE PROCEDURE trigger_data(23,'skidoo');
2094
2095 CREATE TRIGGER trig_row_after_insupd
2096 AFTER INSERT OR UPDATE ON rem1
2097 FOR EACH ROW
2098 WHEN (NEW.f2 like '%update%')
2099 EXECUTE PROCEDURE trigger_data(23,'skidoo');
2100
2101 -- Insert or update not matching: nothing happens
2102 INSERT INTO rem1 values(1, 'insert');
2103 UPDATE rem1 set f2 = 'test';
2104
2105 -- Insert or update matching: triggers are fired
2106 INSERT INTO rem1 values(2, 'update');
2107 UPDATE rem1 set f2 = 'update update' where f1 = '2';
2108
2109 CREATE TRIGGER trig_row_before_delete
2110 BEFORE DELETE ON rem1
2111 FOR EACH ROW
2112 WHEN (OLD.f2 like '%update%')
2113 EXECUTE PROCEDURE trigger_data(23,'skidoo');
2114
2115 CREATE TRIGGER trig_row_after_delete
2116 AFTER DELETE ON rem1
2117 FOR EACH ROW
2118 WHEN (OLD.f2 like '%update%')
2119 EXECUTE PROCEDURE trigger_data(23,'skidoo');
2120
2121 -- Trigger is fired for f1=2, not for f1=1
2122 DELETE FROM rem1;
2123
2124 -- cleanup
2125 DROP TRIGGER trig_row_before_insupd ON rem1;
2126 DROP TRIGGER trig_row_after_insupd ON rem1;
2127 DROP TRIGGER trig_row_before_delete ON rem1;
2128 DROP TRIGGER trig_row_after_delete ON rem1;
2129
2130
2131 -- Test various RETURN statements in BEFORE triggers.
2132
2133 CREATE FUNCTION trig_row_before_insupdate() RETURNS TRIGGER AS $$
2134 BEGIN
2135 NEW.f2 := NEW.f2 || ' triggered !';
2136 RETURN NEW;
2137 END
2138 $$ language plpgsql;
2139
2140 CREATE TRIGGER trig_row_before_insupd
2141 BEFORE INSERT OR UPDATE ON rem1
2142 FOR EACH ROW EXECUTE PROCEDURE trig_row_before_insupdate();
2143
2144 -- The new values should have 'triggered' appended
2145 INSERT INTO rem1 values(1, 'insert');
2146 SELECT * from loc1;
2147 INSERT INTO rem1 values(2, 'insert') RETURNING f2;
2148 SELECT * from loc1;
2149 UPDATE rem1 set f2 = '';
2150 SELECT * from loc1;
2151 UPDATE rem1 set f2 = 'skidoo' RETURNING f2;
2152 SELECT * from loc1;
2153
2154 EXPLAIN (verbose, costs off)
2155 UPDATE rem1 set f1 = 10; -- all columns should be transmitted
2156 UPDATE rem1 set f1 = 10;
2157 SELECT * from loc1;
2158
2159 DELETE FROM rem1;
2160
2161 -- Add a second trigger, to check that the changes are propagated correctly
2162 -- from trigger to trigger
2163 CREATE TRIGGER trig_row_before_insupd2
2164 BEFORE INSERT OR UPDATE ON rem1
2165 FOR EACH ROW EXECUTE PROCEDURE trig_row_before_insupdate();
2166
2167 INSERT INTO rem1 values(1, 'insert');
2168 SELECT * from loc1;
2169 INSERT INTO rem1 values(2, 'insert') RETURNING f2;
2170 SELECT * from loc1;
2171 UPDATE rem1 set f2 = '';
2172 SELECT * from loc1;
2173 UPDATE rem1 set f2 = 'skidoo' RETURNING f2;
2174 SELECT * from loc1;
2175
2176 DROP TRIGGER trig_row_before_insupd ON rem1;
2177 DROP TRIGGER trig_row_before_insupd2 ON rem1;
2178
2179 DELETE from rem1;
2180
2181 INSERT INTO rem1 VALUES (1, 'test');
2182
2183 -- Test with a trigger returning NULL
2184 CREATE FUNCTION trig_null() RETURNS TRIGGER AS $$
2185 BEGIN
2186 RETURN NULL;
2187 END
2188 $$ language plpgsql;
2189
2190 CREATE TRIGGER trig_null
2191 BEFORE INSERT OR UPDATE OR DELETE ON rem1
2192 FOR EACH ROW EXECUTE PROCEDURE trig_null();
2193
2194 -- Nothing should have changed.
2195 INSERT INTO rem1 VALUES (2, 'test2');
2196
2197 SELECT * from loc1;
2198
2199 UPDATE rem1 SET f2 = 'test2';
2200
2201 SELECT * from loc1;
2202
2203 DELETE from rem1;
2204
2205 SELECT * from loc1;
2206
2207 DROP TRIGGER trig_null ON rem1;
2208 DELETE from rem1;
2209
2210 -- Test a combination of local and remote triggers
2211 CREATE TRIGGER trig_row_before
2212 BEFORE INSERT OR UPDATE OR DELETE ON rem1
2213 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2214
2215 CREATE TRIGGER trig_row_after
2216 AFTER INSERT OR UPDATE OR DELETE ON rem1
2217 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2218
2219 CREATE TRIGGER trig_local_before BEFORE INSERT OR UPDATE ON loc1
2220 FOR EACH ROW EXECUTE PROCEDURE trig_row_before_insupdate();
2221
2222 INSERT INTO rem1(f2) VALUES ('test');
2223 UPDATE rem1 SET f2 = 'testo';
2224
2225 -- Test returning a system attribute
2226 INSERT INTO rem1(f2) VALUES ('test') RETURNING ctid;
2227
2228 -- cleanup
2229 DROP TRIGGER trig_row_before ON rem1;
2230 DROP TRIGGER trig_row_after ON rem1;
2231 DROP TRIGGER trig_local_before ON loc1;
2232
2233
2234 -- Test direct foreign table modification functionality
2235 EXPLAIN (verbose, costs off)
2236 DELETE FROM rem1; -- can be pushed down
2237 EXPLAIN (verbose, costs off)
2238 DELETE FROM rem1 WHERE false; -- currently can't be pushed down
2239
2240 -- Test with statement-level triggers
2241 CREATE TRIGGER trig_stmt_before
2242 BEFORE DELETE OR INSERT OR UPDATE ON rem1
2243 FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func();
2244 EXPLAIN (verbose, costs off)
2245 UPDATE rem1 set f2 = ''; -- can be pushed down
2246 EXPLAIN (verbose, costs off)
2247 DELETE FROM rem1; -- can be pushed down
2248 DROP TRIGGER trig_stmt_before ON rem1;
2249
2250 CREATE TRIGGER trig_stmt_after
2251 AFTER DELETE OR INSERT OR UPDATE ON rem1
2252 FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func();
2253 EXPLAIN (verbose, costs off)
2254 UPDATE rem1 set f2 = ''; -- can be pushed down
2255 EXPLAIN (verbose, costs off)
2256 DELETE FROM rem1; -- can be pushed down
2257 DROP TRIGGER trig_stmt_after ON rem1;
2258
2259 -- Test with row-level ON INSERT triggers
2260 CREATE TRIGGER trig_row_before_insert
2261 BEFORE INSERT ON rem1
2262 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2263 EXPLAIN (verbose, costs off)
2264 UPDATE rem1 set f2 = ''; -- can be pushed down
2265 EXPLAIN (verbose, costs off)
2266 DELETE FROM rem1; -- can be pushed down
2267 DROP TRIGGER trig_row_before_insert ON rem1;
2268
2269 CREATE TRIGGER trig_row_after_insert
2270 AFTER INSERT ON rem1
2271 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2272 EXPLAIN (verbose, costs off)
2273 UPDATE rem1 set f2 = ''; -- can be pushed down
2274 EXPLAIN (verbose, costs off)
2275 DELETE FROM rem1; -- can be pushed down
2276 DROP TRIGGER trig_row_after_insert ON rem1;
2277
2278 -- Test with row-level ON UPDATE triggers
2279 CREATE TRIGGER trig_row_before_update
2280 BEFORE UPDATE ON rem1
2281 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2282 EXPLAIN (verbose, costs off)
2283 UPDATE rem1 set f2 = ''; -- can't be pushed down
2284 EXPLAIN (verbose, costs off)
2285 DELETE FROM rem1; -- can be pushed down
2286 DROP TRIGGER trig_row_before_update ON rem1;
2287
2288 CREATE TRIGGER trig_row_after_update
2289 AFTER UPDATE ON rem1
2290 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2291 EXPLAIN (verbose, costs off)
2292 UPDATE rem1 set f2 = ''; -- can't be pushed down
2293 EXPLAIN (verbose, costs off)
2294 DELETE FROM rem1; -- can be pushed down
2295 DROP TRIGGER trig_row_after_update ON rem1;
2296
2297 -- Test with row-level ON DELETE triggers
2298 CREATE TRIGGER trig_row_before_delete
2299 BEFORE DELETE ON rem1
2300 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2301 EXPLAIN (verbose, costs off)
2302 UPDATE rem1 set f2 = ''; -- can be pushed down
2303 EXPLAIN (verbose, costs off)
2304 DELETE FROM rem1; -- can't be pushed down
2305 DROP TRIGGER trig_row_before_delete ON rem1;
2306
2307 CREATE TRIGGER trig_row_after_delete
2308 AFTER DELETE ON rem1
2309 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2310 EXPLAIN (verbose, costs off)
2311 UPDATE rem1 set f2 = ''; -- can be pushed down
2312 EXPLAIN (verbose, costs off)
2313 DELETE FROM rem1; -- can't be pushed down
2314 DROP TRIGGER trig_row_after_delete ON rem1;
2315
2316
2317 -- We are allowed to create transition-table triggers on both kinds of
2318 -- inheritance even if they contain foreign tables as children, but currently
2319 -- collecting transition tuples from such foreign tables is not supported.
2320
2321 CREATE TABLE local_tbl (a text, b int);
2322 CREATE FOREIGN TABLE foreign_tbl (a text, b int)
2323 SERVER loopback OPTIONS (table_name 'local_tbl');
2324
2325 INSERT INTO foreign_tbl VALUES ('AAA', 42);
2326
2327 -- Test case for partition hierarchy
2328 CREATE TABLE parent_tbl (a text, b int) PARTITION BY LIST (a);
2329 ALTER TABLE parent_tbl ATTACH PARTITION foreign_tbl FOR VALUES IN ('AAA');
2330
2331 CREATE TRIGGER parent_tbl_insert_trig
2332 AFTER INSERT ON parent_tbl REFERENCING NEW TABLE AS new_table
2333 FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func();
2334 CREATE TRIGGER parent_tbl_update_trig
2335 AFTER UPDATE ON parent_tbl REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
2336 FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func();
2337 CREATE TRIGGER parent_tbl_delete_trig
2338 AFTER DELETE ON parent_tbl REFERENCING OLD TABLE AS old_table
2339 FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func();
2340
2341 INSERT INTO parent_tbl VALUES ('AAA', 42);
2342
2343 COPY parent_tbl (a, b) FROM stdin;
2344 AAA 42
2345 \.
2346
2347 ALTER SERVER loopback OPTIONS (ADD batch_size '10');
2348
2349 INSERT INTO parent_tbl VALUES ('AAA', 42);
2350
2351 COPY parent_tbl (a, b) FROM stdin;
2352 AAA 42
2353 \.
2354
2355 ALTER SERVER loopback OPTIONS (DROP batch_size);
2356
2357 EXPLAIN (VERBOSE, COSTS OFF)
2358 UPDATE parent_tbl SET b = b + 1;
2359 UPDATE parent_tbl SET b = b + 1;
2360
2361 EXPLAIN (VERBOSE, COSTS OFF)
2362 DELETE FROM parent_tbl;
2363 DELETE FROM parent_tbl;
2364
2365 ALTER TABLE parent_tbl DETACH PARTITION foreign_tbl;
2366 DROP TABLE parent_tbl;
2367
2368 -- Test case for non-partition hierarchy
2369 CREATE TABLE parent_tbl (a text, b int);
2370 ALTER FOREIGN TABLE foreign_tbl INHERIT parent_tbl;
2371
2372 CREATE TRIGGER parent_tbl_update_trig
2373 AFTER UPDATE ON parent_tbl REFERENCING OLD TABLE AS old_table NEW TABLE AS new_table
2374 FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func();
2375 CREATE TRIGGER parent_tbl_delete_trig
2376 AFTER DELETE ON parent_tbl REFERENCING OLD TABLE AS old_table
2377 FOR EACH STATEMENT EXECUTE PROCEDURE trigger_func();
2378
2379 EXPLAIN (VERBOSE, COSTS OFF)
2380 UPDATE parent_tbl SET b = b + 1;
2381 UPDATE parent_tbl SET b = b + 1;
2382
2383 EXPLAIN (VERBOSE, COSTS OFF)
2384 DELETE FROM parent_tbl;
2385 DELETE FROM parent_tbl;
2386
2387 ALTER FOREIGN TABLE foreign_tbl NO INHERIT parent_tbl;
2388 DROP TABLE parent_tbl;
2389
2390 -- Cleanup
2391 DROP FOREIGN TABLE foreign_tbl;
2392 DROP TABLE local_tbl;
2393
2394 -- ===================================================================
2395 -- test inheritance features
2396 -- ===================================================================
2397
2398 CREATE TABLE a (aa TEXT);
2399 CREATE TABLE loct (aa TEXT, bb TEXT);
2400 ALTER TABLE a SET (autovacuum_enabled = 'false');
2401 ALTER TABLE loct SET (autovacuum_enabled = 'false');
2402 CREATE FOREIGN TABLE b (bb TEXT) INHERITS (a)
2403 SERVER loopback OPTIONS (table_name 'loct');
2404
2405 INSERT INTO a(aa) VALUES('aaa');
2406 INSERT INTO a(aa) VALUES('aaaa');
2407 INSERT INTO a(aa) VALUES('aaaaa');
2408
2409 INSERT INTO b(aa) VALUES('bbb');
2410 INSERT INTO b(aa) VALUES('bbbb');
2411 INSERT INTO b(aa) VALUES('bbbbb');
2412
2413 SELECT tableoid::regclass, * FROM a;
2414 SELECT tableoid::regclass, * FROM b;
2415 SELECT tableoid::regclass, * FROM ONLY a;
2416
2417 UPDATE a SET aa = 'zzzzzz' WHERE aa LIKE 'aaaa%';
2418
2419 SELECT tableoid::regclass, * FROM a;
2420 SELECT tableoid::regclass, * FROM b;
2421 SELECT tableoid::regclass, * FROM ONLY a;
2422
2423 UPDATE b SET aa = 'new';
2424
2425 SELECT tableoid::regclass, * FROM a;
2426 SELECT tableoid::regclass, * FROM b;
2427 SELECT tableoid::regclass, * FROM ONLY a;
2428
2429 UPDATE a SET aa = 'newtoo';
2430
2431 SELECT tableoid::regclass, * FROM a;
2432 SELECT tableoid::regclass, * FROM b;
2433 SELECT tableoid::regclass, * FROM ONLY a;
2434
2435 DELETE FROM a;
2436
2437 SELECT tableoid::regclass, * FROM a;
2438 SELECT tableoid::regclass, * FROM b;
2439 SELECT tableoid::regclass, * FROM ONLY a;
2440
2441 DROP TABLE a CASCADE;
2442 DROP TABLE loct;
2443
2444 -- Check SELECT FOR UPDATE/SHARE with an inherited source table
2445 create table loct1 (f1 int, f2 int, f3 int);
2446 create table loct2 (f1 int, f2 int, f3 int);
2447
2448 alter table loct1 set (autovacuum_enabled = 'false');
2449 alter table loct2 set (autovacuum_enabled = 'false');
2450
2451 create table foo (f1 int, f2 int);
2452 create foreign table foo2 (f3 int) inherits (foo)
2453 server loopback options (table_name 'loct1');
2454 create table bar (f1 int, f2 int);
2455 create foreign table bar2 (f3 int) inherits (bar)
2456 server loopback options (table_name 'loct2');
2457
2458 alter table foo set (autovacuum_enabled = 'false');
2459 alter table bar set (autovacuum_enabled = 'false');
2460
2461 insert into foo values(1,1);
2462 insert into foo values(3,3);
2463 insert into foo2 values(2,2,2);
2464 insert into foo2 values(4,4,4);
2465 insert into bar values(1,11);
2466 insert into bar values(2,22);
2467 insert into bar values(6,66);
2468 insert into bar2 values(3,33,33);
2469 insert into bar2 values(4,44,44);
2470 insert into bar2 values(7,77,77);
2471
2472 explain (verbose, costs off)
2473 select * from bar where f1 in (select f1 from foo) for update;
2474 select * from bar where f1 in (select f1 from foo) for update;
2475
2476 explain (verbose, costs off)
2477 select * from bar where f1 in (select f1 from foo) for share;
2478 select * from bar where f1 in (select f1 from foo) for share;
2479
2480 -- Now check SELECT FOR UPDATE/SHARE with an inherited source table,
2481 -- where the parent is itself a foreign table
2482 create table loct4 (f1 int, f2 int, f3 int);
2483 create foreign table foo2child (f3 int) inherits (foo2)
2484 server loopback options (table_name 'loct4');
2485
2486 explain (verbose, costs off)
2487 select * from bar where f1 in (select f1 from foo2) for share;
2488 select * from bar where f1 in (select f1 from foo2) for share;
2489
2490 drop foreign table foo2child;
2491
2492 -- And with a local child relation of the foreign table parent
2493 create table foo2child (f3 int) inherits (foo2);
2494
2495 explain (verbose, costs off)
2496 select * from bar where f1 in (select f1 from foo2) for share;
2497 select * from bar where f1 in (select f1 from foo2) for share;
2498
2499 drop table foo2child;
2500
2501 -- Check UPDATE with inherited target and an inherited source table
2502 explain (verbose, costs off)
2503 update bar set f2 = f2 + 100 where f1 in (select f1 from foo);
2504 update bar set f2 = f2 + 100 where f1 in (select f1 from foo);
2505
2506 select tableoid::regclass, * from bar order by 1,2;
2507
2508 -- Check UPDATE with inherited target and an appendrel subquery
2509 explain (verbose, costs off)
2510 update bar set f2 = f2 + 100
2511 from
2512 ( select f1 from foo union all select f1+3 from foo ) ss
2513 where bar.f1 = ss.f1;
2514 update bar set f2 = f2 + 100
2515 from
2516 ( select f1 from foo union all select f1+3 from foo ) ss
2517 where bar.f1 = ss.f1;
2518
2519 select tableoid::regclass, * from bar order by 1,2;
2520
2521 -- Test forcing the remote server to produce sorted data for a merge join,
2522 -- but the foreign table is an inheritance child.
2523 truncate table loct1;
2524 truncate table only foo;
2525 \set num_rows_foo 2000
2526 insert into loct1 select generate_series(0, :num_rows_foo, 2), generate_series(0, :num_rows_foo, 2), generate_series(0, :num_rows_foo, 2);
2527 insert into foo select generate_series(1, :num_rows_foo, 2), generate_series(1, :num_rows_foo, 2);
2528 SET enable_hashjoin to false;
2529 SET enable_nestloop to false;
2530 alter foreign table foo2 options (use_remote_estimate 'true');
2531 create index i_loct1_f1 on loct1(f1);
2532 create index i_foo_f1 on foo(f1);
2533 analyze foo;
2534 analyze loct1;
2535 -- inner join; expressions in the clauses appear in the equivalence class list
2536 explain (verbose, costs off)
2537 select foo.f1, loct1.f1 from foo join loct1 on (foo.f1 = loct1.f1) order by foo.f2 offset 10 limit 10;
2538 select foo.f1, loct1.f1 from foo join loct1 on (foo.f1 = loct1.f1) order by foo.f2 offset 10 limit 10;
2539 -- outer join; expressions in the clauses do not appear in equivalence class
2540 -- list but no output change as compared to the previous query
2541 explain (verbose, costs off)
2542 select foo.f1, loct1.f1 from foo left join loct1 on (foo.f1 = loct1.f1) order by foo.f2 offset 10 limit 10;
2543 select foo.f1, loct1.f1 from foo left join loct1 on (foo.f1 = loct1.f1) order by foo.f2 offset 10 limit 10;
2544 RESET enable_hashjoin;
2545 RESET enable_nestloop;
2546
2547 -- Test that WHERE CURRENT OF is not supported
2548 begin;
2549 declare c cursor for select * from bar where f1 = 7;
2550 fetch from c;
2551 update bar set f2 = null where current of c;
2552 rollback;
2553
2554 explain (verbose, costs off)
2555 delete from foo where f1 < 5 returning *;
2556 delete from foo where f1 < 5 returning *;
2557 explain (verbose, costs off)
2558 update bar set f2 = f2 + 100 returning *;
2559 update bar set f2 = f2 + 100 returning *;
2560
2561 -- Test that UPDATE/DELETE with inherited target works with row-level triggers
2562 CREATE TRIGGER trig_row_before
2563 BEFORE UPDATE OR DELETE ON bar2
2564 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2565
2566 CREATE TRIGGER trig_row_after
2567 AFTER UPDATE OR DELETE ON bar2
2568 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
2569
2570 explain (verbose, costs off)
2571 update bar set f2 = f2 + 100;
2572 update bar set f2 = f2 + 100;
2573
2574 explain (verbose, costs off)
2575 delete from bar where f2 < 400;
2576 delete from bar where f2 < 400;
2577
2578 -- cleanup
2579 drop table foo cascade;
2580 drop table bar cascade;
2581 drop table loct1;
2582 drop table loct2;
2583
2584 -- Test pushing down UPDATE/DELETE joins to the remote server
2585 create table parent (a int, b text);
2586 create table loct1 (a int, b text);
2587 create table loct2 (a int, b text);
2588 create foreign table remt1 (a int, b text)
2589 server loopback options (table_name 'loct1');
2590 create foreign table remt2 (a int, b text)
2591 server loopback options (table_name 'loct2');
2592 alter foreign table remt1 inherit parent;
2593
2594 insert into remt1 values (1, 'foo');
2595 insert into remt1 values (2, 'bar');
2596 insert into remt2 values (1, 'foo');
2597 insert into remt2 values (2, 'bar');
2598
2599 analyze remt1;
2600 analyze remt2;
2601
2602 explain (verbose, costs off)
2603 update parent set b = parent.b || remt2.b from remt2 where parent.a = remt2.a returning *;
2604 update parent set b = parent.b || remt2.b from remt2 where parent.a = remt2.a returning *;
2605 explain (verbose, costs off)
2606 delete from parent using remt2 where parent.a = remt2.a returning parent;
2607 delete from parent using remt2 where parent.a = remt2.a returning parent;
2608
2609 -- cleanup
2610 drop foreign table remt1;
2611 drop foreign table remt2;
2612 drop table loct1;
2613 drop table loct2;
2614 drop table parent;
2615
2616 -- ===================================================================
2617 -- test tuple routing for foreign-table partitions
2618 -- ===================================================================
2619
2620 -- Test insert tuple routing
2621 create table itrtest (a int, b text) partition by list (a);
2622 create table loct1 (a int check (a in (1)), b text);
2623 create foreign table remp1 (a int check (a in (1)), b text) server loopback options (table_name 'loct1');
2624 create table loct2 (a int check (a in (2)), b text);
2625 create foreign table remp2 (b text, a int check (a in (2))) server loopback options (table_name 'loct2');
2626 alter table itrtest attach partition remp1 for values in (1);
2627 alter table itrtest attach partition remp2 for values in (2);
2628
2629 insert into itrtest values (1, 'foo');
2630 insert into itrtest values (1, 'bar') returning *;
2631 insert into itrtest values (2, 'baz');
2632 insert into itrtest values (2, 'qux') returning *;
2633 insert into itrtest values (1, 'test1'), (2, 'test2') returning *;
2634
2635 select tableoid::regclass, * FROM itrtest;
2636 select tableoid::regclass, * FROM remp1;
2637 select tableoid::regclass, * FROM remp2;
2638
2639 delete from itrtest;
2640
2641 -- MERGE ought to fail cleanly
2642 merge into itrtest using (select 1, 'foo') as source on (true)
2643 when matched then do nothing;
2644
2645 create unique index loct1_idx on loct1 (a);
2646
2647 -- DO NOTHING without an inference specification is supported
2648 insert into itrtest values (1, 'foo') on conflict do nothing returning *;
2649 insert into itrtest values (1, 'foo') on conflict do nothing returning *;
2650
2651 -- But other cases are not supported
2652 insert into itrtest values (1, 'bar') on conflict (a) do nothing;
2653 insert into itrtest values (1, 'bar') on conflict (a) do update set b = excluded.b;
2654
2655 select tableoid::regclass, * FROM itrtest;
2656
2657 delete from itrtest;
2658
2659 drop index loct1_idx;
2660
2661 -- Test that remote triggers work with insert tuple routing
2662 create function br_insert_trigfunc() returns trigger as $$
2663 begin
2664 new.b := new.b || ' triggered !';
2665 return new;
2666 end
2667 $$ language plpgsql;
2668 create trigger loct1_br_insert_trigger before insert on loct1
2669 for each row execute procedure br_insert_trigfunc();
2670 create trigger loct2_br_insert_trigger before insert on loct2
2671 for each row execute procedure br_insert_trigfunc();
2672
2673 -- The new values are concatenated with ' triggered !'
2674 insert into itrtest values (1, 'foo') returning *;
2675 insert into itrtest values (2, 'qux') returning *;
2676 insert into itrtest values (1, 'test1'), (2, 'test2') returning *;
2677 with result as (insert into itrtest values (1, 'test1'), (2, 'test2') returning *) select * from result;
2678
2679 drop trigger loct1_br_insert_trigger on loct1;
2680 drop trigger loct2_br_insert_trigger on loct2;
2681
2682 drop table itrtest;
2683 drop table loct1;
2684 drop table loct2;
2685
2686 -- Test update tuple routing
2687 create table utrtest (a int, b text) partition by list (a);
2688 create table loct (a int check (a in (1)), b text);
2689 create foreign table remp (a int check (a in (1)), b text) server loopback options (table_name 'loct');
2690 create table locp (a int check (a in (2)), b text);
2691 alter table utrtest attach partition remp for values in (1);
2692 alter table utrtest attach partition locp for values in (2);
2693
2694 insert into utrtest values (1, 'foo');
2695 insert into utrtest values (2, 'qux');
2696
2697 select tableoid::regclass, * FROM utrtest;
2698 select tableoid::regclass, * FROM remp;
2699 select tableoid::regclass, * FROM locp;
2700
2701 -- It's not allowed to move a row from a partition that is foreign to another
2702 update utrtest set a = 2 where b = 'foo' returning *;
2703
2704 -- But the reverse is allowed
2705 update utrtest set a = 1 where b = 'qux' returning *;
2706
2707 select tableoid::regclass, * FROM utrtest;
2708 select tableoid::regclass, * FROM remp;
2709 select tableoid::regclass, * FROM locp;
2710
2711 -- The executor should not let unexercised FDWs shut down
2712 update utrtest set a = 1 where b = 'foo';
2713
2714 -- Test that remote triggers work with update tuple routing
2715 create trigger loct_br_insert_trigger before insert on loct
2716 for each row execute procedure br_insert_trigfunc();
2717
2718 delete from utrtest;
2719 insert into utrtest values (2, 'qux');
2720
2721 -- Check case where the foreign partition is a subplan target rel
2722 explain (verbose, costs off)
2723 update utrtest set a = 1 where a = 1 or a = 2 returning *;
2724 -- The new values are concatenated with ' triggered !'
2725 update utrtest set a = 1 where a = 1 or a = 2 returning *;
2726
2727 delete from utrtest;
2728 insert into utrtest values (2, 'qux');
2729
2730 -- Check case where the foreign partition isn't a subplan target rel
2731 explain (verbose, costs off)
2732 update utrtest set a = 1 where a = 2 returning *;
2733 -- The new values are concatenated with ' triggered !'
2734 update utrtest set a = 1 where a = 2 returning *;
2735
2736 drop trigger loct_br_insert_trigger on loct;
2737
2738 -- We can move rows to a foreign partition that has been updated already,
2739 -- but can't move rows to a foreign partition that hasn't been updated yet
2740
2741 delete from utrtest;
2742 insert into utrtest values (1, 'foo');
2743 insert into utrtest values (2, 'qux');
2744
2745 -- Test the former case:
2746 -- with a direct modification plan
2747 explain (verbose, costs off)
2748 update utrtest set a = 1 returning *;
2749 update utrtest set a = 1 returning *;
2750
2751 delete from utrtest;
2752 insert into utrtest values (1, 'foo');
2753 insert into utrtest values (2, 'qux');
2754
2755 -- with a non-direct modification plan
2756 explain (verbose, costs off)
2757 update utrtest set a = 1 from (values (1), (2)) s(x) where a = s.x returning *;
2758 update utrtest set a = 1 from (values (1), (2)) s(x) where a = s.x returning *;
2759
2760 -- Change the definition of utrtest so that the foreign partition get updated
2761 -- after the local partition
2762 delete from utrtest;
2763 alter table utrtest detach partition remp;
2764 drop foreign table remp;
2765 alter table loct drop constraint loct_a_check;
2766 alter table loct add check (a in (3));
2767 create foreign table remp (a int check (a in (3)), b text) server loopback options (table_name 'loct');
2768 alter table utrtest attach partition remp for values in (3);
2769 insert into utrtest values (2, 'qux');
2770 insert into utrtest values (3, 'xyzzy');
2771
2772 -- Test the latter case:
2773 -- with a direct modification plan
2774 explain (verbose, costs off)
2775 update utrtest set a = 3 returning *;
2776 update utrtest set a = 3 returning *; -- ERROR
2777
2778 -- with a non-direct modification plan
2779 explain (verbose, costs off)
2780 update utrtest set a = 3 from (values (2), (3)) s(x) where a = s.x returning *;
2781 update utrtest set a = 3 from (values (2), (3)) s(x) where a = s.x returning *; -- ERROR
2782
2783 drop table utrtest;
2784 drop table loct;
2785
2786 -- Test copy tuple routing
2787 create table ctrtest (a int, b text) partition by list (a);
2788 create table loct1 (a int check (a in (1)), b text);
2789 create foreign table remp1 (a int check (a in (1)), b text) server loopback options (table_name 'loct1');
2790 create table loct2 (a int check (a in (2)), b text);
2791 create foreign table remp2 (b text, a int check (a in (2))) server loopback options (table_name 'loct2');
2792 alter table ctrtest attach partition remp1 for values in (1);
2793 alter table ctrtest attach partition remp2 for values in (2);
2794
2795 copy ctrtest from stdin;
2796 1 foo
2797 2 qux
2798 \.
2799
2800 select tableoid::regclass, * FROM ctrtest;
2801 select tableoid::regclass, * FROM remp1;
2802 select tableoid::regclass, * FROM remp2;
2803
2804 -- Copying into foreign partitions directly should work as well
2805 copy remp1 from stdin;
2806 1 bar
2807 \.
2808
2809 select tableoid::regclass, * FROM remp1;
2810
2811 delete from ctrtest;
2812
2813 -- Test copy tuple routing with the batch_size option enabled
2814 alter server loopback options (add batch_size '2');
2815
2816 copy ctrtest from stdin;
2817 1 foo
2818 1 bar
2819 2 baz
2820 2 qux
2821 1 test1
2822 2 test2
2823 \.
2824
2825 select tableoid::regclass, * FROM ctrtest;
2826 select tableoid::regclass, * FROM remp1;
2827 select tableoid::regclass, * FROM remp2;
2828
2829 delete from ctrtest;
2830
2831 alter server loopback options (drop batch_size);
2832
2833 drop table ctrtest;
2834 drop table loct1;
2835 drop table loct2;
2836
2837 -- ===================================================================
2838 -- test COPY FROM
2839 -- ===================================================================
2840
2841 create table loc2 (f1 int, f2 text);
2842 alter table loc2 set (autovacuum_enabled = 'false');
2843 create foreign table rem2 (f1 int, f2 text) server loopback options(table_name 'loc2');
2844
2845 -- Test basic functionality
2846 copy rem2 from stdin;
2847 1 foo
2848 2 bar
2849 \.
2850 select * from rem2;
2851
2852 delete from rem2;
2853
2854 -- Test check constraints
2855 alter table loc2 add constraint loc2_f1positive check (f1 >= 0);
2856 alter foreign table rem2 add constraint rem2_f1positive check (f1 >= 0);
2857
2858 -- check constraint is enforced on the remote side, not locally
2859 copy rem2 from stdin;
2860 1 foo
2861 2 bar
2862 \.
2863 copy rem2 from stdin; -- ERROR
2864 -1 xyzzy
2865 \.
2866 select * from rem2;
2867
2868 alter foreign table rem2 drop constraint rem2_f1positive;
2869 alter table loc2 drop constraint loc2_f1positive;
2870
2871 delete from rem2;
2872
2873 -- Test local triggers
2874 create trigger trig_stmt_before before insert on rem2
2875 for each statement execute procedure trigger_func();
2876 create trigger trig_stmt_after after insert on rem2
2877 for each statement execute procedure trigger_func();
2878 create trigger trig_row_before before insert on rem2
2879 for each row execute procedure trigger_data(23,'skidoo');
2880 create trigger trig_row_after after insert on rem2
2881 for each row execute procedure trigger_data(23,'skidoo');
2882
2883 copy rem2 from stdin;
2884 1 foo
2885 2 bar
2886 \.
2887 select * from rem2;
2888
2889 drop trigger trig_row_before on rem2;
2890 drop trigger trig_row_after on rem2;
2891 drop trigger trig_stmt_before on rem2;
2892 drop trigger trig_stmt_after on rem2;
2893
2894 delete from rem2;
2895
2896 create trigger trig_row_before_insert before insert on rem2
2897 for each row execute procedure trig_row_before_insupdate();
2898
2899 -- The new values are concatenated with ' triggered !'
2900 copy rem2 from stdin;
2901 1 foo
2902 2 bar
2903 \.
2904 select * from rem2;
2905
2906 drop trigger trig_row_before_insert on rem2;
2907
2908 delete from rem2;
2909
2910 create trigger trig_null before insert on rem2
2911 for each row execute procedure trig_null();
2912
2913 -- Nothing happens
2914 copy rem2 from stdin;
2915 1 foo
2916 2 bar
2917 \.
2918 select * from rem2;
2919
2920 drop trigger trig_null on rem2;
2921
2922 delete from rem2;
2923
2924 -- Test remote triggers
2925 create trigger trig_row_before_insert before insert on loc2
2926 for each row execute procedure trig_row_before_insupdate();
2927
2928 -- The new values are concatenated with ' triggered !'
2929 copy rem2 from stdin;
2930 1 foo
2931 2 bar
2932 \.
2933 select * from rem2;
2934
2935 drop trigger trig_row_before_insert on loc2;
2936
2937 delete from rem2;
2938
2939 create trigger trig_null before insert on loc2
2940 for each row execute procedure trig_null();
2941
2942 -- Nothing happens
2943 copy rem2 from stdin;
2944 1 foo
2945 2 bar
2946 \.
2947 select * from rem2;
2948
2949 drop trigger trig_null on loc2;
2950
2951 delete from rem2;
2952
2953 -- Test a combination of local and remote triggers
2954 create trigger rem2_trig_row_before before insert on rem2
2955 for each row execute procedure trigger_data(23,'skidoo');
2956 create trigger rem2_trig_row_after after insert on rem2
2957 for each row execute procedure trigger_data(23,'skidoo');
2958 create trigger loc2_trig_row_before_insert before insert on loc2
2959 for each row execute procedure trig_row_before_insupdate();
2960
2961 copy rem2 from stdin;
2962 1 foo
2963 2 bar
2964 \.
2965 select * from rem2;
2966
2967 drop trigger rem2_trig_row_before on rem2;
2968 drop trigger rem2_trig_row_after on rem2;
2969 drop trigger loc2_trig_row_before_insert on loc2;
2970
2971 delete from rem2;
2972
2973 -- test COPY FROM with foreign table created in the same transaction
2974 create table loc3 (f1 int, f2 text);
2975 begin;
2976 create foreign table rem3 (f1 int, f2 text)
2977 server loopback options(table_name 'loc3');
2978 copy rem3 from stdin;
2979 1 foo
2980 2 bar
2981 \.
2982 commit;
2983 select * from rem3;
2984 drop foreign table rem3;
2985 drop table loc3;
2986
2987 -- Test COPY FROM with the batch_size option enabled
2988 alter server loopback options (add batch_size '2');
2989
2990 -- Test basic functionality
2991 copy rem2 from stdin;
2992 1 foo
2993 2 bar
2994 3 baz
2995 \.
2996 select * from rem2;
2997
2998 delete from rem2;
2999
3000 -- Test check constraints
3001 alter table loc2 add constraint loc2_f1positive check (f1 >= 0);
3002 alter foreign table rem2 add constraint rem2_f1positive check (f1 >= 0);
3003
3004 -- check constraint is enforced on the remote side, not locally
3005 copy rem2 from stdin;
3006 1 foo
3007 2 bar
3008 3 baz
3009 \.
3010 copy rem2 from stdin; -- ERROR
3011 -1 xyzzy
3012 \.
3013 select * from rem2;
3014
3015 alter foreign table rem2 drop constraint rem2_f1positive;
3016 alter table loc2 drop constraint loc2_f1positive;
3017
3018 delete from rem2;
3019
3020 -- Test remote triggers
3021 create trigger trig_row_before_insert before insert on loc2
3022 for each row execute procedure trig_row_before_insupdate();
3023
3024 -- The new values are concatenated with ' triggered !'
3025 copy rem2 from stdin;
3026 1 foo
3027 2 bar
3028 3 baz
3029 \.
3030 select * from rem2;
3031
3032 drop trigger trig_row_before_insert on loc2;
3033
3034 delete from rem2;
3035
3036 create trigger trig_null before insert on loc2
3037 for each row execute procedure trig_null();
3038
3039 -- Nothing happens
3040 copy rem2 from stdin;
3041 1 foo
3042 2 bar
3043 3 baz
3044 \.
3045 select * from rem2;
3046
3047 drop trigger trig_null on loc2;
3048
3049 delete from rem2;
3050
3051 -- Check with zero-column foreign table; batch insert will be disabled
3052 alter table loc2 drop column f1;
3053 alter table loc2 drop column f2;
3054 alter table rem2 drop column f1;
3055 alter table rem2 drop column f2;
3056 copy rem2 from stdin;
3057
3058
3059
3060 \.
3061 select * from rem2;
3062
3063 delete from rem2;
3064
3065 alter server loopback options (drop batch_size);
3066
3067 -- ===================================================================
3068 -- test for TRUNCATE
3069 -- ===================================================================
3070 CREATE TABLE tru_rtable0 (id int primary key);
3071 CREATE FOREIGN TABLE tru_ftable (id int)
3072 SERVER loopback OPTIONS (table_name 'tru_rtable0');
3073 INSERT INTO tru_rtable0 (SELECT x FROM generate_series(1,10) x);
3074
3075 CREATE TABLE tru_ptable (id int) PARTITION BY HASH(id);
3076 CREATE TABLE tru_ptable__p0 PARTITION OF tru_ptable
3077 FOR VALUES WITH (MODULUS 2, REMAINDER 0);
3078 CREATE TABLE tru_rtable1 (id int primary key);
3079 CREATE FOREIGN TABLE tru_ftable__p1 PARTITION OF tru_ptable
3080 FOR VALUES WITH (MODULUS 2, REMAINDER 1)
3081 SERVER loopback OPTIONS (table_name 'tru_rtable1');
3082 INSERT INTO tru_ptable (SELECT x FROM generate_series(11,20) x);
3083
3084 CREATE TABLE tru_pk_table(id int primary key);
3085 CREATE TABLE tru_fk_table(fkey int references tru_pk_table(id));
3086 INSERT INTO tru_pk_table (SELECT x FROM generate_series(1,10) x);
3087 INSERT INTO tru_fk_table (SELECT x % 10 + 1 FROM generate_series(5,25) x);
3088 CREATE FOREIGN TABLE tru_pk_ftable (id int)
3089 SERVER loopback OPTIONS (table_name 'tru_pk_table');
3090
3091 CREATE TABLE tru_rtable_parent (id int);
3092 CREATE TABLE tru_rtable_child (id int);
3093 CREATE FOREIGN TABLE tru_ftable_parent (id int)
3094 SERVER loopback OPTIONS (table_name 'tru_rtable_parent');
3095 CREATE FOREIGN TABLE tru_ftable_child () INHERITS (tru_ftable_parent)
3096 SERVER loopback OPTIONS (table_name 'tru_rtable_child');
3097 INSERT INTO tru_rtable_parent (SELECT x FROM generate_series(1,8) x);
3098 INSERT INTO tru_rtable_child (SELECT x FROM generate_series(10, 18) x);
3099
3100 -- normal truncate
3101 SELECT sum(id) FROM tru_ftable; -- 55
3102 TRUNCATE tru_ftable;
3103 SELECT count(*) FROM tru_rtable0; -- 0
3104 SELECT count(*) FROM tru_ftable; -- 0
3105
3106 -- 'truncatable' option
3107 ALTER SERVER loopback OPTIONS (ADD truncatable 'false');
3108 TRUNCATE tru_ftable; -- error
3109 ALTER FOREIGN TABLE tru_ftable OPTIONS (ADD truncatable 'true');
3110 TRUNCATE tru_ftable; -- accepted
3111 ALTER FOREIGN TABLE tru_ftable OPTIONS (SET truncatable 'false');
3112 TRUNCATE tru_ftable; -- error
3113 ALTER SERVER loopback OPTIONS (DROP truncatable);
3114 ALTER FOREIGN TABLE tru_ftable OPTIONS (SET truncatable 'false');
3115 TRUNCATE tru_ftable; -- error
3116 ALTER FOREIGN TABLE tru_ftable OPTIONS (SET truncatable 'true');
3117 TRUNCATE tru_ftable; -- accepted
3118
3119 -- partitioned table with both local and foreign tables as partitions
3120 SELECT sum(id) FROM tru_ptable; -- 155
3121 TRUNCATE tru_ptable;
3122 SELECT count(*) FROM tru_ptable; -- 0
3123 SELECT count(*) FROM tru_ptable__p0; -- 0
3124 SELECT count(*) FROM tru_ftable__p1; -- 0
3125 SELECT count(*) FROM tru_rtable1; -- 0
3126
3127 -- 'CASCADE' option
3128 SELECT sum(id) FROM tru_pk_ftable; -- 55
3129 TRUNCATE tru_pk_ftable; -- failed by FK reference
3130 TRUNCATE tru_pk_ftable CASCADE;
3131 SELECT count(*) FROM tru_pk_ftable; -- 0
3132 SELECT count(*) FROM tru_fk_table; -- also truncated,0
3133
3134 -- truncate two tables at a command
3135 INSERT INTO tru_ftable (SELECT x FROM generate_series(1,8) x);
3136 INSERT INTO tru_pk_ftable (SELECT x FROM generate_series(3,10) x);
3137 SELECT count(*) from tru_ftable; -- 8
3138 SELECT count(*) from tru_pk_ftable; -- 8
3139 TRUNCATE tru_ftable, tru_pk_ftable CASCADE;
3140 SELECT count(*) from tru_ftable; -- 0
3141 SELECT count(*) from tru_pk_ftable; -- 0
3142
3143 -- truncate with ONLY clause
3144 -- Since ONLY is specified, the table tru_ftable_child that inherits
3145 -- tru_ftable_parent locally is not truncated.
3146 TRUNCATE ONLY tru_ftable_parent;
3147 SELECT sum(id) FROM tru_ftable_parent; -- 126
3148 TRUNCATE tru_ftable_parent;
3149 SELECT count(*) FROM tru_ftable_parent; -- 0
3150
3151 -- in case when remote table has inherited children
3152 CREATE TABLE tru_rtable0_child () INHERITS (tru_rtable0);
3153 INSERT INTO tru_rtable0 (SELECT x FROM generate_series(5,9) x);
3154 INSERT INTO tru_rtable0_child (SELECT x FROM generate_series(10,14) x);
3155 SELECT sum(id) FROM tru_ftable; -- 95
3156
3157 -- Both parent and child tables in the foreign server are truncated
3158 -- even though ONLY is specified because ONLY has no effect
3159 -- when truncating a foreign table.
3160 TRUNCATE ONLY tru_ftable;
3161 SELECT count(*) FROM tru_ftable; -- 0
3162
3163 INSERT INTO tru_rtable0 (SELECT x FROM generate_series(21,25) x);
3164 INSERT INTO tru_rtable0_child (SELECT x FROM generate_series(26,30) x);
3165 SELECT sum(id) FROM tru_ftable; -- 255
3166 TRUNCATE tru_ftable; -- truncate both of parent and child
3167 SELECT count(*) FROM tru_ftable; -- 0
3168
3169 -- cleanup
3170 DROP FOREIGN TABLE tru_ftable_parent, tru_ftable_child, tru_pk_ftable,tru_ftable__p1,tru_ftable;
3171 DROP TABLE tru_rtable0, tru_rtable1, tru_ptable, tru_ptable__p0, tru_pk_table, tru_fk_table,
3172 tru_rtable_parent,tru_rtable_child, tru_rtable0_child;
3173
3174 -- ===================================================================
3175 -- test IMPORT FOREIGN SCHEMA
3176 -- ===================================================================
3177
3178 CREATE SCHEMA import_source;
3179 CREATE TABLE import_source.t1 (c1 int, c2 varchar NOT NULL);
3180 CREATE TABLE import_source.t2 (c1 int default 42, c2 varchar NULL, c3 text collate "POSIX");
3181 CREATE TYPE typ1 AS (m1 int, m2 varchar);
3182 CREATE TABLE import_source.t3 (c1 timestamptz default now(), c2 typ1);
3183 CREATE TABLE import_source."x 4" (c1 float8, "C 2" text, c3 varchar(42));
3184 CREATE TABLE import_source."x 5" (c1 float8);
3185 ALTER TABLE import_source."x 5" DROP COLUMN c1;
3186 CREATE TABLE import_source."x 6" (c1 int, c2 int generated always as (c1 * 2) stored);
3187 CREATE TABLE import_source.t4 (c1 int) PARTITION BY RANGE (c1);
3188 CREATE TABLE import_source.t4_part PARTITION OF import_source.t4
3189 FOR VALUES FROM (1) TO (100);
3190 CREATE TABLE import_source.t4_part2 PARTITION OF import_source.t4
3191 FOR VALUES FROM (100) TO (200);
3192
3193 CREATE SCHEMA import_dest1;
3194 IMPORT FOREIGN SCHEMA import_source FROM SERVER loopback INTO import_dest1;
3195 \det+ import_dest1.*
3196 \d import_dest1.*
3197
3198 -- Options
3199 CREATE SCHEMA import_dest2;
3200 IMPORT FOREIGN SCHEMA import_source FROM SERVER loopback INTO import_dest2
3201 OPTIONS (import_default 'true');
3202 \det+ import_dest2.*
3203 \d import_dest2.*
3204 CREATE SCHEMA import_dest3;
3205 IMPORT FOREIGN SCHEMA import_source FROM SERVER loopback INTO import_dest3
3206 OPTIONS (import_collate 'false', import_generated 'false', import_not_null 'false');
3207 \det+ import_dest3.*
3208 \d import_dest3.*
3209
3210 -- Check LIMIT TO and EXCEPT
3211 CREATE SCHEMA import_dest4;
3212 IMPORT FOREIGN SCHEMA import_source LIMIT TO (t1, nonesuch, t4_part)
3213 FROM SERVER loopback INTO import_dest4;
3214 \det+ import_dest4.*
3215 IMPORT FOREIGN SCHEMA import_source EXCEPT (t1, "x 4", nonesuch, t4_part)
3216 FROM SERVER loopback INTO import_dest4;
3217 \det+ import_dest4.*
3218
3219 -- Assorted error cases
3220 IMPORT FOREIGN SCHEMA import_source FROM SERVER loopback INTO import_dest4;
3221 IMPORT FOREIGN SCHEMA nonesuch FROM SERVER loopback INTO import_dest4;
3222 IMPORT FOREIGN SCHEMA nonesuch FROM SERVER loopback INTO notthere;
3223 IMPORT FOREIGN SCHEMA nonesuch FROM SERVER nowhere INTO notthere;
3224
3225 -- Check case of a type present only on the remote server.
3226 -- We can fake this by dropping the type locally in our transaction.
3227 CREATE TYPE "Colors" AS ENUM ('red', 'green', 'blue');
3228 CREATE TABLE import_source.t5 (c1 int, c2 text collate "C", "Col" "Colors");
3229
3230 CREATE SCHEMA import_dest5;
3231 BEGIN;
3232 DROP TYPE "Colors" CASCADE;
3233 IMPORT FOREIGN SCHEMA import_source LIMIT TO (t5)
3234 FROM SERVER loopback INTO import_dest5; -- ERROR
3235
3236 ROLLBACK;
3237
3238 BEGIN;
3239
3240
3241 CREATE SERVER fetch101 FOREIGN DATA WRAPPER postgres_fdw OPTIONS( fetch_size '101' );
3242
3243 SELECT count(*)
3244 FROM pg_foreign_server
3245 WHERE srvname = 'fetch101'
3246 AND srvoptions @> array['fetch_size=101'];
3247
3248 ALTER SERVER fetch101 OPTIONS( SET fetch_size '202' );
3249
3250 SELECT count(*)
3251 FROM pg_foreign_server
3252 WHERE srvname = 'fetch101'
3253 AND srvoptions @> array['fetch_size=101'];
3254
3255 SELECT count(*)
3256 FROM pg_foreign_server
3257 WHERE srvname = 'fetch101'
3258 AND srvoptions @> array['fetch_size=202'];
3259
3260 CREATE FOREIGN TABLE table30000 ( x int ) SERVER fetch101 OPTIONS ( fetch_size '30000' );
3261
3262 SELECT COUNT(*)
3263 FROM pg_foreign_table
3264 WHERE ftrelid = 'table30000'::regclass
3265 AND ftoptions @> array['fetch_size=30000'];
3266
3267 ALTER FOREIGN TABLE table30000 OPTIONS ( SET fetch_size '60000');
3268
3269 SELECT COUNT(*)
3270 FROM pg_foreign_table
3271 WHERE ftrelid = 'table30000'::regclass
3272 AND ftoptions @> array['fetch_size=30000'];
3273
3274 SELECT COUNT(*)
3275 FROM pg_foreign_table
3276 WHERE ftrelid = 'table30000'::regclass
3277 AND ftoptions @> array['fetch_size=60000'];
3278
3279 ROLLBACK;
3280
3281 -- ===================================================================
3282 -- test partitionwise joins
3283 -- ===================================================================
3284 SET enable_partitionwise_join=on;
3285
3286 CREATE TABLE fprt1 (a int, b int, c varchar) PARTITION BY RANGE(a);
3287 CREATE TABLE fprt1_p1 (LIKE fprt1);
3288 CREATE TABLE fprt1_p2 (LIKE fprt1);
3289 ALTER TABLE fprt1_p1 SET (autovacuum_enabled = 'false');
3290 ALTER TABLE fprt1_p2 SET (autovacuum_enabled = 'false');
3291 INSERT INTO fprt1_p1 SELECT i, i, to_char(i/50, 'FM0000') FROM generate_series(0, 249, 2) i;
3292 INSERT INTO fprt1_p2 SELECT i, i, to_char(i/50, 'FM0000') FROM generate_series(250, 499, 2) i;
3293 CREATE FOREIGN TABLE ftprt1_p1 PARTITION OF fprt1 FOR VALUES FROM (0) TO (250)
3294 SERVER loopback OPTIONS (table_name 'fprt1_p1', use_remote_estimate 'true');
3295 CREATE FOREIGN TABLE ftprt1_p2 PARTITION OF fprt1 FOR VALUES FROM (250) TO (500)
3296 SERVER loopback OPTIONS (TABLE_NAME 'fprt1_p2');
3297 ANALYZE fprt1;
3298 ANALYZE fprt1_p1;
3299 ANALYZE fprt1_p2;
3300
3301 CREATE TABLE fprt2 (a int, b int, c varchar) PARTITION BY RANGE(b);
3302 CREATE TABLE fprt2_p1 (LIKE fprt2);
3303 CREATE TABLE fprt2_p2 (LIKE fprt2);
3304 ALTER TABLE fprt2_p1 SET (autovacuum_enabled = 'false');
3305 ALTER TABLE fprt2_p2 SET (autovacuum_enabled = 'false');
3306 INSERT INTO fprt2_p1 SELECT i, i, to_char(i/50, 'FM0000') FROM generate_series(0, 249, 3) i;
3307 INSERT INTO fprt2_p2 SELECT i, i, to_char(i/50, 'FM0000') FROM generate_series(250, 499, 3) i;
3308 CREATE FOREIGN TABLE ftprt2_p1 (b int, c varchar, a int)
3309 SERVER loopback OPTIONS (table_name 'fprt2_p1', use_remote_estimate 'true');
3310 ALTER TABLE fprt2 ATTACH PARTITION ftprt2_p1 FOR VALUES FROM (0) TO (250);
3311 CREATE FOREIGN TABLE ftprt2_p2 PARTITION OF fprt2 FOR VALUES FROM (250) TO (500)
3312 SERVER loopback OPTIONS (table_name 'fprt2_p2', use_remote_estimate 'true');
3313 ANALYZE fprt2;
3314 ANALYZE fprt2_p1;
3315 ANALYZE fprt2_p2;
3316
3317 -- inner join three tables
3318 EXPLAIN (COSTS OFF)
3319 SELECT t1.a,t2.b,t3.c FROM fprt1 t1 INNER JOIN fprt2 t2 ON (t1.a = t2.b) INNER JOIN fprt1 t3 ON (t2.b = t3.a) WHERE t1.a % 25 =0 ORDER BY 1,2,3;
3320 SELECT t1.a,t2.b,t3.c FROM fprt1 t1 INNER JOIN fprt2 t2 ON (t1.a = t2.b) INNER JOIN fprt1 t3 ON (t2.b = t3.a) WHERE t1.a % 25 =0 ORDER BY 1,2,3;
3321
3322 -- left outer join + nullable clause
3323 EXPLAIN (VERBOSE, COSTS OFF)
3324 SELECT t1.a,t2.b,t2.c FROM fprt1 t1 LEFT JOIN (SELECT * FROM fprt2 WHERE a < 10) t2 ON (t1.a = t2.b and t1.b = t2.a) WHERE t1.a < 10 ORDER BY 1,2,3;
3325 SELECT t1.a,t2.b,t2.c FROM fprt1 t1 LEFT JOIN (SELECT * FROM fprt2 WHERE a < 10) t2 ON (t1.a = t2.b and t1.b = t2.a) WHERE t1.a < 10 ORDER BY 1,2,3;
3326
3327 -- with whole-row reference; partitionwise join does not apply
3328 EXPLAIN (COSTS OFF)
3329 SELECT t1.wr, t2.wr FROM (SELECT t1 wr, a FROM fprt1 t1 WHERE t1.a % 25 = 0) t1 FULL JOIN (SELECT t2 wr, b FROM fprt2 t2 WHERE t2.b % 25 = 0) t2 ON (t1.a = t2.b) ORDER BY 1,2;
3330 SELECT t1.wr, t2.wr FROM (SELECT t1 wr, a FROM fprt1 t1 WHERE t1.a % 25 = 0) t1 FULL JOIN (SELECT t2 wr, b FROM fprt2 t2 WHERE t2.b % 25 = 0) t2 ON (t1.a = t2.b) ORDER BY 1,2;
3331
3332 -- join with lateral reference
3333 EXPLAIN (COSTS OFF)
3334 SELECT t1.a,t1.b FROM fprt1 t1, LATERAL (SELECT t2.a, t2.b FROM fprt2 t2 WHERE t1.a = t2.b AND t1.b = t2.a) q WHERE t1.a%25 = 0 ORDER BY 1,2;
3335 SELECT t1.a,t1.b FROM fprt1 t1, LATERAL (SELECT t2.a, t2.b FROM fprt2 t2 WHERE t1.a = t2.b AND t1.b = t2.a) q WHERE t1.a%25 = 0 ORDER BY 1,2;
3336
3337 -- with PHVs, partitionwise join selected but no join pushdown
3338 EXPLAIN (COSTS OFF)
3339 SELECT t1.a, t1.phv, t2.b, t2.phv FROM (SELECT 't1_phv' phv, * FROM fprt1 WHERE a % 25 = 0) t1 FULL JOIN (SELECT 't2_phv' phv, * FROM fprt2 WHERE b % 25 = 0) t2 ON (t1.a = t2.b) ORDER BY t1.a, t2.b;
3340 SELECT t1.a, t1.phv, t2.b, t2.phv FROM (SELECT 't1_phv' phv, * FROM fprt1 WHERE a % 25 = 0) t1 FULL JOIN (SELECT 't2_phv' phv, * FROM fprt2 WHERE b % 25 = 0) t2 ON (t1.a = t2.b) ORDER BY t1.a, t2.b;
3341
3342 -- test FOR UPDATE; partitionwise join does not apply
3343 EXPLAIN (COSTS OFF)
3344 SELECT t1.a, t2.b FROM fprt1 t1 INNER JOIN fprt2 t2 ON (t1.a = t2.b) WHERE t1.a % 25 = 0 ORDER BY 1,2 FOR UPDATE OF t1;
3345 SELECT t1.a, t2.b FROM fprt1 t1 INNER JOIN fprt2 t2 ON (t1.a = t2.b) WHERE t1.a % 25 = 0 ORDER BY 1,2 FOR UPDATE OF t1;
3346
3347 RESET enable_partitionwise_join;
3348
3349
3350 -- ===================================================================
3351 -- test partitionwise aggregates
3352 -- ===================================================================
3353
3354 CREATE TABLE pagg_tab (a int, b int, c text) PARTITION BY RANGE(a);
3355
3356 CREATE TABLE pagg_tab_p1 (LIKE pagg_tab);
3357 CREATE TABLE pagg_tab_p2 (LIKE pagg_tab);
3358 CREATE TABLE pagg_tab_p3 (LIKE pagg_tab);
3359
3360 INSERT INTO pagg_tab_p1 SELECT i % 30, i % 50, to_char(i/30, 'FM0000') FROM generate_series(1, 3000) i WHERE (i % 30) < 10;
3361 INSERT INTO pagg_tab_p2 SELECT i % 30, i % 50, to_char(i/30, 'FM0000') FROM generate_series(1, 3000) i WHERE (i % 30) < 20 and (i % 30) >= 10;
3362 INSERT INTO pagg_tab_p3 SELECT i % 30, i % 50, to_char(i/30, 'FM0000') FROM generate_series(1, 3000) i WHERE (i % 30) < 30 and (i % 30) >= 20;
3363
3364 -- Create foreign partitions
3365 CREATE FOREIGN TABLE fpagg_tab_p1 PARTITION OF pagg_tab FOR VALUES FROM (0) TO (10) SERVER loopback OPTIONS (table_name 'pagg_tab_p1');
3366 CREATE FOREIGN TABLE fpagg_tab_p2 PARTITION OF pagg_tab FOR VALUES FROM (10) TO (20) SERVER loopback OPTIONS (table_name 'pagg_tab_p2');
3367 CREATE FOREIGN TABLE fpagg_tab_p3 PARTITION OF pagg_tab FOR VALUES FROM (20) TO (30) SERVER loopback OPTIONS (table_name 'pagg_tab_p3');
3368
3369 ANALYZE pagg_tab;
3370 ANALYZE fpagg_tab_p1;
3371 ANALYZE fpagg_tab_p2;
3372 ANALYZE fpagg_tab_p3;
3373
3374 -- When GROUP BY clause matches with PARTITION KEY.
3375 -- Plan with partitionwise aggregates is disabled
3376 SET enable_partitionwise_aggregate TO false;
3377 EXPLAIN (COSTS OFF)
3378 SELECT a, sum(b), min(b), count(*) FROM pagg_tab GROUP BY a HAVING avg(b) < 22 ORDER BY 1;
3379
3380 -- Plan with partitionwise aggregates is enabled
3381 SET enable_partitionwise_aggregate TO true;
3382 EXPLAIN (COSTS OFF)
3383 SELECT a, sum(b), min(b), count(*) FROM pagg_tab GROUP BY a HAVING avg(b) < 22 ORDER BY 1;
3384 SELECT a, sum(b), min(b), count(*) FROM pagg_tab GROUP BY a HAVING avg(b) < 22 ORDER BY 1;
3385
3386 -- Check with whole-row reference
3387 -- Should have all the columns in the target list for the given relation
3388 EXPLAIN (VERBOSE, COSTS OFF)
3389 SELECT a, count(t1) FROM pagg_tab t1 GROUP BY a HAVING avg(b) < 22 ORDER BY 1;
3390 SELECT a, count(t1) FROM pagg_tab t1 GROUP BY a HAVING avg(b) < 22 ORDER BY 1;
3391
3392 -- When GROUP BY clause does not match with PARTITION KEY.
3393 EXPLAIN (COSTS OFF)
3394 SELECT b, avg(a), max(a), count(*) FROM pagg_tab GROUP BY b HAVING sum(a) < 700 ORDER BY 1;
3395
3396 -- ===================================================================
3397 -- access rights and superuser
3398 -- ===================================================================
3399
3400 -- Non-superuser cannot create a FDW without a password in the connstr
3401 CREATE ROLE regress_nosuper NOSUPERUSER;
3402
3403 GRANT USAGE ON FOREIGN DATA WRAPPER postgres_fdw TO regress_nosuper;
3404
3405 SET ROLE regress_nosuper;
3406
3407 SHOW is_superuser;
3408
3409 -- This will be OK, we can create the FDW
3410 DO $d$
3411 BEGIN
3412 EXECUTE $$CREATE SERVER loopback_nopw FOREIGN DATA WRAPPER postgres_fdw
3413 OPTIONS (dbname '$$||current_database()||$$',
3414 port '$$||current_setting('port')||$$'
3415 )$$;
3416 END;
3417 $d$;
3418
3419 -- But creation of user mappings for non-superusers should fail
3420 CREATE USER MAPPING FOR public SERVER loopback_nopw;
3421 CREATE USER MAPPING FOR CURRENT_USER SERVER loopback_nopw;
3422
3423 CREATE FOREIGN TABLE pg_temp.ft1_nopw (
3424 c1 int NOT NULL,
3425 c2 int NOT NULL,
3426 c3 text,
3427 c4 timestamptz,
3428 c5 timestamp,
3429 c6 varchar(10),
3430 c7 char(10) default 'ft1',
3431 c8 user_enum
3432 ) SERVER loopback_nopw OPTIONS (schema_name 'public', table_name 'ft1');
3433
3434 SELECT 1 FROM ft1_nopw LIMIT 1;
3435
3436 -- If we add a password to the connstr it'll fail, because we don't allow passwords
3437 -- in connstrs only in user mappings.
3438
3439 ALTER SERVER loopback_nopw OPTIONS (ADD password 'dummypw');
3440
3441 -- If we add a password for our user mapping instead, we should get a different
3442 -- error because the password wasn't actually *used* when we run with trust auth.
3443 --
3444 -- This won't work with installcheck, but neither will most of the FDW checks.
3445
3446 ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (ADD password 'dummypw');
3447
3448 SELECT 1 FROM ft1_nopw LIMIT 1;
3449
3450 -- Unpriv user cannot make the mapping passwordless
3451 ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (ADD password_required 'false');
3452
3453
3454 SELECT 1 FROM ft1_nopw LIMIT 1;
3455
3456 RESET ROLE;
3457
3458 -- But the superuser can
3459 ALTER USER MAPPING FOR regress_nosuper SERVER loopback_nopw OPTIONS (ADD password_required 'false');
3460
3461 SET ROLE regress_nosuper;
3462
3463 -- Should finally work now
3464 SELECT 1 FROM ft1_nopw LIMIT 1;
3465
3466 -- unpriv user also cannot set sslcert / sslkey on the user mapping
3467 -- first set password_required so we see the right error messages
3468 ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (SET password_required 'true');
3469 ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (ADD sslcert 'foo.crt');
3470 ALTER USER MAPPING FOR CURRENT_USER SERVER loopback_nopw OPTIONS (ADD sslkey 'foo.key');
3471
3472 -- We're done with the role named after a specific user and need to check the
3473 -- changes to the public mapping.
3474 DROP USER MAPPING FOR CURRENT_USER SERVER loopback_nopw;
3475
3476 -- This will fail again as it'll resolve the user mapping for public, which
3477 -- lacks password_required=false
3478 SELECT 1 FROM ft1_nopw LIMIT 1;
3479
3480 RESET ROLE;
3481
3482 -- The user mapping for public is passwordless and lacks the password_required=false
3483 -- mapping option, but will work because the current user is a superuser.
3484 SELECT 1 FROM ft1_nopw LIMIT 1;
3485
3486 -- cleanup
3487 DROP USER MAPPING FOR public SERVER loopback_nopw;
3488 DROP OWNED BY regress_nosuper;
3489 DROP ROLE regress_nosuper;
3490
3491 -- Clean-up
3492 RESET enable_partitionwise_aggregate;
3493
3494 -- Two-phase transactions are not supported.
3495 BEGIN;
3496 SELECT count(*) FROM ft1;
3497 -- error here
3498 PREPARE TRANSACTION 'fdw_tpc';
3499 ROLLBACK;
3500
3501 -- ===================================================================
3502 -- reestablish new connection
3503 -- ===================================================================
3504
3505 -- Change application_name of remote connection to special one
3506 -- so that we can easily terminate the connection later.
3507 ALTER SERVER loopback OPTIONS (application_name 'fdw_retry_check');
3508
3509 -- Make sure we have a remote connection.
3510 SELECT 1 FROM ft1 LIMIT 1;
3511
3512 -- Terminate the remote connection and wait for the termination to complete.
3513 -- (If a cache flush happens, the remote connection might have already been
3514 -- dropped; so code this step in a way that doesn't fail if no connection.)
3515 DO $$ BEGIN
3516 PERFORM pg_terminate_backend(pid, 180000) FROM pg_stat_activity
3517 WHERE application_name = 'fdw_retry_check';
3518 END $$;
3519
3520 -- This query should detect the broken connection when starting new remote
3521 -- transaction, reestablish new connection, and then succeed.
3522 BEGIN;
3523 SELECT 1 FROM ft1 LIMIT 1;
3524
3525 -- If we detect the broken connection when starting a new remote
3526 -- subtransaction, we should fail instead of establishing a new connection.
3527 -- Terminate the remote connection and wait for the termination to complete.
3528 DO $$ BEGIN
3529 PERFORM pg_terminate_backend(pid, 180000) FROM pg_stat_activity
3530 WHERE application_name = 'fdw_retry_check';
3531 END $$;
3532 SAVEPOINT s;
3533 -- The text of the error might vary across platforms, so only show SQLSTATE.
3534 \set VERBOSITY sqlstate
3535 SELECT 1 FROM ft1 LIMIT 1; -- should fail
3536 \set VERBOSITY default
3537 COMMIT;
3538
3539 -- =============================================================================
3540 -- test connection invalidation cases and postgres_fdw_get_connections function
3541 -- =============================================================================
3542 -- Let's ensure to close all the existing cached connections.
3543 SELECT 1 FROM postgres_fdw_disconnect_all();
3544 -- No cached connections, so no records should be output.
3545 SELECT server_name FROM postgres_fdw_get_connections() ORDER BY 1;
3546 -- This test case is for closing the connection in pgfdw_xact_callback
3547 BEGIN;
3548 -- Connection xact depth becomes 1 i.e. the connection is in midst of the xact.
3549 SELECT 1 FROM ft1 LIMIT 1;
3550 SELECT 1 FROM ft7 LIMIT 1;
3551 -- List all the existing cached connections. loopback and loopback3 should be
3552 -- output.
3553 SELECT server_name FROM postgres_fdw_get_connections() ORDER BY 1;
3554 -- Connections are not closed at the end of the alter and drop statements.
3555 -- That's because the connections are in midst of this xact,
3556 -- they are just marked as invalid in pgfdw_inval_callback.
3557 ALTER SERVER loopback OPTIONS (ADD use_remote_estimate 'off');
3558 DROP SERVER loopback3 CASCADE;
3559 -- List all the existing cached connections. loopback and loopback3
3560 -- should be output as invalid connections. Also the server name and user name
3561 -- for loopback3 should be NULL because both server and user mapping were
3562 -- dropped. In this test, the PIDs of remote backends can be gathered from
3563 -- pg_stat_activity, and remote_backend_pid should match one of those PIDs.
3564 SELECT server_name, user_name = CURRENT_USER as "user_name = CURRENT_USER",
3565 valid, used_in_xact, closed,
3566 remote_backend_pid = ANY(SELECT pid FROM pg_stat_activity
3567 WHERE backend_type = 'client backend' AND pid <> pg_backend_pid())
3568 as remote_backend_pid
3569 FROM postgres_fdw_get_connections() ORDER BY 1;
3570 -- The invalid connections get closed in pgfdw_xact_callback during commit.
3571 COMMIT;
3572 -- All cached connections were closed while committing above xact, so no
3573 -- records should be output.
3574 SELECT server_name FROM postgres_fdw_get_connections() ORDER BY 1;
3575
3576 -- =======================================================================
3577 -- test postgres_fdw_disconnect and postgres_fdw_disconnect_all functions
3578 -- =======================================================================
3579 BEGIN;
3580 -- Ensure to cache loopback connection.
3581 SELECT 1 FROM ft1 LIMIT 1;
3582 -- Ensure to cache loopback2 connection.
3583 SELECT 1 FROM ft6 LIMIT 1;
3584 -- List all the existing cached connections. loopback and loopback2 should be
3585 -- output.
3586 SELECT server_name FROM postgres_fdw_get_connections() ORDER BY 1;
3587 -- Issue a warning and return false as loopback connection is still in use and
3588 -- can not be closed.
3589 SELECT postgres_fdw_disconnect('loopback');
3590 -- List all the existing cached connections. loopback and loopback2 should be
3591 -- output.
3592 SELECT server_name FROM postgres_fdw_get_connections() ORDER BY 1;
3593 -- Return false as connections are still in use, warnings are issued.
3594 -- But disable warnings temporarily because the order of them is not stable.
3595 SET client_min_messages = 'ERROR';
3596 SELECT postgres_fdw_disconnect_all();
3597 RESET client_min_messages;
3598 COMMIT;
3599 -- Ensure that loopback2 connection is closed.
3600 SELECT 1 FROM postgres_fdw_disconnect('loopback2');
3601 SELECT server_name FROM postgres_fdw_get_connections() WHERE server_name = 'loopback2';
3602 -- Return false as loopback2 connection is closed already.
3603 SELECT postgres_fdw_disconnect('loopback2');
3604 -- Return an error as there is no foreign server with given name.
3605 SELECT postgres_fdw_disconnect('unknownserver');
3606 -- Let's ensure to close all the existing cached connections.
3607 SELECT 1 FROM postgres_fdw_disconnect_all();
3608 -- No cached connections, so no records should be output.
3609 SELECT server_name FROM postgres_fdw_get_connections() ORDER BY 1;
3610
3611 -- =============================================================================
3612 -- test case for having multiple cached connections for a foreign server
3613 -- =============================================================================
3614 CREATE ROLE regress_multi_conn_user1 SUPERUSER;
3615 CREATE ROLE regress_multi_conn_user2 SUPERUSER;
3616 CREATE USER MAPPING FOR regress_multi_conn_user1 SERVER loopback;
3617 CREATE USER MAPPING FOR regress_multi_conn_user2 SERVER loopback;
3618
3619 BEGIN;
3620 -- Will cache loopback connection with user mapping for regress_multi_conn_user1
3621 SET ROLE regress_multi_conn_user1;
3622 SELECT 1 FROM ft1 LIMIT 1;
3623 RESET ROLE;
3624
3625 -- Will cache loopback connection with user mapping for regress_multi_conn_user2
3626 SET ROLE regress_multi_conn_user2;
3627 SELECT 1 FROM ft1 LIMIT 1;
3628 RESET ROLE;
3629
3630 -- Should output two connections for loopback server
3631 SELECT server_name FROM postgres_fdw_get_connections() ORDER BY 1;
3632 COMMIT;
3633 -- Let's ensure to close all the existing cached connections.
3634 SELECT 1 FROM postgres_fdw_disconnect_all();
3635 -- No cached connections, so no records should be output.
3636 SELECT server_name FROM postgres_fdw_get_connections() ORDER BY 1;
3637
3638 -- Clean up
3639 DROP USER MAPPING FOR regress_multi_conn_user1 SERVER loopback;
3640 DROP USER MAPPING FOR regress_multi_conn_user2 SERVER loopback;
3641 DROP ROLE regress_multi_conn_user1;
3642 DROP ROLE regress_multi_conn_user2;
3643
3644 -- ===================================================================
3645 -- Test foreign server level option keep_connections
3646 -- ===================================================================
3647 -- By default, the connections associated with foreign server are cached i.e.
3648 -- keep_connections option is on. Set it to off.
3649 ALTER SERVER loopback OPTIONS (keep_connections 'off');
3650 -- connection to loopback server is closed at the end of xact
3651 -- as keep_connections was set to off.
3652 SELECT 1 FROM ft1 LIMIT 1;
3653 -- No cached connections, so no records should be output.
3654 SELECT server_name FROM postgres_fdw_get_connections() ORDER BY 1;
3655 ALTER SERVER loopback OPTIONS (SET keep_connections 'on');
3656
3657 -- ===================================================================
3658 -- batch insert
3659 -- ===================================================================
3660
3661 BEGIN;
3662
3663 CREATE SERVER batch10 FOREIGN DATA WRAPPER postgres_fdw OPTIONS( batch_size '10' );
3664
3665 SELECT count(*)
3666 FROM pg_foreign_server
3667 WHERE srvname = 'batch10'
3668 AND srvoptions @> array['batch_size=10'];
3669
3670 ALTER SERVER batch10 OPTIONS( SET batch_size '20' );
3671
3672 SELECT count(*)
3673 FROM pg_foreign_server
3674 WHERE srvname = 'batch10'
3675 AND srvoptions @> array['batch_size=10'];
3676
3677 SELECT count(*)
3678 FROM pg_foreign_server
3679 WHERE srvname = 'batch10'
3680 AND srvoptions @> array['batch_size=20'];
3681
3682 CREATE FOREIGN TABLE table30 ( x int ) SERVER batch10 OPTIONS ( batch_size '30' );
3683
3684 SELECT COUNT(*)
3685 FROM pg_foreign_table
3686 WHERE ftrelid = 'table30'::regclass
3687 AND ftoptions @> array['batch_size=30'];
3688
3689 ALTER FOREIGN TABLE table30 OPTIONS ( SET batch_size '40');
3690
3691 SELECT COUNT(*)
3692 FROM pg_foreign_table
3693 WHERE ftrelid = 'table30'::regclass
3694 AND ftoptions @> array['batch_size=30'];
3695
3696 SELECT COUNT(*)
3697 FROM pg_foreign_table
3698 WHERE ftrelid = 'table30'::regclass
3699 AND ftoptions @> array['batch_size=40'];
3700
3701 ROLLBACK;
3702
3703 CREATE TABLE batch_table ( x int );
3704
3705 CREATE FOREIGN TABLE ftable ( x int ) SERVER loopback OPTIONS ( table_name 'batch_table', batch_size '10' );
3706 EXPLAIN (VERBOSE, COSTS OFF) INSERT INTO ftable SELECT * FROM generate_series(1, 10) i;
3707 INSERT INTO ftable SELECT * FROM generate_series(1, 10) i;
3708 INSERT INTO ftable SELECT * FROM generate_series(11, 31) i;
3709 INSERT INTO ftable VALUES (32);
3710 INSERT INTO ftable VALUES (33), (34);
3711 SELECT COUNT(*) FROM ftable;
3712 TRUNCATE batch_table;
3713 DROP FOREIGN TABLE ftable;
3714
3715 -- Disable batch insert
3716 CREATE FOREIGN TABLE ftable ( x int ) SERVER loopback OPTIONS ( table_name 'batch_table', batch_size '1' );
3717 EXPLAIN (VERBOSE, COSTS OFF) INSERT INTO ftable VALUES (1), (2);
3718 INSERT INTO ftable VALUES (1), (2);
3719 SELECT COUNT(*) FROM ftable;
3720
3721 -- Disable batch inserting into foreign tables with BEFORE ROW INSERT triggers
3722 -- even if the batch_size option is enabled.
3723 ALTER FOREIGN TABLE ftable OPTIONS ( SET batch_size '10' );
3724 CREATE TRIGGER trig_row_before BEFORE INSERT ON ftable
3725 FOR EACH ROW EXECUTE PROCEDURE trigger_data(23,'skidoo');
3726 EXPLAIN (VERBOSE, COSTS OFF) INSERT INTO ftable VALUES (3), (4);
3727 INSERT INTO ftable VALUES (3), (4);
3728 SELECT COUNT(*) FROM ftable;
3729
3730 -- Clean up
3731 DROP TRIGGER trig_row_before ON ftable;
3732 DROP FOREIGN TABLE ftable;
3733 DROP TABLE batch_table;
3734
3735 -- Use partitioning
3736 CREATE TABLE batch_table ( x int ) PARTITION BY HASH (x);
3737
3738 CREATE TABLE batch_table_p0 (LIKE batch_table);
3739 CREATE FOREIGN TABLE batch_table_p0f
3740 PARTITION OF batch_table
3741 FOR VALUES WITH (MODULUS 3, REMAINDER 0)
3742 SERVER loopback
3743 OPTIONS (table_name 'batch_table_p0', batch_size '10');
3744
3745 CREATE TABLE batch_table_p1 (LIKE batch_table);
3746 CREATE FOREIGN TABLE batch_table_p1f
3747 PARTITION OF batch_table
3748 FOR VALUES WITH (MODULUS 3, REMAINDER 1)
3749 SERVER loopback
3750 OPTIONS (table_name 'batch_table_p1', batch_size '1');
3751
3752 CREATE TABLE batch_table_p2
3753 PARTITION OF batch_table
3754 FOR VALUES WITH (MODULUS 3, REMAINDER 2);
3755
3756 INSERT INTO batch_table SELECT * FROM generate_series(1, 66) i;
3757 SELECT COUNT(*) FROM batch_table;
3758
3759 -- Clean up
3760 DROP TABLE batch_table;
3761 DROP TABLE batch_table_p0;
3762 DROP TABLE batch_table_p1;
3763
3764 -- Check that batched mode also works for some inserts made during
3765 -- cross-partition updates
3766 CREATE TABLE batch_cp_upd_test (a int) PARTITION BY LIST (a);
3767 CREATE TABLE batch_cp_upd_test1 (LIKE batch_cp_upd_test);
3768 CREATE FOREIGN TABLE batch_cp_upd_test1_f
3769 PARTITION OF batch_cp_upd_test
3770 FOR VALUES IN (1)
3771 SERVER loopback
3772 OPTIONS (table_name 'batch_cp_upd_test1', batch_size '10');
3773 CREATE TABLE batch_cp_upd_test2 PARTITION OF batch_cp_upd_test
3774 FOR VALUES IN (2);
3775 CREATE TABLE batch_cp_upd_test3 (LIKE batch_cp_upd_test);
3776 CREATE FOREIGN TABLE batch_cp_upd_test3_f
3777 PARTITION OF batch_cp_upd_test
3778 FOR VALUES IN (3)
3779 SERVER loopback
3780 OPTIONS (table_name 'batch_cp_upd_test3', batch_size '1');
3781
3782 -- Create statement triggers on remote tables that "log" any INSERTs
3783 -- performed on them.
3784 CREATE TABLE cmdlog (cmd text);
3785 CREATE FUNCTION log_stmt() RETURNS TRIGGER LANGUAGE plpgsql AS $$
3786 BEGIN INSERT INTO public.cmdlog VALUES (TG_OP || ' on ' || TG_RELNAME); RETURN NULL; END;
3787 $$;
3788 CREATE TRIGGER stmt_trig AFTER INSERT ON batch_cp_upd_test1
3789 FOR EACH STATEMENT EXECUTE FUNCTION log_stmt();
3790 CREATE TRIGGER stmt_trig AFTER INSERT ON batch_cp_upd_test3
3791 FOR EACH STATEMENT EXECUTE FUNCTION log_stmt();
3792
3793 -- This update moves rows from the local partition 'batch_cp_upd_test2' to the
3794 -- foreign partition 'batch_cp_upd_test1', one that has insert batching
3795 -- enabled, so a single INSERT for both rows.
3796 INSERT INTO batch_cp_upd_test VALUES (2), (2);
3797 UPDATE batch_cp_upd_test t SET a = 1 FROM (VALUES (1), (2)) s(a) WHERE t.a = s.a AND s.a = 2;
3798
3799 -- This one moves rows from the local partition 'batch_cp_upd_test2' to the
3800 -- foreign partition 'batch_cp_upd_test2', one that has insert batching
3801 -- disabled, so separate INSERTs for the two rows.
3802 INSERT INTO batch_cp_upd_test VALUES (2), (2);
3803 UPDATE batch_cp_upd_test t SET a = 3 FROM (VALUES (1), (2)) s(a) WHERE t.a = s.a AND s.a = 2;
3804
3805 SELECT tableoid::regclass, * FROM batch_cp_upd_test ORDER BY 1;
3806
3807 -- Should see 1 INSERT on batch_cp_upd_test1 and 2 on batch_cp_upd_test3 as
3808 -- described above.
3809 SELECT * FROM cmdlog ORDER BY 1;
3810
3811 -- Clean up
3812 DROP TABLE batch_cp_upd_test;
3813 DROP TABLE batch_cp_upd_test1;
3814 DROP TABLE batch_cp_upd_test3;
3815 DROP TABLE cmdlog;
3816 DROP FUNCTION log_stmt();
3817
3818 -- Use partitioning
3819 ALTER SERVER loopback OPTIONS (ADD batch_size '10');
3820
3821 CREATE TABLE batch_table ( x int, field1 text, field2 text) PARTITION BY HASH (x);
3822
3823 CREATE TABLE batch_table_p0 (LIKE batch_table);
3824 ALTER TABLE batch_table_p0 ADD CONSTRAINT p0_pkey PRIMARY KEY (x);
3825 CREATE FOREIGN TABLE batch_table_p0f
3826 PARTITION OF batch_table
3827 FOR VALUES WITH (MODULUS 2, REMAINDER 0)
3828 SERVER loopback
3829 OPTIONS (table_name 'batch_table_p0');
3830
3831 CREATE TABLE batch_table_p1 (LIKE batch_table);
3832 ALTER TABLE batch_table_p1 ADD CONSTRAINT p1_pkey PRIMARY KEY (x);
3833 CREATE FOREIGN TABLE batch_table_p1f
3834 PARTITION OF batch_table
3835 FOR VALUES WITH (MODULUS 2, REMAINDER 1)
3836 SERVER loopback
3837 OPTIONS (table_name 'batch_table_p1');
3838
3839 INSERT INTO batch_table SELECT i, 'test'||i, 'test'|| i FROM generate_series(1, 50) i;
3840 SELECT COUNT(*) FROM batch_table;
3841 SELECT * FROM batch_table ORDER BY x;
3842
3843 -- Clean up
3844 DROP TABLE batch_table;
3845 DROP TABLE batch_table_p0;
3846 DROP TABLE batch_table_p1;
3847
3848 ALTER SERVER loopback OPTIONS (DROP batch_size);
3849
3850 -- Test that pending inserts are handled properly when needed
3851 CREATE TABLE batch_table (a text, b int);
3852 CREATE FOREIGN TABLE ftable (a text, b int)
3853 SERVER loopback
3854 OPTIONS (table_name 'batch_table', batch_size '2');
3855 CREATE TABLE ltable (a text, b int);
3856 CREATE FUNCTION ftable_rowcount_trigf() RETURNS trigger LANGUAGE plpgsql AS
3857 $$
3858 begin
3859 raise notice '%: there are % rows in ftable',
3860 TG_NAME, (SELECT count(*) FROM ftable);
3861 if TG_OP = 'DELETE' then
3862 return OLD;
3863 else
3864 return NEW;
3865 end if;
3866 end;
3867 $$;
3868 CREATE TRIGGER ftable_rowcount_trigger
3869 BEFORE INSERT OR UPDATE OR DELETE ON ltable
3870 FOR EACH ROW EXECUTE PROCEDURE ftable_rowcount_trigf();
3871
3872 WITH t AS (
3873 INSERT INTO ltable VALUES ('AAA', 42), ('BBB', 42) RETURNING *
3874 )
3875 INSERT INTO ftable SELECT * FROM t;
3876
3877 SELECT * FROM ltable;
3878 SELECT * FROM ftable;
3879 DELETE FROM ftable;
3880
3881 WITH t AS (
3882 UPDATE ltable SET b = b + 100 RETURNING *
3883 )
3884 INSERT INTO ftable SELECT * FROM t;
3885
3886 SELECT * FROM ltable;
3887 SELECT * FROM ftable;
3888 DELETE FROM ftable;
3889
3890 WITH t AS (
3891 DELETE FROM ltable RETURNING *
3892 )
3893 INSERT INTO ftable SELECT * FROM t;
3894
3895 SELECT * FROM ltable;
3896 SELECT * FROM ftable;
3897 DELETE FROM ftable;
3898
3899 -- Clean up
3900 DROP FOREIGN TABLE ftable;
3901 DROP TABLE batch_table;
3902 DROP TRIGGER ftable_rowcount_trigger ON ltable;
3903 DROP TABLE ltable;
3904
3905 CREATE TABLE parent (a text, b int) PARTITION BY LIST (a);
3906 CREATE TABLE batch_table (a text, b int);
3907 CREATE FOREIGN TABLE ftable
3908 PARTITION OF parent
3909 FOR VALUES IN ('AAA')
3910 SERVER loopback
3911 OPTIONS (table_name 'batch_table', batch_size '2');
3912 CREATE TABLE ltable
3913 PARTITION OF parent
3914 FOR VALUES IN ('BBB');
3915 CREATE TRIGGER ftable_rowcount_trigger
3916 BEFORE INSERT ON ltable
3917 FOR EACH ROW EXECUTE PROCEDURE ftable_rowcount_trigf();
3918
3919 INSERT INTO parent VALUES ('AAA', 42), ('BBB', 42), ('AAA', 42), ('BBB', 42);
3920
3921 SELECT tableoid::regclass, * FROM parent;
3922
3923 -- Clean up
3924 DROP FOREIGN TABLE ftable;
3925 DROP TABLE batch_table;
3926 DROP TRIGGER ftable_rowcount_trigger ON ltable;
3927 DROP TABLE ltable;
3928 DROP TABLE parent;
3929 DROP FUNCTION ftable_rowcount_trigf;
3930
3931 -- ===================================================================
3932 -- test asynchronous execution
3933 -- ===================================================================
3934
3935 ALTER SERVER loopback OPTIONS (DROP extensions);
3936 ALTER SERVER loopback OPTIONS (ADD async_capable 'true');
3937 ALTER SERVER loopback2 OPTIONS (ADD async_capable 'true');
3938
3939 CREATE TABLE async_pt (a int, b int, c text) PARTITION BY RANGE (a);
3940 CREATE TABLE base_tbl1 (a int, b int, c text);
3941 CREATE TABLE base_tbl2 (a int, b int, c text);
3942 CREATE FOREIGN TABLE async_p1 PARTITION OF async_pt FOR VALUES FROM (1000) TO (2000)
3943 SERVER loopback OPTIONS (table_name 'base_tbl1');
3944 CREATE FOREIGN TABLE async_p2 PARTITION OF async_pt FOR VALUES FROM (2000) TO (3000)
3945 SERVER loopback2 OPTIONS (table_name 'base_tbl2');
3946 INSERT INTO async_p1 SELECT 1000 + i, i, to_char(i, 'FM0000') FROM generate_series(0, 999, 5) i;
3947 INSERT INTO async_p2 SELECT 2000 + i, i, to_char(i, 'FM0000') FROM generate_series(0, 999, 5) i;
3948 ANALYZE async_pt;
3949
3950 -- simple queries
3951 CREATE TABLE result_tbl (a int, b int, c text);
3952
3953 EXPLAIN (VERBOSE, COSTS OFF)
3954 INSERT INTO result_tbl SELECT * FROM async_pt WHERE b % 100 = 0;
3955 INSERT INTO result_tbl SELECT * FROM async_pt WHERE b % 100 = 0;
3956
3957 SELECT * FROM result_tbl ORDER BY a;
3958 DELETE FROM result_tbl;
3959
3960 EXPLAIN (VERBOSE, COSTS OFF)
3961 INSERT INTO result_tbl SELECT * FROM async_pt WHERE b === 505;
3962 INSERT INTO result_tbl SELECT * FROM async_pt WHERE b === 505;
3963
3964 SELECT * FROM result_tbl ORDER BY a;
3965 DELETE FROM result_tbl;
3966
3967 EXPLAIN (VERBOSE, COSTS OFF)
3968 INSERT INTO result_tbl SELECT a, b, 'AAA' || c FROM async_pt WHERE b === 505;
3969 INSERT INTO result_tbl SELECT a, b, 'AAA' || c FROM async_pt WHERE b === 505;
3970
3971 SELECT * FROM result_tbl ORDER BY a;
3972 DELETE FROM result_tbl;
3973
3974 -- Test error handling, if accessing one of the foreign partitions errors out
3975 CREATE FOREIGN TABLE async_p_broken PARTITION OF async_pt FOR VALUES FROM (10000) TO (10001)
3976 SERVER loopback OPTIONS (table_name 'non_existent_table');
3977 SELECT * FROM async_pt;
3978 DROP FOREIGN TABLE async_p_broken;
3979
3980 -- Check case where multiple partitions use the same connection
3981 CREATE TABLE base_tbl3 (a int, b int, c text);
3982 CREATE FOREIGN TABLE async_p3 PARTITION OF async_pt FOR VALUES FROM (3000) TO (4000)
3983 SERVER loopback2 OPTIONS (table_name 'base_tbl3');
3984 INSERT INTO async_p3 SELECT 3000 + i, i, to_char(i, 'FM0000') FROM generate_series(0, 999, 5) i;
3985 ANALYZE async_pt;
3986
3987 EXPLAIN (VERBOSE, COSTS OFF)
3988 INSERT INTO result_tbl SELECT * FROM async_pt WHERE b === 505;
3989 INSERT INTO result_tbl SELECT * FROM async_pt WHERE b === 505;
3990
3991 SELECT * FROM result_tbl ORDER BY a;
3992 DELETE FROM result_tbl;
3993
3994 -- Test ExecAppendAsyncReset code path that drains outstanding async requests
3995 -- (case where subplans are re-scanned with parameter changes)
3996 EXPLAIN (VERBOSE, COSTS OFF)
3997 SELECT o.x FROM (VALUES (2505), (3505)) o(x), LATERAL (SELECT a FROM async_pt WHERE a = o.x OR a = 1505 LIMIT 1) s ORDER BY o.x;
3998 SELECT o.x FROM (VALUES (2505), (3505)) o(x), LATERAL (SELECT a FROM async_pt WHERE a = o.x OR a = 1505 LIMIT 1) s ORDER BY o.x;
3999
4000 EXPLAIN (VERBOSE, COSTS OFF)
4001 SELECT o.x FROM (VALUES (2505), (3505)) o(x), LATERAL (SELECT a FROM async_pt WHERE a = o.x OR (a = 1505 AND o.x = 2505) LIMIT 1) s ORDER BY o.x;
4002 SELECT o.x FROM (VALUES (2505), (3505)) o(x), LATERAL (SELECT a FROM async_pt WHERE a = o.x OR (a = 1505 AND o.x = 2505) LIMIT 1) s ORDER BY o.x;
4003
4004 DROP FOREIGN TABLE async_p3;
4005 DROP TABLE base_tbl3;
4006
4007 -- Check case where the partitioned table has local/remote partitions
4008 CREATE TABLE async_p3 PARTITION OF async_pt FOR VALUES FROM (3000) TO (4000);
4009 INSERT INTO async_p3 SELECT 3000 + i, i, to_char(i, 'FM0000') FROM generate_series(0, 999, 5) i;
4010 ANALYZE async_pt;
4011
4012 EXPLAIN (VERBOSE, COSTS OFF)
4013 INSERT INTO result_tbl SELECT * FROM async_pt WHERE b === 505;
4014 INSERT INTO result_tbl SELECT * FROM async_pt WHERE b === 505;
4015
4016 SELECT * FROM result_tbl ORDER BY a;
4017 DELETE FROM result_tbl;
4018
4019 -- partitionwise joins
4020 SET enable_partitionwise_join TO true;
4021
4022 CREATE TABLE join_tbl (a1 int, b1 int, c1 text, a2 int, b2 int, c2 text);
4023
4024 EXPLAIN (VERBOSE, COSTS OFF)
4025 INSERT INTO join_tbl SELECT * FROM async_pt t1, async_pt t2 WHERE t1.a = t2.a AND t1.b = t2.b AND t1.b % 100 = 0;
4026 INSERT INTO join_tbl SELECT * FROM async_pt t1, async_pt t2 WHERE t1.a = t2.a AND t1.b = t2.b AND t1.b % 100 = 0;
4027
4028 SELECT * FROM join_tbl ORDER BY a1;
4029 DELETE FROM join_tbl;
4030
4031 EXPLAIN (VERBOSE, COSTS OFF)
4032 INSERT INTO join_tbl SELECT t1.a, t1.b, 'AAA' || t1.c, t2.a, t2.b, 'AAA' || t2.c FROM async_pt t1, async_pt t2 WHERE t1.a = t2.a AND t1.b = t2.b AND t1.b % 100 = 0;
4033 INSERT INTO join_tbl SELECT t1.a, t1.b, 'AAA' || t1.c, t2.a, t2.b, 'AAA' || t2.c FROM async_pt t1, async_pt t2 WHERE t1.a = t2.a AND t1.b = t2.b AND t1.b % 100 = 0;
4034
4035 SELECT * FROM join_tbl ORDER BY a1;
4036 DELETE FROM join_tbl;
4037
4038 RESET enable_partitionwise_join;
4039
4040 -- Test rescan of an async Append node with do_exec_prune=false
4041 SET enable_hashjoin TO false;
4042
4043 EXPLAIN (VERBOSE, COSTS OFF)
4044 INSERT INTO join_tbl SELECT * FROM async_p1 t1, async_pt t2 WHERE t1.a = t2.a AND t1.b = t2.b AND t1.b % 100 = 0;
4045 INSERT INTO join_tbl SELECT * FROM async_p1 t1, async_pt t2 WHERE t1.a = t2.a AND t1.b = t2.b AND t1.b % 100 = 0;
4046
4047 SELECT * FROM join_tbl ORDER BY a1;
4048 DELETE FROM join_tbl;
4049
4050 RESET enable_hashjoin;
4051
4052 -- Test interaction of async execution with plan-time partition pruning
4053 EXPLAIN (VERBOSE, COSTS OFF)
4054 SELECT * FROM async_pt WHERE a < 3000;
4055
4056 EXPLAIN (VERBOSE, COSTS OFF)
4057 SELECT * FROM async_pt WHERE a < 2000;
4058
4059 -- Test interaction of async execution with run-time partition pruning
4060 SET plan_cache_mode TO force_generic_plan;
4061
4062 PREPARE async_pt_query (int, int) AS
4063 INSERT INTO result_tbl SELECT * FROM async_pt WHERE a < $1 AND b === $2;
4064
4065 EXPLAIN (VERBOSE, COSTS OFF)
4066 EXECUTE async_pt_query (3000, 505);
4067 EXECUTE async_pt_query (3000, 505);
4068
4069 SELECT * FROM result_tbl ORDER BY a;
4070 DELETE FROM result_tbl;
4071
4072 EXPLAIN (VERBOSE, COSTS OFF)
4073 EXECUTE async_pt_query (2000, 505);
4074 EXECUTE async_pt_query (2000, 505);
4075
4076 SELECT * FROM result_tbl ORDER BY a;
4077 DELETE FROM result_tbl;
4078
4079 RESET plan_cache_mode;
4080
4081 CREATE TABLE local_tbl(a int, b int, c text);
4082 INSERT INTO local_tbl VALUES (1505, 505, 'foo'), (2505, 505, 'bar');
4083 ANALYZE local_tbl;
4084
4085 CREATE INDEX base_tbl1_idx ON base_tbl1 (a);
4086 CREATE INDEX base_tbl2_idx ON base_tbl2 (a);
4087 CREATE INDEX async_p3_idx ON async_p3 (a);
4088 ANALYZE base_tbl1;
4089 ANALYZE base_tbl2;
4090 ANALYZE async_p3;
4091
4092 ALTER FOREIGN TABLE async_p1 OPTIONS (use_remote_estimate 'true');
4093 ALTER FOREIGN TABLE async_p2 OPTIONS (use_remote_estimate 'true');
4094
4095 EXPLAIN (VERBOSE, COSTS OFF)
4096 SELECT * FROM local_tbl, async_pt WHERE local_tbl.a = async_pt.a AND local_tbl.c = 'bar';
4097 EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF)
4098 SELECT * FROM local_tbl, async_pt WHERE local_tbl.a = async_pt.a AND local_tbl.c = 'bar';
4099 SELECT * FROM local_tbl, async_pt WHERE local_tbl.a = async_pt.a AND local_tbl.c = 'bar';
4100
4101 ALTER FOREIGN TABLE async_p1 OPTIONS (DROP use_remote_estimate);
4102 ALTER FOREIGN TABLE async_p2 OPTIONS (DROP use_remote_estimate);
4103
4104 DROP TABLE local_tbl;
4105 DROP INDEX base_tbl1_idx;
4106 DROP INDEX base_tbl2_idx;
4107 DROP INDEX async_p3_idx;
4108
4109 -- UNION queries
4110 SET enable_sort TO off;
4111 SET enable_incremental_sort TO off;
4112 -- Adjust fdw_startup_cost so that we get an unordered path in the Append.
4113 ALTER SERVER loopback2 OPTIONS (ADD fdw_startup_cost '0.00');
4114
4115 EXPLAIN (VERBOSE, COSTS OFF)
4116 INSERT INTO result_tbl
4117 (SELECT a, b, 'AAA' || c FROM async_p1 ORDER BY a LIMIT 10)
4118 UNION
4119 (SELECT a, b, 'AAA' || c FROM async_p2 WHERE b < 10);
4120 INSERT INTO result_tbl
4121 (SELECT a, b, 'AAA' || c FROM async_p1 ORDER BY a LIMIT 10)
4122 UNION
4123 (SELECT a, b, 'AAA' || c FROM async_p2 WHERE b < 10);
4124
4125 SELECT * FROM result_tbl ORDER BY a;
4126 DELETE FROM result_tbl;
4127
4128 EXPLAIN (VERBOSE, COSTS OFF)
4129 INSERT INTO result_tbl
4130 (SELECT a, b, 'AAA' || c FROM async_p1 ORDER BY a LIMIT 10)
4131 UNION ALL
4132 (SELECT a, b, 'AAA' || c FROM async_p2 WHERE b < 10);
4133 INSERT INTO result_tbl
4134 (SELECT a, b, 'AAA' || c FROM async_p1 ORDER BY a LIMIT 10)
4135 UNION ALL
4136 (SELECT a, b, 'AAA' || c FROM async_p2 WHERE b < 10);
4137
4138 SELECT * FROM result_tbl ORDER BY a;
4139 DELETE FROM result_tbl;
4140
4141 RESET enable_incremental_sort;
4142 RESET enable_sort;
4143 ALTER SERVER loopback2 OPTIONS (DROP fdw_startup_cost);
4144
4145 -- Disable async execution if we use gating Result nodes for pseudoconstant
4146 -- quals
4147 EXPLAIN (VERBOSE, COSTS OFF)
4148 SELECT * FROM async_pt WHERE CURRENT_USER = SESSION_USER;
4149
4150 EXPLAIN (VERBOSE, COSTS OFF)
4151 (SELECT * FROM async_p1 WHERE CURRENT_USER = SESSION_USER)
4152 UNION ALL
4153 (SELECT * FROM async_p2 WHERE CURRENT_USER = SESSION_USER);
4154
4155 EXPLAIN (VERBOSE, COSTS OFF)
4156 SELECT * FROM ((SELECT * FROM async_p1 WHERE b < 10) UNION ALL (SELECT * FROM async_p2 WHERE b < 10)) s WHERE CURRENT_USER = SESSION_USER;
4157
4158 -- Test that pending requests are processed properly
4159 SET enable_mergejoin TO false;
4160 SET enable_hashjoin TO false;
4161
4162 EXPLAIN (VERBOSE, COSTS OFF)
4163 SELECT * FROM async_pt t1, async_p2 t2 WHERE t1.a = t2.a AND t1.b === 505;
4164 SELECT * FROM async_pt t1, async_p2 t2 WHERE t1.a = t2.a AND t1.b === 505;
4165
4166 CREATE TABLE local_tbl (a int, b int, c text);
4167 INSERT INTO local_tbl VALUES (1505, 505, 'foo');
4168 ANALYZE local_tbl;
4169
4170 EXPLAIN (VERBOSE, COSTS OFF)
4171 SELECT * FROM local_tbl t1 LEFT JOIN (SELECT *, (SELECT count(*) FROM async_pt WHERE a < 3000) FROM async_pt WHERE a < 3000) t2 ON t1.a = t2.a;
4172 EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF)
4173 SELECT * FROM local_tbl t1 LEFT JOIN (SELECT *, (SELECT count(*) FROM async_pt WHERE a < 3000) FROM async_pt WHERE a < 3000) t2 ON t1.a = t2.a;
4174 SELECT * FROM local_tbl t1 LEFT JOIN (SELECT *, (SELECT count(*) FROM async_pt WHERE a < 3000) FROM async_pt WHERE a < 3000) t2 ON t1.a = t2.a;
4175
4176 EXPLAIN (VERBOSE, COSTS OFF)
4177 SELECT * FROM async_pt t1 WHERE t1.b === 505 LIMIT 1;
4178 EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF)
4179 SELECT * FROM async_pt t1 WHERE t1.b === 505 LIMIT 1;
4180 SELECT * FROM async_pt t1 WHERE t1.b === 505 LIMIT 1;
4181
4182 -- Check with foreign modify
4183 CREATE TABLE base_tbl3 (a int, b int, c text);
4184 CREATE FOREIGN TABLE remote_tbl (a int, b int, c text)
4185 SERVER loopback OPTIONS (table_name 'base_tbl3');
4186 INSERT INTO remote_tbl VALUES (2505, 505, 'bar');
4187
4188 CREATE TABLE base_tbl4 (a int, b int, c text);
4189 CREATE FOREIGN TABLE insert_tbl (a int, b int, c text)
4190 SERVER loopback OPTIONS (table_name 'base_tbl4');
4191
4192 EXPLAIN (VERBOSE, COSTS OFF)
4193 INSERT INTO insert_tbl (SELECT * FROM local_tbl UNION ALL SELECT * FROM remote_tbl);
4194 INSERT INTO insert_tbl (SELECT * FROM local_tbl UNION ALL SELECT * FROM remote_tbl);
4195
4196 SELECT * FROM insert_tbl ORDER BY a;
4197
4198 -- Check with direct modify
4199 EXPLAIN (VERBOSE, COSTS OFF)
4200 WITH t AS (UPDATE remote_tbl SET c = c || c RETURNING *)
4201 INSERT INTO join_tbl SELECT * FROM async_pt LEFT JOIN t ON (async_pt.a = t.a AND async_pt.b = t.b) WHERE async_pt.b === 505;
4202 WITH t AS (UPDATE remote_tbl SET c = c || c RETURNING *)
4203 INSERT INTO join_tbl SELECT * FROM async_pt LEFT JOIN t ON (async_pt.a = t.a AND async_pt.b = t.b) WHERE async_pt.b === 505;
4204
4205 SELECT * FROM join_tbl ORDER BY a1;
4206 DELETE FROM join_tbl;
4207
4208 DROP TABLE local_tbl;
4209 DROP FOREIGN TABLE remote_tbl;
4210 DROP FOREIGN TABLE insert_tbl;
4211 DROP TABLE base_tbl3;
4212 DROP TABLE base_tbl4;
4213
4214 RESET enable_mergejoin;
4215 RESET enable_hashjoin;
4216
4217 -- Test that UPDATE/DELETE with inherited target works with async_capable enabled
4218 EXPLAIN (VERBOSE, COSTS OFF)
4219 UPDATE async_pt SET c = c || c WHERE b = 0 RETURNING *;
4220 UPDATE async_pt SET c = c || c WHERE b = 0 RETURNING *;
4221 EXPLAIN (VERBOSE, COSTS OFF)
4222 DELETE FROM async_pt WHERE b = 0 RETURNING *;
4223 DELETE FROM async_pt WHERE b = 0 RETURNING *;
4224
4225 -- Check EXPLAIN ANALYZE for a query that scans empty partitions asynchronously
4226 DELETE FROM async_p1;
4227 DELETE FROM async_p2;
4228 DELETE FROM async_p3;
4229
4230 EXPLAIN (ANALYZE, COSTS OFF, SUMMARY OFF, TIMING OFF, BUFFERS OFF)
4231 SELECT * FROM async_pt;
4232
4233 -- Clean up
4234 DROP TABLE async_pt;
4235 DROP TABLE base_tbl1;
4236 DROP TABLE base_tbl2;
4237 DROP TABLE result_tbl;
4238 DROP TABLE join_tbl;
4239
4240 -- Test ExecAppendAsyncReset code path that drains outstanding async requests
4241 -- (case where subplans are re-scanned without parameter changes)
4242 CREATE TABLE base_tbl (a int, b int);
4243 INSERT INTO base_tbl VALUES (1, 11), (2, 22), (3, 33);
4244 CREATE FOREIGN TABLE foreign_tbl (b int)
4245 SERVER loopback OPTIONS (table_name 'base_tbl');
4246 CREATE FOREIGN TABLE foreign_tbl2 () INHERITS (foreign_tbl)
4247 SERVER loopback OPTIONS (table_name 'base_tbl');
4248
4249 EXPLAIN (VERBOSE, COSTS OFF)
4250 SELECT a FROM base_tbl WHERE (a, random() > 0) IN (SELECT a, random() > 0 FROM foreign_tbl);
4251 SELECT a FROM base_tbl WHERE (a, random() > 0) IN (SELECT a, random() > 0 FROM foreign_tbl);
4252
4253 -- Clean up
4254 DROP FOREIGN TABLE foreign_tbl CASCADE;
4255 DROP TABLE base_tbl;
4256
4257 ALTER SERVER loopback OPTIONS (DROP async_capable);
4258 ALTER SERVER loopback2 OPTIONS (DROP async_capable);
4259
4260 -- ===================================================================
4261 -- test invalid server, foreign table and foreign data wrapper options
4262 -- ===================================================================
4263 -- Invalid fdw_startup_cost option
4264 CREATE SERVER inv_scst FOREIGN DATA WRAPPER postgres_fdw
4265 OPTIONS(fdw_startup_cost '100$%$#$#');
4266 -- Invalid fdw_tuple_cost option
4267 CREATE SERVER inv_scst FOREIGN DATA WRAPPER postgres_fdw
4268 OPTIONS(fdw_tuple_cost '100$%$#$#');
4269 -- Invalid fetch_size option
4270 CREATE FOREIGN TABLE inv_fsz (c1 int )
4271 SERVER loopback OPTIONS (fetch_size '100$%$#$#');
4272 -- Invalid batch_size option
4273 CREATE FOREIGN TABLE inv_bsz (c1 int )
4274 SERVER loopback OPTIONS (batch_size '100$%$#$#');
4275
4276 -- No option is allowed to be specified at foreign data wrapper level
4277 ALTER FOREIGN DATA WRAPPER postgres_fdw OPTIONS (nonexistent 'fdw');
4278
4279 -- ===================================================================
4280 -- test postgres_fdw.application_name GUC
4281 -- ===================================================================
4282 -- To avoid race conditions in checking the remote session's application_name,
4283 -- use this view to make the remote session itself read its application_name.
4284 CREATE VIEW my_application_name AS
4285 SELECT application_name FROM pg_stat_activity WHERE pid = pg_backend_pid();
4286
4287 CREATE FOREIGN TABLE remote_application_name (application_name text)
4288 SERVER loopback2
4289 OPTIONS (schema_name 'public', table_name 'my_application_name');
4290
4291 SELECT count(*) FROM remote_application_name;
4292
4293 -- Specify escape sequences in application_name option of a server
4294 -- object so as to test that they are replaced with status information
4295 -- expectedly. Note that we are also relying on ALTER SERVER to force
4296 -- the remote session to be restarted with its new application name.
4297 --
4298 -- Since pg_stat_activity.application_name may be truncated to less than
4299 -- NAMEDATALEN characters, note that substring() needs to be used
4300 -- at the condition of test query to make sure that the string consisting
4301 -- of database name and process ID is also less than that.
4302 ALTER SERVER loopback2 OPTIONS (application_name 'fdw_%d%p');
4303 SELECT count(*) FROM remote_application_name
4304 WHERE application_name =
4305 substring('fdw_' || current_database() || pg_backend_pid() for
4306 current_setting('max_identifier_length')::int);
4307
4308 -- postgres_fdw.application_name overrides application_name option
4309 -- of a server object if both settings are present.
4310 ALTER SERVER loopback2 OPTIONS (SET application_name 'fdw_wrong');
4311 SET postgres_fdw.application_name TO 'fdw_%a%u%%';
4312 SELECT count(*) FROM remote_application_name
4313 WHERE application_name =
4314 substring('fdw_' || current_setting('application_name') ||
4315 CURRENT_USER || '%' for current_setting('max_identifier_length')::int);
4316 RESET postgres_fdw.application_name;
4317
4318 -- Test %c (session ID) and %C (cluster name) escape sequences.
4319 ALTER SERVER loopback2 OPTIONS (SET application_name 'fdw_%C%c');
4320 SELECT count(*) FROM remote_application_name
4321 WHERE application_name =
4322 substring('fdw_' || current_setting('cluster_name') ||
4323 to_hex(trunc(EXTRACT(EPOCH FROM (SELECT backend_start FROM
4324 pg_stat_get_activity(pg_backend_pid()))))::integer) || '.' ||
4325 to_hex(pg_backend_pid())
4326 for current_setting('max_identifier_length')::int);
4327
4328 -- Clean up.
4329 DROP FOREIGN TABLE remote_application_name;
4330 DROP VIEW my_application_name;
4331
4332 -- ===================================================================
4333 -- test parallel commit and parallel abort
4334 -- ===================================================================
4335 ALTER SERVER loopback OPTIONS (ADD parallel_commit 'true');
4336 ALTER SERVER loopback OPTIONS (ADD parallel_abort 'true');
4337 ALTER SERVER loopback2 OPTIONS (ADD parallel_commit 'true');
4338 ALTER SERVER loopback2 OPTIONS (ADD parallel_abort 'true');
4339
4340 CREATE TABLE ploc1 (f1 int, f2 text);
4341 CREATE FOREIGN TABLE prem1 (f1 int, f2 text)
4342 SERVER loopback OPTIONS (table_name 'ploc1');
4343 CREATE TABLE ploc2 (f1 int, f2 text);
4344 CREATE FOREIGN TABLE prem2 (f1 int, f2 text)
4345 SERVER loopback2 OPTIONS (table_name 'ploc2');
4346
4347 BEGIN;
4348 INSERT INTO prem1 VALUES (101, 'foo');
4349 INSERT INTO prem2 VALUES (201, 'bar');
4350 COMMIT;
4351 SELECT * FROM prem1;
4352 SELECT * FROM prem2;
4353
4354 BEGIN;
4355 SAVEPOINT s;
4356 INSERT INTO prem1 VALUES (102, 'foofoo');
4357 INSERT INTO prem2 VALUES (202, 'barbar');
4358 RELEASE SAVEPOINT s;
4359 COMMIT;
4360 SELECT * FROM prem1;
4361 SELECT * FROM prem2;
4362
4363 -- This tests executing DEALLOCATE ALL against foreign servers in parallel
4364 -- during pre-commit
4365 BEGIN;
4366 SAVEPOINT s;
4367 INSERT INTO prem1 VALUES (103, 'baz');
4368 INSERT INTO prem2 VALUES (203, 'qux');
4369 ROLLBACK TO SAVEPOINT s;
4370 RELEASE SAVEPOINT s;
4371 INSERT INTO prem1 VALUES (104, 'bazbaz');
4372 INSERT INTO prem2 VALUES (204, 'quxqux');
4373 COMMIT;
4374 SELECT * FROM prem1;
4375 SELECT * FROM prem2;
4376
4377 BEGIN;
4378 INSERT INTO prem1 VALUES (105, 'test1');
4379 INSERT INTO prem2 VALUES (205, 'test2');
4380 ABORT;
4381 SELECT * FROM prem1;
4382 SELECT * FROM prem2;
4383
4384 -- This tests executing DEALLOCATE ALL against foreign servers in parallel
4385 -- during post-abort
4386 BEGIN;
4387 SAVEPOINT s;
4388 INSERT INTO prem1 VALUES (105, 'test1');
4389 INSERT INTO prem2 VALUES (205, 'test2');
4390 ROLLBACK TO SAVEPOINT s;
4391 RELEASE SAVEPOINT s;
4392 INSERT INTO prem1 VALUES (105, 'test1');
4393 INSERT INTO prem2 VALUES (205, 'test2');
4394 ABORT;
4395 SELECT * FROM prem1;
4396 SELECT * FROM prem2;
4397
4398 ALTER SERVER loopback OPTIONS (DROP parallel_commit);
4399 ALTER SERVER loopback OPTIONS (DROP parallel_abort);
4400 ALTER SERVER loopback2 OPTIONS (DROP parallel_commit);
4401 ALTER SERVER loopback2 OPTIONS (DROP parallel_abort);
4402
4403 -- ===================================================================
4404 -- test for ANALYZE sampling
4405 -- ===================================================================
4406
4407 CREATE TABLE analyze_table (id int, a text, b bigint);
4408
4409 CREATE FOREIGN TABLE analyze_ftable (id int, a text, b bigint)
4410 SERVER loopback OPTIONS (table_name 'analyze_table');
4411
4412 INSERT INTO analyze_table (SELECT x FROM generate_series(1,1000) x);
4413 ANALYZE analyze_table;
4414
4415 SET default_statistics_target = 10;
4416 ANALYZE analyze_table;
4417
4418 ALTER SERVER loopback OPTIONS (analyze_sampling 'invalid');
4419
4420 ALTER SERVER loopback OPTIONS (analyze_sampling 'auto');
4421 ANALYZE analyze_ftable;
4422
4423 ALTER SERVER loopback OPTIONS (SET analyze_sampling 'system');
4424 ANALYZE analyze_ftable;
4425
4426 ALTER SERVER loopback OPTIONS (SET analyze_sampling 'bernoulli');
4427 ANALYZE analyze_ftable;
4428
4429 ALTER SERVER loopback OPTIONS (SET analyze_sampling 'random');
4430 ANALYZE analyze_ftable;
4431
4432 ALTER SERVER loopback OPTIONS (SET analyze_sampling 'off');
4433 ANALYZE analyze_ftable;
4434
4435 -- cleanup
4436 DROP FOREIGN TABLE analyze_ftable;
4437 DROP TABLE analyze_table;
4438
4439 -- ===================================================================
4440 -- test for postgres_fdw_get_connections function with check_conn = true
4441 -- ===================================================================
4442
4443 -- Disable debug_discard_caches in order to manage remote connections
4444 SET debug_discard_caches TO '0';
4445
4446 -- The text of the error might vary across platforms, so only show SQLSTATE.
4447 \set VERBOSITY sqlstate
4448
4449 SELECT 1 FROM postgres_fdw_disconnect_all();
4450 ALTER SERVER loopback OPTIONS (SET application_name 'fdw_conn_check');
4451 SELECT 1 FROM ft1 LIMIT 1;
4452
4453 -- Since the remote server is still connected, "closed" should be FALSE,
4454 -- or NULL if the connection status check is not available.
4455 -- In this test, the remote backend handling this connection should have
4456 -- application_name set to "fdw_conn_check", so remote_backend_pid should
4457 -- match the PID from the pg_stat_activity entry with that application_name.
4458 SELECT server_name,
4459 CASE WHEN closed IS NOT true THEN false ELSE true END AS closed,
4460 remote_backend_pid = (SELECT pid FROM pg_stat_activity
4461 WHERE application_name = 'fdw_conn_check') AS remote_backend_pid
4462 FROM postgres_fdw_get_connections(true);
4463
4464 -- After terminating the remote backend, if the connection entry is still in
4465 -- the cache, "closed" should be TRUE, or NULL if the connection status check
4466 -- is not available, and remote_backend_pid should still show the non-zero PID
4467 -- of the terminated remote backend. Concurrent invalidation can remove the
4468 -- idle cached connection before the next statement, in which case
4469 -- postgres_fdw_get_connections(true) can legitimately return no rows.
4470 DO $$ BEGIN
4471 PERFORM pg_terminate_backend(pid, 180000) FROM pg_stat_activity
4472 WHERE application_name = 'fdw_conn_check';
4473 END $$;
4474 WITH terminated_conn AS (
4475 SELECT server_name,
4476 CASE WHEN closed IS NOT false THEN true ELSE false END AS closed,
4477 remote_backend_pid <> 0 AS remote_backend_pid
4478 FROM postgres_fdw_get_connections(true)
4479 )
4480 SELECT CASE
4481 WHEN count(*) = 0 THEN true
4482 WHEN count(*) = 1 THEN bool_and(server_name = 'loopback'
4483 AND closed
4484 AND remote_backend_pid)
4485 ELSE false
4486 END AS ok
4487 FROM terminated_conn;
4488
4489 -- In an explicit transaction, concurrent invalidation may mark the
4490 -- connection invalid but cannot discard it before transaction end, so the
4491 -- terminated connection should remain visible in the cache.
4492 SELECT 1 FROM postgres_fdw_disconnect_all();
4493 SET client_min_messages = 'ERROR';
4494 BEGIN;
4495 SELECT 1 FROM ft1 LIMIT 1;
4496 DO $$ BEGIN
4497 PERFORM pg_terminate_backend(pid, 180000) FROM pg_stat_activity
4498 WHERE application_name = 'fdw_conn_check';
4499 END $$;
4500 SELECT server_name, used_in_xact,
4501 CASE WHEN closed IS NOT false THEN true ELSE false END AS closed,
4502 remote_backend_pid <> 0 AS remote_backend_pid
4503 FROM postgres_fdw_get_connections(true);
4504 ABORT;
4505 RESET client_min_messages;
4506
4507 -- Clean up
4508 \set VERBOSITY default
4509 RESET debug_discard_caches;
4510
4511 -- ===================================================================
4512 -- test cleanup of failed connections on abort
4513 -- ===================================================================
4514
4515 CREATE VIEW my_backend_pid (pid) AS SELECT pg_backend_pid();
4516 CREATE FOREIGN TABLE remote_backend_pid (pid int)
4517 SERVER loopback OPTIONS (table_name 'my_backend_pid');
4518 CREATE FUNCTION wait_for_backend_termination(int) RETURNS void AS $$
4519 BEGIN
4520 WHILE (SELECT count(*) FROM pg_stat_activity WHERE pid = $1) > 0
4521 LOOP
4522 PERFORM pg_stat_clear_snapshot();
4523 END LOOP;
4524 END
4525 $$ LANGUAGE plpgsql;
4526
4527 SET client_min_messages = 'ERROR';
4528
4529 BEGIN;
4530 DECLARE c CURSOR FOR SELECT * FROM ft1 ORDER BY c1;
4531 SAVEPOINT s;
4532 SELECT pid AS remote_pid FROM remote_backend_pid \gset
4533 SELECT pg_terminate_backend(:remote_pid);
4534 SELECT wait_for_backend_termination(:remote_pid);
4535 ROLLBACK TO SAVEPOINT s;
4536 SELECT pid FROM remote_backend_pid;
4537 ROLLBACK TO SAVEPOINT s;
4538 RELEASE SAVEPOINT s;
4539 FETCH c;
4540 ABORT;
4541
4542 BEGIN;
4543 DECLARE c CURSOR FOR SELECT * FROM ft1 ORDER BY c1;
4544 FETCH c;
4545 SAVEPOINT s;
4546 SELECT pid AS remote_pid FROM remote_backend_pid \gset
4547 SELECT pg_terminate_backend(:remote_pid);
4548 SELECT wait_for_backend_termination(:remote_pid);
4549 ROLLBACK TO SAVEPOINT s;
4550 SELECT pid FROM remote_backend_pid;
4551 ROLLBACK TO SAVEPOINT s;
4552 RELEASE SAVEPOINT s;
4553 CLOSE c;
4554 ABORT;
4555
4556 RESET client_min_messages;
4557
4558 DROP FUNCTION wait_for_backend_termination(int);
4559 DROP FOREIGN TABLE remote_backend_pid;
4560 DROP VIEW my_backend_pid;