aboutsummaryrefslogtreecommitdiff
path: root/contrib/postgres_fdw
diff options
context:
space:
mode:
authorFujii Masao <fujii@postgresql.org>2021-04-27 14:41:27 +0900
committerFujii Masao <fujii@postgresql.org>2021-04-27 14:41:27 +0900
commit8e9ea08bae93a754d5075b7bc9c0b2bc71958bfd (patch)
tree317013f67abe8af08f2fae54493f8e45b49e0822 /contrib/postgres_fdw
parent3fa17d37716f978f80dfcdab4e7c73f3a24e7a48 (diff)
downloadpostgresql-8e9ea08bae93a754d5075b7bc9c0b2bc71958bfd.tar.gz
postgresql-8e9ea08bae93a754d5075b7bc9c0b2bc71958bfd.zip
Don't pass "ONLY" options specified in TRUNCATE to foreign data wrapper.
Commit 8ff1c94649 allowed TRUNCATE command to truncate foreign tables. Previously the information about "ONLY" options specified in TRUNCATE command were passed to the foreign data wrapper. Then postgres_fdw constructed the TRUNCATE command to issue the remote server and included "ONLY" options in it based on the passed information. On the other hand, "ONLY" options specified in SELECT, UPDATE or DELETE have no effect when accessing or modifying the remote table, i.e., are not passed to the foreign data wrapper. So it's inconsistent to make only TRUNCATE command pass the "ONLY" options to the foreign data wrapper. Therefore this commit changes the TRUNCATE command so that it doesn't pass the "ONLY" options to the foreign data wrapper, for the consistency with other statements. Also this commit changes postgres_fdw so that it always doesn't include "ONLY" options in the TRUNCATE command that it constructs. Author: Fujii Masao Reviewed-by: Bharath Rupireddy, Kyotaro Horiguchi, Justin Pryzby, Zhihong Yu Discussion: https://postgr.es/m/551ed8c1-f531-818b-664a-2cecdab99cd8@oss.nttdata.com
Diffstat (limited to 'contrib/postgres_fdw')
-rw-r--r--contrib/postgres_fdw/deparse.c13
-rw-r--r--contrib/postgres_fdw/expected/postgres_fdw.out24
-rw-r--r--contrib/postgres_fdw/postgres_fdw.c4
-rw-r--r--contrib/postgres_fdw/postgres_fdw.h1
-rw-r--r--contrib/postgres_fdw/sql/postgres_fdw.sql16
5 files changed, 31 insertions, 27 deletions
diff --git a/contrib/postgres_fdw/deparse.c b/contrib/postgres_fdw/deparse.c
index bdc4c3620d0..7a798530e3a 100644
--- a/contrib/postgres_fdw/deparse.c
+++ b/contrib/postgres_fdw/deparse.c
@@ -2179,24 +2179,19 @@ deparseAnalyzeSql(StringInfo buf, Relation rel, List **retrieved_attrs)
void
deparseTruncateSql(StringInfo buf,
List *rels,
- List *rels_extra,
DropBehavior behavior,
bool restart_seqs)
{
- ListCell *lc1,
- *lc2;
+ ListCell *cell;
appendStringInfoString(buf, "TRUNCATE ");
- forboth(lc1, rels, lc2, rels_extra)
+ foreach(cell, rels)
{
- Relation rel = lfirst(lc1);
- int extra = lfirst_int(lc2);
+ Relation rel = lfirst(cell);
- if (lc1 != list_head(rels))
+ if (cell != list_head(rels))
appendStringInfoString(buf, ", ");
- if (extra & TRUNCATE_REL_CONTEXT_ONLY)
- appendStringInfoString(buf, "ONLY ");
deparseRelation(buf, rel);
}
diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out
index 5070d932394..8e1cc695081 100644
--- a/contrib/postgres_fdw/expected/postgres_fdw.out
+++ b/contrib/postgres_fdw/expected/postgres_fdw.out
@@ -8218,13 +8218,13 @@ drop table loc3;
-- test for TRUNCATE
-- ===================================================================
CREATE TABLE tru_rtable0 (id int primary key);
-CREATE TABLE tru_rtable1 (id int primary key);
CREATE FOREIGN TABLE tru_ftable (id int)
SERVER loopback OPTIONS (table_name 'tru_rtable0');
INSERT INTO tru_rtable0 (SELECT x FROM generate_series(1,10) x);
CREATE TABLE tru_ptable (id int) PARTITION BY HASH(id);
CREATE TABLE tru_ptable__p0 PARTITION OF tru_ptable
FOR VALUES WITH (MODULUS 2, REMAINDER 0);
+CREATE TABLE tru_rtable1 (id int primary key);
CREATE FOREIGN TABLE tru_ftable__p1 PARTITION OF tru_ptable
FOR VALUES WITH (MODULUS 2, REMAINDER 1)
SERVER loopback OPTIONS (table_name 'tru_rtable1');
@@ -8364,6 +8364,8 @@ SELECT count(*) from tru_pk_ftable; -- 0
(1 row)
-- truncate with ONLY clause
+-- Since ONLY is specified, the table tru_ftable_child that inherits
+-- tru_ftable_parent locally is not truncated.
TRUNCATE ONLY tru_ftable_parent;
SELECT sum(id) FROM tru_ftable_parent; -- 126
sum
@@ -8388,22 +8390,26 @@ SELECT sum(id) FROM tru_ftable; -- 95
95
(1 row)
-TRUNCATE ONLY tru_ftable; -- truncate only parent portion
-SELECT sum(id) FROM tru_ftable; -- 60
- sum
------
- 60
+-- Both parent and child tables in the foreign server are truncated
+-- even though ONLY is specified because ONLY has no effect
+-- when truncating a foreign table.
+TRUNCATE ONLY tru_ftable;
+SELECT count(*) FROM tru_ftable; -- 0
+ count
+-------
+ 0
(1 row)
INSERT INTO tru_rtable0 (SELECT x FROM generate_series(21,25) x);
-SELECT sum(id) FROM tru_ftable; -- 175
+INSERT INTO tru_rtable0_child (SELECT x FROM generate_series(26,30) x);
+SELECT sum(id) FROM tru_ftable; -- 255
sum
-----
- 175
+ 255
(1 row)
TRUNCATE tru_ftable; -- truncate both of parent and child
-SELECT count(*) FROM tru_ftable; -- empty
+SELECT count(*) FROM tru_ftable; -- 0
count
-------
0
diff --git a/contrib/postgres_fdw/postgres_fdw.c b/contrib/postgres_fdw/postgres_fdw.c
index e201b5404e6..8bcdc8d6160 100644
--- a/contrib/postgres_fdw/postgres_fdw.c
+++ b/contrib/postgres_fdw/postgres_fdw.c
@@ -401,7 +401,6 @@ static void postgresExplainForeignModify(ModifyTableState *mtstate,
static void postgresExplainDirectModify(ForeignScanState *node,
ExplainState *es);
static void postgresExecForeignTruncate(List *rels,
- List *rels_extra,
DropBehavior behavior,
bool restart_seqs);
static bool postgresAnalyzeForeignTable(Relation relation,
@@ -2881,7 +2880,6 @@ postgresExplainDirectModify(ForeignScanState *node, ExplainState *es)
*/
static void
postgresExecForeignTruncate(List *rels,
- List *rels_extra,
DropBehavior behavior,
bool restart_seqs)
{
@@ -2964,7 +2962,7 @@ postgresExecForeignTruncate(List *rels,
/* Construct the TRUNCATE command string */
initStringInfo(&sql);
- deparseTruncateSql(&sql, rels, rels_extra, behavior, restart_seqs);
+ deparseTruncateSql(&sql, rels, behavior, restart_seqs);
/* Issue the TRUNCATE command to remote server */
do_sql_command(conn, sql.data);
diff --git a/contrib/postgres_fdw/postgres_fdw.h b/contrib/postgres_fdw/postgres_fdw.h
index 5d44b753140..9591c0f6c26 100644
--- a/contrib/postgres_fdw/postgres_fdw.h
+++ b/contrib/postgres_fdw/postgres_fdw.h
@@ -209,7 +209,6 @@ extern void deparseAnalyzeSql(StringInfo buf, Relation rel,
List **retrieved_attrs);
extern void deparseTruncateSql(StringInfo buf,
List *rels,
- List *rels_extra,
DropBehavior behavior,
bool restart_seqs);
extern void deparseStringLiteral(StringInfo buf, const char *val);
diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql
index 74590089bd1..dcd36a9753e 100644
--- a/contrib/postgres_fdw/sql/postgres_fdw.sql
+++ b/contrib/postgres_fdw/sql/postgres_fdw.sql
@@ -2355,7 +2355,6 @@ drop table loc3;
-- test for TRUNCATE
-- ===================================================================
CREATE TABLE tru_rtable0 (id int primary key);
-CREATE TABLE tru_rtable1 (id int primary key);
CREATE FOREIGN TABLE tru_ftable (id int)
SERVER loopback OPTIONS (table_name 'tru_rtable0');
INSERT INTO tru_rtable0 (SELECT x FROM generate_series(1,10) x);
@@ -2363,6 +2362,7 @@ INSERT INTO tru_rtable0 (SELECT x FROM generate_series(1,10) x);
CREATE TABLE tru_ptable (id int) PARTITION BY HASH(id);
CREATE TABLE tru_ptable__p0 PARTITION OF tru_ptable
FOR VALUES WITH (MODULUS 2, REMAINDER 0);
+CREATE TABLE tru_rtable1 (id int primary key);
CREATE FOREIGN TABLE tru_ftable__p1 PARTITION OF tru_ptable
FOR VALUES WITH (MODULUS 2, REMAINDER 1)
SERVER loopback OPTIONS (table_name 'tru_rtable1');
@@ -2428,6 +2428,8 @@ SELECT count(*) from tru_ftable; -- 0
SELECT count(*) from tru_pk_ftable; -- 0
-- truncate with ONLY clause
+-- Since ONLY is specified, the table tru_ftable_child that inherits
+-- tru_ftable_parent locally is not truncated.
TRUNCATE ONLY tru_ftable_parent;
SELECT sum(id) FROM tru_ftable_parent; -- 126
TRUNCATE tru_ftable_parent;
@@ -2439,13 +2441,17 @@ INSERT INTO tru_rtable0 (SELECT x FROM generate_series(5,9) x);
INSERT INTO tru_rtable0_child (SELECT x FROM generate_series(10,14) x);
SELECT sum(id) FROM tru_ftable; -- 95
-TRUNCATE ONLY tru_ftable; -- truncate only parent portion
-SELECT sum(id) FROM tru_ftable; -- 60
+-- Both parent and child tables in the foreign server are truncated
+-- even though ONLY is specified because ONLY has no effect
+-- when truncating a foreign table.
+TRUNCATE ONLY tru_ftable;
+SELECT count(*) FROM tru_ftable; -- 0
INSERT INTO tru_rtable0 (SELECT x FROM generate_series(21,25) x);
-SELECT sum(id) FROM tru_ftable; -- 175
+INSERT INTO tru_rtable0_child (SELECT x FROM generate_series(26,30) x);
+SELECT sum(id) FROM tru_ftable; -- 255
TRUNCATE tru_ftable; -- truncate both of parent and child
-SELECT count(*) FROM tru_ftable; -- empty
+SELECT count(*) FROM tru_ftable; -- 0
-- cleanup
DROP FOREIGN TABLE tru_ftable_parent, tru_ftable_child, tru_pk_ftable,tru_ftable__p1,tru_ftable;