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 1sk7gQ-007sfZ-Dv for pgsql-hackers@arkaria.postgresql.org; Fri, 30 Aug 2024 19:53:22 +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 1sk7gN-009sZX-ON for pgsql-hackers@arkaria.postgresql.org; Fri, 30 Aug 2024 19:53:20 +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 1sk7dG-009mFi-6Q for pgsql-hackers@lists.postgresql.org; Fri, 30 Aug 2024 19:50:07 +0000 Received: from mail-ot1-x336.google.com ([2607:f8b0:4864:20::336]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sk7dD-002Dys-1W for pgsql-hackers@lists.postgresql.org; Fri, 30 Aug 2024 19:50:05 +0000 Received: by mail-ot1-x336.google.com with SMTP id 46e09a7af769-70f6118f1b5so1397853a34.0 for ; Fri, 30 Aug 2024 12:50:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dunslane-net.20230601.gappssmtp.com; s=20230601; t=1725047401; x=1725652201; darn=lists.postgresql.org; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :from:references:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=5e5oc88s6IwwbeXWEjOi3HGaxt513t/JbvkAKheLywA=; b=HwCCiHBv5UfbLx64IEB78RYhNwRwWfqhs8yCyQaVIoF3PLhatebAkSWa8rqDjT4/hN q1JmMopf95gTLxMGFsv+Vu7xdpO2udjiJ2QS8q394ILhL72iG6dKCTfC58CotCVgSC9H MNPkvOXr7GhGE/sewUU+u8/8Lv3WnQxyTNjSudeqhLOAiptiRlWKqFwT074njBNEe/+v sKEWVBbdERZWb7C1iaCoOmLrIbVqXin9ZEKdcnCEeswrwJtwFEWfUyY+m5TOE496IZgg DUii9UxmE0XiKgsWZxYR8pwIoO8EmyrxDCzWcFP5N5R8tm2m5+JExn3JHySUT1jKWxHw eHpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725047401; x=1725652201; h=content-transfer-encoding:in-reply-to:autocrypt:content-language :from:references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5e5oc88s6IwwbeXWEjOi3HGaxt513t/JbvkAKheLywA=; b=vJx6RZ3u62e073L6daaDDz+/KuJaglwiwNJL+B/c1Vx/qqb4mB3t225SLzY3U1uiWg jr/1CVoPF9G313uZn8wlvN5sZ1ujPpWe05wEZro2rFoxMp4AEETTNtTxiHau1/t9nSLW aZP+q2ypc7O1cTzYgCxpWyGf0AfNht4bq9afpNmsnO+/GuO6u8HZmf0oh1L78eFXGN1i 8voYVz5tqhuPGT2V+BCaeXv1mSHKWNdMo0UjbPh9n5kNp8ZBiwwftRnBGpAoOsERPucx XmtanHWHpxVsAxd7n4j7YUZEbZu2EBT+Vbgwvs9M8n7xGr+rixz8yA10V08i2Czb2xjW sg6w== X-Forwarded-Encrypted: i=1; AJvYcCV/uqZeYXweTJPb364p+1APXDADtY18lIeg2ZUEh5s/4ZDAOmWe4qI+QpCqUAUpcM/SnuTCOKiZzPnqc0LU@lists.postgresql.org X-Gm-Message-State: AOJu0YyxXe8aPq2UYrphbe2waoVjJQtHLhXGDiaEppNPa4MEnXM2Jvqe dfiFILhxuqSMH9HGkM3HLBXO78mul6uUMfh1+uhQleUgZ3H8w09ZcAVLUm76nonLzzJ8zjBkian f X-Google-Smtp-Source: AGHT+IF+ArHC4NltmqOFGuCSunYGayiCJdnKCEV0pCJusoP1bR/zt6zrG5jseyqQDY0Rs3XN22HsLQ== X-Received: by 2002:a05:6830:7105:b0:709:4793:8757 with SMTP id 46e09a7af769-70f5c343bd1mr9601819a34.2.1725047400954; Fri, 30 Aug 2024 12:50:00 -0700 (PDT) Received: from ?IPV6:2605:a601:9180:9800::2bb? ([2605:a601:9180:9800::2bb]) by smtp.googlemail.com with ESMTPSA id d75a77b69052e-45682d66c9dsm16840141cf.76.2024.08.30.12.49.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 30 Aug 2024 12:50:00 -0700 (PDT) Message-ID: Date: Fri, 30 Aug 2024 15:49:57 -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: Mark Murawski , 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: Andrew Dunstan Content-Language: en-US Autocrypt: addr=andrew@dunslane.net; keydata= xsBNBE7KWFkBCAClridxur2AIc7eW2AR7izbfp3EnNefie2HbLF0izW5Ik5UjX2HBXBx4syI gY6b0ugohXrr274+baoAlvSbq6cAoQuEVrk5IZFzt20b1Xkx65FwGSEj526yiKLocqkJceSq Xr9xcA5SGY+FZv441chh5SU92v4q6z+6LPpoHOh97ptAVXZYNTtU0LevyvD5lja0TzbvJm6C eFXitJfnm1pLEr0DGJCR/iUOl/N62Kh4855zZC7NHIjQHPOvV5Stz/l5ilDhvGVk+xkXFPys SjZoUr1rXhYLpiyi5sR0X9FHXT0KnGuz1F5ERO7ZTLSSQ6fJwPj6gOk9K+vvoKvoeql5ABEB AAHNJEFuZHJldyBEdW5zdGFuIDxhbmRyZXdAZHVuc2xhbmU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mTpoLC11HLoPae6YN9nBNQRZDcEFEu54OaVOqlIdbA6m+POIBCXZdHOFc0 WoDTgxHRzC1jgQNidyd6tKqcsVJs0dzF0oKTmFFmUAqTdJO12LBuNA1rlqrR3EtpYk8B/wtS 5dIMD7Q8hwQpL+4C6GNpb6ZKnPkLi47pDOLhz2qBrqN+rqUEsT3YnExYpzj5yOBi+FlmV1Hw 49QYe1sn2ZPs In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-08-29 Th 5:50 PM, Mark Murawski wrote: > > > 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 > > > > > > > -- Andrew Dunstan EDB: https://www.enterprisedb.com