pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #9398: DELETE refering to a materialized view produces "cannot lock rows in materialized view" error
5+ messages / 3 participants
[nested] [flat]

* BUG #9398: DELETE refering to a materialized view produces "cannot lock rows in materialized view" error
@ 2014-03-01 09:51  no-email@example.com
  0 siblings, 1 reply; 5+ messages in thread

From: no-email@example.com @ 2014-03-01 09:51 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      9398
Logged by:          nakag
Email address:      no-email@example.com
PostgreSQL version: 9.3.3
Operating system:   Linux
Description:        

CREATE TABLE base ( id int primary key );
CREATE MATERIALIZED VIEW mv AS SELECT * FROM base;
CREATE TABLE d ( id int primary key );
DELETE FROM d WHERE EXISTS ( SELECT * FROM mv WHERE mv.id = d.id );

The above code produces an ERROR "cannot lock rows in materialized view."


-- 
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 #9398: DELETE refering to a materialized view produces "cannot lock rows in materialized view" error
@ 2014-03-02 03:15  Michael Paquier <michael.paquier@gmail.com>
  parent: no-email@example.com
  0 siblings, 1 reply; 5+ messages in thread

From: Michael Paquier @ 2014-03-02 03:15 UTC (permalink / raw)
  To: no-email@example.com; +Cc: pgsql-bugs

On Sat, Mar 1, 2014 at 6:51 PM,  <no-email@example.com> wrote:
> The following bug has been logged on the website:
>
> Bug reference:      9398
> Logged by:          nakag
> Email address:      no-email@example.com
> PostgreSQL version: 9.3.3
> Operating system:   Linux
> Description:
>
> CREATE TABLE base ( id int primary key );
> CREATE MATERIALIZED VIEW mv AS SELECT * FROM base;
> CREATE TABLE d ( id int primary key );
> DELETE FROM d WHERE EXISTS ( SELECT * FROM mv WHERE mv.id = d.id );
>
> The above code produces an ERROR "cannot lock rows in materialized view."
This smells like a limitation to matviews and not a bug... This error
message refers to CheckValidRowMarkRel:execMain.c:
        case RELKIND_MATVIEW:
            /* Should not get here */
            ereport(ERROR,
                    (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                     errmsg("cannot lock rows in materialized view \"%s\"",
                            RelationGetRelationName(rel))));
            break;
Even if it is clearly written that this code path should not be
taken... Well it is actually taken.

Note that doing a similar operation on a foreign table or a view works:
=# create table aa (a int);
CREATE TABLE
=# create materialized view bb as select * from aa;
SELECT 0
=# delete from aa using bb where aa.a = bb.a;
ERROR:  42809: cannot lock rows in materialized view "bb"
LOCATION:  CheckValidRowMarkRel, execMain.c:1109
Time: 0.929 ms
=# create view cc as select * from aa;
CREATE VIEW
Time: 10.108 ms
=# delete from aa using cc where aa.a = cc.a;
DELETE 0
-- Create FDW server, etc...
=# CREATE FOREIGN TABLE aa_foreign (a int) SERVER postgres_server
OPTIONS (table_name 'aa');
CREATE FOREIGN TABLE
Time: 2.290 ms
=# delete from aa using aa_foreign where aa.a = aa_foreign.a;
DELETE 0

For views, planner expands the view to the parent relations to not
face this error. But this is not doable for a matview because I do not
think we can take locks on its rows without support for incremental
updates. Am I right? Shouldn't the error message be more explicit
here?
Regards,
-- 
Michael


-- 
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 #9398: DELETE refering to a materialized view produces "cannot lock rows in materialized view" error
@ 2014-03-05 23:46  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2014-03-05 23:46 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: no-email@example.com; pgsql-bugs; Kevin Grittner <kgrittn@ymail.com>

Michael Paquier <michael.paquier@gmail.com> writes:
> On Sat, Mar 1, 2014 at 6:51 PM,  <no-email@example.com> wrote:
>> CREATE TABLE base ( id int primary key );
>> CREATE MATERIALIZED VIEW mv AS SELECT * FROM base;
>> CREATE TABLE d ( id int primary key );
>> DELETE FROM d WHERE EXISTS ( SELECT * FROM mv WHERE mv.id = d.id );
>> 
>> The above code produces an ERROR "cannot lock rows in materialized view."

