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 1sjn3Y-003zyi-Cw for pgsql-hackers@arkaria.postgresql.org; Thu, 29 Aug 2024 21:51:53 +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 1sjn2X-008GOt-IJ for pgsql-hackers@arkaria.postgresql.org; Thu, 29 Aug 2024 21:50:50 +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 1sjn2W-008GOl-TA for pgsql-hackers@lists.postgresql.org; Thu, 29 Aug 2024 21:50:49 +0000 Received: from a4i45.smtp2go.com ([158.120.80.45]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sjn2S-00298L-6o for pgsql-hackers@lists.postgresql.org; Thu, 29 Aug 2024 21:50:48 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smtpcorp.com; i=@smtpcorp.com; q=dns/txt; s=a1-4; t=1724968242; h=feedback-id : x-smtpcorp-track : date : message-id : to : subject : from : reply-to : sender : list-unsubscribe : list-unsubscribe-post; bh=vcFnSunGKX+FqPFIE8ktugsZ6LtI2+x46028ZBiDfsY=; b=UbpCn8xH3n20Ql37lmHDRQ6s7+rcDwdj17SrIoHm32o5d7FE33jp5IXqHTjiK5clGBib9 ke4R/pDS/rrkTP6q8B7CUeZFlUx+ZXg+72mgpSh+yimHLYZfpAfDMyKtlv3WmLYwH5DwAEv OJI+yiWhzkPK3wLZ5rMV3RotI+3RbbvQyDn2XjFynlACYSokjJh8Wnt33jG2WnlFS1ul5HR WOzxxfb/V20fm4iLSFtiwDX50KFpnibc5Qpbme0YAJr9qQN2+8QeA8bTN8vJDDqMYcTpeOr ldL4hx4qZRT7XgvFwNtpfDo8yG5/IrueZJPXFWnIa7LPhvmQBvk1a2VvWnfA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intellasoft.net; i=@intellasoft.net; q=dns/txt; s=s567718; t=1724968242; h=from : subject : to : message-id : date; bh=vcFnSunGKX+FqPFIE8ktugsZ6LtI2+x46028ZBiDfsY=; b=e1bVgnndZJLu1drt/DHPf+uzaEpTG78DFn4gJs1065APFIcFxuWglU7C1zf01vvH43/lr Se15g8ydYGxi1e1vte05kb2pNq82VDTtLVsP9A3xRwghLS/rye+zVv1fYCtYKfUrzpdymMT cpy3s7Xw0CtoBqW3ctLqi8hjOsensAIvmBm4Sv3UlgdWYNg5Fc/hEAApW3rzWeygaadiKJT CwI00DWIoPKTiuDuLH/ffLXReUqRDfPdqutk16beY9QAKLv3iytxBybLw32XN1u4EtlKGoi dKkxYhbqLJzt7oKKbsVPZrJKIhAFjkzAUWn17RPlmuVZUvZvkHl+3C0TUWqA== Received: from [10.237.239.148] (helo=mail.bluekaba.com) by smtpcorp.com with esmtpsa (TLS1.3:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.97.1-S2G) (envelope-from ) id 1sjn2N-FnQW0hPnatM-TDvv for pgsql-hackers@lists.postgresql.org; Thu, 29 Aug 2024 21:50:39 +0000 Received: from office.intellasoft.net ([50.75.184.42] helo=[192.168.5.80]) by mail.bluekaba.com with esmtpsa (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1sjn2K-0008KW-9t for pgsql-hackers@lists.postgresql.org; Thu, 29 Aug 2024 17:50:36 -0400 Message-ID: Date: Thu, 29 Aug 2024 17:50:35 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: pl/pgperl Patch for adding $_FN detail just like triggers have for $_TD To: pgsql-hackers@lists.postgresql.org References: <5d3f6ead-fb58-44bd-9ef4-2283af66a664@intellasoft.net> <8527bb5f-7507-4b44-badf-4ba53e63f531@intellasoft.net> <1b0bef66-f5ae-434e-a9e1-201c5fbab4ce@dunslane.net> <102c0925-7ba5-4aec-9160-945d4b5ee4bf@intellasoft.net> From: Mark Murawski Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Report-Abuse: Please forward a copy of this message, including all headers, to Feedback-ID: 567718m:567718aNKEb_J:567718sDc6AKoUBE X-smtpcorp-track: 8EqmAnqsUpKU.83I_MFisRbTP.WILC_AwbLES List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 8/29/24 16:54, Andrew Dunstan wrote: > > On 2024-08-29 Th 1:01 PM, Mark Murawski wrote: >> >> >> On 8/29/24 11:56, Andrew Dunstan wrote: >>> >>> On 2024-08-28 We 5:53 PM, Mark Murawski wrote: >>>> Hi Hackers! >>>> >>>> This would be version v1 of this feature >>>> >>>> Basically, the subject says it all: pl/pgperl Patch for being able >>>> to tell which function you're in. >>>> This is a hashref so it will be possible to populate new and >>>> exciting other details in the future as the need arises >>>> >>>> This also greatly improves logging capabilities for things like >>>> catching warnings,  Because as it stands right now, there's no >>>> information that can assist with locating the source of a warning >>>> like this: >>>> >>>> # tail -f /var/log/postgresql.log >>>> ******* GOT A WARNING - Use of uninitialized value $prefix in >>>> concatenation (.) or string at (eval 531) line 48. >>>> >>>> Now, with $_FN you can do this: >>>> >>>> >>>> CREATE OR REPLACE FUNCTION throw_warning() RETURNS text LANGUAGE >>>> plperlu AS $function$ >>>> >>>> use warnings; >>>> use strict; >>>> use Data::Dumper; >>>> >>>> $SIG{__WARN__} = sub { >>>>   elog(NOTICE, Dumper($_FN)); >>>> >>>>   print STDERR "In Function: $_FN->{name}: $_[0]\n"; >>>> }; >>>> >>>> my $a; >>>> print "$a"; # uninit! >>>> >>>> return undef; >>>> >>>> $function$ >>>> ; >>>> >>>> This patch is against 12 which is still our production branch. This >>>> could easily be also patched against newer releases as well. >>>> >>>> I've been using this code in production now for about 3 years, it's >>>> greatly helped track down issues.  And there shouldn't be anything >>>> platform-specific here, it's all regular perl API >>>> >>>> I'm not sure about adding testing.  This is my first postgres >>>> patch, so any guidance on adding regression testing would be >>>> appreciated. >>>> >>>> The rationale for this has come from the need to know the source >>>> function name, and we've typically resorted to things like this in >>>> the past: >>>> >>>> CREATE OR REPLACE FUNCTION throw_warning() RETURNS text LANGUAGE >>>> plperlu AS $function$ >>>> my $function_name = 'throw_warning'; >>>> $SIG{__WARN__} = sub { print STDERR "In Function: $function_name: >>>> $_[0]\n"; } >>>> $function$ >>>> ; >>>> >>>> We've literally had to copy/paste this all over and it's something >>>> that postgres should just 'give you' since it knows the name >>>> already, just like when triggers pass you $_TD with all the >>>> pertinent information >>>> >>>> A wishlist item would be for postgres plperl to automatically >>>> prepend the function name and schema when throwing perl warnings so >>>> you don't have to do your own __WARN__ handler, but this is the >>>> next best thing. >>> >>> >>> >>> I'm not necessarily opposed to this, but the analogy to $_TD isn't >>> really apt.  You can't know the trigger data at compile time, >>> whereas you can know the function's name at compile time, using just >>> the mechanism you find irksome. >>> >>> And if we're going to do it for plperl, shouldn't we do it for other >>> PLs? >>> >>> >>> >> >> Hi Andrew, >> >> >> Thanks for the feedback. >> >> >> 1) Why is this not similar to _TD?  It literally operates >> identically. At run-time it passes you $_TD  for triggers. Same her >> for functions.  This is all run-time.   What exactly is the issue >> you're trying to point out? > > > It's not the same as the trigger data case because the function name > is knowable at compile time, as in fact you have demonstrated. You > just find it a bit inconvenient to have to code for that knowledge. By > contrast, trigger data is ONLY knowable at run time. > > But I don't see that it is such a heavy burden to have to write > >   my $funcname = "foo"; > > or similar in your function. > > > cheers > > > andrew > > -- > Andrew Dunstan > EDB: https://www.enterprisedb.com > > > Understood, regarding knowability.  Trigger data is definitely going to be very dynamic in that regard. No, It's not a heavy burden to hard code the function name.  But what my ideal goal would be is this: CREATE OR REPLACE FUNCTION throw_warning() RETURNS text LANGUAGE plperlu AS $function$ use 'PostgresWarnHandler'; # <--- imagine this registers a WARN handler and outputs $_FN->{name} for you as part of the final warning my $a; print $a; .... etc and then there's nothing 'hard coded' regarding the name of the function, anywhere.  It just seems nonsensical that postgres plperl can't send you the name of your registered function and there's absolutely no way to get it other than hard coding the name (redundantly, considering you're already know the name when you're defining the function in the first place) But even better would be catching the warn at the plperl level, prepending the function name to the warn message, and then you only need: CREATE OR REPLACE FUNCTION throw_warning() RETURNS text LANGUAGE plperlu AS $function$ my $a; print $a; .... etc And then in this hypothetical situation -- magic ensues, and you're left with this: # tail -f /var/log/postgresql.log ******* GOT A WARNING - Use of uninitialized value $a in concatenation (.) or string in function public.throw_warning() line 1