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 1wZSXK-0013ZJ-0G for pgsql-hackers@arkaria.postgresql.org; Tue, 16 Jun 2026 12:04:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wZSXI-00HC6I-2k for pgsql-hackers@arkaria.postgresql.org; Tue, 16 Jun 2026 12:04:56 +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.96) (envelope-from ) id 1wZSXI-00HC69-1f for pgsql-hackers@lists.postgresql.org; Tue, 16 Jun 2026 12:04:56 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wZSXG-00000000jHW-1laG for pgsql-hackers@postgresql.org; Tue, 16 Jun 2026 12:04:56 +0000 Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-691c5776f95so8722871a12.3 for ; Tue, 16 Jun 2026 05:04:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1781611492; cv=none; d=google.com; s=arc-20240605; b=eIQ610oxxj43SoOxjVcF7pODUo46/yD6wV+upAC1dAljLk58/ZM861gEmgZMduhu92 TFHgb8cwQdKaWmIMeHZb6l37l/KJkz7Kr9hkJ8uRz8h87rWovlfX4NrNQyTDKVJNDD+0 O/5Ymc/N2KRjIZ6iQP+x+6XKogz41bJRK3kySj4D/GlRWsgajhns4tJLztwCnlRjROlY kTVvjCeDwqcNfanC2Ria+dEUfYFgjKIlTNDXQsG1rAK52ErwKvY7RvfaaHONnQunkXtp ofueTH7S+JAWq8+N131ncw+5Gtjc4Xkatj/AIQumE6ljhn/TFi8KYRIsZVWjUkj+n4QS TI9w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=5Xz7+2csVhsMrUHQknh8e2y1BAv67D3HI4QjnNcoVx4=; fh=RtKzEpXoD8f5g8ALyk2iFgCaStl6hybx1LByTbSyruA=; b=OkOX+KCfxBSuP622RlCqxulakRYxXw9cKTXcQU1hmrICXXr6d4XhsfEM+1fbFisGIR dzoOa4EarFGY4xkJmKyT/jRVgGVu37RwqsCgB70CwBHoh6cphQWR/8Hfrw64gx/nemPb QzkSgEV9zg7upfp87QYxq6AHF8BlTM3W57BfmoWoVKNQKr7Q4jBwcTjedGLhRPj6BblJ 44Wm5reLdrLX7HhCNKex4TTPKgWJ7vFrvGqQvxhu7vKBuGgobStMpq0QBtxZOGkecOAZ YCGWkVrIwW0OgfmQnR81ZHt/14ySypRTUkCZqoT1Mejt8iuz7uo4zqliRQ3svEbVoFpt G5gw==; darn=postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1781611492; x=1782216292; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=5Xz7+2csVhsMrUHQknh8e2y1BAv67D3HI4QjnNcoVx4=; b=NGuhnnPJezg/Ox8tmP1hVUh6GA91qqntc4vzpmaR9o3qAbvhVyhDZlF/ZyjdPP1opP sNuAShKJEehgPmYLvH6DZN/i6mfgU4FBsObt+RYM4H7CRQf0lRPOVfRMCdKPBzAgpFhl uuHU+KU7g+iM1sgPjvdC7ak9ICtPT1JmV8jFtpyxEVBOJmH9o3RZ2yzFJc+FrAGj1akn xuj1if5/PDGnBBblyqBYCFQZ1nryQibCJYYzcNI4MVs01Xroo1VolcFnoKP/PNL7f7k+ M7jPh3yfDRMsyJm7+ALOWCNWU88W7Gtju87WN5c64KFoVEW5GvLW2P+aZxy8YXOZPKis pTzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781611492; x=1782216292; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=5Xz7+2csVhsMrUHQknh8e2y1BAv67D3HI4QjnNcoVx4=; b=k3OpcwB9a7bhtz9/0vQZE4vceWvb2qLT5JIXCsU79q4sdedLWBcg1ZvKsa6rooqz8y WhEIqp0rt5ooMdTsIT3T09Vf4glnG1YuDo7c72psptOuhq2mUOkr/J60o5Yn0Q26eoE7 vic68VDx5oGCqH/Q/WT2ntGUhDgeu1XC9zanaGHTx0da9Gxp9XEiSe6ngb/ycbQ+SO0/ XLyqFrteDndh4cmLGAWLj9nvNMBxpDTfKiyh84VGZtl3gsA7q0iD9ogGdkIYuSDTk64X sR1eT1laD72mL7HnoNsWJfLYufgDQUouA9K7l9yiY5D323+HzJ2YIn5rehjksO2PFr7b NHwg== X-Gm-Message-State: AOJu0YwtlymYuD2/qAmUINRVs6/jU6CJDcxJj6YYbrUhQt3UCjhoLRpA b/LlVyDbHdwhm8dHRdRJgLB/VId3BwV0d5jtUbk7/r+GBS8hPJK5Ad20DwOadODNri14QvRpfAJ polw9y6leeAHdEYnafWyNOydfhJaPw3I= X-Gm-Gg: Acq92OGwgOrCGOG2hrgvaqwz1o7hC9a3QBu3otCHJMXRE30xc4JOjZx3gb90W8AG85z 5cG85cy4H0un8hH7sNaqbCCS752o4pn8AYt1iL3u+OBRrsHY+QWsRiniO/jXqJFQgl3JDS4HNFo r5jylZkI7pOWibmtehg8cQEuKP0v+a3QrKY5pf0t2qIln4JFXddbfLGmyD7wxJ6fkfzjJ8PR/Q6 fb7FM+Z3IIOONEaw4zaAgy4Pwzhn6KbxN8F3o7sjoZMPacs26LMHiAnum9CvW2tAn4kh2+JjWv3 Yh7Dyvov7E83sVusUHUThRf/6b+L3rEXxJugOUDHl0hGsOidsVuTp8doxIrOTmVBXZlg31Hu9x3 Dub33801EBIqYOVU+sKa4B+OxhKhtZmGu+fJdKpvV X-Received: by 2002:a05:6402:4493:b0:691:af08:a9fd with SMTP id 4fb4d7f45d1cf-69378a03b9cmr8707488a12.11.1781611492144; Tue, 16 Jun 2026 05:04:52 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Etsuro Fujita Date: Tue, 16 Jun 2026 21:04:37 +0900 X-Gm-Features: AVVi8CfEr_Jas7a0DHg0y5Zg52IDnImJ1PCpKcTWUkFeyhgHDgeD0jZB2OlUjHo Message-ID: Subject: Re: use of SPI by postgresImportForeignStatistics To: Robert Haas Cc: "pgsql-hackers@postgresql.org" , Corey Huinker Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Robert, On Tue, Jun 16, 2026 at 2:50=E2=80=AFAM Robert Haas = wrote: > I'm concerned about the way that postgresImportForeignStatistics is > doing what it does. First, it's using SPI to execute two constant SQL > queries, attimport_sql and attclear_sql. However, it doesn't seem to > set the search_path, and these queries aren't fully robust against a > possibly-unsafe search_path (e.g. the call to > pg_restore_attribute_stats is schema-qualified, but the type names are > not!). Good catch! > Furthermore, the function we're calling takes a schema name and > a relation name as two separate arguments, rather than a single OID > argument. I'm not sure it's completely guaranteed that we'll end up > affecting the same relation that we locked in > postgresImportForeignStatistics, because e.g. what if the containing > schema is being concurrently renamed? Ugh. As pg_restore_attribute_stats/pg_restore_relation_stats are volatile functions, SPI executes the queries in read-write mode, causing an error, like "schema "foo" does not exist", or other unexpected results in such a case. Not sure what to do about this issue right now. Will think about it some more. > But even if it is absolutely guaranteed that we latch onto the correct > relation, this seems like the fundamentally wrong way to do this kind > of thing. It seems crazy to me that instead of exposing an interface > that would be well-suited to direct use by an FDW, the > statistics-import stuff exposes an interface that can only be > reasonably called via the FunctionCallInfo interface, which then > results in postgres_fdw needing to jump through hoops to construct and > execute SQL statements. I thought it would be a good idea to use pg_restore_attribute_stats/pg_restore_relation_stats, because future changes in attribute/relation stats would be absorbed by these functions, which would lower the maintenance cost of this feature. Thanks for the comments! Best regards, Etsuro Fujita