Hi, i get this error on my system
Drupal 5.2
installed modules:
Project
Project issue
Content Access
all latest versions
* warning: pg_query() [function.pg-query]: Query failed: ERROR: syntax error at or near "ON" at character 24 in /var/www/virtual/alarmix.de/qm/htdocs/includes/database.pgsql.inc on line 125.
* user warning: query: SELECT COUNT( DISTINCT ON (n.nid) n.nid) FROM node n INNER JOIN project_issues p ON p.nid = n.nid INNER JOIN node_revisions r ON r.vid = n.vid INNER JOIN users u ON p.assigned = u.uid WHERE n.status = 1 AND ((p.sid = '1' OR p.sid = '2' OR p.sid = '4')) in /var/www/virtual/alarmix.de/qm/htdocs/includes/database.pgsql.inc on line 144.
* warning: pg_query() [function.pg-query]: Query failed: ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions in /var/www/virtual/alarmix.de/qm/htdocs/includes/database.pgsql.inc on line 125.
* user warning: query: SELECT DISTINCT ON (n.nid) n.nid FROM node n INNER JOIN project_issues p ON p.nid = n.nid INNER JOIN node_revisions r ON r.vid = n.vid INNER JOIN users u ON p.assigned = u.uid WHERE n.status = 1 AND ((p.sid = '1' OR p.sid = '2' OR p.sid = '4')) ORDER BY n.changed DESC LIMIT 20 OFFSET 0 in /var/www/virtual/alarmix.de/qm/htdocs/includes/database.pgsql.inc on line 144.
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | pg_count_fix.patch | 1.32 KB | hunmonk |
Comments
Comment #1
fagothen this is probably a bug of the project module. content access just uses drupal node access API.
Comment #2
hunmonk commentedif you wish me to debug, you must include exact step by step instructions on how to reproduce this error.
thanks.
Comment #3
mklein1 commentedI've run into the same issue. In this case, I simply entered a project and a couple of issues and then selected "View all support requests".
Postgres is a bit more rigid its interpretation of the SQL. In SELECT COUNT (DISTINCT) query, the problem is in the COUNT(DISTINCT ON (n.nid) n.nid) clause. SELECT COUNT(DISTINCT n.nid) works. Since the nid is all that is returned from the query and the DISTINCT expression is also the nid, there shouldn't be any need to express it twice.
As for the second query, Postgres requires that the ORDER BY expression match the DISTINCT ON expression. So, ORDER BY n.changed should be ORDER BY n.nid.
Hope this helps.
Comment #4
mmilano commentedHere is more information from my experience. The first time I did a check-out with no -r, second time I downloaded the latest release. Both versions produced the same result.
Postgres 8.1
Drupal 5.3
Project 5.x-1.0
Project Issues 5.x-1.1
I did a fresh install of 5.3 and added a new project and a new issue.
path: project/issues/1
errors:
# arning: pg_query() [function.pg-query]: Query failed: ERROR: syntax error at or near "ON" at character 24 in /home/user/site/public_html/includes/database.pgsql.inc on line 125.
# user warning: query: SELECT COUNT( DISTINCT ON (n.nid) n.nid) FROM node n INNER JOIN project_issues p ON p.nid = n.nid INNER JOIN node_revisions r ON r.vid = n.vid INNER JOIN users u ON p.assigned = u.uid WHERE n.status = 1 AND ((p.pid = '1')) AND ((p.sid = '1' OR p.sid = '2' OR p.sid = '4')) in /home/user/site/public_html/includes/database.pgsql.inc on line 144.
# warning: pg_query() [function.pg-query]: Query failed: ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions in /home/user/site/public_html/includes/database.pgsql.inc on line 125.
# user warning: query: SELECT DISTINCT ON (n.nid) n.nid FROM node n INNER JOIN project_issues p ON p.nid = n.nid INNER JOIN node_revisions r ON r.vid = n.vid INNER JOIN users u ON p.assigned = u.uid WHERE n.status = 1 AND ((p.pid = '1')) AND ((p.sid = '1' OR p.sid = '2' OR p.sid = '4')) ORDER BY n.changed DESC LIMIT 20 OFFSET 0 in /home/user/site/public_html/includes/database.pgsql.inc on line 144.
Comment #5
mmilano commentedThe errors in question are caused by og and/or og_project.
We did some work tonight to try to fix the issue and we were lead to the bad SQL being generated by the output of pager_query() and db_rewrite_sql() functions in issue.inc.
I lost track of the changes and original line numbers so I did a fresh install (db and files) so I could report something useful here and I ended up finding out something more interesting.
Previously I had installed og and og_project, but this time I did not. I was now able to use issue tracking with postgres just fine. The problems are only introduced after enabling og and og_project. Even after disabling og/og_project, the postgres errors will persist if those were ever enabled.
Comment #6
hunmonk commentedreassigning to og_project -- seems like the most likely culprit.
Comment #7
dwwog_project desperately needs love. patches welcome in the mean time.
Comment #8
mmilano commentedafter looking at og_project, i figured there's no way it could be that. i ended up tracking the issue down to core.
includes/database.pgsql.inc
function db_distinct_field($table, $field, $query)
$query passed in
SELECT count(node.nid) FROM {node} node LEFT JOIN {og} og ON node.nid = og.nid INNER JOIN {users} users ON node.uid = users.uid WHERE (node.status = '1') AND (og.directory = '1') AND (node.type IN ('group'))
return
SELECT count( DISTINCT ON (node.nid) node.nid) FROM {node} node LEFT JOIN {og} og ON node.nid = og.nid INNER JOIN {users} users ON node.uid = users.uid WHERE (node.status = '1') AND (og.directory = '1') AND (node.type IN ('group'))
pgsql doesn't like: count( DISTINCT ON (node.nid) node.nid)
I was able to correct the issue by omitting 'ON (table.field)' if 'count(table.field)' was passed in to the function. Just the $field_to_select line was changed. I'm not sure of the global impact although it shouldn't hurt anything if it was going to break in count() case anyway.
original function:
my fix:
og, og_project, project, and project_issue work well now with og access control enabled.
Comment #9
hunmonk commentedhere's a patch generated from the above changes. my regex foo is pretty weak -- could somebody strong in that area double-check the expression?
also, would be great if some other folks could install this patch and see if it fixes the problem for them as well.
Comment #10
hunmonk commentedbetter title
Comment #11
drummWould it be possible to fix this at the level pager_query() is called with the original query? A separate $count_query, which does not get mangled, could be passed.
Comment #12
mmilano commentedIsn't it a legit case that someone may want to use db_distinct_field() on a count() query?
Comment #13
drummDepends on if this is a systematic problem, or just one odd query. It looks like this does need a general solution since http://drupal.org/node/125434 is a duplicate of this. The only patch there worth considering simply removed 'ON.'
Comment #14
whalebeach commentedI experienced the same DISTINCT ON query problem on Views module pager query. I tried the patch above and the problem is away. thanks.
Comment #15
smk-ka commentedCross-posting from http://drupal.org/node/196831, sorry...
According to the Postgres manual, DISTINCT ON is considered "bad style":
I'm not sure, though, why it was used before, that is, if there (ever) was a need to use this variant.
FYI, from the reference documentation:
SELECT DISTINCT select_list ...SELECT DISTINCT ON (expression [, expression ...]) select_list ...http://www.postgresql.org/docs/8.1/interactive/queries-select-lists.html...
aggregate_name (DISTINCT expression [ , ... ] )http://www.postgresql.org/docs/8.1/interactive/sql-expressions.html#SYNT...
Comment #16
catchbugs get fixed in the development version.
Comment #17
ij commentedIt seems that the patch from above (http://drupal.org/files/issues/pg_count_fix.patch) doesn't work anymore for drupal 5.5:
database.pgsql.inc is already patched with the pg_count_fix.patch:
Any tips?
Comment #18
catchIf 6.x is already fixed (and 5.x as well?) then this can be marked as fixed - or ideally a duplicate of the issue that solved it.
Comment #19
ij commentedAs I already stated, but maybe it was not clear enough:
The mentioned patch worked fine with Drupal 5.3. Then I upgraded beginning of this week to 5.5 and now it doesn't work anymore. So I consider the bug not fixed for 5.5. Therefore I'm setting the version to 5.5.
Furthermore the patch only applies to DISTINCT ON clauses combined with count(), but the usuage of DISTINCT ON (table.column) table.column doesn't work at all in pgsql.
When using the function db_distinct_field from database.myspql.inc there are no errors, but users are not able to successfully post to forums, i.e. they can post, but the posts won't show up in the forum.
Something is horribly broken, IMHO.
Regards,
Ingo
Comment #20
ij commentedI've found that the above patches causes problems in various modules. Once content_access module is working fine, but forum modules gives errors whereas a different patch gives a working forum, but a bailing out content_access module.
Finally I've found another patch to this issue which works perfectly fine for me for some weeks now and I would like to see it included in the next 5.x release:
http://drupal.org/node/128846#comment-678398
Comment #21
nospam2 commentedij, I updated that patch last week to solve another problem reported by Gábor Hojtsy. Here is the new one: http://drupal.org/node/128846#comment-759767
Salvis is trying to get the patch committed. We need Postgresql users to test the patch. This patch is by far the best fix for a wide range of Postgresql problems. Please help getting the patch committed: http://groups.drupal.org/node/6980
Since the thread where I posted that patches fixed most of the Postgresql problems including this one, I think you guys should stop posting on this thread. Instead, post your comments here: http://drupal.org/node/128846
Comment #22
drummLooks like everything moved to http://drupal.org/node/128846.