postgresql version of count query