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 1uz8wx-00HIyp-Tm for pgsql-performance@arkaria.postgresql.org; Thu, 18 Sep 2025 07:21:03 +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 1uz8wv-00EOtT-W8 for pgsql-performance@arkaria.postgresql.org; Thu, 18 Sep 2025 07:21:02 +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 1uz8wv-00EOtG-MC for pgsql-performance@lists.postgresql.org; Thu, 18 Sep 2025 07:21:01 +0000 Received: from mail-lj1-x233.google.com ([2a00:1450:4864:20::233]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uz8wr-001SrW-3D for pgsql-performance@lists.postgresql.org; Thu, 18 Sep 2025 07:21:01 +0000 Received: by mail-lj1-x233.google.com with SMTP id 38308e7fff4ca-3515a0bca13so19621911fa.1 for ; Thu, 18 Sep 2025 00:20:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1758180058; x=1758784858; 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=DqHLb0OBja58ETonfxu/99LKALkBbyL6lEPZkFqJPf0=; b=INjgSDBf02OehV6bL0FfzZPGnAwtchvMUHB1lRInqfmA1cnCLhsPHNxyi4OwmeGzbR bIKrScEzCYfmRK48DI7C6RHppEM9USUQEd+QV+DfrcKvqeF1tdyMR+uVdl+lt5wuL1h+ 9R4qzDC6AkXXeNaSh/B4Xc84gY3IMgVP86xgDQ3Ymmf6iTrwVhvV3iG+bIQ87hY88Wvy xbLPeETE4o/3aCB4+Lu38sSJe0omK0BEge0o+HKE4GkMROSzJ1d5+XZ1aS0wmWSAQ1s9 5aKCitFJgq27WS1LdeDjMvOwA2Dlenbfyyg95/CpCFki7ksQAhBUnj4V+d12LtAlv2Xp 9RHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1758180058; x=1758784858; 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=DqHLb0OBja58ETonfxu/99LKALkBbyL6lEPZkFqJPf0=; b=N9sdAMKxsildH/2eqFK1612ehoKo7Ngp8L/BzGAkW9kD7PqCC85ZX+BOIK5eFAhOwV XO/GRUKxP31baHLBbH2IFTWAB52uEWUCG0RZ7IXLGYwcbIknNmj6enKPVq0evqOwvF1l KmkiSFWl9KAsQJiZbr6Y0j3/fsjlPTCtt+b242GH4+hnpz3CXTr/5e90l5bYd2hnj05r kFYKCO81gAfFsGClxX23ez9JM5R4PTkV3JZkUWqcYuzqPu+bntyqaXIYKELnFqpMNQE0 B7+g30rPn632TvbyOx64NJv8fMM9SSSR41ED8ZcnJSfqObxCn/XLMhlBNIrFlw8ctHgl 9yjQ== X-Gm-Message-State: AOJu0Yz30BkjzhjNvZU6tATZWwfWb3WNJOwh3o2oL7zwEKJCcjeiIzy6 Z2qqA2bWA9NsNh9HrOFIZlzowBUjRdo7uuhWLAaPwSGenz8z/RNpoGBey2TFsNUUtCnpoG1ONsZ iakS+TYCqPo4WW1x/PuSLwnA0PZqxT4SbCA== X-Gm-Gg: ASbGncv0rJgokI/I/WmG4Q4oYrVMzS2/sOn4no5q4Vl29swBcVfyzijnDe1sesIVgrz hKJTBgsiEAr5I0RwNMIM/5D7/AwgjSAQf/7Fi+/SXFdHLl/yld+lIrs3oxnhYo9pFO/+lKee9G4 LXhZF//OQ/j3Az35O9LOVwbzqtuFckO+AwtoyXhLcnZOyIv1I5GPmGqlle9XvlbFOJBHmi8lRfD oUhodhzmBzXpX45anMWeEicOucCAxoDZceUT+LM7mvI5hxSKDz5zYuNms/F X-Google-Smtp-Source: AGHT+IGqABXh48SMja92lHSkvIe9i8iSL/PKuqinPbRja+9DDKeNtVfS72YkmvE/uAKXCuiCRw8f8ztPQNlfxhOBvb8= X-Received: by 2002:a05:651c:1987:b0:336:b891:18db with SMTP id 38308e7fff4ca-3617b0da50dmr7593601fa.2.1758180058113; Thu, 18 Sep 2025 00:20:58 -0700 (PDT) MIME-Version: 1.0 References: <8f25c08c-acb2-47ca-b8c1-6664fc31a361@thefreecat.org> In-Reply-To: <8f25c08c-acb2-47ca-b8c1-6664fc31a361@thefreecat.org> From: David Rowley Date: Thu, 18 Sep 2025 19:20:46 +1200 X-Gm-Features: AS18NWC1HC1oyTK44ebECJBlsH6yvvRmjBOq-7G5jCD7lEs5crIw1-Mj3Rr2NjY Message-ID: Subject: Re: Why isn't PG using an index-only scan? To: Jean-Christophe BOGGIO Cc: "pgsql-performance@lists.postgresql.org" Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 18 Sept 2025 at 18:36, Jean-Christophe BOGGIO wrote: > Insert on copyrightad (cost=613790.59..4448045.97 rows=0 width=0) > -> Merge Join (cost=613790.59..4448045.97 rows=84972138 width=328) > Merge Cond: (((c.imcompid)::numeric) = ip.sipa_ip_code) > -> Sort (cost=35712.97..36475.44 rows=304986 width=8) > Sort Key: ((c.imcompid)::numeric) > -> Index Only Scan using ix_ad_imcompid on ad c (cost=0.42..7931.21 rows=304986 width=8) > -> Materialize (cost=578077.61..594521.17 rows=3288712 width=23) > -> Sort (cost=578077.61..586299.39 rows=3288712 width=23) > Sort Key: ip.sipa_ip_code > -> Hash Join (cost=56043.04..154644.48 rows=3288712 width=23) > Hash Cond: ((o.imworkid)::numeric = ip.sipa_song_code) > -> Seq Scan on oeu o (cost=0.00..41901.33 rows=1587533 width=8) > -> Hash (cost=48542.24..48542.24 rows=600064 width=25) > -> Seq Scan on msipfl ip (cost=0.00..48542.24 rows=600064 width=25) > Filter: ((sipa_comp_or_publ)::text = 'C'::text) > Is it normal that PG is doing a seq scan on table oeu but an index-only scan on ad? I had to stop the query after 5 hours, how can I make this faster? Of course I ran VACUUM ANALYZE. Yes. Since *all* records of "oeu" are required and they're not required in any particular order, then Seq Scan should be the fastest way to access those records. 5 hours seems very slow for the estimated number of records. Have you tried running the SELECT using EXPLAIN ANALYZE without the INSERT part? Even if the 84 million Merge Join row estimate is accurate, 5 hours seems excessively long. If it still takes a long time, you might try SET enable_mergejoin = 0; and run the EXPLAIN ANALYZE SELECT .. part. That'll at least give us more accurate row counts of what we're actually working with. David