I have recently discovered a PostgreSQL behavior which is strange and problematic in my opinion.
Consider this simple query
SELECT
'something confidential'
WHERE FALSE;
The nonsense clause WHERE FALSE
resembles here a very strict permission check.
That works as expected: Nothing is returned.
Now, imagine you add an additional column e.g. a count
function.
This gives is this query:
SELECT
'something confidential'
,count(CURRENT_DATE)
WHERE FALSE;
(The parameter of count
could be anything. CURRENT_DATE
is just a random example.)
Now, we get
something confidential | 0
I guess, this is somehow on purpose, in the sense that SELECT count(CURRENT_DATE);
returns 1 if the condition is truthy and 0 if the condition is falsy.
However, I consider this as problematic in situations where you are not aware of it.
So, my questions are
- How come? What is the background of this behavior?
- How can I make such a query return zero rows instead of one row with value 0 for
count
? - Is there a way directly in the query for preventing accidentally returning rows you don't want to return by adding aggregate functions? (I.e. apart from external tests etc.)
I'm using PostgreSQL 12.1, but the behavior is the same for older versions.
Best Answer
This is standard SQL behaviour for pretty much all databases.
There are three ways how rows are returned:
In other words, aggregating without grouping behaves as if there is a single group, i.e., as if you had written
GROUP BY ()
(a GROUP BY clause with an empty list results in a single group 'from nowhere', similar to how a query without FROM results in a single row 'from nowhere').To prevent returning this single-group row, you can
add an explicit GROUP BY, so that you end up in case 3. above (after the WHERE has filtered out all rows, there is no value from which a group could be created):
move the aggregation into a subquery, so that the permission check affects the output of that:
move the permission check into the HAVING clause (if this is possible), so that the output for the single group is thrown away: