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.96) (envelope-from ) id 1vWK5w-00H2EL-0B for pgsql-general@arkaria.postgresql.org; Thu, 18 Dec 2025 19:55:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vWK5u-003vxU-3D for pgsql-general@arkaria.postgresql.org; Thu, 18 Dec 2025 19:55:27 +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.96) (envelope-from ) id 1vWK5u-003vxI-29 for pgsql-general@lists.postgresql.org; Thu, 18 Dec 2025 19:55:27 +0000 Received: from mail-yx1-xb130.google.com ([2607:f8b0:4864:20::b130]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vWK5s-001TDj-39 for pgsql-general@postgresql.org; Thu, 18 Dec 2025 19:55:27 +0000 Received: by mail-yx1-xb130.google.com with SMTP id 956f58d0204a3-6442e2dd8bbso974268d50.0 for ; Thu, 18 Dec 2025 11:55:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1766087723; x=1766692523; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=hr2VrWpUvQjVtjI8mV6cUGBb88oB/Y/uCacjRSXANWk=; b=kfPDhRujDUMvTLleYpX1Q8MKVla3lLcZ/fiVey9VuFMGY+H3ofEdjJ7umu8vzCNeoR JR+nnHSCnfpC61ngCGEHIAlnfAYjLD22EPVYcyGZ2zK30uAC2Dg6Krcg3zNoNhf3EcYX 5vuz1AX281fYIM2k6rsWNFkvapu4kBJWMv7QV+GK4W7BRhhyHOPYao32opahJtUBGq4d xCk4hgKFQTupe3HJneRsVCW/uwtsD1CYFJ1KTt8pXr6NpZEQDOG5TeMi3779cNS0oGpZ M5PBL0b9HgtlNoe0Wsp4MFTGJFqT4F+cHKvcpIM5BlATRbFjA0wnLRgujgw0QIxv+BzD I+ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766087723; x=1766692523; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=hr2VrWpUvQjVtjI8mV6cUGBb88oB/Y/uCacjRSXANWk=; b=AyRIH4GLGu0n3JvGdFijSbr5MRWKqC5iOh5jWVuNwD0Hy86fUbHPjxtGsMYWaYmn++ oKOcp5lfRRVQ6NEM5M89djpOOTd5phGDkqwmQI02c6R9PAXVxJN4pTo0nAidv3upwLxh 9ExcU4ryU4LE8Wcv1sp+WUY3vja9cl7nuxQmgu+lqyYk3C1NQZt5RTSPYtVFjUck74zS Gt1QD02d//5fZS3D20OVdPr4Un1uTdaPMEb5N98U5fhw4huqDGfDmQzOyOyLWqrfaLXw P1oTpIGg/ohQBwG7BfhENRkSamSEE39OWWr2wHu5YUSN05eHsBfe6SlkG1GjC8t7R1Z8 O1ZA== X-Gm-Message-State: AOJu0YwtgYTeJnXc0L4BUjOvLPPC/SqHpEGaXSvjvr+WbwB0SUag8KKf boAfqGXQ2SlkK5EPB+5Y9uIwMFOnLfx0BvVi495TM5Kz4NrWJbHeYfAPLXzyXeUJKQfT3aERtJB u42zBqvbdkYAhjuWoccAYYRNVEbzuO5s= X-Gm-Gg: AY/fxX6TsrmEmjsIX6PxAyxebH4s7hRL1XjOyUZcTxIEYn5JtBvAjehcitk1RiFxc2p Psw0757CDpZSO2gOkDzDK0oo8EQfhpWYrH19L+6GwZu/TVz65gL3bXnA6qecKIRjRh1JJs7SzZ5 A2GQNsebBnBt1lIQgbh1gG3ESKTeOD0LOeUdeqZsQr8oYiOa7C1plHRv41BZ26FB55Yaihq9dF1 NAzi2y80sS186RDhlitN9VEGEfdxXqXYtjXyZ0IUqh9ru+Ew4QlVYSdGydu3nENnHsSN4/bYdc2 jB5+1lox7TtqpCG+G/Z3+ynw7drR X-Google-Smtp-Source: AGHT+IFl38myOAdm3s3PKMAI0XlUaSz+fFePfIguPEI5OAz8orcKWOy/RKxozuCYMuJM8p5F/6y+CYySACUGR5907DE= X-Received: by 2002:a53:b788:0:b0:644:60d9:750f with SMTP id 956f58d0204a3-6466a924029mr286203d50.95.1766087723297; Thu, 18 Dec 2025 11:55:23 -0800 (PST) MIME-Version: 1.0 References: <87bjjv1v96.fsf@gmail.com> In-Reply-To: From: Matthew Planchard Date: Thu, 18 Dec 2025 14:55:12 -0500 X-Gm-Features: AQt7F2ryCSECLmssQE-whH7xbyjkHKXHct3W7SKzHgoMrbUivxIPIjh4RtTbedg Message-ID: Subject: Re: Dealing with SeqScans when Time-based Partitions Cut Over To: Ron Johnson Cc: pgsql-general Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thanks very much for the reply and the suggestions, a lot of good stuff to look at. > What's autovacuum_analyze_scale_factor set to? The default 20% is prett= y high. > autovacuum_naptime might need to be dropped, too. This is just whatever the default is, so I will look into updating these settings. > And maybe have the shell script that the cron job runs sleep only 5 secon= ds in the ANALY loop. Good suggestion. Our job runner will not run duplicate jobs if one is still running, so I may even just have this run every second. > How about just force seqscan off when the table is created? > ALTER TABLE SET (enable_seqscan =3D off); I didn't know this could be set on the table level! Our partitions are created by partman, but we have a nightly job that does maintenance stuff that could go through and update this setting on future partitions before they come into use. I'm always hesitant to mess too much with the planner, but this might be the ticket. On Thu, Dec 18, 2025 at 2:32=E2=80=AFPM Ron Johnson wrote: > > On Thu, Dec 18, 2025 at 1:48=E2=80=AFPM Matthew Planchard wrote: >> >> >> In a table with high insert frequency (~1.5k rows/s) and high query >> frequency (~1k queries/s), partitioned by record creation time, we have >> observed the following behavior: >> >> * When the current time crosses a partition boundary, all new records >> are written to the new partition, which was previously empty, as >> expected >> >> * Because the planner's latest knowledge of the partition was based on >> its state prior to the cutover, it assumes the partition is empty and >> creates plans that use sequential scans >> >> * The table accumulates tens to hundreds of thousands of rows, and the >> sequentail scans start to use nearly 100% of available database CPU >> >> * Eventually the planner updates thee stats and all is well, but the >> cycle repeats the next time the partitions cut over. >> >> We have tried setting up a cron job that runs ANALYZE on the most recent >> partition of the table every 15 seconds at the start of the hour, and >> while this does help in reducing the magnitude and duration of the >> problem, it is insufficient to fully resolve it (our engineers are still >> getting daily pages for high DB CPU utilization). > > > What's autovacuum_analyze_scale_factor set to? The default 20% is prett= y high. > autovacuum_naptime might need to be dropped, too. > > And maybe have the shell script that the cron job runs sleep only 5 secon= ds in the ANALY loop. > >> >> We have considered maintaining a separate connection pool with >> connections that have `enable_seqscan` set to `off`, and updating the >> application to use that pool for these queries, but I was hoping the >> community might have some better suggestions. > > > How about just force seqscan off when the table is created? > ALTER TABLE SET (enable_seqscan =3D off); > > -- > Death to , and butter sauce. > Don't boil me, I'm still alive. > lobster!