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 1pMsEV-0007bj-G9 for pgsql-general@arkaria.postgresql.org; Tue, 31 Jan 2023 15:07:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pMsEU-0003YD-FM for pgsql-general@arkaria.postgresql.org; Tue, 31 Jan 2023 15:07:38 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pMsEU-0003Y0-28 for pgsql-general@lists.postgresql.org; Tue, 31 Jan 2023 15:07:38 +0000 Received: from mout.gmx.net ([212.227.15.19]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pMsEQ-0007Tb-RF for pgsql-general@lists.postgresql.org; Tue, 31 Jan 2023 15:07:36 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=s31663417; t=1675177652; bh=/wemz4LVP6FE0ges67EsBBb55UJkZUsjswFU0zBfjxA=; h=X-UI-Sender-Class:Date:From:To:Subject; b=Je2YxPJ1Gs3SiowiBPOvGK3w3aoGatNo6TskXfxKRtB8+AleLicka6782y2oIGe0C BkBA+es+9SH8eETw2DMAi00SaZOJ+uFjAx9ClZ2ZsL9Zu4865aZ0yiG4CIGQbaF0Id G10SzqW/cQEYGbIO7YFXiiVlJSeRSADCjoOvaO6sv2ABpL87SckMZebq1aw0mXSmRL oGdU9lP4sf4GXiTRCBpyeH1+q+7ZpN90BzGB104UVYFbNy62ds3a5oFDet0N+neJzU 9EGWf0hU2G62oKt5DGSyaUrEdNZay0KKDLX1ez/xzRnXenkPTeasfXso6FAJ7HXQ6g MQdDh0zkexVkA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [10.9.70.52] ([185.55.107.82]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1Mwwdl-1oPxx139aN-00yNIf for ; Tue, 31 Jan 2023 16:07:32 +0100 Date: Tue, 31 Jan 2023 16:07:31 +0100 (CET) From: Dimitrios Apostolou To: pgsql-general@lists.postgresql.org Subject: SELECT * FROM huge_table LIMIT 10; Why does it take more than 10 min to complete, with cold caches Message-ID: MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset=US-ASCII X-Provags-ID: V03:K1:yDVB3fjQJazEU75RloEaI09R17q2YcUzGYyEbjR5Fg2cwFkyZyW Un27gZRc8+vlNDUCAVzBOyH6pzL+oBBVM4bSUPv+ih8fhU2LJKUg/gD2mfzWnelU3E9HyPV yhmCzL6CzYYfOd07uHnn1MtRl7fJWvI5WS/jqgqhrnK5J9+PPWJmuZ802D6Yaw/Jcb1VJoV LpqGsBYBbs+yzIyymda1w== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:H/lQg8d06NM=;5VoiX2NDtXOtWy/e/Njwg3Tii/w xQ7rVF48xH9fLA1cpvMSlKnmgwUBPAQlRmrFRp3tbPr8n63bumGHdbawns6jKjXniiPKs2WLI GOV4Bsi1zqkPffE7JGiNWwiV20u+JlWPoL90u06GXgHM/Yhsw+uvC+TdQz18xTLqfg3C/Eh+1 45rNM6v7sH95gtxz4qzRCcFy+FFIpojhsV538OexRWerhFRnE9QJg2V4A0yDVu6mweibWTX/P 2fLeYBY5Hu3bP4jUI4JEmnl53GmnoF/joeS3CvW6k63dHQlJCvrleZOLp82tpq70aRcu2xjul 42/3KvO3iUbzwkqS9Ya80WNSLeDnav9MIg5IRS1N2Qs5+/P9bfelB0BLkB3dQOHYvmzIDFb1W seLdSHOe6wYXOJZqHn8MCP+ihpranC/nif8UlF3mQbOmKRlRSvkPZKwyX03gwjnMy5sWOlzG1 vTiLoK3gi79HtjeWOzqbdco5XIlStul99F9Z2/kfDXO6dV0uRxkm0pVc4PWJx+wMjEWyWaRMv NcKbkpf6OQqFWPJtyLpD/p3x7NZ3msTjKdp+DTmR9XSpr1mESFAPFdCjMsJX81V/gBx/NAgtx QnjlPzpfWY96TRPzri+2kYw3cvc3R7NTTo/lNaZyuHdgTfmnO6Wlw87xI+VuAV0P25GAjhVC4 LKCBJacpISP4yyBal7Dtvq7GVErEP9qvXOryHdD3ynyfp4J+mbIZ5A+V1PHOnVtRDSJOFjOUj sZEUKZIeTGSyaIVz8Di4c0BAJMnUfyJdhDeIzRVktLWe7cUxw/tohgZZExRRMuj625da5P9xT 3HjMqFCJUL1niAqV2E/DdQ+WbC3a+AqPuAQ5KnlYa+9ZTPbfSYv0MxZB8XCVViBB1X4ABYGr3 LGbq16/JVI3mo3PsGXTq7S8Y8Ec7AnIp1VACPUocnuGwQc/yYjrcVKF6W Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello list, I run a very simple SELECT on a huge table (350M rows, 10 columns of type integer or timestamp). The server is an old Mac Mini with 4GB RAM and a slow HDD. All caches are cold either via reboot, or by restarting postgres and using the `purge` command to flush the OS cache. The question is why this simple query is taking so long to complete. Most of the time I see constant 30MB/s read I/O. The memory usage is low and the system is not swapping or in other ways unhealthy. Postgres is not serving any other requests. Is postgres reading all the table sequentially, just to return 10 rows in no particular order? I'm trying to understand what PostgreSQL is doing and the data access patterns involved. All feedback is appreciated. # SELECT version(); PostgreSQL 15.0 on x86_64-apple-darwin20.6.0, compiled by Apple clang ve= rsion 12.0.0 (clang-1200.0.32.29), 64-bit # EXPLAIN (ANALYZE, VERBOSE,BUFFERS,SETTINGS) SELECT * FROM test_runs_raw= LIMIT 10; Limit (cost=3D0.00..0.29 rows=3D10 width=3D42) (actual time=3D746005.25= 1..746014.910 rows=3D10 loops=3D1) Output: run_n, test_name_n, workitem_n, started_on, duration_ms, test_= result_n, test_executable_n, test_function_n, test_datatag_n Buffers: shared read=3D2334526 I/O Timings: shared/local read=3D691137.029 -> Seq Scan on public.test_runs_raw (cost=3D0.00..9250235.80 rows=3D= 317603680 width=3D42) (actual time=3D745910.672..745910.677 rows=3D10 loop= s=3D1) Output: run_n, test_name_n, workitem_n, started_on, duration_ms,= test_result_n, test_executable_n, test_function_n, test_datatag_n Buffers: shared read=3D2334526 I/O Timings: shared/local read=3D691137.029 Settings: effective_cache_size =3D '2GB', max_parallel_workers_per_gathe= r =3D '0', work_mem =3D '64MB' Planning: Buffers: shared hit=3D69 read=3D18 I/O Timings: shared/local read=3D265.550 Planning Time: 271.719 ms Execution Time: 746057.424 ms (14 rows) Repeating the query returns instantaneously, and EXPLAIN shows shared hit=3D14 and no reads. Changing the LIMIT to 100 takes another 3min to respond, with constant read I/O while it runs: # EXPLAIN (ANALYZE, VERBOSE,BUFFERS,SETTINGS) SELECT * FROM test_runs_raw= LIMIT 100; Limit (cost=3D0.00..2.91 rows=3D100 width=3D42) (actual time=3D0.030..1= 84692.698 rows=3D100 loops=3D1) Output: run_n, test_name_n, workitem_n, started_on, duration_ms, test_= result_n, test_executable_n, test_function_n, test_datatag_n Buffers: shared hit=3D14 read=3D772427 I/O Timings: shared/local read=3D171484.072 -> Seq Scan on public.test_runs_raw (cost=3D0.00..9250235.80 rows=3D= 317603680 width=3D42) (actual time=3D0.029..184692.664 rows=3D100 loops=3D= 1) Output: run_n, test_name_n, workitem_n, started_on, duration_ms,= test_result_n, test_executable_n, test_function_n, test_datatag_n Buffers: shared hit=3D14 read=3D772427 I/O Timings: shared/local read=3D171484.072 Settings: effective_cache_size =3D '2GB', max_parallel_workers_per_gathe= r =3D '0', work_mem =3D '64MB' Planning Time: 0.083 ms Execution Time: 184692.748 ms (11 rows) Reruning this is instantaneous again. Other postgres settings: shared_buffers =3D 1024MB effective_io_concurrency =3D 0 wal_compression =3D zstd wal_recycle =3D on min_wal_size =3D 1GB max_wal_size =3D 10GB checkpoint_timeout =3D 15min Bonus question: some time ago the server crashed and rebooted, and after a long WAL recovery and some hours of idle time, I run the same query. It took even longer to respond, and besides reading, there was also a lot writing involved. What kind of writes was postgres doing? # EXPLAIN (ANALYZE, VERBOSE,BUFFERS,SETTINGS) SELECT * FROM test_runs_raw= LIMIT 10; Limit (cost=3D0.00..0.29 rows=3D10 width=3D42) (actual time=3D1284114.8= 10..1284114.819 rows=3D10 loops=3D1) Output: run_n, test_name_n, workitem_n, started_on, duration_ms, test_= result_n, test_executable_n, test_function_n, test_datatag_n Buffers: shared hit=3D47490 read=3D2287036 dirtied=3D1809974 written= =3D1762484 I/O Timings: shared/local read=3D1041111.264 write=3D107258.150 -> Seq Scan on public.test_runs_raw (cost=3D0.00..9250235.80 rows=3D= 317603680 width=3D42) (actual time=3D1283955.130..1283955.135 rows=3D10 lo= ops=3D1) Output: run_n, test_name_n, workitem_n, started_on, duration_ms,= test_result_n, test_executable_n, test_function_n, test_datatag_n Buffers: shared hit=3D47490 read=3D2287036 dirtied=3D1809974 wri= tten=3D1762484 I/O Timings: shared/local read=3D1041111.264 write=3D107258.150 Settings: effective_cache_size =3D '2GB', max_parallel_workers_per_gathe= r =3D '0', work_mem =3D '64MB' Planning Time: 0.545 ms Execution Time: 1284114.972 ms (11 rows) Relevant settings: synchronous_commit =3D off wal_recycle =3D on min_wal_size =3D 1GB max_wal_size =3D 10GB track_io_timing =3D on track_wal_io_timing =3D on wal_compression =3D zstd Thanks in advance, Dimitris