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 1ueDYX-006Wib-Uv for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 14:01:22 +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 1ueDYW-008OcU-Ud for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jul 2025 14:01:21 +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.94.2) (envelope-from ) id 1ueDYW-008ObY-Ja for pgsql-hackers@lists.postgresql.org; Tue, 22 Jul 2025 14:01:20 +0000 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ueDYU-000DQ7-2h for pgsql-hackers@postgresql.org; Tue, 22 Jul 2025 14:01:19 +0000 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-451d54214adso39945365e9.3 for ; Tue, 22 Jul 2025 07:01:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753192877; x=1753797677; darn=postgresql.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=NcdyviE33tjFuA/r/54fV8XhDMNqROMzg9wVwnoSAAc=; b=YLgFDtKkdH/4QTgM97jNV170bSaXkfZqkkmtuKyi+goEn3hypyYMhRhf/Zxhn4WsVC ul5fXceqxxWtnLw4cdU/RqTfsBCLNQMsi3gfkBsChVcSkpmhDkPg41QQ5kP+i0vVvsLI 2yH2ECGj/+eutNEWf9VQgej2ObW1pwu+PfM/7sS2hSTvecqCnOHWLuwGf2qmB1xSSdYD QArw1IdRhTjRIf6GEqL0fTrsO9ejz5Mrgbq8QMRkEjuPERP5l2CcPDp1J9Yc0AScs5Lj MQuu49oByJWm3Y1TMnAsSZdRpJtEcoms1U9gN/OrnSZJOTsf8A5Y/jfe12+tDro55aTJ CW5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753192877; x=1753797677; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NcdyviE33tjFuA/r/54fV8XhDMNqROMzg9wVwnoSAAc=; b=PsUzNxL8Hssqb/qCuK49J5J7XgEZ5DenXBQMArEKmGFphj4epvEsWuEQjw5GorM5YL Cqzy+y+VtS4xhiL2tp5wC8zNSJOcr0RIAA2loG8rUAFDlTv+fb1PFvGWXUuzAaGLRc2p wPmdUtUWo0SaFab4lynjnNz+eOHqBRl+RNygjzl6uf2keIxrB0QcSumpj+s32GrmF90y ynaXLgXRXzFPLo9U3qA08yywgJGKBWHz8HjFrecdYmS8kfj9YYMjQ5S78KUHsfhT6zr3 lpR9YvALgOlMTfWW6z/Ib8U4zjLXAg4ytli2PGnawqYA3knetVVELJIzwGBcmQTWfBK5 HGtg== X-Forwarded-Encrypted: i=1; AJvYcCVasPxCkr88zi7H7h6Fj7/cXxg11sZu4b+4WryFCFIXbo7ysLsrx/T1vffhtUKYEiAyrx1b8U3OPilmIvKw@postgresql.org X-Gm-Message-State: AOJu0YyuRUOJCUpK4QQZ7mPu1pwm8PPV94frzE8IW1iVYkUtpaw0pMHi TlZ6dI+x6JKQLMNGtKOWde5jeh4ol3QY+WqyH+e7kWUPPASTmPAEuRz+2TQcCqor X-Gm-Gg: ASbGnctpt7BftqLt91kC7ixtedGVdmER2Zavb0K/0SMekuzk94ksT0txyZ8D3Xaf85B Dd7hyq1e80LaQbH8cSjnpYGe9FufM+tSE/W4aWJMIUJie/0n1C79sI+sGX6Fxh3Sit12cJHqyvy ocV3oQ3LDP6AKfRRwl7mED8I5DDTlHYf4QCdHXLJiwIe3q9jQ5GXj7rhTSI1hv0CaLrK82o9CFG MYIP2mvprouwNCyUsCZoyqX3/mQP84JzaBdceuTUe/bOqt0fu9ffjG2e238obF0gTMacAb51j4g tXTdRGXKGlZYwnq0d0biKM58VHnVxUguZbXZd6fsWT1gtpvkM97QzIAjEb4RdbSF2Jd6dkZuX94 s1gYKy9jrPdQ5B0WMbx1mR58Z2hP7ESm8kg== X-Google-Smtp-Source: AGHT+IECUjSDvUUMySzOZ+Ac/JPPwt1B9Brw7teVCSsxEuXZODo0ipANJQzjsvecCdrJp4ueIs+7Zw== X-Received: by 2002:a05:600c:c16d:b0:442:dc75:5625 with SMTP id 5b1f17b1804b1-456352d0b70mr190273015e9.5.1753192876637; Tue, 22 Jul 2025 07:01:16 -0700 (PDT) Received: from [192.168.15.242] ([80.251.191.198]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3b61ca315basm13540950f8f.39.2025.07.22.07.01.15 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 22 Jul 2025 07:01:15 -0700 (PDT) Message-ID: Date: Tue, 22 Jul 2025 16:01:15 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Memory consumed by paths during partitionwise join planning To: Ashutosh Bapat Cc: David Rowley , pgsql-hackers References: <695ac356-87e6-45b3-a487-1e07b526ed52@postgrespro.ru> <3bcf826a-6d69-4232-b43b-d5894be8bd6f@gmail.com> Content-Language: en-US From: Andrei Lepikhov In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 18/7/2025 13:48, Ashutosh Bapat wrote: > On Mon, Jul 7, 2025 at 8:43 PM Andrei Lepikhov wrote: > if (!IsA(new_path, IndexPath)) > - pfree(new_path); > + free_path(new_path, 0, false); > > Why don't we free the subpaths if they aren't referenced anymore? During testing, I discovered that we sometimes encounter unusual cases. For example, imagine pathlist: {SeqScan, IndexOnlyScan1, IndexScan2} Someone may decide that Sort+SeqScan may be cheaper than IndexScan2. Or, IncrementalSort+IndexOnlyScan1 is cheaper than IndexScan2 ... And add this path to the same pathlist. I am unsure which exact combinations may arise in the planner, but without strict rules, someone may forget to increment the refcounter. >> To address this complexity, I propose the following solutions: >> 1. Localise reference management within the add_path function. >> 2. Transition from a 'strict' approach, which removes all unused paths, >> to a more straightforward 'pinning' method. In this approach, we would >> simply mark a path as 'used' when someone who was added to the upper >> path list references it. Removing less memory, we leave the code much >> simpler. > Yes. This was one of the ideas Tom had proposed earlier in another > thread to manage paths better and avoid dangling pointers. May be it's > worth starting with that first. Get rid of special handling of index > paths and then improve the memory situation. However, even with that, > I think we should free more paths than less. It seems like one more trade-off: more eager cleaning means more resources spent. P.S. path_walker makes sense to implement. -- regards, Andrei Lepikhov