&quot;We are migrating our analytics db from Mongo to PG but we hit a wall because SELECT COUNT(DISTINCT x)) performs abysmally in Postgres.&quot;<p><a href="https:&#x2F;&#x2F;github.com&#x2F;aggregateknowledge&#x2F;postgresql-hll" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;aggregateknowledge&#x2F;postgresql-hll</a> ?<p>or even<p><a href="https:&#x2F;&#x2F;www.periscopedata.com&#x2F;blog&#x2F;hyperloglog-in-pure-sql.html" rel="nofollow">https:&#x2F;&#x2F;www.periscopedata.com&#x2F;blog&#x2F;hyperloglog-in-pure-sql.h...</a> ?