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 1rUrFb-00DPLq-Mp for pgsql-hackers@arkaria.postgresql.org; Tue, 30 Jan 2024 16:46:20 +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 1rUrFa-007qTy-RC for pgsql-hackers@arkaria.postgresql.org; Tue, 30 Jan 2024 16:46:18 +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 1rUrFZ-007qTq-6Z for pgsql-hackers@lists.postgresql.org; Tue, 30 Jan 2024 16:46:18 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rUrFW-004BEo-8l for pgsql-hackers@lists.postgresql.org; Tue, 30 Jan 2024 16:46:15 +0000 Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailout.nyi.internal (Postfix) with ESMTP id 686A75C00AF; Tue, 30 Jan 2024 11:46:13 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute7.internal (MEProxy); Tue, 30 Jan 2024 11:46:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ilmari.org; h=cc :cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm2; t=1706633173; x=1706719573; bh=AKXYnAKDdLjcPnyaPehTrfhkHi+qZsvEmE3iDS6MJjc=; b= U1zTsxX1ATvdEDR6f2xW7tW/SE515/mAt+GmH8CSP//3cMBLopH3VSW+uKmOq2S0 2Afeh3xKe8X0cNPPdMlYJLJR87Q0T5HlCgaL7wNqZ7YiQbHOfPaEIfKyW8bLbE5c dztPbirqSw2xJIOGt3PPBRNU6byajOoipqRBf6lt0jMidJu8XxKecOHku8W8r7oa nUL2m2ydZk1GQ+z2uUmd5pXWIUiVqLguhCsundX6bP667kdv2cuSRptO26XO4RgY B+MvZMVB2RQwlwCa0nnHWnmGz3iorXNTHwHSjldKkT6Ki1yJYulkhlBF7/cCIM8T /QVQlLfxU1RsY8iE0yJqmw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1706633173; x= 1706719573; bh=AKXYnAKDdLjcPnyaPehTrfhkHi+qZsvEmE3iDS6MJjc=; b=x 9i8fGcXKx+Vw4PIi0m3SgE3vpflOOfxdvRpZly0SM8rHLvQkOehXxTiytPdfur4a IkD5oJ8psUc5M2282F9NMRXdbdrKXWCZKlzAYtRkyLywRh4+JgQ+AqNS/o/qQ+aP Z8h7liFBt7b1/KqN137xVDLgkw2yFdVTNqUjDBU4Dc8ewgu9JvigUmxSLfz5LUma mkkRRN5AcULbZonQfX8QRHfZlKHbjFCb5L7zQs16dEP+7cGeFEnpCvHmxThjy7qk bA0LDm83dNCWVc+/o4E5MNadv/c+ndXm4jqUz5Pu6YC/VWRe5l6vtyzZqghf5TtE DpMivppfxmRKP5UpF/rlQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrfedtjedgvdeiucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufhffjgfkfgggtgfgsehtkeertddtreejnecuhfhrohhmpeffrghg fhhinhhnucfklhhmrghrihcuofgrnhhnshonkhgvrhcuoehilhhmrghrihesihhlmhgrrh hirdhorhhgqeenucggtffrrghtthgvrhhnpedtjeevueejgeejvdffuedujeethffhhefg tdfhieejffekveekgfdtffejhfdtieenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehilhhmrghrihesihhlmhgrrhhirdhorhhg X-ME-Proxy: Feedback-ID: i1ff147bf:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 30 Jan 2024 11:46:11 -0500 (EST) From: =?utf-8?Q?Dagfinn_Ilmari_Manns=C3=A5ker?= To: Pavel Stehule Cc: vignesh C , Ivan Panchenko , Tom Lane , Peter Eisentraut , pgsql-hackers@lists.postgresql.org, Greg Sabino Mullane Subject: Re: Bytea PL/Perl transform References: <1679178327.555465221@f141.i.mail.ru> <1689349073.74310713@f308.i.mail.ru> <3450081.1689366442@sss.pgh.pa.us> <1689888552.84649942@f381.i.mail.ru> <87ttmultc8.fsf@wibble.ilmari.org> <87r0hylrqq.fsf@wibble.ilmari.org> Date: Tue, 30 Jan 2024 16:46:10 +0000 In-Reply-To: (Pavel Stehule's message of "Tue, 30 Jan 2024 17:24:21 +0100") Message-ID: <87o7d2lqgd.fsf@wibble.ilmari.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Pavel Stehule writes: > út 30. 1. 2024 v 17:18 odesílatel Dagfinn Ilmari Mannsåker < > ilmari@ilmari.org> napsal: > >> Pavel Stehule writes: >> >> > út 30. 1. 2024 v 16:43 odesílatel Dagfinn Ilmari Mannsåker < >> > ilmari@ilmari.org> napsal: >> > >> >> Pavel Stehule writes: >> >> >> >> > I inserted perl reference support - hstore_plperl and json_plperl does >> >> it. >> >> > >> >> > +<->/* Dereference references recursively. */ >> >> > +<->while (SvROK(in)) >> >> > +<-><-->in = SvRV(in); >> >> >> >> That code in hstore_plperl and json_plperl is only relevant because they >> >> deal with non-scalar values (hashes for hstore, and also arrays for >> >> json) which must be passed as references. The recursive nature of the >> >> dereferencing is questionable, and masked the bug fixed by commit >> >> 1731e3741cbbf8e0b4481665d7d523bc55117f63. >> >> >> >> bytea_plperl only deals with scalars (specifically strings), so should >> >> not concern itself with references. In fact, this code breaks returning >> >> objects with overloaded stringification, for example: >> >> >> >> CREATE FUNCTION plperlu_overload() RETURNS bytea LANGUAGE plperlu >> >> TRANSFORM FOR TYPE bytea >> >> AS $$ >> >> package StringOverload { use overload '""' => sub { "stuff" }; } >> >> return bless {}, "StringOverload"; >> >> $$; >> >> >> >> This makes the server crash with an assertion failure from Perl because >> >> SvPVbyte() was passed a non-scalar value: >> >> >> >> postgres: ilmari regression_bytea_plperl [local] SELECT: sv.c:2865: >> >> Perl_sv_2pv_flags: >> >> Assertion `SvTYPE(sv) != SVt_PVAV && SvTYPE(sv) != SVt_PVHV && >> SvTYPE(sv) >> >> != SVt_PVFM' failed. >> >> >> >> If I remove the dereferincing loop it succeeds: >> >> >> >> SELECT encode(plperlu_overload(), 'escape') AS string; >> >> string >> >> -------- >> >> stuff >> >> (1 row) >> >> >> >> Attached is a v2 patch which removes the dereferencing and includes the >> >> above example as a test. >> >> >> > >> > But without dereference it returns bad value. >> >> Where exactly does it return a bad value? The existing tests pass, and >> the one I included shows that it does the right thing in that case too. >> If you pass it an unblessed reference it returns the stringified version >> of that, as expected. >> > > ugly test code > > (2024-01-30 13:44:28) postgres=# CREATE or replace FUNCTION > perl_inverse_bytes(bytea) RETURNS bytea > TRANSFORM FOR TYPE bytea > AS $$ my $bytes = pack 'H*', '0123'; my $ref = \$bytes; You are returning a reference, not a string. > return $ref; > $$ LANGUAGE plperlu; > CREATE FUNCTION > (2024-01-30 13:44:33) postgres=# select perl_inverse_bytes(''), ' '::bytea; > ┌──────────────────────────────────────┬───────┐ > │ perl_inverse_bytes │ bytea │ > ╞══════════════════════════════════════╪═══════╡ > │ \x5343414c41522830783130656134333829 │ \x20 │ > └──────────────────────────────────────┴───────┘ > (1 row) ~=# select encode('\x5343414c41522830783130656134333829', 'escape'); ┌───────────────────┐ │ encode │ ├───────────────────┤ │ SCALAR(0x10ea438) │ └───────────────────┘ This is how Perl stringifies references in the absence of overloading. Return the byte string directly from your function and it will do the right thing: CREATE FUNCTION plperlu_bytes() RETURNS bytea LANGUAGE plperlu TRANSFORM FOR TYPE bytea AS $$ return pack 'H*', '0123'; $$; SELECT plperlu_bytes(); plperlu_bytes --------------- \x0123 (1 row) - ilmari