pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: Rupert Agnew <RAgnew@albacorecapital.com>
Cc: pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
Subject: Re: Cannot refresh materialized view concurrently if you have a column name called "mv"
Date: Thu, 3 Mar 2022 12:51:57 -0500
Message-ID: <YiEAPU0XnxHbQ19+@momjian.us> (raw)
In-Reply-To: <8ef7dd97598843f188c7f1d3490ed635@ALBACAPUKEX01.ad.accap.co.uk>
References: <8ef7dd97598843f188c7f1d3490ed635@ALBACAPUKEX01.ad.accap.co.uk>

On Thu, Mar  3, 2022 at 02:53:47PM +0000, Rupert Agnew wrote:
> Hi PSQL bugs,
> 
> I have discovered a bug where a column name called “mv” in a materialized view
> cannot be refreshed concurrently due to an internal PostgreSQL table alias also
> named “mv”.
> 
> Refreshing materialized view without the concurrently keyword runs just fine.
> 
> This can be replicated with the following few lines of code:
> 
> create materialized view tmp_mv as
> 
> select 1 id, 2 mv;
> 
> create unique index tmp_mv_index on tmp_mv(id);
> 
> refresh materialized view concurrently tmp_mv;
> 
> The error (42702) outputted:
> 
> psycopg2.errors.AmbiguousColumn: column reference "mv" is ambiguous
> 
> LINE 1: ...alog.=) mv.id AND newdata OPERATOR(pg_catalog.*=) mv) WHERE ...
> 
>                                                              ^
> 
> QUERY:  CREATE TEMP TABLE pg_temp_13.pg_temp_153460785_2 AS SELECT mv.ctid AS
> tid, newdata FROM public.tmp_mv mv FULL JOIN pg_temp_13.pg_temp_153460785
> newdata ON (newdata.id OPERATOR(pg_catalog.=) mv.id AND newdata OPERATOR
> (pg_catalog.*=) mv) WHERE newdata IS NULL OR mv IS NULL ORDER BY tid
> 
> To solve this you need to add the table alias to the column name in the where
> statement on the final line in the error message highlighted above.
> 
>  
> 
> I am running the following PostgreSQL version:
> 
> PostgreSQL 10.14 on x86_64-pc-linux-gnu, compiled by
> x86_64-unknown-linux-gnu-gcc (GCC) 4.9.4, 64-bit

Uh, this is fixed in Postgres 10.18:

	https://www.postgresql.org/docs/10/release-10-18.html

	Avoid alias conflicts in queries generated for REFRESH MATERIALIZED VIEW
	CONCURRENTLY (Tom Lane, Bharath Rupireddy)
	
	This command failed on materialized views containing columns with
	certain names, notably mv and newdata.

by this commit:

	https://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=9179a82d7a

	commit 9179a82d7a
	Author: Tom Lane <tgl@sss.pgh.pa.us>
	Date:   Sat Aug 7 13:29:32 2021 -0400
	
	    Really fix the ambiguity in REFRESH MATERIALIZED VIEW CONCURRENTLY.
	
	    Rather than trying to pick table aliases that won't conflict with
	    any possible user-defined matview column name, adjust the queries'
	    syntax so that the aliases are only used in places where they can't be
	    mistaken for column names.  Mostly this consists of writing "alias.*"
	    not just "alias", which adds clarity for humans as well as machines.
	    We do have the issue that "SELECT alias.*" acts differently from
	    "SELECT alias", but we can use the same hack ruleutils.c uses for
	    whole-row variables in SELECT lists: write "alias.*::compositetype".
	
	    We might as well revert to the original aliases after doing this;
	    they're a bit easier to read.
	
	    Like 75d66d10e, back-patch to all supported branches.
	
	    Discussion: https://postgr.es/m/2488325.1628261320@sss.pgh.pa.us

So, upgrade to 10.18, or idally 10.20, and it should work fine.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  If only the physical world exists, free will is an illusion.






view thread (2+ messages)

Message-ID: <YiEAPU0XnxHbQ19+@momjian.us>
Permalink:  ../YiEAPU0XnxHbQ19+@momjian.us/
Also on:    postgresql.org/message-id/YiEAPU0XnxHbQ19+@momjian.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: bruce@momjian.us, RAgnew@albacorecapital.com
  Subject: Re: Cannot refresh materialized view concurrently if you have a column name called "mv"
  In-Reply-To: <YiEAPU0XnxHbQ19+@momjian.us>
  References: <8ef7dd97598843f188c7f1d3490ed635@ALBACAPUKEX01.ad.accap.co.uk> <YiEAPU0XnxHbQ19+@momjian.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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