Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tDoVF-003MON-Ae for pgsql-general@arkaria.postgresql.org; Wed, 20 Nov 2024 17:28:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tDoVD-007uu8-IX for pgsql-general@arkaria.postgresql.org; Wed, 20 Nov 2024 17:28:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tDoVD-007uu0-7g for pgsql-general@lists.postgresql.org; Wed, 20 Nov 2024 17:28:31 +0000 Received: from mail-il1-x129.google.com ([2607:f8b0:4864:20::129]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tDoVA-002xYm-Tj for pgsql-general@lists.postgresql.org; Wed, 20 Nov 2024 17:28:30 +0000 Received: by mail-il1-x129.google.com with SMTP id e9e14a558f8ab-3a78c3f07dbso2384935ab.0 for ; Wed, 20 Nov 2024 09:28:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1732123707; x=1732728507; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=pbpmoQjNdyU/gGsYk0v6nBJfXisEriAYVPM+1ug+tvI=; b=gYQ9T/V0yns5uay9KUOkzlN3fGEnj0ktGOAiGDeWj6ZLIVLDgn+Wi5l8rpaoFeSIoL sStjMUi+J1PE9TaCZ25X1hS4xjRZ4V9xGSfeJjNguZV3B782PeZWdeao0D64RIVz3uUO 7yA/AvGaEUyUFRGaoM9bJ0WUBGzuz5TWZTMaJYre0ZRe6uWSAYn14FrqIm+k6r1zvWFC tp4snEAHwVaCj+RwI83wUnoQYHx8wT3sKQ7h/NWKrzLQYChedBbgrWP3tow0G8s9YdGH HgDGlmFr+PnLtrpnS8pnJ9HvdZNG0VPALfVUZ/nYU84jkOEwWSDUdDKXAqGt3Sr4v/o7 nxfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732123707; x=1732728507; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=pbpmoQjNdyU/gGsYk0v6nBJfXisEriAYVPM+1ug+tvI=; b=o+Za/6wRwUCPBZkfKGw+HWriwpFdFqnkdT7onWAzm4pxQLYIxjOyxoeUHiJz5u99ay aSFtS9R3wZEyScJKBL6BIN9uPnZqRUqomZw3cHZFrbdA3Pyfn4CwA0ZprU3TuJerYCGw Fwdo6HW/acUAV7IAaqcP1p1O/NnAisqb4SbDQNzM+Qglt4lxcF24H3/rxpfZRZ0TnYtH gHsOWuhQNo8meel5MuFAo8o4DnTJ7E6CeRwX2tg/KJJFgN0KFvhs5WAt4E1K7Gc2EqJM a5+3La8biFesgDmdaQfS2CwWq8cBlTtiO0CG98zmMdh7SuiQGD41YBj8+xEWiOAUAMI6 7qSA== X-Forwarded-Encrypted: i=1; AJvYcCWm/ehJnli61zMlBC0O39uNlxuzbcHoSpqwllIOvVkaoAqiYj69WIDJ2zNI7VLLmFxuSf0nZEpu+IJ6sEMI@lists.postgresql.org X-Gm-Message-State: AOJu0YyWzk6drTvBfV+m8qNsouWDAvqaFfbss5jTSiKZQtND0ZnsDtd7 VRQiZpdrk/yRz2EbExL2fFlAhP5UnnkJsdriys6PqNh77p6UVFqHRq241LHKk2i0PVhenkIz6VR TuYgOOoGoqdTXv59llGKwa6dbh4w= X-Gm-Gg: ASbGncvkXR2OZWqlEJiQ5wMpacWqzpJKr3W5hfnUa85XrHOtLmrtSyavfXoBKOO+Vxy iuBwcbXWzpswbKPgdjkGrc3gWGnUr6RI= X-Google-Smtp-Source: AGHT+IFfSJcNXZATzrK//kUU++Tcs8B/b+EqDFnaTnieAJQSByZf+53nbQtu+2yOf6orrp6CmZtEowxTp8EmC0Q/+rU= X-Received: by 2002:a92:ca4b:0:b0:3a7:6d7a:7629 with SMTP id e9e14a558f8ab-3a786593e3fmr43802155ab.20.1732123706932; Wed, 20 Nov 2024 09:28:26 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Justin Date: Wed, 20 Nov 2024 12:28:15 -0500 Message-ID: Subject: Re: Suddenly all queries moved to seq scan To: Sreejith P Cc: Daniel Gustafsson , pgsql-general@lists.postgresql.org Content-Type: multipart/alternative; boundary="00000000000042460006275b7ab9" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000042460006275b7ab9 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Nov 20, 2024 at 9:09=E2=80=AFAM Sreejith P = wrote: > > > > Queries were taking 20 ms started taking 60 seconds. So have done SQL > analyse to understand about query plan. There we found that query planner > taking seq scan instead in index scan. > > I would like to add one ore point. A delete query were running in DB fro= m > 2 days for deleting around 80 million records. This can cause this specific problem where the number of dead tuples and lack of autovacuum running can cause the statistics to favor a sequential scan over an index scan. Taking into account the length of time the delete took it would hold a number of datapages and tuples in a lock state, which can lead to blocking queries and prevent autovacuum/analyze. It is best to do bulk deletes in batches and have a rest period between batches to allow autovacuum and analyze to keep up.. Doing deletes in batches reduces the number of resources being consumed. --00000000000042460006275b7ab9 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Wed, Nov 20, 2024 at 9:09=E2=80=AF= AM Sreejith P <sreejith@lifetr= enz.com> wrote:



Queries were taking 20 ms started taking 60 seconds. So have done SQL analy= se to understand about query plan. There we found that query planner taking= seq scan instead in index scan.

I would like to add one ore point.=C2=A0 A delete query were running in DB = from 2 days for deleting around 80 million records.

This can cause this specific problem where the number of dead tupl= es and lack of autovacuum running can cause the statistics to favor a seque= ntial scan over an index scan.=C2=A0 Taking into account the length of time= the delete took it would hold a number of datapages and tuples in a lock s= tate, which can lead to blocking queries and prevent autovacuum/analyze.=C2= =A0

It is best to do bulk deletes in batches and have a rest period = between=C2=A0 batches to allow autovacuum and analyze to keep up..=C2=A0 Do= ing=C2=A0 deletes in batches reduces the number of resources being consumed= .

=C2=A0 =C2=A0 =C2=A0=C2=A0
--00000000000042460006275b7ab9--