pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #8275: Updateable View based on inheritance (partition) throws Error on INSERT Statement
5+ messages / 4 participants
[nested] [flat]
* BUG #8275: Updateable View based on inheritance (partition) throws Error on INSERT Statement
@ 2013-07-02 04:59 rushabh.lathia@gmail.com
0 siblings, 1 reply; 5+ messages in thread
From: rushabh.lathia@gmail.com @ 2013-07-02 04:59 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 8275
Logged by: Rushabh Lathia
Email address: rushabh.lathia@gmail.com
PostgreSQL version: 9.2.4
Operating system: All
Description:
View based on inheritance throws error on insert statement.
Testcase:
DROP TABLE tp_sales cascade;
CREATE TABLE tp_sales
(
salesman_id INT4,
salesman_name VARCHAR,
sales_region VARCHAR,
sales_amount INT4
);
create table tp_sales_p_india ( check (sales_region = 'INDIA')) inherits
(tp_sales);
create table tp_sales_p_rest (check (sales_region != 'INDIA')) inherits
(tp_sales);
CREATE OR REPLACE FUNCTION tp_sales_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.sales_region = 'INDIA' THEN
INSERT INTO tp_sales_p_india VALUES (NEW.*);
ELSE
INSERT INTO tp_sales_p_rest VALUES (NEW.*);
END IF;
RETURN NULL;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER insert_tp_sales_trigger
BEFORE INSERT ON tp_sales
FOR EACH ROW EXECUTE PROCEDURE tp_sales_insert_trigger();
INSERT INTO tp_sales VALUES (100,'Foo','INDIA',15000);
INSERT INTO tp_sales VALUES (110,'Bar','UK',24000);
CREATE view view_tp_sales as SELECT * FROM tp_sales;
-- run insert on view
postgres=# INSERT INTO view_tp_sales VALUES (120,'XYZ','INDIA',11000);
ERROR: new row for relation "tp_sales_p_rest" violates check constraint
"tp_sales_p_rest_sales_region_check"
DETAIL: Failing row contains (120, XYZ, INDIA, 11000).
postgres=# INSERT INTO view_tp_sales VALUES (120,'ABC','HELLO',11000);
ERROR: new row for relation "tp_sales_p_india" violates check constraint
"tp_sales_p_india_sales_region_check"
DETAIL: Failing row contains (120, ABC, HELLO, 11000).
postgres=# select version();
version
-----------------------------------------------------------------------------------------------------------------
PostgreSQL 9.4devel on x86_64-unknown-linux-gnu, compiled by gcc (GCC)
4.4.7 20120313 (Red Hat 4.4.7-3), 64-bit
(1 row)
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #8275: Updateable View based on inheritance (partition) throws Error on INSERT Statement
@ 2013-07-02 07:44 Rushabh Lathia <rushabh.lathia@gmail.com>
parent: rushabh.lathia@gmail.com
0 siblings, 1 reply; 5+ messages in thread
From: Rushabh Lathia @ 2013-07-02 07:44 UTC (permalink / raw)
To: pgsql-bugs
Looking further I just found that, if we don't want query to scan through
child table then we should use ONLY during CREATE VIEW.
So if I replaced my create view query with:
CREATE view view_tp_sales as SELECT * FROM ONLY tp_sales;
Then INSERT stmt working find.
So when you create VIEW on top of inheritance (partition) table you need to
create it using ONLY keyword, right ?
anyone please correct me if I am wrong.
Regards,
Rushabh
On Tue, Jul 2, 2013 at 10:29 AM, <rushabh.lathia@gmail.com> wrote:
> The following bug has been logged on the website:
>
> Bug reference: 8275
> Logged by: Rushabh Lathia
> Email address: rushabh.lathia@gmail.com
> PostgreSQL version: 9.2.4
> Operating system: All
> Description:
>
> View based on inheritance throws error on insert statement.
>
>
> Testcase:
>
>
> DROP TABLE tp_sales cascade;
>
>
> CREATE TABLE tp_sales
> (
> salesman_id INT4,
> salesman_name VARCHAR,
> sales_region VARCHAR,
> sales_amount INT4
> );
>
>
> create table tp_sales_p_india ( check (sales_region = 'INDIA')) inherits
> (tp_sales);
> create table tp_sales_p_rest (check (sales_region != 'INDIA')) inherits
> (tp_sales);
>
>
> CREATE OR REPLACE FUNCTION tp_sales_insert_trigger()
> RETURNS TRIGGER AS $$
> BEGIN
> IF NEW.sales_region = 'INDIA' THEN
> INSERT INTO tp_sales_p_india VALUES (NEW.*);
> ELSE
> INSERT INTO tp_sales_p_rest VALUES (NEW.*);
> END IF;
> RETURN NULL;
> END;
> $$
> LANGUAGE plpgsql;
>
>
>
>
> CREATE TRIGGER insert_tp_sales_trigger
> BEFORE INSERT ON tp_sales
> FOR EACH ROW EXECUTE PROCEDURE tp_sales_insert_trigger();
>
>
> INSERT INTO tp_sales VALUES (100,'Foo','INDIA',15000);
> INSERT INTO tp_sales VALUES (110,'Bar','UK',24000);
>
>
> CREATE view view_tp_sales as SELECT * FROM tp_sales;
>
>
> -- run insert on view
> postgres=# INSERT INTO view_tp_sales VALUES (120,'XYZ','INDIA',11000);
> ERROR: new row for relation "tp_sales_p_rest" violates check constraint
> "tp_sales_p_rest_sales_region_check"
> DETAIL: Failing row contains (120, XYZ, INDIA, 11000).
> postgres=# INSERT INTO view_tp_sales VALUES (120,'ABC','HELLO',11000);
> ERROR: new row for relation "tp_sales_p_india" violates check constraint
> "tp_sales_p_india_sales_region_check"
> DETAIL: Failing row contains (120, ABC, HELLO, 11000).
> postgres=# select version();
> version
>
>
> -----------------------------------------------------------------------------------------------------------------
> PostgreSQL 9.4devel on x86_64-unknown-linux-gnu, compiled by gcc (GCC)
> 4.4.7 20120313 (Red Hat 4.4.7-3), 64-bit
> (1 row)
>
>
>
>
> --
> Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-bugs
>
--
Rushabh Lathia
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #8275: Updateable View based on inheritance (partition) throws Error on INSERT Statement
@ 2013-07-02 08:30 Dean Rasheed <dean.a.rasheed@gmail.com>
parent: Rushabh Lathia <rushabh.lathia@gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: Dean Rasheed @ 2013-07-02 08:30 UTC (permalink / raw)
To: Rushabh Lathia <rushabh.lathia@gmail.com>; +Cc: pgsql-bugs
On 2 July 2013 08:44, Rushabh Lathia <rushabh.lathia@gmail.com> wrote:
> Looking further I just found that, if we don't want query to scan through
> child table then we should use ONLY during CREATE VIEW.
>
> So if I replaced my create view query with:
>
> CREATE view view_tp_sales as SELECT * FROM ONLY tp_sales;
>
> Then INSERT stmt working find.
>
> So when you create VIEW on top of inheritance (partition) table you need to
> create it using ONLY keyword, right ?
> anyone please correct me if I am wrong.
>
> Regards,
> Rushabh
>
>
> On Tue, Jul 2, 2013 at 10:29 AM, <rushabh.lathia@gmail.com> wrote:
>>
>> The following bug has been logged on the website:
>>
>> Bug reference: 8275
>> Logged by: Rushabh Lathia
>> Email address: rushabh.lathia@gmail.com
>> PostgreSQL version: 9.2.4
>> Operating system: All
>> Description:
>>
>> View based on inheritance throws error on insert statement.
>>
>>
>> Testcase:
>>
>>
>> DROP TABLE tp_sales cascade;
>>
>>
>> CREATE TABLE tp_sales
>> (
>> salesman_id INT4,
>> salesman_name VARCHAR,
>> sales_region VARCHAR,
>> sales_amount INT4
>> );
>>
>>
>> create table tp_sales_p_india ( check (sales_region = 'INDIA')) inherits
>> (tp_sales);
>> create table tp_sales_p_rest (check (sales_region != 'INDIA')) inherits
>> (tp_sales);
>>
>>
>> CREATE OR REPLACE FUNCTION tp_sales_insert_trigger()
>> RETURNS TRIGGER AS $$
>> BEGIN
>> IF NEW.sales_region = 'INDIA' THEN
>> INSERT INTO tp_sales_p_india VALUES (NEW.*);
>> ELSE
>> INSERT INTO tp_sales_p_rest VALUES (NEW.*);
>> END IF;
>> RETURN NULL;
>> END;
>> $$
>> LANGUAGE plpgsql;
>>
>>
>>
>>
>> CREATE TRIGGER insert_tp_sales_trigger
>> BEFORE INSERT ON tp_sales
>> FOR EACH ROW EXECUTE PROCEDURE tp_sales_insert_trigger();
>>
>>
>> INSERT INTO tp_sales VALUES (100,'Foo','INDIA',15000);
>> INSERT INTO tp_sales VALUES (110,'Bar','UK',24000);
>>
>>
>> CREATE view view_tp_sales as SELECT * FROM tp_sales;
>>
>>
>> -- run insert on view
>> postgres=# INSERT INTO view_tp_sales VALUES (120,'XYZ','INDIA',11000);
>> ERROR: new row for relation "tp_sales_p_rest" violates check constraint
>> "tp_sales_p_rest_sales_region_check"
>> DETAIL: Failing row contains (120, XYZ, INDIA, 11000).
>> postgres=# INSERT INTO view_tp_sales VALUES (120,'ABC','HELLO',11000);
>> ERROR: new row for relation "tp_sales_p_india" violates check constraint
>> "tp_sales_p_india_sales_region_check"
>> DETAIL: Failing row contains (120, ABC, HELLO, 11000).
>> postgres=# select version();
>> version
>>
>>
>> -----------------------------------------------------------------------------------------------------------------
>> PostgreSQL 9.4devel on x86_64-unknown-linux-gnu, compiled by gcc (GCC)
>> 4.4.7 20120313 (Red Hat 4.4.7-3), 64-bit
>> (1 row)
>>
This is a bug. Thanks for the report!
I think the rewritten query should only use inheritance if inheritance
was requested in the original query, *and* if inheritance was enabled
in the view's query, per attached patch against HEAD.
We should probably also include some additional regression tests for
these kinds of query.
Regards,
Dean
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
updatable-views-inh.patch (application/octet-stream, 979B) download
diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c
new file mode 100644
index a467588..085e22b
*** a/src/backend/rewrite/rewriteHandler.c
--- b/src/backend/rewrite/rewriteHandler.c
*************** rewriteTargetView(Query *parsetree, Rela
*** 2387,2392 ****
--- 2387,2401 ----
new_rte = (RangeTblEntry *) copyObject(base_rte);
parsetree->rtable = lappend(parsetree->rtable, new_rte);
new_rt_index = list_length(parsetree->rtable);
+ new_rte->inFromCl = view_rte->inFromCl;
+
+ /*
+ * Only use inheritance for the new target RTE if it was requested in the
+ * outer query *and* in the view query. If the outer query didn't request
+ * inheritance, or the view query doesn't include inherited relations, then
+ * we shouldn't include any relations inherited by the new target RTE.
+ */
+ new_rte->inh = view_rte->inh && base_rte->inh;
/*
* Make a copy of the view's targetlist, adjusting its Vars to reference
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #8275: Updateable View based on inheritance (partition) throws Error on INSERT Statement
@ 2013-07-02 10:42 Dean Rasheed <dean.a.rasheed@gmail.com>
parent: Dean Rasheed <dean.a.rasheed@gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: Dean Rasheed @ 2013-07-02 10:42 UTC (permalink / raw)
To: Rushabh Lathia <rushabh.lathia@gmail.com>; +Cc: pgsql-bugs
On 2 July 2013 09:30, Dean Rasheed <dean.a.rasheed@gmail.com> wrote:
> I think the rewritten query should only use inheritance if inheritance
> was requested in the original query, *and* if inheritance was enabled
> in the view's query, per attached patch against HEAD.
On second thoughts, I think this should only apply to INSERT.
UPDATE and DELETE should continue work the same as SELECT, respecting
the inheritance flag from the view query regardless of the inheritance
flag in the outer query.
Attached is an updated patch for HEAD, with regression tests. This
should also be applied to the 9.3beta branch.
Regards,
Dean
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
updatable-views-inh.patch (application/octet-stream, 4.9K) download
diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c
new file mode 100644
index a467588..42d66a0
*** a/src/backend/rewrite/rewriteHandler.c
--- b/src/backend/rewrite/rewriteHandler.c
*************** rewriteTargetView(Query *parsetree, Rela
*** 2387,2392 ****
--- 2387,2402 ----
new_rte = (RangeTblEntry *) copyObject(base_rte);
parsetree->rtable = lappend(parsetree->rtable, new_rte);
new_rt_index = list_length(parsetree->rtable);
+ new_rte->inFromCl = view_rte->inFromCl;
+
+ /*
+ * For INSERT, don't use inheritance on the new target RTE even if is is
+ * enabled in the view query --- we only want to insert into the base
+ * parent relation. For UPDATE/DELETE, respect the view query's
+ * inheritance flag.
+ */
+ if (parsetree->commandType == CMD_INSERT)
+ new_rte->inh = false;
/*
* Make a copy of the view's targetlist, adjusting its Vars to reference
diff --git a/src/test/regress/expected/updatable_views.out b/src/test/regress/expected/updatable_views.out
new file mode 100644
index ecb61e0..1363103
*** a/src/test/regress/expected/updatable_views.out
--- b/src/test/regress/expected/updatable_views.out
*************** SELECT * FROM rw_view1;
*** 1063,1065 ****
--- 1063,1165 ----
DROP TABLE base_tbl CASCADE;
NOTICE: drop cascades to view rw_view1
+ -- inheritance tests
+ CREATE TABLE base_tbl_parent (a int);
+ CREATE TABLE base_tbl_child (CHECK (a > 0)) INHERITS (base_tbl_parent);
+ INSERT INTO base_tbl_parent SELECT * FROM generate_series(-8, -1);
+ INSERT INTO base_tbl_child SELECT * FROM generate_series(1, 8);
+ CREATE VIEW rw_view1 AS SELECT * FROM base_tbl_parent;
+ CREATE VIEW rw_view2 AS SELECT * FROM ONLY base_tbl_parent;
+ SELECT * FROM rw_view1 ORDER BY a;
+ a
+ ----
+ -8
+ -7
+ -6
+ -5
+ -4
+ -3
+ -2
+ -1
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ (16 rows)
+
+ SELECT * FROM ONLY rw_view1 ORDER BY a;
+ a
+ ----
+ -8
+ -7
+ -6
+ -5
+ -4
+ -3
+ -2
+ -1
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ (16 rows)
+
+ SELECT * FROM rw_view2 ORDER BY a;
+ a
+ ----
+ -8
+ -7
+ -6
+ -5
+ -4
+ -3
+ -2
+ -1
+ (8 rows)
+
+ INSERT INTO rw_view1 VALUES (-100), (100);
+ INSERT INTO rw_view2 VALUES (-200), (200);
+ UPDATE rw_view1 SET a = a*10 WHERE a IN (-1, 1); -- Should produce -10 and 10
+ UPDATE ONLY rw_view1 SET a = a*10 WHERE a IN (-2, 2); -- Should produce -20 and 20
+ UPDATE rw_view2 SET a = a*10 WHERE a IN (-3, 3); -- Should produce -30 only
+ UPDATE ONLY rw_view2 SET a = a*10 WHERE a IN (-4, 4); -- Should produce -40 only
+ DELETE FROM rw_view1 WHERE a IN (-5, 5); -- Should delete -5 and 5
+ DELETE FROM ONLY rw_view1 WHERE a IN (-6, 6); -- Should delete -6 and 6
+ DELETE FROM rw_view2 WHERE a IN (-7, 7); -- Should delete -7 only
+ DELETE FROM ONLY rw_view2 WHERE a IN (-8, 8); -- Should delete -8 only
+ SELECT * FROM ONLY base_tbl_parent ORDER BY a;
+ a
+ ------
+ -200
+ -100
+ -40
+ -30
+ -20
+ -10
+ 100
+ 200
+ (8 rows)
+
+ SELECT * FROM base_tbl_child ORDER BY a;
+ a
+ ----
+ 3
+ 4
+ 7
+ 8
+ 10
+ 20
+ (6 rows)
+
+ DROP TABLE base_tbl_parent, base_tbl_child CASCADE;
+ NOTICE: drop cascades to 2 other objects
+ DETAIL: drop cascades to view rw_view1
+ drop cascades to view rw_view2
diff --git a/src/test/regress/sql/updatable_views.sql b/src/test/regress/sql/updatable_views.sql
new file mode 100644
index 49dfedd..c8a1c62
*** a/src/test/regress/sql/updatable_views.sql
--- b/src/test/regress/sql/updatable_views.sql
*************** UPDATE rw_view1 SET arr[1] = 42, arr[2]
*** 509,511 ****
--- 509,543 ----
SELECT * FROM rw_view1;
DROP TABLE base_tbl CASCADE;
+
+ -- inheritance tests
+
+ CREATE TABLE base_tbl_parent (a int);
+ CREATE TABLE base_tbl_child (CHECK (a > 0)) INHERITS (base_tbl_parent);
+ INSERT INTO base_tbl_parent SELECT * FROM generate_series(-8, -1);
+ INSERT INTO base_tbl_child SELECT * FROM generate_series(1, 8);
+
+ CREATE VIEW rw_view1 AS SELECT * FROM base_tbl_parent;
+ CREATE VIEW rw_view2 AS SELECT * FROM ONLY base_tbl_parent;
+
+ SELECT * FROM rw_view1 ORDER BY a;
+ SELECT * FROM ONLY rw_view1 ORDER BY a;
+ SELECT * FROM rw_view2 ORDER BY a;
+
+ INSERT INTO rw_view1 VALUES (-100), (100);
+ INSERT INTO rw_view2 VALUES (-200), (200);
+
+ UPDATE rw_view1 SET a = a*10 WHERE a IN (-1, 1); -- Should produce -10 and 10
+ UPDATE ONLY rw_view1 SET a = a*10 WHERE a IN (-2, 2); -- Should produce -20 and 20
+ UPDATE rw_view2 SET a = a*10 WHERE a IN (-3, 3); -- Should produce -30 only
+ UPDATE ONLY rw_view2 SET a = a*10 WHERE a IN (-4, 4); -- Should produce -40 only
+
+ DELETE FROM rw_view1 WHERE a IN (-5, 5); -- Should delete -5 and 5
+ DELETE FROM ONLY rw_view1 WHERE a IN (-6, 6); -- Should delete -6 and 6
+ DELETE FROM rw_view2 WHERE a IN (-7, 7); -- Should delete -7 only
+ DELETE FROM ONLY rw_view2 WHERE a IN (-8, 8); -- Should delete -8 only
+
+ SELECT * FROM ONLY base_tbl_parent ORDER BY a;
+ SELECT * FROM base_tbl_child ORDER BY a;
+
+ DROP TABLE base_tbl_parent, base_tbl_child CASCADE;
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #8275: Updateable View based on inheritance (partition) throws Error on INSERT Statement
@ 2013-07-03 16:29 Tom Lane <tgl@sss.pgh.pa.us>
parent: Dean Rasheed <dean.a.rasheed@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Tom Lane @ 2013-07-03 16:29 UTC (permalink / raw)
To: Dean Rasheed <dean.a.rasheed@gmail.com>; +Cc: Rushabh Lathia <rushabh.lathia@gmail.com>; pgsql-bugs
Dean Rasheed <dean.a.rasheed@gmail.com> writes:
> On 2 July 2013 09:30, Dean Rasheed <dean.a.rasheed@gmail.com> wrote:
>> I think the rewritten query should only use inheritance if inheritance
>> was requested in the original query, *and* if inheritance was enabled
>> in the view's query, per attached patch against HEAD.
> On second thoughts, I think this should only apply to INSERT.
> UPDATE and DELETE should continue work the same as SELECT, respecting
> the inheritance flag from the view query regardless of the inheritance
> flag in the outer query.
[ thinks for a bit... ] Yeah, I agree.
> Attached is an updated patch for HEAD, with regression tests. This
> should also be applied to the 9.3beta branch.
Applied with minor adjustments --- mainly, I took out the inFromCl
twiddling, which AFAICS is neither necessary (nothing downstream of this
looks at inFromCl) nor clearly correct.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2013-07-03 16:29 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-07-02 04:59 BUG #8275: Updateable View based on inheritance (partition) throws Error on INSERT Statement rushabh.lathia@gmail.com
2013-07-02 07:44 ` Rushabh Lathia <rushabh.lathia@gmail.com>
2013-07-02 08:30 ` Dean Rasheed <dean.a.rasheed@gmail.com>
2013-07-02 10:42 ` Dean Rasheed <dean.a.rasheed@gmail.com>
2013-07-03 16:29 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox