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 1u1qFD-008vrN-IE for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Apr 2025 17:26:47 +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 1u1qFB-001qxe-PG for pgsql-hackers@arkaria.postgresql.org; Mon, 07 Apr 2025 17:26:46 +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 1u1qFB-001qxW-6J for pgsql-hackers@lists.postgresql.org; Mon, 07 Apr 2025 17:26:45 +0000 Received: from 003.mia.mailroute.net ([199.89.3.6]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1u1qF9-003Vvw-0f for pgsql-hackers@postgresql.org; Mon, 07 Apr 2025 17:26:44 +0000 Received: from localhost (localhost [127.0.0.1]) by 003.mia.mailroute.net (Postfix) with ESMTP id 4ZWbjZ14rxzlfl2g; Mon, 7 Apr 2025 17:26:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=acm.org; h= content-transfer-encoding:content-type:content-type:in-reply-to :mime-version:user-agent:date:date:message-id:from:from :references:subject:subject:received:received; s=mr01; t= 1744046801; x=1746638802; bh=VSRWJtnlf2AVBjMAMhD6q0zRc4C3RGMz7hj rTvN3gj4=; b=Iv+9buJAcrB5/y89d3fdhIDqMw1nkH9kzQiPUi4oHFzu2upuRZy k5PIIatvdIvZyN2cGJTnR9sw2OJInYCkVz/elBDgEj+/2pXNLp6Kwhf5BUGf/u/A nSa99hGJTunn8Y3fv0RteUNysvYkcbiJB3eG5LUTwkxrpIH6DtgZv4KwsMU4EqlV qHj93xxpkWbwKE0NwVBPhO2oArVHJv65aE2Pm6FOf4pr6eOywsJG6Uk0mS+2KTac Wq9Non3B2ljocEREnDZJUYw5CewP9apmQ0AAwFg/3wGj4hmdmPJbyPQhWbUe8t1e dErnXfFxxUJ0gFeSYwq9Kc7fWGfYu5WHSIQ== X-Virus-Scanned: by MailRoute Received: from 003.mia.mailroute.net ([127.0.0.1]) by localhost (003.mia [127.0.0.1]) (mroute_mailscanner, port 10029) with LMTP id 46Td6pf2aqM0; Mon, 7 Apr 2025 17:26:41 +0000 (UTC) Received: from [73.103.120.3] (c-73-103-120-3.hsd1.in.comcast.net [73.103.120.3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: jcflack@acm.org) by 003.mia.mailroute.net (Postfix) with ESMTPSA id 4ZWbjW4Mx1zlwf88; Mon, 7 Apr 2025 17:26:38 +0000 (UTC) Subject: Re: FmgrInfo allocation patterns (and PL handling as staged programming) To: Tom Lane References: <67F2B767.9040507@acm.org> <2965818.1743960827@sss.pgh.pa.us> <2968353.1743962392@sss.pgh.pa.us> Cc: PostgreSQL Hackers From: Chapman Flack X-Enigmail-Draft-Status: N1110 Message-ID: <67F40ACD.7040507@acm.org> Date: Mon, 7 Apr 2025 13:26:37 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.7.0 MIME-Version: 1.0 In-Reply-To: <2968353.1743962392@sss.pgh.pa.us> Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 04/06/25 13:59, Tom Lane wrote: > polymorphic arguments: the element type of an anyarray argument can > change on-the-fly from one call to the next in the same query. I > think this is only possible when you're fed pg_stats.most_common_vals > or one of its sibling columns, but that's enough to be a problem. > That's why all of our array-munging functions that use fn_extra to > cache type-dependent state are careful to check the element type > against the cache every single time. This was really bumming me out. I thought "what on earth does that do to the rest of your surrounding query, say if you have anyelement types in the args or return value also?". So the answer to that part is easy: if a routine's types include both anyarray and anyelement (like, say, unnest), it just can't be applied to one of those statistics columns. An attempt to use it that way in a query is rejected early, before any attempt to call the routine: ERROR: cannot determine element type of "anyarray" argument STATEMENT: select unnest(stavalues1) from pg_statistic limit 1; Now, if you have a routine that uses anyarray but no corresponding element pseudotype, you are allowed to apply that to a statistics column. It can even have anyarray as a return/output type also. When it's applied to a statistics column, here's what my dispatcher sees: # select foo(stavalues1) from pg_statistic limit 1; RegProcedure[1255,16861,0]public.foo essentialChecks: checkBody true RegProcedure[1255,16861,0]public.foo prepare(): inputsTemplate : [RegType.Unresolved[1247,2277,0]pg_catalog.anyarray] unresolvedInputs : {0} outputsTemplate : [RegType.Unresolved[1247,2277,0]pg_catalog.anyarray] unresolvedOutputs: {0} RegProcedure[1255,16861,0]public.foo Template.specialize(): precomputed id : 39d9314d inputsDescriptor : [RegType.Unresolved[1247,2277,0]pg_catalog.anyarray] inputsAreSpread : false stableInputs : {} outputsDescriptor: [RegType.Unresolved[1247,2277,0]pg_catalog.anyarray] So the polymorphic type resolution applied at specialize() time "succeeded", raised no error, and left the pseudotype anyarray in both the inputs and outputs supposedly-"resolved" results. That seems to make for a tidy way of recognizing the situation. If you go to specialize and find anyarray among your supposedly-resolved call-site types, well, you know you're in Wonderland and can adjust behavior accordingly. Regards, -Chap