pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Johnston <polobo@yahoo.com>
To: 'Samuel Gendler' <sgendler@ideasculptor.com>
To: pgsql-sql@postgresql.org
Subject: Re: ambiguous local variable name in 9.0 proc
Date: Wed, 21 Sep 2011 19:49:05 -0400
Message-ID: <00c301cc78b9$0ca38e30$25eaaa90$@yahoo.com> (raw)
In-Reply-To: <CAEV0TzB5GDuaG=7OOv_7m6OWagAkkUmkeh=1a62E46o4wKdtgg@mail.gmail.com>
References: <CAEV0TzB5GDuaG=7OOv_7m6OWagAkkUmkeh=1a62E46o4wKdtgg@mail.gmail.com>

 

From: pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org]
On Behalf Of Samuel Gendler
Sent: Wednesday, September 21, 2011 7:35 PM
To: pgsql-sql@postgresql.org
Subject: [SQL] ambiguous local variable name in 9.0 proc

 

I've got a stored proc (which worked fine in 8.3 and 8.4) that is declared
as such:

 

CREATE OR REPLACE FUNCTION reporting_mgmt.aggregate_timescales_impl (

       div_start TIMESTAMP WITHOUT TIME ZONE,

       tbl_schema VARCHAR, 

       tbl_root VARCHAR, 

       fine_timescale VARCHAR,

       coarse_timescale VARCHAR, 

       coarser_timescale VARCHAR,

       fact_fields VARCHAR, 

       dim_fields VARCHAR, 

       sum_fields VARCHAR) 

RETURNS INTEGER

AS $$

 

Within that proc, I've got the following line:

 

    IF EXISTS ( SELECT table_name FROM information_schema.tables

                  WHERE table_schema = tbl_schema

                        AND table_name = tbl_fine_part_old ) THEN

        IF EXISTS ( SELECT status FROM reporting_mgmt.etl_status AS e

                     WHERE tbl_schema = e.tbl_schema

                       AND tbl_root = e.tbl_root

                       AND div_start_old = e.fine_time

                       AND coarse_timescale = e.coarse_scale

                       AND status = 0 ) THEN

 

And in 9.0, it is generating the following error:

 

ERROR:  column reference "tbl_schema" is ambiguous

LINE 2:                      WHERE tbl_schema = e.tbl_schema

                                  ^

DETAIL:  It could refer to either a PL/pgSQL variable or a table column.

QUERY:  SELECT EXISTS ( SELECT status FROM reporting_mgmt.etl_status AS e

                    WHERE tbl_schema = e.tbl_schema

                      AND tbl_root = e.tbl_root

                      AND div_start_old = e.fine_time 

                      AND coarse_timescale = e.coarse_scale

                      AND status = 0 )

CONTEXT:  PL/pgSQL function "aggregate_timescales_impl" line 52 at IF

PL/pgSQL function "aggregate_timescales" line 23 at RETURN

 

Of course, it is true that tbl_schema could refer to the column in table
etl_status, except older versions of postgres seemed to correctly figure out
that comparing a column to itself isn't terribly useful, so it must be
referring to the pl/pgsql variable rather than the table column.

 

I'm happy to modify the proc definition, except that I am unsure how to do
so other than to rename the variable, which is my least favourite way to do
that.  I'd far rather qualify the name somehow, so that it knows that I am
refering to a local variable, if at all possible.  Suggestions?

 

 

Not tested but I think all local variables are implicitly scoped to the
function name so you should be able to do the following:

 

WHERE reporting_mgmt.aggregate_timescales_impl.tbl_schema = e.tbl_schema

 

You are going to have the same problem with other fields as well (e.g.,
tbl_root).

 

I believe there is some way to define the function so that it does not throw
that particular error; it would be up to you make sure that the ambiguity is
being resolved correctly (which it should in this particular case).  Maybe
look for "SET" variables.

 

You could also copy the tbl_schema variable value into a different variable.

 

DECLARE tbl_schema_copy VARCHAR;  tbl_schema_copy := tbl_schema;

 

. WHERE tbl_schema_copy = e.tbl_schema .

 

David J.

 

 

 

view thread (4+ messages)  latest in thread

Message-ID: <00c301cc78b9$0ca38e30$25eaaa90$@yahoo.com>
Permalink:  ../00c301cc78b9$0ca38e30$25eaaa90$@yahoo.com/
Also on:    postgresql.org/message-id/00c301cc78b9$0ca38e30$25eaaa90$@yahoo.com

 · 

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 using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: polobo@yahoo.com, sgendler@ideasculptor.com
  Subject: Re: ambiguous local variable name in 9.0 proc
  In-Reply-To: <00c301cc78b9$0ca38e30$25eaaa90$@yahoo.com>

* 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