Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jRyMi-0000Qi-V9 for pgsql-general@arkaria.postgresql.org; Fri, 24 Apr 2020 13:27:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jRyMf-000546-6y for pgsql-general@arkaria.postgresql.org; Fri, 24 Apr 2020 13:27:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jRyMe-000521-RR for pgsql-general@lists.postgresql.org; Fri, 24 Apr 2020 13:27:32 +0000 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jRyMX-0000gA-Mn for pgsql-general@lists.postgresql.org; Fri, 24 Apr 2020 13:27:32 +0000 Received: by mail-wr1-x441.google.com with SMTP id s10so10830399wrr.0 for ; Fri, 24 Apr 2020 06:27:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=h5AblGDSAitEX84lGjDGp2DitVHwI0rNhh/2kUeXRIs=; b=AjiQJgsudQMSM7D/rjLu1b02uCV/XILJdK2ivFBj9ih3g7h7vGDOum8IYxJW58ZZig Fi0AP7SFE5Z3JBdslD3EC7sWD0D+o8uEOmcKUjlXAgvHUUdppJbtvIiui0UtoxZNxsLC CSGeu5YbSAPxfUmiqYjdmhNy0GWCXISZWncZgnU6lc151eCqPm0F21/9RDwNS01VMi6z WXvRx6mu6TQ+ckO6vnVYv71O7STxx5nJ1CTpUWNYOZM0ZHxKdezhgV6D0XVt5PVZQu22 ybQiqxJQAXY8b2yBf97VO41LkvBR9qTobvDg7SjaSmG3YicHB3YqaIXJcD404E49HsNP nL7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=h5AblGDSAitEX84lGjDGp2DitVHwI0rNhh/2kUeXRIs=; b=T6v0UJjSQvZmOtOxD2wtfhZuGSEgzpIEowKz3SlDVhSZfo9vBw/hV2ozRsAxIIra49 3m5uRVlZQ3uwYPUTf4jFb8ycGOUlSykbF9AzLpCBjt4pJ1DYGsMnEi8Bt6hqNfyyIS7n OVeLahJtEEHNhY2rGEjdWPg3594o/smpvJrWmE6eqnWPjIAjKIgk/DMMig81An2EcciR F070tbLcRtmk4UEcLq1ZyHMq+m4+9JH9UKO8yTaAEKw8aFnJ52bv6Yz3INjPz5uJgrgl DHWcQf3Z0gnxyR5mlUKYplmIzclpXF6vCGf770WQedsfL3sAjxCfOag/Z8hS0HmIeL8J D/ZA== X-Gm-Message-State: AGi0PuZ43vt7HeHJybfklT4ULpsMvg4vbP/8nSAeIQxubiq+rBMe3wNs alviEePCY781xkwOExcLk+XrmA== X-Google-Smtp-Source: APiQypLe7qu+V61SXZS2fThdV9AgPOMiAS4UnojPEKU8NuxZhLdpoSkCAW3fmXwsbR3gk2syVZSdhw== X-Received: by 2002:adf:d4d0:: with SMTP id w16mr11204167wrk.264.1587734844483; Fri, 24 Apr 2020 06:27:24 -0700 (PDT) Received: from dynamic-emn4hxml2cppifwvs-pd01.res.v6.highway.a1.net (dynamic-emn4hxml2cppifwvs-pd01.res.v6.highway.a1.net. [2001:871:60:4e5a:64f2:c8d:7b1c:3a38]) by smtp.gmail.com with ESMTPSA id 91sm8661251wra.37.2020.04.24.06.27.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Apr 2020 06:27:23 -0700 (PDT) Message-ID: <24e2c22c9f1f1bc38c5a88d3e0c8fef5b980cd9a.camel@cybertec.at> Subject: Re: Weird behaviour of C extension function From: Laurenz Albe To: Amaury Bouchard , pgsql-general@lists.postgresql.org Date: Fri, 24 Apr 2020 15:27:12 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.5 (3.32.5-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, 2020-04-24 at 14:53 +0200, Amaury Bouchard wrote: > I have a really strange behaviour with a C function, wich gets a text as parameter. > Everything works fine when I call the function directly, giving a text string as parameter. But a problem occurs when I try to read data from a table. > > To illustrate the problem, I stripped the function down to the minimum. The source code is below, but first, here is the behaviour : > > Direct call > ----------- > > select passthru('hello world!'), passthru('utf8 çhàràtérs'), passthru(' h3110 123 456 '); > INFO: INPUT STRING: 'hello world!' (12) > INFO: INPUT STRING: 'utf8 çhàràtérs' (18) > INFO: INPUT STRING: ' h3110 123 456 ' (15) > > (as you can see, the log messages show the correct input, with the number of bytes between parentheses) > > Reading a table data > -------------------- > > create table mytable ( str text); > > insert into mytable (str) values ('hello world!'), ('utf8 çhàràtérs'), (' h3110 123 456 '); > > select passthru(str) from mytable; > INFO: INPUT STRING: 'lo world!' (12) > INFO: INPUT STRING: '8 çhàràtérs' (18) > INFO: INPUT STRING: '110 123 456 � > ' (15) > INFO: INPUT STRING: '��' (5) > INFO: INPUT STRING: '' (3) > > There, you can see that the pointer seems to be shifted 3 bytes farther. > > Do you have any clue for this strange behaviour? > > > The source code > --------------- > > #include "postgres.h" > #include "fmgr.h" > #include "funcapi.h" > > // PG module init > #ifdef PG_MODULE_MAGIC > PG_MODULE_MAGIC; > #endif > void _PG_init(void); > Datum passthru(PG_FUNCTION_ARGS); > PG_FUNCTION_INFO_V1(passthru); > > void _PG_init() { > } > > Datum passthru(PG_FUNCTION_ARGS) { > // get the input string > text *input = PG_GETARG_TEXT_PP(0); > char *input_pt = (char*)VARDATA(input); > int32 input_len = VARSIZE_ANY_EXHDR(input); > // create a null terminated copy of the input string > char *str_copy = calloc(1, input_len + 1); > memcpy(str_copy, input_pt, input_len); > // log message > elog(INFO, "INPUT STRING: '%s' (%d)", str_copy, input_len); > free(str_copy); > PG_RETURN_NULL(); > } You find this in "postgres.h": * In consumers oblivious to data alignment, call PG_DETOAST_DATUM_PACKED(), * VARDATA_ANY(), VARSIZE_ANY() and VARSIZE_ANY_EXHDR(). Elsewhere, call * PG_DETOAST_DATUM(), VARDATA() and VARSIZE(). Directly fetching an int16, * int32 or wider field in the struct representing the datum layout requires * aligned data. memcpy() is alignment-oblivious, as are most operations on * datatypes, such as text, whose layout struct contains only char fields. So you should use VARDATA_ANY. What happens is that these short text columns have a 1-byte TOAST header, but you ship the first 4 bytes unconditionally, assuming they were detoasted. Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com