<i>We are migrating our analytics db from Mongo to PG but we hit a wall because SELECT COUNT(DISTINCT x)) performs abysmally in Postgres.</i><p>An alternative is PipelineDB, which is built on Postgres (and drop-in compatible, supposedly) and should provide an efficient implementation for those kinds of queries: <a href="https:&#x2F;&#x2F;www.pipelinedb.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.pipelinedb.com&#x2F;</a><p>I&#x27;ve never used it, though.