Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ohYnu-0003WH-7n for pgsql-general@arkaria.postgresql.org; Sun, 09 Oct 2022 16:05:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ohYnt-0007Wy-4Z for pgsql-general@arkaria.postgresql.org; Sun, 09 Oct 2022 16:05:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ohYns-0007Wp-NE for pgsql-general@lists.postgresql.org; Sun, 09 Oct 2022 16:05:24 +0000 Received: from mail-qv1-xf32.google.com ([2607:f8b0:4864:20::f32]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ohYnp-0007k5-I2 for pgsql-general@lists.postgresql.org; Sun, 09 Oct 2022 16:05:24 +0000 Received: by mail-qv1-xf32.google.com with SMTP id l19so5968093qvu.4 for ; Sun, 09 Oct 2022 09:05:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:from:content-language:references:to:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to; bh=eJEQ5XY5dfO7T0igPNZo3pCMON2YgVudSItrxVvyKDk=; b=WFj1uoBQM5JDX4ZHCM4t9L6+97JRdBjvZhhLeMABFdoJ25fpVxcHcwh52XS1NDOWtl 4eF3UxHMsAJY7wNcHQhUFRqoPudnrceH03bErxPdBenhEtQkULn7nfLLrRd4SWOrWpHL Yfb4JAhmaUgzbSKM729J9PrJLCXGVpVnRJQMjWLgu92L99Xqm6E/ykuuAjyM5m9js1Ui aV54z586ARge7D5EZOdBqTHawxknFRqrwUcs8kwbWMz+rbkJ2YheF0wCUSzJ7kcw9bwS Gp58gUaM2L8uMOErEAA36UsjE6EFALA9Hz2T3eBKkrdEsgAhGJKARUAxZXkLOLUAiK6v dMZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:from:content-language:references:to:subject:user-agent :mime-version:date:message-id:x-gm-message-state:from:to:cc:subject :date:message-id:reply-to; bh=eJEQ5XY5dfO7T0igPNZo3pCMON2YgVudSItrxVvyKDk=; b=SyEU6NktgvUztDlc/vIVJEZB0NZXIqUluu+aH2DWFTs/U5xL/bJRokyEBmq3WYFE94 qbZDPEhMxL+2fyBlNrBePmqg/kF4ZwUwb9Ez88tz49nZaHzk5WISptUOufOET5nYCxmv RQNvCP7he/HffM+24JoTJTEm2s4BdjkP9DfyIP6OXVRja16avpq3J447ivK5hJ9YSKsF p48t5KQzWmoh+HHp3HvzLDlfpiZSxWNiOzVtQ82reUXSlD7pn+vkXSOzkLrMO5O5BIf6 bvH1wiNS3YpxhAuGQR8W/frIWVrGqwSREN4m2b9g5LYVM3YcFyd+1PB07KqmctQfS6PH yRVQ== X-Gm-Message-State: ACrzQf3OxwI0b2zqWqC2laR478fHwmbpJojOYPdVsSVbssl4H0txanY4 8YWJzeCEjqcoB8X8N2UP64hG51+58rOSSUd2 X-Google-Smtp-Source: AMsMyM5h/wgCE9Bv6FvI7R5f8P9lFn59cMHyjCIaLfbPVQrpw1go9vev5K6goTXIEWLtW4Q9Cx7avA== X-Received: by 2002:a0c:dd11:0:b0:4b1:97db:abe with SMTP id u17-20020a0cdd11000000b004b197db0abemr11617428qvk.95.1665331518634; Sun, 09 Oct 2022 09:05:18 -0700 (PDT) Received: from [192.168.2.100] (pool-72-79-30-4.nwrknj.east.verizon.net. [72.79.30.4]) by smtp.gmail.com with ESMTPSA id ge27-20020a05622a5c9b00b00342b7e4241fsm6674491qtb.77.2022.10.09.09.05.17 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 09 Oct 2022 09:05:18 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------LaYgYexwGswc3fDQUajnF50U" Message-ID: <9023ff4a-55e3-58f8-b92b-5302282035d5@gmail.com> Date: Sun, 9 Oct 2022 12:05:17 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.1 Subject: Re: Different execution plan between PostgreSQL 8.4 and 12.11 To: pgsql-general@lists.postgresql.org References: <23fe7e4b.5866.183bc03584f.Coremail.gzhcoder@126.com> Content-Language: en-US From: Mladen Gogala In-Reply-To: <23fe7e4b.5866.183bc03584f.Coremail.gzhcoder@126.com> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------LaYgYexwGswc3fDQUajnF50U Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 10/9/22 05:11, gzh wrote: > > Hi, > > > I have had a Perl Website working for 7 years and have had no problems > > until a few weeks ago I replaced my database server with a newer one. > > > Database server (old): PostgreSQL 8.4 32bit > > Database server (new): PostgreSQL 12.11 64bit > > > I run following sql in PostgreSQL 8.4 and PostgreSQL 12.11, it returns > different execution plan. > > > --PostgreSQL 8.4 > > --------------- > > > old=# select count(1) from analyze_word_reports; > >   count > > --------- > >  9164136 > > (1 row) > > > old=# select indexdef from pg_indexes where > tablename='analyze_word_reports'; > >  indexdef > > ------------------------------------------------------------------------------------------- > >  CREATE INDEX analyze_word_reports_index_cseid ON analyze_word_reports > USING btree (cseid) > >  CREATE UNIQUE INDEX analyze_word_reports_pkey ON analyze_word_reports > USING btree (seq) > > (2 rows) > > > old=# explain analyze select 2 from analyze_word_reports where (cseid > = 94) limit 1; > >                                  QUERY PLAN > > --------------------------------------------------------------------------------------------------------------------------------------------------------------------- > >  Limit  (cost=0.00..0.63 rows=1 width=0) (actual time=0.893..0.893 > rows=0 loops=1) > >    ->  Index Scan using analyze_word_reports_index_cseid on > analyze_word_reports (cost=0.00..18621.98 rows=29707 width=0) (actual > time=0.892..0.892 rows=0 loops=1) > >          Index Cond: (cseid = 94) > >  Total runtime: 0.941 ms > > (4 rows) > > > > --PostgreSQL 12.11 > > --------------- > > > new=# select count(1) from analyze_word_reports; > >   count > > --------- > >  20131947 > > (1 row) > > > new=# select indexdef from pg_indexes where > tablename='analyze_word_reports'; > >  indexdef > > ------------------------------------------------------------------------------------------- > >  CREATE INDEX analyze_word_reports_index_cseid ON analyze_word_reports > USING btree (cseid) > >  CREATE UNIQUE INDEX analyze_word_reports_pkey ON analyze_word_reports > USING btree (seq) > > (2 rows) > > > new=# explain analyze select 2 from analyze_word_reports where (cseid > = 94) limit 1; > >                  QUERY PLAN > > ------------------------------------------------------------------------------------------------------------------------------------- > > Limit  (cost=0.00..0.41 rows=1 width=4) (actual > time=4908.459..4908.462 rows=1 loops=1) > >    ->  Seq Scan on analyze_word_reports (cost=0.00..528610.15 > rows=1278561 width=4) (actual time=4908.455..4908.456 rows=1 loops=1) > >          Filter: (cseid = 94) > >          Rows Removed by Filter: 15477750 > > Planning Time: 0.411 ms > > Execution Time: 4908.498 ms > > (6 行) > > > > Although PostgreSQL 8.4 has more than 10 million rows of data less > than PostgreSQL 12.11, > > PostgreSQL 12.11 is too slow and analyze_word_reports_index_cseid does > not seem to work. > > I'm guessing that the limit keyword of PostgreSQL 12.11 causes the > index not to work. > > But I don't understand why PostgreSQL 8.4 is normal. > > > What is the reason for this and is there any easy way to maintain > compatibility? > > > Regards, > > > -- > > > gzh > Well, as someone who has worked on more than one database, I can tell you that new version always means new plans. Most of the time, the new plans are better but sometimes they're not. Your problem is probably caused by one or two SQL statements that have changed plans. I would advise installing pg_hint_plan extension and fixing those few SQL queries manually. PostgreSQL would probably benefit from something like Oracle baselines, which are a good mechanism for carrying plans over to the new version. If you don't want to install the new extension, you can try by running vacuum analyze on the database. Also, upgrading to PgSQL 12 doesn't make much sense given the fact that PgSQL 15 will be released in a few weeks. Can you upgrade to PgSQL 14? Regards -- Mladen Gogala Database Consultant Tel: (347) 321-1217 https://dbwhisperer.wordpress.com --------------LaYgYexwGswc3fDQUajnF50U Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 10/9/22 05:11, gzh wrote:

Hi, 


I have had a Perl Website working for 7 years and have had no problems

until a few weeks ago I replaced my database server with a newer one.


Database server (old): PostgreSQL 8.4 32bit

Database server (new): PostgreSQL 12.11 64bit


I run following sql in PostgreSQL 8.4 and PostgreSQL 12.11, it returns different execution plan.


--PostgreSQL 8.4

---------------


old=# select count(1) from analyze_word_reports;

  count

---------

 9164136

(1 row)


old=# select indexdef from pg_indexes where tablename='analyze_word_reports';

                                         indexdef

-------------------------------------------------------------------------------------------

 CREATE INDEX analyze_word_reports_index_cseid ON analyze_word_reports USING btree (cseid)

 CREATE UNIQUE INDEX analyze_word_reports_pkey ON analyze_word_reports USING btree (seq)

(2 rows)


old=# explain analyze select 2 from analyze_word_reports where (cseid = 94) limit 1;

                                                                             QUERY PLAN                                      

---------------------------------------------------------------------------------------------------------------------------------------------------------------------

 Limit  (cost=0.00..0.63 rows=1 width=0) (actual time=0.893..0.893 rows=0 loops=1)

   ->  Index Scan using analyze_word_reports_index_cseid on analyze_word_reports  (cost=0.00..18621.98 rows=29707 width=0) (actual time=0.892..0.892 rows=0 loops=1)

         Index Cond: (cseid = 94)

 Total runtime: 0.941 ms

(4 rows)



--PostgreSQL 12.11

---------------


new=# select count(1) from analyze_word_reports;

  count

---------

 20131947

(1 row)


new=# select indexdef from pg_indexes where tablename='analyze_word_reports';

                                         indexdef

-------------------------------------------------------------------------------------------

 CREATE INDEX analyze_word_reports_index_cseid ON analyze_word_reports USING btree (cseid)

 CREATE UNIQUE INDEX analyze_word_reports_pkey ON analyze_word_reports USING btree (seq)

(2 rows)


new=# explain analyze select 2 from analyze_word_reports where (cseid = 94) limit 1;

                                                             QUERY PLAN                                                              

-------------------------------------------------------------------------------------------------------------------------------------

Limit  (cost=0.00..0.41 rows=1 width=4) (actual time=4908.459..4908.462 rows=1 loops=1)

   ->  Seq Scan on analyze_word_reports  (cost=0.00..528610.15 rows=1278561 width=4) (actual time=4908.455..4908.456 rows=1 loops=1)

         Filter: (cseid = 94)

         Rows Removed by Filter: 15477750

Planning Time: 0.411 ms

Execution Time: 4908.498 ms

(6 行)



Although PostgreSQL 8.4 has more than 10 million rows of data less than PostgreSQL 12.11,

PostgreSQL 12.11 is too slow and analyze_word_reports_index_cseid does not seem to work.

I'm guessing that the limit keyword of PostgreSQL 12.11 causes the index not to work. 

But I don't understand why PostgreSQL 8.4 is normal.


What is the reason for this and is there any easy way to maintain compatibility?


Regards,


--


gzh

Well, as someone who has worked on more than one database, I can tell you that new version always means new plans. Most of the time, the new plans are better but sometimes they're not. Your problem is probably caused by one or two SQL statements that have changed plans. I would advise installing pg_hint_plan extension and fixing those few SQL queries manually. PostgreSQL would probably benefit from something like Oracle baselines, which are a good mechanism for carrying plans over to the new version.

If you don't want to install the new extension, you can try by running vacuum analyze on the database. Also, upgrading to PgSQL 12 doesn't make much sense given the fact that PgSQL 15 will be released in a few weeks. Can you upgrade to PgSQL 14?

Regards

-- 
Mladen Gogala
Database Consultant
Tel: (347) 321-1217
https://dbwhisperer.wordpress.com
--------------LaYgYexwGswc3fDQUajnF50U--