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 1w6OAs-004FkF-2r for pgsql-general@arkaria.postgresql.org; Sat, 28 Mar 2026 07:33:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w6OAr-00DkhG-0X for pgsql-general@arkaria.postgresql.org; Sat, 28 Mar 2026 07:33:37 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1w6OAq-00Dkh8-2e for pgsql-general@lists.postgresql.org; Sat, 28 Mar 2026 07:33:37 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w6OAo-00000001SZ9-2Tmt for pgsql-general@lists.postgresql.org; Sat, 28 Mar 2026 07:33:36 +0000 Received: by mail-wm1-x344.google.com with SMTP id 5b1f17b1804b1-486fb112c09so28363295e9.1 for ; Sat, 28 Mar 2026 00:33:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1774683213; x=1775288013; darn=lists.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=mKr+zWb4sqaU1YyLGVcEvX4xQyLnIBd5vq6uRvYWNMs=; b=dzGZ4CdktRWSYGSGXsdBTyLSzQFNsdCmPfWApl2IC0mEaJYIyMDCNCdzIH8DX3xnY0 75neE1SntcWgkfFDFpUmHExBg9tcmsrf9/dWQluvrzTM54AwAzUXnellGnoc8dfrK7Sr G7jDPAgCx9lT8EZgMANYZvm5kyWXNHJgmHrpRek9vYotFVfV2/vkRLH2Uc4O+aJJo3As 8j+yWvvcTjd4MkDFVXiauilkv/7VVbZz+HY7eFFAYVPaSx8DnS141mWTyDiDnqodCl4N +QEdToxciJMukq5HAYCgJjQjmy+d0qhj9pm+OajucdMX/zY/h/fQJdYP5vZQ2TIpUvsV 2/0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774683213; x=1775288013; 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=mKr+zWb4sqaU1YyLGVcEvX4xQyLnIBd5vq6uRvYWNMs=; b=QaEgexyYbjkEtkZy+sRWDV2dVjVfdEEdKKMVEh+AZ+e80HhcECl5w03N4F6z9vfBcQ /dojAzNEg1zqjl3HArybCroGXqxL+pq4wZwD6PR5nHIVz9ub2CKRl6vppHyg0S87vVrl g2V9U6w6XioR9jJeyFLS0qOAqhB5BwZLXDjH1DIjFxvVrdEkRvfDBXJzXAXgg34rtLhm Exm4CEbXaopGPw+oeCLx9UYHNZv4oSXqnRjRhO2H6CLliECcU3aIOkoVKnXPoOJvgVgP jgbq5fRU/tz+UBb5unkJP8r/L6SMkpW1156ymzWn24ViKfoYzsJWX/bV8+iJVuH+luMx bj5Q== X-Forwarded-Encrypted: i=1; AJvYcCVOkIyQtTMZ/uROqiDiO/HLme+3lfhO+DAa6TNfb9URbsKJ1hqUKBHCW9pNborPyNIBCwazKb/MbuWa7g1w@lists.postgresql.org X-Gm-Message-State: AOJu0YxKUeSuDVNmbFK7mqhhaeJPm+SFddZ3Fnt3lcCknf/n9C924eQu stek6ejzTrU2xepcwm2Q/Sdc2Ghh7kW25CatVLYTyPP+CdOHa4PUGwTz9kKjNYJ3nUd8hKdyszf rpyuaIaijwMq1 X-Gm-Gg: ATEYQzwEK9z8vFJnnG3eK22FC4Q156166NyhoKVVCNyo47gJdt5SSIS3FwpeuawoD/u LcXwApxbcwX+PDpXiAIP9zvGluVLNE/IqFgghj8AO70YCrfFozsMXCpq/WpsSIMfw1XT7bZZz3E fWEYYeblLEP7fl03uBs9APAzKLYOnVrtBm/cZgjGExyw+qYl6Yq7Vty0PyAmRKkEQ/agbT0mI4c 7SDuiyr9SxHODxe/Yt74AJKz2MYHV8OMdWeT54s8o6XHhbeB/GFyZlYUdw9MwKjC2ZRTtDPQuN8 M+viuDufIJljoQ+XOcPRBbXjQRDzC3l8PMEFY0i+1VPPbQhEFxJDH9Iw0FTX2//QpnW4KAqykq8 NtdUDg5LeyMdnO+/bQIpTiwIkbO3jBXgWSmgA5ikVAH7TiOWtIk6VwV9r1KA+aj9GBOhxkWChSw OfK8e49qtZuqey8WlfqfAohx2fBFFUK1dSxCuEQSPuDFJtQHZZqCNjkMA= X-Received: by 2002:a05:600c:3490:b0:485:4136:99a8 with SMTP id 5b1f17b1804b1-48727f6cbc5mr80437375e9.22.1774683212908; Sat, 28 Mar 2026 00:33:32 -0700 (PDT) Received: from laurenz.albe-K4N0CV00F97414D ([2001:871:270:4fd5:603f:45c2:9807:c104]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-487306996cbsm27597165e9.13.2026.03.28.00.33.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Mar 2026 00:33:32 -0700 (PDT) Message-ID: <04e783a75738a548708b40732b263e7d2a3119ab.camel@cybertec.at> Subject: Re: slow sql query for big items From: Laurenz Albe To: Hua W Peng , pgsql-general@lists.postgresql.org Date: Sat, 28 Mar 2026 08:33:31 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.58.3 (3.58.3-1.fc43) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sat, 2026-03-28 at 15:07 +0800, Hua W Peng wrote: > I have a common table for telemetry data. the stru is: > [25 columns] >=20 > Data records are growing by about 10 million every day, reaching 300 mill= ion per month. > In this case, even a simple COUNT(*) query becomes extremely slow, taking= about > 7-8 minutes to finish. > I am running PostgreSQL 14 on Ubuntu 22.04 with a 24GB shared buffer. > And, though in our test env we have timescaledb enabled: >=20 > Triggers: > =C2=A0 =C2=A0 ts_insert_blocker BEFORE INSERT ON dms_data_gzdy FOR EACH R= OW EXECUTE FUNCTION _timescaledb_functions.insert_blocker() > Number of child tables: 9 (Use \d+ to list them.) >=20 > But in production env there is no timescaledb which can't be installed as= well. >=20 > Can you help me? First, a test environment should be as similar to production as possible, otherwise it cannot serve its purpose. There is little you can do about speeding up count(*), it is bound to be sl= ow. See https://www.cybertec-postgresql.com/en/postgresql-count-made-fast/ But if counting the rows is really your use case, you are doing something w= rong. I suspect that your real problem are other queries. The way to get good performance with large tables it to have your queries u= se an index scan. The indexes you need will depend on your queries, so withou= t knowing the queries, it is impossible to recommend anything. Partitioning is not primarily a measuer for improving query performance, but it would still be a smart idea, primarily to be able to delete old data efficiently. It doesn't matter if you use TimescaleDB for partitioning or use PostgreSQL's support directly. Yours, Laurenz Albe