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 1wbzyC-0030Z1-0f for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Jun 2026 12:11:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wbzyA-00Bj87-0x for pgsql-hackers@arkaria.postgresql.org; Tue, 23 Jun 2026 12:11:10 +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 1wbzy9-00Bj7e-39 for pgsql-hackers@lists.postgresql.org; Tue, 23 Jun 2026 12:11:10 +0000 Received: from mail-ej1-x634.google.com ([2a00:1450:4864:20::634]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wbzy7-00000001utX-2lfH for pgsql-hackers@postgresql.org; Tue, 23 Jun 2026 12:11:09 +0000 Received: by mail-ej1-x634.google.com with SMTP id a640c23a62f3a-bed19623d6eso793926066b.1 for ; Tue, 23 Jun 2026 05:11:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1782216666; cv=none; d=google.com; s=arc-20240605; b=c+Xwx6l32KqHYMJHf1glD4as2kBpvU0QiLNuauPJF9KkuPR87aFxsYku9p94sY6AJw +ZaQmL4GwUkMFUUhfgUQ/08uElSoc8Nz7h1eTUpkN1SOnCo4JFdSwreCcXV1ZDncK5/p 7JYpvzEQOnDHG2ggeYWjhJuq2piRu5wv7Xl3BaR0geLRYpGsIQyQwzhR3AyrWNvdn29M VnuAQ9aJYxPBahjdSG+0NQ9OdQb4P/EhOOrBxaiG6TTOY83zRVlG0X83EA90ppGmrmoE BcB6wiagZztGB5dBrHopMRzNU715L6RcWaJCEfIjrXeuEGw6wBAt926vIx6v2xn48MsU AgcA== 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=EhbbdMV3LpV1v3BhO+AylCN/6zdbR3fbN5zCUPaUZI0=; fh=QDsp72xQCGvluTfQXNYfDhnNeJ1n8YEVb5SVIwuL8oU=; b=lfbJig42hJc9mtEeQ24J12ZiQYl/easLhGDAZpoadnMMvuXtV6ghSCkMGbN/lnPPPT SStp6tVEyz5OGxDeQZu0xFxAF/V2YcpXiwVhWNgK3ClxqIVI4QCINylly9yzPvMtOb/P cgLfDRBbWPjtwkr1Z+LU2wL7F/qFrkPyg+oinviF4HoQwkRg1LB9ekpaGWjWzshpzL0+ Ff/PZHNI3kaPQQu9MgzHHDYDno538hEEegitiVnrPUA1Oxk5IZGHOFqm6LnBoE1bfNiA 0RcHP1FJYBnHi343pLkVKP3+Y5nVE01ODU5bg2oi+CPSb9EmtiDmRxVcxz8r5/KlONWk O7CQ==; 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=1782216666; x=1782821466; 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=EhbbdMV3LpV1v3BhO+AylCN/6zdbR3fbN5zCUPaUZI0=; b=Id0R2MvUN/VZsm9q00ry3m9B2C9d9jl5au1nsAeY1Tz2cf/9WxsflEzO8tu0XBmAOr xFJUfoL3UdQqzrOA9DZ49MELroYRiSd5uXbkwHdQX9RKYP4xFmDkIu18tHFiexHKeQb0 kG/MbJjRxbqSP8zbgkrigVW43IS9MSplQu3ffQ3BZLe2qZknTghulCEqAQ6/WX1FANhg 6GZs93y6ivmiXrH2ec3uYMF6yYmQgpUCFOGXyP2bukh8NCFBznjSBus7d1j4jsRTd09L 3d4cftt/Emk112Y0Q5jpaDUN/SEKIJtwp2gZGnV+SRIgh0RMClGV6Lz975G5y0qUOqQU 1OTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782216666; x=1782821466; 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=EhbbdMV3LpV1v3BhO+AylCN/6zdbR3fbN5zCUPaUZI0=; b=l+NCekZRPa5MAWfV1dAfhwe3NLd6jAttQbt5VlTlV6jgp3E2rvqe+IKAAMqaUAO20f AtEzJ4UCLLM2uYFoPV+h+h4sz3N+DM6718AqJPPL0AC+T1sz5p19S1Af2AHRZO9nauAJ otonlBBIE0IQRGOQsNwky7EVGRU3sN15kch1DK8QuVDytDTu6U9h0P74rCrXf548H3uS owSATSay3yODwUWBxbXB1VH526p1nSmY57w/Ci6UwkkjP2nU3LqXOr9t72FgGC6buQxB CY7KKx7Tng1GzeESbeJ1dpgaAonhOkGgVNKJUVn33/R3ZcF3+m56s7veuh7L5cnvyrWm Q2Ow== X-Forwarded-Encrypted: i=1; AFNElJ9OR1dtPBq67IIjOc2vIDUgZ0Tgu4T+xd03669kMat4sORghb7/ghSFoojMYmQ40UC3yKT2qWjips9ovrN4@postgresql.org X-Gm-Message-State: AOJu0YzvfMZmviBW/IwzPqRNMPrBasPSVtHTuOZRtSfXFu6iPnIrVBHM KQn79kzzfVPPOr/HXGrj1/KbVYMu94KPyFi7fnMRgmMPx9WMUooB12yx2GeKBEMoubVMrQxQqDN 98Qd5+j6YS6WHcUo5dLKMGeEVWOW0AZs= X-Gm-Gg: AfdE7ckpD39EfuaEQY7JLxyu9bwuQjYKA07KxXstz/7h83Jgfk4qGn70u7/p3+h9+8W 6iJ8vwmp5KZCvNFibmLA2ewttXT2JXlvTmXdPiXKJh06MjkWC934IaDrgVdYlWe9U4RfTU6oXsm ZWnkWp7oX2xz2xzMJ+bWhqsYiCJBM/Tm2XLqCKlTWZFxG4aJZ0qpentaFC6RItKj2gC+wywfcU5 zLdDcVRstPZZ63nyqbr0zY6veakaXmY9gPngb3CXH91srKD/Q6h4al5kFAbI68CrG23/IZk/Esm vomS8HPVLZBVop/rOmwPu3lxsIVTVQJh5e+xsvgVPg== X-Received: by 2002:a17:907:c001:b0:c08:22ae:6f46 with SMTP id a640c23a62f3a-c10810aa410mr119988066b.53.1782216666007; Tue, 23 Jun 2026 05:11:06 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Robert Haas Date: Tue, 23 Jun 2026 08:10:52 -0400 X-Gm-Features: AVVi8CdD2xrSVqJUJ_6wBb6LCJiYXB3uP0ornYe7NcS1AXLLMuLn-MxIOo62p-0 Message-ID: Subject: Re: use of SPI by postgresImportForeignStatistics To: Corey Huinker Cc: Etsuro Fujita , "pgsql-hackers@postgresql.org" 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 On Mon, Jun 22, 2026 at 3:08=E2=80=AFPM Corey Huinker wrote: > The first bit of dissonance comes from the SQL-level functions having sch= emaname+relname parameters, and the reloid is resolved via RangeVarGetRelid= Extended() which has a callback to check for correct permissions and settin= g the proper lock level. However, the C-caller would either have the reloid= already, or an already open Relation, but no assurance that the caller has= the correct permissions for that table or the correct lock level on the ta= ble. So either we make an equivalent to RangeVarGetRelidExtended() that tak= es an oid, or the C-caller has to derive a RangeVar, call the existing Rang= eVarGetRelidExtended() function, and verify the result reloid against the s= upplied parameter. I went with deriving the RangeVar and putting an Assert = on the before/after reloids, but perhaps the smarter play is to make a func= tion that checks for ShareUpdateExclusiveLock on the Relation, and then doe= s the equivalent of RangeVarCallbackForStats(). I don't think this is really a problem. If the caller is specifying the OID, they should have called RangeVarGetRelidExtended themselves. Permissions-checking, locking, and opening the relation should all happen simultaneously, and the logic shouldn't be duplicated later. > A smaller bit of dissonance was with RecoveryInProgress(), which if I rec= all we're checking before RangeVarGetRelidExtended() to avoid trying to tak= e a lock that will fail. That check might not be meaningful if the C call t= akes a relation, thus ensuring that some level of locking worked, thus we a= ren't in recovery. I don't quite follow this part. > Next is the existing validation functions stats_check_required_arg(), sta= ts_check_arg_array(), and stats_check_arg_pairs() all work on values indexe= d by the positional functioncallinfo and the corresponding relstatsinfo/att= statsinfo structure, and this makes a lot of type checking and value checki= ng compact and uniform. If we want to keep this sort of uniformity, the re= sulting StatsData structure will end up looking a lot like the FunctionCall= Info that we already had. Yeah, this is worth thinking about. You could consider putting an array inside the struct and use #defines for the indexes. And instead of having a separate Boolean for each index, you could use the same index to reference the N'th bit of a single integer flag variable. > Next is the fact that the end-destination for every value passed in is a = Datum for a pg_statistic heaptuple. Most Datum values are checked only for = their null-ness and if they're the correct type, so the value itself is usu= ally just passed directly from fcinfo into the heap tuple values[] array. T= he float[] values are checked for number of elements and whether any elemen= ts are NULL, but that is done via array functions that take a Datum input. = Only in a few cases do we actually look at the actual internal value of the= Datum (reltuples, attname, attnum, the anyarrays), so there's little to ga= in there. Right, so the question is whether it makes more sense to pass down C strings or Datums. > There's some additional hassle in the fact that pg_restore_attribute_stat= s() can take an attnum parameter OR an attname parameter, but not both. I w= as able to resolve that in a semi-elegant fashion, but the other issues hav= e convinced me that we're probably better off continuing to use the Functio= nCallInfo version of attribute_stats_update(), though perhaps with a differ= ent name, allowing us to use that name for the new API call instead of impo= rt_attribute_statistics(). On that particular point, I think I'm still unconvinced, but I also haven't looked deeply into this just yet, so maybe I'm wrong. --=20 Robert Haas EDB: http://www.enterprisedb.com