| 1 |
-- |
| 2 |
-- Test foreign-data wrapper file_fdw. |
| 3 |
-- |
| 4 |
-- directory paths are passed to us in environment variables |
| 5 |
\getenv abs_srcdir PG_ABS_SRCDIR |
| 6 |
-- Clean up in case a prior regression run failed |
| 7 |
SET client_min_messages TO 'warning'; |
| 8 |
DROP ROLE IF EXISTS regress_file_fdw_superuser, regress_file_fdw_user, regress_no_priv_user; |
| 9 |
RESET client_min_messages; |
| 10 |
CREATE ROLE regress_file_fdw_superuser LOGIN SUPERUSER; -- is a superuser |
| 11 |
CREATE ROLE regress_file_fdw_user LOGIN; -- has priv and user mapping |
| 12 |
CREATE ROLE regress_no_priv_user LOGIN; -- has priv but no user mapping |
| 13 |
-- Install file_fdw |
| 14 |
CREATE EXTENSION file_fdw; |
| 15 |
-- create function to filter unstable results of EXPLAIN |
| 16 |
CREATE FUNCTION explain_filter(text) RETURNS setof text |
| 17 |
LANGUAGE plpgsql AS |
| 18 |
$$ |
| 19 |
declare |
| 20 |
ln text; |
| 21 |
begin |
| 22 |
for ln in execute $1 |
| 23 |
loop |
| 24 |
-- Remove the path portion of foreign file names |
| 25 |
ln := regexp_replace(ln, 'Foreign File: .*/([a-z.]+)$', 'Foreign File: .../\1'); |
| 26 |
return next ln; |
| 27 |
end loop; |
| 28 |
end; |
| 29 |
$$; |
| 30 |
-- regress_file_fdw_superuser owns fdw-related objects |
| 31 |
SET ROLE regress_file_fdw_superuser; |
| 32 |
CREATE SERVER file_server FOREIGN DATA WRAPPER file_fdw; |
| 33 |
-- privilege tests |
| 34 |
SET ROLE regress_file_fdw_user; |
| 35 |
CREATE FOREIGN DATA WRAPPER file_fdw2 HANDLER file_fdw_handler VALIDATOR file_fdw_validator; -- ERROR |
| 36 |
ERROR: permission denied to create foreign-data wrapper "file_fdw2" |
| 37 |
HINT: Must be superuser to create a foreign-data wrapper. |
| 38 |
CREATE SERVER file_server2 FOREIGN DATA WRAPPER file_fdw; -- ERROR |
| 39 |
ERROR: permission denied for foreign-data wrapper file_fdw |
| 40 |
CREATE USER MAPPING FOR regress_file_fdw_user SERVER file_server; -- ERROR |
| 41 |
ERROR: permission denied for foreign server file_server |
| 42 |
SET ROLE regress_file_fdw_superuser; |
| 43 |
GRANT USAGE ON FOREIGN SERVER file_server TO regress_file_fdw_user; |
| 44 |
SET ROLE regress_file_fdw_user; |
| 45 |
CREATE USER MAPPING FOR regress_file_fdw_user SERVER file_server; |
| 46 |
-- create user mappings and grant privilege to test users |
| 47 |
SET ROLE regress_file_fdw_superuser; |
| 48 |
CREATE USER MAPPING FOR regress_file_fdw_superuser SERVER file_server; |
| 49 |
CREATE USER MAPPING FOR regress_no_priv_user SERVER file_server; |
| 50 |
-- validator tests |
| 51 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (foo 'bar'); -- ERROR |
| 52 |
ERROR: invalid option "foo" |
| 53 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS ("a=b" 'true'); -- ERROR |
| 54 |
ERROR: invalid option name "a=b": must not contain "=" |
| 55 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'xml'); -- ERROR |
| 56 |
ERROR: COPY format "xml" not recognized |
| 57 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', quote ':'); -- ERROR |
| 58 |
ERROR: COPY QUOTE requires CSV mode |
| 59 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', escape ':'); -- ERROR |
| 60 |
ERROR: COPY ESCAPE requires CSV mode |
| 61 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'binary', header 'true'); -- ERROR |
| 62 |
ERROR: cannot specify HEADER in BINARY mode |
| 63 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'binary', quote ':'); -- ERROR |
| 64 |
ERROR: COPY QUOTE requires CSV mode |
| 65 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'binary', escape ':'); -- ERROR |
| 66 |
ERROR: COPY ESCAPE requires CSV mode |
| 67 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', delimiter 'a'); -- ERROR |
| 68 |
ERROR: COPY delimiter cannot be "a" |
| 69 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', escape '-'); -- ERROR |
| 70 |
ERROR: COPY ESCAPE requires CSV mode |
| 71 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'csv', quote '-', null '=-='); -- ERROR |
| 72 |
ERROR: CSV quote character must not appear in the NULL specification |
| 73 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'csv', delimiter '-', null '=-='); -- ERROR |
| 74 |
ERROR: COPY delimiter character must not appear in the NULL specification |
| 75 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'csv', delimiter '-', quote '-'); -- ERROR |
| 76 |
ERROR: COPY delimiter and quote must be different |
| 77 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'csv', delimiter '---'); -- ERROR |
| 78 |
ERROR: COPY delimiter must be a single one-byte character |
| 79 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'csv', quote '---'); -- ERROR |
| 80 |
ERROR: COPY quote must be a single one-byte character |
| 81 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'csv', escape '---'); -- ERROR |
| 82 |
ERROR: COPY escape must be a single one-byte character |
| 83 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', delimiter '\'); -- ERROR |
| 84 |
ERROR: COPY delimiter cannot be "\" |
| 85 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', delimiter '.'); -- ERROR |
| 86 |
ERROR: COPY delimiter cannot be "." |
| 87 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', delimiter '1'); -- ERROR |
| 88 |
ERROR: COPY delimiter cannot be "1" |
| 89 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'text', delimiter 'a'); -- ERROR |
| 90 |
ERROR: COPY delimiter cannot be "a" |
| 91 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'csv', delimiter ' |
| 92 |
'); -- ERROR |
| 93 |
ERROR: COPY delimiter cannot be newline or carriage return |
| 94 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'csv', null ' |
| 95 |
'); -- ERROR |
| 96 |
ERROR: COPY null representation cannot use newline or carriage return |
| 97 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (on_error 'unsupported'); -- ERROR |
| 98 |
ERROR: COPY ON_ERROR "unsupported" not recognized |
| 99 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (format 'binary', on_error 'ignore'); -- ERROR |
| 100 |
ERROR: only ON_ERROR STOP is allowed in BINARY mode |
| 101 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (log_verbosity 'unsupported'); -- ERROR |
| 102 |
ERROR: COPY LOG_VERBOSITY "unsupported" not recognized |
| 103 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (reject_limit '1'); -- ERROR |
| 104 |
ERROR: COPY REJECT_LIMIT requires ON_ERROR to be set to IGNORE |
| 105 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (on_error 'ignore', reject_limit '0'); -- ERROR |
| 106 |
ERROR: REJECT_LIMIT (0) must be greater than zero |
| 107 |
CREATE FOREIGN TABLE tbl () SERVER file_server; -- ERROR |
| 108 |
ERROR: either filename or program is required for file_fdw foreign tables |
| 109 |
\set filename :abs_srcdir '/data/agg.data' |
| 110 |
CREATE FOREIGN TABLE agg_text ( |
| 111 |
a int2 CHECK (a >= 0), |
| 112 |
b float4 |
| 113 |
) SERVER file_server |
| 114 |
OPTIONS (format 'text', filename :'filename', delimiter ' ', null '\N'); |
| 115 |
GRANT SELECT ON agg_text TO regress_file_fdw_user; |
| 116 |
\set filename :abs_srcdir '/data/agg.csv' |
| 117 |
CREATE FOREIGN TABLE agg_csv ( |
| 118 |
a int2, |
| 119 |
b float4 |
| 120 |
) SERVER file_server |
| 121 |
OPTIONS (format 'csv', filename :'filename', header 'true', delimiter ';', quote '@', escape '"', null ''); |
| 122 |
ALTER FOREIGN TABLE agg_csv ADD CHECK (a >= 0); |
| 123 |
\set filename :abs_srcdir '/data/agg.bad' |
| 124 |
CREATE FOREIGN TABLE agg_bad ( |
| 125 |
a int2, |
| 126 |
b float4 |
| 127 |
) SERVER file_server |
| 128 |
OPTIONS (format 'csv', filename :'filename', header 'true', delimiter ';', quote '@', escape '"', null ''); |
| 129 |
ALTER FOREIGN TABLE agg_bad ADD CHECK (a >= 0); |
| 130 |
-- test header matching |
| 131 |
\set filename :abs_srcdir '/data/list1.csv' |
| 132 |
CREATE FOREIGN TABLE header_match ("1" int, foo text) SERVER file_server |
| 133 |
OPTIONS (format 'csv', filename :'filename', delimiter ',', header 'match'); |
| 134 |
SELECT * FROM header_match; |
| 135 |
1 | foo |
| 136 |
---+----- |
| 137 |
1 | bar |
| 138 |
(1 row) |
| 139 |
|
| 140 |
CREATE FOREIGN TABLE header_doesnt_match (a int, foo text) SERVER file_server |
| 141 |
OPTIONS (format 'csv', filename :'filename', delimiter ',', header 'match'); |
| 142 |
SELECT * FROM header_doesnt_match; -- ERROR |
| 143 |
ERROR: column name mismatch in header line field 1: got "1", expected "a" |
| 144 |
CONTEXT: COPY header_doesnt_match, line 1: "1,foo" |
| 145 |
-- per-column options tests |
| 146 |
\set filename :abs_srcdir '/data/text.csv' |
| 147 |
CREATE FOREIGN TABLE text_csv ( |
| 148 |
word1 text OPTIONS (force_not_null 'true'), |
| 149 |
word2 text OPTIONS (force_not_null 'off'), |
| 150 |
word3 text OPTIONS (force_null 'true'), |
| 151 |
word4 text OPTIONS (force_null 'off') |
| 152 |
) SERVER file_server |
| 153 |
OPTIONS (format 'text', filename :'filename', null 'NULL'); |
| 154 |
SELECT * FROM text_csv; -- ERROR |
| 155 |
ERROR: COPY FORCE_NOT_NULL requires CSV mode |
| 156 |
ALTER FOREIGN TABLE text_csv OPTIONS (SET format 'csv'); |
| 157 |
\pset null _null_ |
| 158 |
SELECT * FROM text_csv; |
| 159 |
word1 | word2 | word3 | word4 |
| 160 |
-------+--------+--------+-------- |
| 161 |
AAA | aaa | 123 | |
| 162 |
XYZ | xyz | | 321 |
| 163 |
NULL | _null_ | _null_ | _null_ |
| 164 |
NULL | _null_ | _null_ | _null_ |
| 165 |
ABC | abc | | |
| 166 |
(5 rows) |
| 167 |
|
| 168 |
-- force_not_null and force_null can be used together on the same column |
| 169 |
ALTER FOREIGN TABLE text_csv ALTER COLUMN word1 OPTIONS (force_null 'true'); |
| 170 |
ALTER FOREIGN TABLE text_csv ALTER COLUMN word3 OPTIONS (force_not_null 'true'); |
| 171 |
-- force_not_null is not allowed to be specified at any foreign object level: |
| 172 |
ALTER FOREIGN DATA WRAPPER file_fdw OPTIONS (ADD force_not_null '*'); -- ERROR |
| 173 |
ERROR: invalid option "force_not_null" |
| 174 |
HINT: There are no valid options in this context. |
| 175 |
ALTER SERVER file_server OPTIONS (ADD force_not_null '*'); -- ERROR |
| 176 |
ERROR: invalid option "force_not_null" |
| 177 |
HINT: There are no valid options in this context. |
| 178 |
CREATE USER MAPPING FOR public SERVER file_server OPTIONS (force_not_null '*'); -- ERROR |
| 179 |
ERROR: invalid option "force_not_null" |
| 180 |
HINT: There are no valid options in this context. |
| 181 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (force_not_null '*'); -- ERROR |
| 182 |
ERROR: invalid option "force_not_null" |
| 183 |
-- force_null is not allowed to be specified at any foreign object level: |
| 184 |
ALTER FOREIGN DATA WRAPPER file_fdw OPTIONS (ADD force_null '*'); -- ERROR |
| 185 |
ERROR: invalid option "force_null" |
| 186 |
HINT: There are no valid options in this context. |
| 187 |
ALTER SERVER file_server OPTIONS (ADD force_null '*'); -- ERROR |
| 188 |
ERROR: invalid option "force_null" |
| 189 |
HINT: There are no valid options in this context. |
| 190 |
CREATE USER MAPPING FOR public SERVER file_server OPTIONS (force_null '*'); -- ERROR |
| 191 |
ERROR: invalid option "force_null" |
| 192 |
HINT: There are no valid options in this context. |
| 193 |
CREATE FOREIGN TABLE tbl () SERVER file_server OPTIONS (force_null '*'); -- ERROR |
| 194 |
ERROR: invalid option "force_null" |
| 195 |
-- basic query tests |
| 196 |
SELECT * FROM agg_text WHERE b > 10.0 ORDER BY a; |
| 197 |
a | b |
| 198 |
-----+-------- |
| 199 |
42 | 324.78 |
| 200 |
100 | 99.097 |
| 201 |
(2 rows) |
| 202 |
|
| 203 |
SELECT * FROM agg_csv ORDER BY a; |
| 204 |
a | b |
| 205 |
-----+--------- |
| 206 |
0 | 0.09561 |
| 207 |
42 | 324.78 |
| 208 |
100 | 99.097 |
| 209 |
(3 rows) |
| 210 |
|
| 211 |
SELECT * FROM agg_csv c JOIN agg_text t ON (t.a = c.a) ORDER BY c.a; |
| 212 |
a | b | a | b |
| 213 |
-----+---------+-----+--------- |
| 214 |
0 | 0.09561 | 0 | 0.09561 |
| 215 |
42 | 324.78 | 42 | 324.78 |
| 216 |
100 | 99.097 | 100 | 99.097 |
| 217 |
(3 rows) |
| 218 |
|
| 219 |
-- error context report tests |
| 220 |
SELECT * FROM agg_bad; -- ERROR |
| 221 |
ERROR: invalid input syntax for type real: "aaa" |
| 222 |
CONTEXT: COPY agg_bad, line 3, column b: "aaa" |
| 223 |
-- on_error, log_verbosity and reject_limit tests |
| 224 |
ALTER FOREIGN TABLE agg_bad OPTIONS (ADD on_error 'ignore'); |
| 225 |
SELECT * FROM agg_bad; |
| 226 |
NOTICE: 2 rows were skipped due to data type incompatibility |
| 227 |
a | b |
| 228 |
-----+-------- |
| 229 |
100 | 99.097 |
| 230 |
42 | 324.78 |
| 231 |
(2 rows) |
| 232 |
|
| 233 |
ALTER FOREIGN TABLE agg_bad OPTIONS (ADD log_verbosity 'silent'); |
| 234 |
SELECT * FROM agg_bad; |
| 235 |
a | b |
| 236 |
-----+-------- |
| 237 |
100 | 99.097 |
| 238 |
42 | 324.78 |
| 239 |
(2 rows) |
| 240 |
|
| 241 |
ALTER FOREIGN TABLE agg_bad OPTIONS (ADD reject_limit '1'); -- ERROR |
| 242 |
SELECT * FROM agg_bad; |
| 243 |
ERROR: skipped more than REJECT_LIMIT (1) rows due to data type incompatibility |
| 244 |
CONTEXT: COPY agg_bad, line 5, column b: "bbb" |
| 245 |
ALTER FOREIGN TABLE agg_bad OPTIONS (SET reject_limit '2'); |
| 246 |
SELECT * FROM agg_bad; |
| 247 |
a | b |
| 248 |
-----+-------- |
| 249 |
100 | 99.097 |
| 250 |
42 | 324.78 |
| 251 |
(2 rows) |
| 252 |
|
| 253 |
ANALYZE agg_bad; |
| 254 |
-- misc query tests |
| 255 |
\t on |
| 256 |
SELECT explain_filter('EXPLAIN (VERBOSE, COSTS FALSE) SELECT * FROM agg_csv'); |
| 257 |
Foreign Scan on public.agg_csv |
| 258 |
Output: a, b |
| 259 |
Foreign File: .../agg.csv |
| 260 |
|
| 261 |
\t off |
| 262 |
PREPARE st(int) AS SELECT * FROM agg_csv WHERE a = $1; |
| 263 |
EXECUTE st(100); |
| 264 |
a | b |
| 265 |
-----+-------- |
| 266 |
100 | 99.097 |
| 267 |
(1 row) |
| 268 |
|
| 269 |
EXECUTE st(100); |
| 270 |
a | b |
| 271 |
-----+-------- |
| 272 |
100 | 99.097 |
| 273 |
(1 row) |
| 274 |
|
| 275 |
DEALLOCATE st; |
| 276 |
-- tableoid |
| 277 |
SELECT tableoid::regclass, b FROM agg_csv; |
| 278 |
tableoid | b |
| 279 |
----------+--------- |
| 280 |
agg_csv | 99.097 |
| 281 |
agg_csv | 0.09561 |
| 282 |
agg_csv | 324.78 |
| 283 |
(3 rows) |
| 284 |
|
| 285 |
-- updates aren't supported |
| 286 |
INSERT INTO agg_csv VALUES(1,2.0); |
| 287 |
ERROR: cannot insert into foreign table "agg_csv" |
| 288 |
UPDATE agg_csv SET a = 1; |
| 289 |
ERROR: cannot update foreign table "agg_csv" |
| 290 |
DELETE FROM agg_csv WHERE a = 100; |
| 291 |
ERROR: cannot delete from foreign table "agg_csv" |
| 292 |
TRUNCATE agg_csv; |
| 293 |
ERROR: cannot truncate foreign table "agg_csv" |
| 294 |
-- but this should be allowed |
| 295 |
SELECT * FROM agg_csv FOR UPDATE; |
| 296 |
a | b |
| 297 |
-----+--------- |
| 298 |
100 | 99.097 |
| 299 |
0 | 0.09561 |
| 300 |
42 | 324.78 |
| 301 |
(3 rows) |
| 302 |
|
| 303 |
-- copy from isn't supported either |
| 304 |
COPY agg_csv FROM STDIN; |
| 305 |
ERROR: cannot insert into foreign table "agg_csv" |
| 306 |
-- constraint exclusion tests |
| 307 |
\t on |
| 308 |
SELECT explain_filter('EXPLAIN (VERBOSE, COSTS FALSE) SELECT * FROM agg_csv WHERE a < 0'); |
| 309 |
Foreign Scan on public.agg_csv |
| 310 |
Output: a, b |
| 311 |
Filter: (agg_csv.a < 0) |
| 312 |
Foreign File: .../agg.csv |
| 313 |
|
| 314 |
\t off |
| 315 |
SELECT * FROM agg_csv WHERE a < 0; |
| 316 |
a | b |
| 317 |
---+--- |
| 318 |
(0 rows) |
| 319 |
|
| 320 |
SET constraint_exclusion = 'on'; |
| 321 |
\t on |
| 322 |
SELECT explain_filter('EXPLAIN (VERBOSE, COSTS FALSE) SELECT * FROM agg_csv WHERE a < 0'); |
| 323 |
Result |
| 324 |
Output: a, b |
| 325 |
One-Time Filter: false |
| 326 |
|
| 327 |
\t off |
| 328 |
SELECT * FROM agg_csv WHERE a < 0; |
| 329 |
a | b |
| 330 |
---+--- |
| 331 |
(0 rows) |
| 332 |
|
| 333 |
RESET constraint_exclusion; |
| 334 |
-- table inheritance tests |
| 335 |
CREATE TABLE agg (a int2, b float4); |
| 336 |
ALTER FOREIGN TABLE agg_csv INHERIT agg; |
| 337 |
SELECT tableoid::regclass, * FROM agg; |
| 338 |
tableoid | a | b |
| 339 |
----------+-----+--------- |
| 340 |
agg_csv | 100 | 99.097 |
| 341 |
agg_csv | 0 | 0.09561 |
| 342 |
agg_csv | 42 | 324.78 |
| 343 |
(3 rows) |
| 344 |
|
| 345 |
SELECT tableoid::regclass, * FROM agg_csv; |
| 346 |
tableoid | a | b |
| 347 |
----------+-----+--------- |
| 348 |
agg_csv | 100 | 99.097 |
| 349 |
agg_csv | 0 | 0.09561 |
| 350 |
agg_csv | 42 | 324.78 |
| 351 |
(3 rows) |
| 352 |
|
| 353 |
SELECT tableoid::regclass, * FROM ONLY agg; |
| 354 |
tableoid | a | b |
| 355 |
----------+---+--- |
| 356 |
(0 rows) |
| 357 |
|
| 358 |
-- updates aren't supported |
| 359 |
UPDATE agg SET a = 1; |
| 360 |
ERROR: cannot update foreign table "agg_csv" |
| 361 |
DELETE FROM agg WHERE a = 100; |
| 362 |
ERROR: cannot delete from foreign table "agg_csv" |
| 363 |
-- but this should be allowed |
| 364 |
SELECT tableoid::regclass, * FROM agg FOR UPDATE; |
| 365 |
tableoid | a | b |
| 366 |
----------+-----+--------- |
| 367 |
agg_csv | 100 | 99.097 |
| 368 |
agg_csv | 0 | 0.09561 |
| 369 |
agg_csv | 42 | 324.78 |
| 370 |
(3 rows) |
| 371 |
|
| 372 |
ALTER FOREIGN TABLE agg_csv NO INHERIT agg; |
| 373 |
DROP TABLE agg; |
| 374 |
-- declarative partitioning tests |
| 375 |
SET ROLE regress_file_fdw_superuser; |
| 376 |
CREATE TABLE pt (a int, b text) partition by list (a); |
| 377 |
\set filename :abs_srcdir '/data/list1.csv' |
| 378 |
CREATE FOREIGN TABLE p1 partition of pt for values in (1) SERVER file_server |
| 379 |
OPTIONS (format 'csv', filename :'filename', delimiter ','); |
| 380 |
CREATE TABLE p2 partition of pt for values in (2); |
| 381 |
SELECT tableoid::regclass, * FROM pt; |
| 382 |
tableoid | a | b |
| 383 |
----------+---+----- |
| 384 |
p1 | 1 | foo |
| 385 |
p1 | 1 | bar |
| 386 |
(2 rows) |
| 387 |
|
| 388 |
SELECT tableoid::regclass, * FROM p1; |
| 389 |
tableoid | a | b |
| 390 |
----------+---+----- |
| 391 |
p1 | 1 | foo |
| 392 |
p1 | 1 | bar |
| 393 |
(2 rows) |
| 394 |
|
| 395 |
SELECT tableoid::regclass, * FROM p2; |
| 396 |
tableoid | a | b |
| 397 |
----------+---+--- |
| 398 |
(0 rows) |
| 399 |
|
| 400 |
\set filename :abs_srcdir '/data/list2.bad' |
| 401 |
COPY pt FROM :'filename' with (format 'csv', delimiter ','); -- ERROR |
| 402 |
ERROR: cannot insert into foreign table "p1" |
| 403 |
CONTEXT: COPY pt, line 2: "1,qux" |
| 404 |
\set filename :abs_srcdir '/data/list2.csv' |
| 405 |
COPY pt FROM :'filename' with (format 'csv', delimiter ','); |
| 406 |
SELECT tableoid::regclass, * FROM pt; |
| 407 |
tableoid | a | b |
| 408 |
----------+---+----- |
| 409 |
p1 | 1 | foo |
| 410 |
p1 | 1 | bar |
| 411 |
p2 | 2 | baz |
| 412 |
p2 | 2 | qux |
| 413 |
(4 rows) |
| 414 |
|
| 415 |
SELECT tableoid::regclass, * FROM p1; |
| 416 |
tableoid | a | b |
| 417 |
----------+---+----- |
| 418 |
p1 | 1 | foo |
| 419 |
p1 | 1 | bar |
| 420 |
(2 rows) |
| 421 |
|
| 422 |
SELECT tableoid::regclass, * FROM p2; |
| 423 |
tableoid | a | b |
| 424 |
----------+---+----- |
| 425 |
p2 | 2 | baz |
| 426 |
p2 | 2 | qux |
| 427 |
(2 rows) |
| 428 |
|
| 429 |
INSERT INTO pt VALUES (1, 'xyzzy'); -- ERROR |
| 430 |
ERROR: cannot insert into foreign table "p1" |
| 431 |
INSERT INTO pt VALUES (2, 'xyzzy'); |
| 432 |
UPDATE pt set a = 1 where a = 2; -- ERROR |
| 433 |
ERROR: cannot insert into foreign table "p1" |
| 434 |
SELECT tableoid::regclass, * FROM pt; |
| 435 |
tableoid | a | b |
| 436 |
----------+---+------- |
| 437 |
p1 | 1 | foo |
| 438 |
p1 | 1 | bar |
| 439 |
p2 | 2 | baz |
| 440 |
p2 | 2 | qux |
| 441 |
p2 | 2 | xyzzy |
| 442 |
(5 rows) |
| 443 |
|
| 444 |
SELECT tableoid::regclass, * FROM p1; |
| 445 |
tableoid | a | b |
| 446 |
----------+---+----- |
| 447 |
p1 | 1 | foo |
| 448 |
p1 | 1 | bar |
| 449 |
(2 rows) |
| 450 |
|
| 451 |
SELECT tableoid::regclass, * FROM p2; |
| 452 |
tableoid | a | b |
| 453 |
----------+---+------- |
| 454 |
p2 | 2 | baz |
| 455 |
p2 | 2 | qux |
| 456 |
p2 | 2 | xyzzy |
| 457 |
(3 rows) |
| 458 |
|
| 459 |
-- Test DELETE/UPDATE/MERGE on a partitioned table when all partitions |
| 460 |
-- are excluded and only the dummy root result relation remains. The |
| 461 |
-- operation is a no-op but should not fail regardless of whether the |
| 462 |
-- foreign child was processed (pruning off) or not (pruning on). |
| 463 |
DROP TABLE p2; |
| 464 |
SET enable_partition_pruning TO off; |
| 465 |
DELETE FROM pt WHERE false; |
| 466 |
UPDATE pt SET b = 'x' WHERE false; |
| 467 |
MERGE INTO pt t USING (VALUES (1, 'x'::text)) AS s(a, b) |
| 468 |
ON false WHEN MATCHED THEN UPDATE SET b = s.b; |
| 469 |
SET enable_partition_pruning TO on; |
| 470 |
DELETE FROM pt WHERE false; |
| 471 |
UPDATE pt SET b = 'x' WHERE false; |
| 472 |
MERGE INTO pt t USING (VALUES (1, 'x'::text)) AS s(a, b) |
| 473 |
ON false WHEN MATCHED THEN UPDATE SET b = s.b; |
| 474 |
DROP TABLE pt; |
| 475 |
-- generated column tests |
| 476 |
\set filename :abs_srcdir '/data/list1.csv' |
| 477 |
CREATE FOREIGN TABLE gft1 (a int, b text, c text GENERATED ALWAYS AS ('foo') STORED) SERVER file_server |
| 478 |
OPTIONS (format 'csv', filename :'filename', delimiter ','); |
| 479 |
SELECT a, c FROM gft1; |
| 480 |
a | c |
| 481 |
---+-------- |
| 482 |
1 | _null_ |
| 483 |
1 | _null_ |
| 484 |
(2 rows) |
| 485 |
|
| 486 |
DROP FOREIGN TABLE gft1; |
| 487 |
-- copy default tests |
| 488 |
\set filename :abs_srcdir '/data/copy_default.csv' |
| 489 |
CREATE FOREIGN TABLE copy_default ( |
| 490 |
id integer, |
| 491 |
text_value text not null default 'test', |
| 492 |
ts_value timestamp without time zone not null default '2022-07-05' |
| 493 |
) SERVER file_server |
| 494 |
OPTIONS (format 'csv', filename :'filename', default '\D'); |
| 495 |
SELECT id, text_value, ts_value FROM copy_default; |
| 496 |
id | text_value | ts_value |
| 497 |
----+------------+-------------------------- |
| 498 |
1 | value | Mon Jul 04 00:00:00 2022 |
| 499 |
2 | test | Sun Jul 03 00:00:00 2022 |
| 500 |
3 | test | Tue Jul 05 00:00:00 2022 |
| 501 |
(3 rows) |
| 502 |
|
| 503 |
DROP FOREIGN TABLE copy_default; |
| 504 |
-- privilege tests |
| 505 |
SET ROLE regress_file_fdw_superuser; |
| 506 |
SELECT * FROM agg_text ORDER BY a; |
| 507 |
a | b |
| 508 |
-----+--------- |
| 509 |
0 | 0.09561 |
| 510 |
42 | 324.78 |
| 511 |
56 | 7.8 |
| 512 |
100 | 99.097 |
| 513 |
(4 rows) |
| 514 |
|
| 515 |
SET ROLE regress_file_fdw_user; |
| 516 |
SELECT * FROM agg_text ORDER BY a; |
| 517 |
a | b |
| 518 |
-----+--------- |
| 519 |
0 | 0.09561 |
| 520 |
42 | 324.78 |
| 521 |
56 | 7.8 |
| 522 |
100 | 99.097 |
| 523 |
(4 rows) |
| 524 |
|
| 525 |
SET ROLE regress_no_priv_user; |
| 526 |
SELECT * FROM agg_text ORDER BY a; -- ERROR |
| 527 |
ERROR: permission denied for foreign table agg_text |
| 528 |
SET ROLE regress_file_fdw_user; |
| 529 |
\t on |
| 530 |
SELECT explain_filter('EXPLAIN (VERBOSE, COSTS FALSE) SELECT * FROM agg_text WHERE a > 0'); |
| 531 |
Foreign Scan on public.agg_text |
| 532 |
Output: a, b |
| 533 |
Filter: (agg_text.a > 0) |
| 534 |
Foreign File: .../agg.data |
| 535 |
|
| 536 |
\t off |
| 537 |
-- file FDW allows foreign tables to be accessed without user mapping |
| 538 |
DROP USER MAPPING FOR regress_file_fdw_user SERVER file_server; |
| 539 |
SELECT * FROM agg_text ORDER BY a; |
| 540 |
a | b |
| 541 |
-----+--------- |
| 542 |
0 | 0.09561 |
| 543 |
42 | 324.78 |
| 544 |
56 | 7.8 |
| 545 |
100 | 99.097 |
| 546 |
(4 rows) |
| 547 |
|
| 548 |
-- privilege tests for object |
| 549 |
SET ROLE regress_file_fdw_superuser; |
| 550 |
ALTER FOREIGN TABLE agg_text OWNER TO regress_file_fdw_user; |
| 551 |
ALTER FOREIGN TABLE agg_text OPTIONS (SET format 'text'); |
| 552 |
SET ROLE regress_file_fdw_user; |
| 553 |
ALTER FOREIGN TABLE agg_text OPTIONS (SET format 'text'); |
| 554 |
ERROR: permission denied to set the "filename" option of a file_fdw foreign table |
| 555 |
DETAIL: Only roles with privileges of the "pg_read_server_files" role may set this option. |
| 556 |
SET ROLE regress_file_fdw_superuser; |
| 557 |
-- cleanup |
| 558 |
RESET ROLE; |
| 559 |
DROP EXTENSION file_fdw CASCADE; |
| 560 |
NOTICE: drop cascades to 9 other objects |
| 561 |
DETAIL: drop cascades to server file_server |
| 562 |
drop cascades to user mapping for regress_file_fdw_superuser on server file_server |
| 563 |
drop cascades to user mapping for regress_no_priv_user on server file_server |
| 564 |
drop cascades to foreign table agg_text |
| 565 |
drop cascades to foreign table agg_csv |
| 566 |
drop cascades to foreign table agg_bad |
| 567 |
drop cascades to foreign table header_match |
| 568 |
drop cascades to foreign table header_doesnt_match |
| 569 |
drop cascades to foreign table text_csv |
| 570 |
DROP ROLE regress_file_fdw_superuser, regress_file_fdw_user, regress_no_priv_user; |