Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g08Yx-0000Hd-BH for pgsql-bugs@arkaria.postgresql.org; Wed, 12 Sep 2018 17:04:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g08Yv-0008H2-H1 for pgsql-bugs@arkaria.postgresql.org; Wed, 12 Sep 2018 17:04:21 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g08Yv-0008FT-74 for pgsql-bugs@lists.postgresql.org; Wed, 12 Sep 2018 17:04:21 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g08Ys-0001FT-HQ for pgsql-bugs@postgresql.org; Wed, 12 Sep 2018 17:04:19 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id w8CH4B3E009925; Wed, 12 Sep 2018 13:04:11 -0400 From: Tom Lane To: Victor Yegorov cc: Mareks.Kalnacs@datakom.lv, pgsql-bugs@postgresql.org, Maris.Rucis@datakom.lv Subject: Re: PostgreSQL 10.0 SELECT LIMIT performance problem In-reply-to: References: <7419b7a3a21d4f1c8509178a071ec636@datakom.lv> Comments: In-reply-to Victor Yegorov message dated "Wed, 12 Sep 2018 19:43:18 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <9923.1536771850.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 12 Sep 2018 13:04:11 -0400 Message-ID: <9924.1536771851@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Victor Yegorov writes: > The fact, that planner is not accurate on the estimates of JSON internal > keys is expected, PostgreSQL is not parsing JSON values when gathering > stats. > You cannot expect planner to be picky about all possible corner cases, i= t > would make planning time enormously huge. Right. The fact that it doesn't make the right guesses without help can't be considered to be a bug in all cases. These are engineering tradeoffs we have to make. > That is the reason I outlined, that important keys should be extracted i= nto > plain columns. If that seems infeasible from an application standpoint, another possibility is to make expression indexes on those important keys. ANALYZE will gather stats on the values of indexed expressions, and then perhaps the planner will have enough info to make better decisions. In the other case mentioned, where the problem is a poor guess about the selectivity of where ( to_tsvector( 'simple'::regconfig, a.ts_vector_fields ) @@ to_tsquery( 'simple', ' (decodedType5b6f0753ossA= larmsGenerator) ' ) ) it's the same problem: the planner has no stats that would let it figure out the selectivity. It can't reasonably extract an estimate on the fly --- if it did, you'd be complaining that planning time was too long. The only way to get reasonable behavior is to set things up so that ANALYZE will accumulate stats about the values of "to_tsvector('simple',a.ts_vector_fields)". You can arrange that either by extracting that into a column, or by making an index on it. regards, tom lane