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 1ttugK-001sG4-Hz for pgsql-hackers@arkaria.postgresql.org; Sun, 16 Mar 2025 20:34:00 +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 1ttugH-000pTb-IP for pgsql-hackers@arkaria.postgresql.org; Sun, 16 Mar 2025 20:33:57 +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 1ttugH-000pTS-79 for pgsql-hackers@lists.postgresql.org; Sun, 16 Mar 2025 20:33:57 +0000 Received: from mail-io1-xd2c.google.com ([2607:f8b0:4864:20::d2c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ttugD-003EfA-1W for pgsql-hackers@lists.postgresql.org; Sun, 16 Mar 2025 20:33:56 +0000 Received: by mail-io1-xd2c.google.com with SMTP id ca18e2360f4ac-85b43b60b6bso121585139f.0 for ; Sun, 16 Mar 2025 13:33:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1742157233; x=1742762033; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=D7nV9dWoI8R61LNjI+RmBIoVSF3HoiptlXQV+2Umf2Y=; b=C3GKKsEOq5d44BAIYnK4M9qqH+tiu6oPTUK70IrIsVNwc8U2/7J/0LjCLSdttedDG+ PIAVe1aLznkofmRtDzcJYiWOicaeLktwtr29Enr9hHH7iYhZu+Cz7uPeEuAaK35E1/by qcq8bGPZ7mzDXaEmdK/IqHPJtCcCaTJOIDcMYP9310N8meqIZMdkPS64Htgd48E7Y4KK ii9+gGWDe1czmVIIa0o62tYVIEKAC9sRGnT3v1S0mGdup4VOV/6xOlnIJB1ZyfU1s83O 7wkb9jNC8CtfbXQ08ylOHpTKWz/mDl/MEW7kPuFv1UQpCVtRcvqzhXB0ebfjUl1LowrD kVPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1742157233; x=1742762033; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=D7nV9dWoI8R61LNjI+RmBIoVSF3HoiptlXQV+2Umf2Y=; b=AUN8ZOIp7rS4i3c3fhQeY5LfvzMNWf7VvJHu4u9qZPk462cznie2ZbI5Wi9NnbsNPe FKmLz4IJ+uFt63rqKH/0lVI/LgqV5X1ivhWf5KRafFHeqNuuA7i2eXu+ByAChd0j0M/C jRqKKej/YcF5VLSsGW6SBE5FKLEIKzHqil+OWjw/0cbT6adZgCxa042j1Jul8pRLOODP 7xbeFweGhK4arlZVE9RB6iNnVPg+XPNyv9PlBYXGwLZXwQ0Woq4iBWmyXswizUTafD3d yJkd8gIwj+0wgw0IwNvx6e6EEpKfJVL0RpprjRCHfreutgNFz6LY4ARmWs+IYSZb+3cn 2kZQ== X-Forwarded-Encrypted: i=1; AJvYcCWfE/ZZTZMmEpXsbcNpYkogUqqbTQjKJwSpNXnj1LsMEi1mgIIvXAn4yI3LgG0gMth856+JbxiGUnyKmv+A@lists.postgresql.org X-Gm-Message-State: AOJu0Yz+BTb0KCDxIF+ywpRStNBd9MNFjt5Q7kwU3UF7AjY7F5ZqtYmo SG2rJjZe5iNWUqAiXxmEx6vuk49iB+KUHGFOBoXhSkvW6tZPLYGi X-Gm-Gg: ASbGncu80K+DEBv6UKrz69Xp4kJzbRu70pCYM2EaAq7j+dAG+3JHh/PkEGkUT7MVhrB 14A1k6oqDBnQPsdgRZ6rIsG4mxPxrFt2GX/AUr9ebFbYJd1YFDVxo/bQ/C1GD8nPvTKCLGSmfgb 304udB2HwJDcbtWVchyakx3uUzvqgfUeBxtHSpPLwsblPReqegnTcdtD+wLGUj+gUAXNG1Oj1G6 2Ar33PikZqtNP3nvE8vPo0W1HJ83N825AVCRRg9fjJr3NJYDbNwHQuv7RTU8OFab1czSiRSgs6/ CLy5c83FxHkl+mLz8wv8u1z2bmYqWt1FaOXgI0k38c59jUcteW86QGPXIAmv/6/pA7rgHaAHKuS 2scFhvcPhxkxDmV0BNcvVjWaZY+dJ9i0m6XHr/Q0S X-Google-Smtp-Source: AGHT+IHkGCqq3tkxfdtBKWVHYwkXDb67td1DQlW4D04XlzzRl1rsyxu266+YWQBNhyjunm1O2Mk5GQ== X-Received: by 2002:a6b:db17:0:b0:85b:5c9a:a9ce with SMTP id ca18e2360f4ac-85db85f8b79mr1183767639f.6.1742157232681; Sun, 16 Mar 2025 13:33:52 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4f2637fabdfsm1941782173.93.2025.03.16.13.33.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Mar 2025 13:33:51 -0700 (PDT) Date: Sun, 16 Mar 2025 15:33:49 -0500 From: Nathan Bossart To: Corey Huinker Cc: Jeff Davis , Robert Treat , Robert Haas , Andres Freund , Tom Lane , Michael Paquier , jian he , Bruce Momjian , Matthias van de Meent , Magnus Hagander , Stephen Frost , Ashutosh Bapat , Peter Smith , PostgreSQL Hackers , alvherre@alvh.no-ip.org Subject: Re: Statistics Import and Export Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thanks for working on this, Corey. On Fri, Mar 14, 2025 at 04:03:16PM -0400, Corey Huinker wrote: > 0003 - > > Storing the restore function calls in the archive entry hogged a lot of > memory and made people nervous. This introduces a new function pointer that > generates those restore SQL calls right before they're written to disk, > thus reducing the memory load from "stats for every object to be dumped" to > just one object. Thanks to Nathan for diagnosing some weird quirks with > various formats. > > 0004 - > > This replaces the query in the prepared statement with one that batches > them 100 relations at a time, and then maintains that result set until it > is consumed. It seems to have obvious speedups. I've been doing a variety of tests with my toy database of 100K relations [0], and I'm seeing around 20% less memory usage. That's still 20% more than without stats, but that's still a pretty nice improvement. I'd propose two small changes to the design: * I tested a variety of batch sizes, and to my suprise, I saw the best results with around 64 relations per batch. I imagine the absolute best batch size will vary greatly depending on the workload. It might also depend on work_mem and friends. * The custom format actually does two WriteToc() calls, and since these patches move the queries to this part of pg_dump, it means we'll run all the queries twice. The comments around this code suggest that the second pass isn't strictly necessary and that it is really only useful for data/parallel restore, so we could probably skip it for no-data dumps. With those two changes, a pg_upgrade-style dump of my test database goes from ~21.6 seconds without these patches to ~11.2 seconds with them. For reference, the same dump without stats takes ~7 seconds on HEAD. [0] https://postgr.es/m/Z9R9-mFbxukqKmg4%40nathan -- nathan