> Yeah, but there is a point about running count(*) one time too many.
> Say if i would like to get a prettyprinting query like this:
>
> SELECT id, @a:=COUNT(*), sum_col::text||'/'@a::text FROM table_name;
>
> That would be DAMN expensive doing with a subselect:
>
> SELECT id, sum_col||'/'||(select COUNT(*) from tabe_name) FROM
> table_name;
>
> I know this example suck eggs, but you get the point where it hurts,
> right?
Are you sure that postgres evaluates that subselect more than once? It
looks to me like it returns a constant result for every row and hence it
will be evaluated once per statement, not once per row. I'm no expert tho.
Can someone answer this?
And if the subselect changes for each row (ie. it's a correlated subquery)
then you cannot use the variable anyway.
It seems to me that if postgres doesn't consider count(*) as a constant then
perhaps it should be taught to? Should be safe shouldn't it? I guess if a
function in your select statemnt is inserting a row then there's trouble.
But if there is, then the sum/count(*) is nonsensical anyway.
Chris