Re: [GENERAL] Count(*) throws error

From: "Simon Riggs" <simon(at)2ndquadrant(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: [GENERAL] Count(*) throws error
Date: 2007-07-11 21:59:36
Message-ID: 1184191176.4316.57.camel@ebony.site
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

On Wed, 2007-07-11 at 17:42 -0400, Tom Lane wrote:
> "Jasbinder Singh Bali" <jsbali(at)gmail(dot)com> writes:
> > I'm using the following statement in my plpgsql function
>
> > SELECT INTO no_rows COUNT(*) FROM tbl_concurrent;
>
> > I have decalred no_rows int4 and initialized it to zero
>
> > Running the function throws the following error:
>
> > ERROR: syntax error at or near "(" at character 13
> > QUERY: SELECT $1 (*) FROM tbl_concurrent
>
> I'll bet a nickel you have a local variable named "count" in that
> function, and plpgsql is blindly trying to substitute its value into
> the SQL query. The replacement of "COUNT" by " $1 " in the query
> text is the tip-off.

I came across a boat load of these the other day. Seems fairly naff that
we substitute variables blindly.

Seems like we could be slightly more friendly without too much bother:
at least only substitute after the VALUES clause in INSERT. We really
shouldn't substitute "var = var" to "$n = $n" either; am I right in
thinking the latter would happen silently and cause potential error?

--
Simon Riggs
EnterpriseDB http://www.enterprisedb.com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2007-07-11 22:13:21 Re: [GENERAL] Count(*) throws error
Previous Message Tom Lane 2007-07-11 21:42:43 Re: Count(*) throws error

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2007-07-11 22:09:55 Re: 2PC-induced lockup
Previous Message Simon Riggs 2007-07-11 21:47:25 Re: 2PC-induced lockup