I recently migrated over to postgresql due to a client restriction. I have a view on the frontpage and noticed the results are different. It is tracked down to the views_build_view function. I get the following query in MySQL:

mysql> SELECT DISTINCT(node.nid), node.sticky AS node_sticky, node.created AS node_created_created FROM node node WHERE (node.promote = '1') AND (node.status = '1') ORDER BY node_sticky DESC, node_created_created DESC LIMIT 0, 3;
+-----+-------------+----------------------+
| nid | node_sticky | node_created_created |
+-----+-------------+----------------------+
| 212 |           0 |           1180633301 |
| 202 |           0 |           1175696565 |
| 183 |           0 |           1174488684 |
+-----+-------------+----------------------+
3 rows in set (0.01 sec)

But in postgres I get the following:

drupal5pgsql=> SELECT DISTINCT ON (node.nid) node.nid, node.sticky AS node_sticky, node.created AS node_created_created FROM node node WHERE (node.promote = '1') AND (node.status = '1') ORDER BY node.nid, node_sticky DESC, node_created_created DESC LIMIT 3 OFFSET 0;
 nid | node_sticky | node_created_created 
-----+-------------+----------------------
 182 |           0 |           1174488223
 183 |           0 |           1174488684
 202 |           0 |           1175696565
(3 rows)

Notice with postgresql it has an extra ORDER BY node.nid, which has to be there for postgresql. I tried moved it to the end and taking it away but got errors:

ERROR:  SELECT DISTINCT ON expressions must match initial ORDER BY expressions

Reading the code I am not certain how the query is built. I think it has something to do with db_rewrite_sql. Not certain why it is adding the DISTINCT in the first place. Has anyone else had this problem? Is there a fix?

Comments

pearcec’s picture

Status: Active » Closed (fixed)

There are loads of issues posted about this issue;

http://drupal.org/search/node/postgresql+DISTINCT+ON

This isn't a views problem so I am going to close it.

pearcec’s picture

http://drupal.org/node/128846 This seems to be the best one.