> This smells like a limitation to matviews and not a bug...

Oh, it's a bug all right.  There is no reason this command should be
rejected.

There are two possible fixes:

1. We could teach the planner (planner.c, around line 2210 in HEAD)
that rows coming from materialized views need to be processed via
ROW_MARK_COPY instead of ROW_MARK_REFERENCE.

2. We could remove the error complaint in CheckValidRowMarkRel(),
allowing a matview row to be marked the same as a regular-table row.

Since matview rows do in fact have TIDs and the same
visibility/vacuumability rules as regular-table rows (no?), I see no
reason that #2 wouldn't work, though I admit I've not actually tried it.
(There might be similar checks on relkind further down that would also
have to be adjusted, for one thing.)  CheckValidRowMarkRel is not really
about locking; the requirement is only that it be possible to fetch back a
previously-read row value using the TID, and be sure that we get the same
tuple value we'd seen earlier in the same query.

Assuming that it does work, I think #2 is a preferable fix to #1,
because #1 implies making a usually-unnecessary copy of each row
selected from the matview.

Comments, objections?

			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

* Re: BUG #9398: DELETE refering to a materialized view produces "cannot lock rows in materialized view" error
@ 2014-03-06 06:25  Michael Paquier <michael.paquier@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Michael Paquier @ 2014-03-06 06:25 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: no-email@example.com; pgsql-bugs; Kevin Grittner <kgrittn@ymail.com>

On Thu, Mar 6, 2014 at 8:46 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Michael Paquier <michael.paquier@gmail.com> writes:
>> On Sat, Mar 1, 2014 at 6:51 PM,  <no-email@example.com> wrote:
>>> CREATE TABLE base ( id int primary key );
>>> CREATE MATERIALIZED VIEW mv AS SELECT * FROM base;
>>> CREATE TABLE d ( id int primary key );
>>> DELETE FROM d WHERE EXISTS ( SELECT * FROM mv WHERE mv.id = d.id );
>>>
>>> The above code produces an ERROR "cannot lock rows in materialized view."
>
>> This smells like a limitation to matviews and not a bug...
>
> Oh, it's a bug all right.  There is no reason this command should be
> rejected.
OK. So I was obviously wrong :)

> 1. We could teach the planner (planner.c, around line 2210 in HEAD)
> that rows coming from materialized views need to be processed via
> ROW_MARK_COPY instead of ROW_MARK_REFERENCE.
>
> 2. We could remove the error complaint in CheckValidRowMarkRel(),
> allowing a matview row to be marked the same as a regular-table row.
>
> Since matview rows do in fact have TIDs and the same
> visibility/vacuumability rules as regular-table rows (no?), I see no
> reason that #2 wouldn't work, though I admit I've not actually tried it.
> (There might be similar checks on relkind further down that would also
> have to be adjusted, for one thing.)  CheckValidRowMarkRel is not really
> about locking; the requirement is only that it be possible to fetch back a
> previously-read row value using the TID, and be sure that we get the same
> tuple value we'd seen earlier in the same query.
>
> Assuming that it does work, I think #2 is a preferable fix to #1,
> because #1 implies making a usually-unnecessary copy of each row
> selected from the matview.
>
> Comments, objections?
I am not much a fan of #1, because this need some extra checks to
prevent locking clauses like FOR SHARE to run on matviews. And by
looking for example at commit 88c5566, this is not allowed.

After digging into #2, I finished with the attached patch that passes
regression tests. The idea is simply to allow a matview to use
ROW_MARK_REFERENCE and ROW_MARK_COPY when its rows are referenced. I
added a regression test as well in the patch.
Regards,
-- 
Michael

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

