Permalink
Browse files

Fix for query locking up db/app server

The previous code retrieved all votes for a given account then just took a count of them. One user in question had over 5000 comment votes, which was causing PostgreSQL to sometimes kill the query with the error, "stack depth limit exceeded HINT:  Increase the configuration parameter "max_stack_depth", after ensuring the platform's stack depth limit is adequate."

[#43729801]
  • Loading branch information...
1 parent 22e2618 commit 51611c453e84b3bdbdb0249f5f9c2870c047ba33 @wezm wezm committed Feb 4, 2013
Showing with 15 additions and 2 deletions.
  1. +15 −2 r2/r2/models/account.py
View
@@ -26,7 +26,9 @@
from geolocator import gislib
from pylons import c, g
from pylons.i18n import _
+import sqlalchemy as sa
+from r2.lib.db import tdb_sql as tdb
from r2.lib.db.thing import Thing, Relation, NotFound
from r2.lib.db.operators import lower
from r2.lib.db.userrel import UserRel
@@ -191,8 +193,19 @@ def get_cached_downvotes(content_cls):
downvotes = g.cache.get(cache_key)
if downvotes is None:
vote_cls = Vote.rel(Account, content_cls)
- downvotes = len(list(vote_cls._query(Vote.c._thing1_id == self._id,
- Vote.c._name == str(-1))))
+
+ # Get a count of content_cls downvotes
+ type = tdb.rel_types_id[vote_cls._type_id]
+ # rt = rel table
+ # dt = data table
+ # tt = thing table
+ rt, account_tt, content_cls_tt, dt = type.rel_table
+
+ cols = [ sa.func.count(rt.c.rel_id) ]
+ where = sa.and_(rt.c.thing1_id == self._id, rt.c.name == '-1')
+ query = sa.select(cols, where)
+ downvotes = query.execute().scalar()
+
g.cache.set(cache_key, downvotes)
return downvotes

0 comments on commit 51611c4

Please sign in to comment.