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 1vWVDN-002ILR-2e for pgsql-general@arkaria.postgresql.org; Fri, 19 Dec 2025 07:47:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vWVDM-006uHF-1V for pgsql-general@arkaria.postgresql.org; Fri, 19 Dec 2025 07:47:53 +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 1vWVDM-006uH6-0O for pgsql-general@lists.postgresql.org; Fri, 19 Dec 2025 07:47:52 +0000 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vWVDK-001Z0a-1m for pgsql-general@postgresql.org; Fri, 19 Dec 2025 07:47:52 +0000 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-477619f8ae5so8445205e9.3 for ; Thu, 18 Dec 2025 23:47:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1766130469; x=1766735269; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=OXyIumzCpEgma2KXYiPo0IQGbTijB71uoQXRyNOPBzo=; b=WDpv4R6/91fKS/nlnrKlaBbPrYfv63UKw+NPiDLw0+s67jx4OyeZntHBQn8iViOjXo rvztJPcSW25rUplhwZig2Of9/y352nv0dsgf1jt5aFt/uWyKR4Oto38S10n1NDhhaJU5 fTO0NFqOJZo9N2ecKZfztzQPjaJTycHXQ+F0qPATK7EDrVgfHRJ0/SKA8+r3xRUsyacf i6LRn0g+tmFfjVuZe+cVEg2vKfS22bYdb8OTW7c86USfC//1UwrsL/89KQZ4yShyduTC mRVlGa7fBalalyvO4gkQk2xyMAAt7oeJXPWb4yVGYOECWjuofYBjTGwG/mioKAxdKv3+ LNOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766130469; x=1766735269; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OXyIumzCpEgma2KXYiPo0IQGbTijB71uoQXRyNOPBzo=; b=YOEtBrjNRATIsU1WM2EZJNFgcWAdjbTDWclYfdrNDsz+fnknTXtyifsPVxLUMjcO8t zeXvwFxvQHgGCLQ+k7u76J+gPa+PpXQXPfJPCKMA2Z8G/MqM6SzB29/oBEAPVxA2LBdi iD9gT/zW7NYZ4vsimb4p5hspz7kjobqGXG5s44kOAbZjqPqNZ52VOWmrkSu0Ye4NZO5y jX11XFBd3WK3DuUsw23Z5olYuDG08UwVdzz+aa+n6insDizkeONEIMNWEullEYAoYlNJ ogyvdojbBnR4X5gc4HCNElS65qnUjET4dc4tXW+bFLVSObM2/NpwlVGsz8dZ6AM4ktR1 gfYw== X-Forwarded-Encrypted: i=1; AJvYcCUZlrdRWD0OFgxZvdNufRce+gR/LB8rfhbhjPQakMos6iVyR1+mqSrfrLam7jh8ffM4FE11BYwijtySo5s6@postgresql.org X-Gm-Message-State: AOJu0YzV/ioH7jLrjI2IbPHl5Lk7g3Z7pqP5wlv6RPyecZXm0GvqmRvN XKfyLK3FgVGGuizeAkC7mFD+S/xyBHwomoUN688Mu/UW+yZO1w9+NyhXzoKQ1E5nQbI= X-Gm-Gg: AY/fxX4i3+0UV5ZWPmcL1evyyazmbj6AiM6uZ4cf4moZhPhJGHBKhD7pAfgwHlP+WVe zpGwSDRx6ws4g1eFF7McPh+/ScQ3MpLgyZ42IY5rRo66QlD6WgsU5JBJiU1KgVTVxs2WHuxzNHF D9kRI3cKKxU0bQQ3B7ZfBbpZ2j19iWm3KSmqCifsNVzyqGZcOBHxnR2rh2fAZmESGaZfPHHddpQ mPbomf2pgTBzy2c5b+NkJCtUhOKa2+g71w9NekKkjMxFX7t1YMd1tU/XUKSg27CLMEDGLMYDE25 AEzSMmb5dMK7U3MR9VKbAek8lsNZsI+A+W7U8LnxW/UWC5mSc7M+eTW4yLfwbGQDG8QDMypW3Gm hFkLxoTB1jRWpIbU4OhQ5PyDY+gQxIg5mKI08X3AGamwJ34RiPbOtEUnnvs1Ym3KAE9DnsNr9TD D3R1Ex3HRxrxuI3DYUqdWYZsplcldJPebwevr1sYwweMM= X-Google-Smtp-Source: AGHT+IEY/Ai4u3Ye/mxCwlZpemo6IMV5CE1+icZ5XzosnyHrkCSEhXOyokJZlgZJ0WoXo4WTzUoTBA== X-Received: by 2002:a05:600c:310c:b0:46e:32dd:1b1a with SMTP id 5b1f17b1804b1-47d1953b941mr11510685e9.7.1766130468864; Thu, 18 Dec 2025 23:47:48 -0800 (PST) Received: from laurenz.albe-K4N0CV00F97414D ([2001:871:270:4cf9:1875:33eb:6694:f468]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-47d193522cdsm30547365e9.4.2025.12.18.23.47.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Dec 2025 23:47:48 -0800 (PST) Message-ID: <31beb34c3108173ea386d567de92da720da4bb07.camel@cybertec.at> Subject: Re: Dealing with SeqScans when Time-based Partitions Cut Over From: Laurenz Albe To: Matthew Planchard , pgsql-general@postgresql.org Date: Fri, 19 Dec 2025 08:47:48 +0100 In-Reply-To: <87bjjv1v96.fsf@gmail.com> References: <87bjjv1v96.fsf@gmail.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2 (3.56.2-2.fc42) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 2025-12-18 at 13:48 -0500, 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: >=20 > * When the current time crosses a partition boundary, all new records > =C2=A0 are written to the new partition, which was previously empty, as > =C2=A0 expected >=20 > * Because the planner's latest knowledge of the partition was based on > =C2=A0 its state prior to the cutover, it assumes the partition is empty = and > =C2=A0 creates plans that use sequential scans >=20 > * The table accumulates tens to hundreds of thousands of rows, and the > =C2=A0 sequentail scans start to use nearly 100% of available database CP= U >=20 > * Eventually the planner updates thee stats and all is well, but the > =C2=A0 cycle repeats the next time the partitions cut over. >=20 > 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). >=20 > 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. I would try to tune autovacuum to check more often: autovacuum_naptime =3D 5s # perhaps even less Then hopefully the new partitions get analyzed early enough. Yours, Laurenz Albe