20140306_fix_matview_refs.patch (text/plain, 2.3K) download *** a/src/backend/executor/execMain.c --- b/src/backend/executor/execMain.c *************** *** 1102,1112 **** CheckValidRowMarkRel(Relation rel, RowMarkType markType) RelationGetRelationName(rel)))); break; case RELKIND_MATVIEW: ! /* Should not get here */ ! ereport(ERROR, ! (errcode(ERRCODE_WRONG_OBJECT_TYPE), ! errmsg("cannot lock rows in materialized view \"%s\"", ! RelationGetRelationName(rel)))); break; case RELKIND_FOREIGN_TABLE: /* Should not get here */ --- 1102,1117 ---- RelationGetRelationName(rel)))); break; case RELKIND_MATVIEW: ! /* ! * Allow referencing rows from materialized views as long as no ! * locking clause is involved. ! */ ! if (markType != ROW_MARK_REFERENCE && ! markType != ROW_MARK_COPY) ! ereport(ERROR, ! (errcode(ERRCODE_WRONG_OBJECT_TYPE), ! errmsg("cannot lock rows in materialized view \"%s\"", ! RelationGetRelationName(rel)))); break; case RELKIND_FOREIGN_TABLE: /* Should not get here */ *** a/src/test/regress/expected/matview.out --- b/src/test/regress/expected/matview.out *************** *** 476,478 **** SELECT * FROM mv_v; --- 476,489 ---- DROP TABLE v CASCADE; NOTICE: drop cascades to materialized view mv_v + -- make sure that matview rows can be referenced as source rows + CREATE TABLE base_table AS SELECT generate_series(1,10) AS a; + CREATE MATERIALIZED VIEW base_matview AS SELECT a FROM base_table; + DELETE FROM base_table USING base_matview WHERE base_matview.a = base_table.a; + SELECT * FROM base_table; + a + --- + (0 rows) + + DROP TABLE base_table CASCADE; + NOTICE: drop cascades to materialized view base_matview *** a/src/test/regress/sql/matview.sql --- b/src/test/regress/sql/matview.sql *************** *** 186,188 **** REFRESH MATERIALIZED VIEW CONCURRENTLY mv_v; --- 186,195 ---- SELECT * FROM v; SELECT * FROM mv_v; DROP TABLE v CASCADE; + + -- make sure that matview rows can be referenced as source rows + CREATE TABLE base_table AS SELECT generate_series(1,10) AS a; + CREATE MATERIALIZED VIEW base_matview AS SELECT a FROM base_table; + DELETE FROM base_table USING base_matview WHERE base_matview.a = base_table.a; + SELECT * FROM base_table; + DROP TABLE base_table CASCADE;
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: BUG #9398: DELETE refering to a materialized view produces "cannot lock rows in materialized view" error
@ 2014-03-06 16:43  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2014-03-06 16:43 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; +Cc: pgsql-bugs; Kevin Grittner <kgrittn@ymail.com>

Michael Paquier <michael.paquier@gmail.com> writes:
> On Thu, Mar 6, 2014 at 8:46 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> 2. We could remove the error complaint in CheckValidRowMarkRel(),
>> allowing a matview row to be marked the same as a regular-table row.
>> 
>> Since matview rows do in fact have TIDs and the same
>> visibility/vacuumability rules as regular-table rows (no?), I see no
>> reason that #2 wouldn't work, though I admit I've not actually tried it.

> After digging into #2, I finished with the attached patch that passes
> regression tests. The idea is simply to allow a matview to use
> ROW_MARK_REFERENCE and ROW_MARK_COPY when its rows are referenced. I
> added a regression test as well in the patch.

The ROW_MARK_COPY test is unnecessary since control will never reach this
function for that case.  (If it did, most of the other case branches would
need to be checking it; checking only here is inconsistent.)

Also, after comparing this to the code in CheckValidResultRel, I wonder
why we aren't allowing SELECT FOR UPDATE in maintenance mode; that is,
shouldn't the test go like this:

	  if (markType != ROW_MARK_REFERENCE &&
	      !MatViewIncrementalMaintenanceIsEnabled())

However, that smells less like a bug fix and more like a feature addition,
so I'll leave it to Kevin to decide whether that's appropriate.  In the
meantime, I committed the clear bug fix with some cosmetic adjustments.

			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:[~2014-03-06 16:43 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-03-01 09:51 BUG #9398: DELETE refering to a materialized view produces "cannot lock rows in materialized view" error no-email@example.com
2014-03-02 03:15 ` Michael Paquier <michael.paquier@gmail.com>
2014-03-05 23:46   ` Tom Lane <tgl@sss.pgh.pa.us>
2014-03-06 06:25     ` Michael Paquier <michael.paquier@gmail.com>
2014-03-06 16:43       ` 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