The database.pgsql.inc being distributed in v5.1 has invalid DISTINCT syntax that causes SQL errors in its PostgreSQL database. A patch correcting this is attached.

I discovered this when upgrading from a PostgreSQL-based 4.7.6 install of Drupal.

This was found on line 431 of the file, as the patch will show.

Comments

dries’s picture

I don't have a PostgreSQL install so I can't double-check this. Can someone confirm this problem, and its patch. Thanks.

drumm’s picture

Version: 5.1 » 6.x-dev
takashi’s picture

StatusFileSize
new5.1 KB

I think rewrite 'SELECT DISTINCT ON (table.field) table.field ...' to 'SELECT DISTINCT (table.field) ...' may not be enough.
The issue is related to the issue I've reported (http://drupal.org/node/128846#comment-218906), isn't it?

takashi’s picture

I think rewrite 'SELECT DISTINCT ON (table.field) table.field ...' to 'SELECT DISTINCT (table.field) ...' may not be enough.
The issue is related to the issue I've reported (http://drupal.org/node/128846#comment-218906), isn't it?

PMunn’s picture

Version: 6.x-dev » 5.1

I've run queries on my node table looking for duplicate NIDs and don't see any. Perhaps this is a problem with MySql?

Either way, I'd rather not hold up this fix in the face of a larger one. It's hard enough to get action on PostgreSQL patches due to its severe minority status in the Drupal world. Please don't chain anything to it that will hold it down longer. Nobody likes to remake the same patch over and over again.

takashi’s picture

I'm talking about PostgreSQL issue, not MySQL. I want to say that

#1: SELECT DISTINCT ON (n.nid) n.nid, n.sticky FROM node n;

and

#2: SELECT DISTINCT (n.nid), n.sticky FROM node n;

is not equal. I think the patch
'database.pgsql_.inc_.20070225.pgsql_.patch' modifies behaviour of
db_distinct_field() from #1 to #2.

With #1 SQL, SELECT keeps only the first row of each set of rows where
nid is equal (this might be what we want to do in db_distinct_field()).

With #2 SQL, duplicated rows (not only nid, but also sticky are equal)
are removed from the result set.

SELECT + distinct-clause + select-list + from-clause

#1

distinct-clause => DISTINCT ON (n.nid)
select-list => n.nid, n.sticky
from-clause => FROM node.n

#2

distinct-clause => DISTINCT
select-list => (n.nid), n.sticky == n.nid, n.sticky
from-clause => FROM node n;

P.S. I'm very sorry that I posted the same comment twice, it was a
technical problem (I was using mobile environment, and it was not
stable).

P.S. The following is a segment from PostgreSQL manual page
(http://www.postgresql.org/docs/8.2/interactive/sql-select.html).

Synopsis

SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
* | expression [ AS output_name ] [, ...]
[ FROM from_item [, ...] ]
[ WHERE condition ]
[ GROUP BY expression [, ...] ]
[ HAVING condition [, ...] ]
[ { UNION | INTERSECT | EXCEPT } [ ALL ] select ]
[ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ]
[ LIMIT { count | ALL } ]
[ OFFSET start ]
[ FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ] [...] ]

DISTINCT Clause

If DISTINCT is specified, all duplicate rows are removed from the
result set (one row is kept from each group of duplicates).

DISTINCT ON ( expression [, ...] ) keeps only the first row of each
set of rows where the given expressions evaluate to equal.

Note that the "first row" of each set is unpredictable unless ORDER BY
is used to ensure that the desired row appears first.

wedge’s picture

Not sure if it's of any help but I have the same issue. The patch supresses the SQL error but the nodes are sorted in the wrong order on my startpage when the patch is applied.

wedge’s picture

Doh!
the patch attached in http://drupal.org/node/128846#comment-218906 seems to solve the sorting issue as well. Thanks takashi, this has been bugging me for a while.

wedge’s picture

Sorry about the spam...
Did some more testing and have run into issues. Will post another update tomorrow.

wedge’s picture

I will continue discussion in http://drupal.org/node/128846 since it's related to the patch posted there.

PMunn’s picture

Yes, the patch here appears to fix the problem, as well as the ones at the core of that issue. See my comments there on other issues.

I'd close this item only once that other patch is accepted.

drumm’s picture

Status: Needs review » Closed (duplicate)

http://drupal.org/node/128846 is more-recently updated and has well-commented code.