Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g8uYf-0001iT-LG for pgsql-general@arkaria.postgresql.org; Sat, 06 Oct 2018 21:56:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g8uYe-00030j-0o for pgsql-general@arkaria.postgresql.org; Sat, 06 Oct 2018 21:56:20 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g8uYd-00030b-Nt for pgsql-general@lists.postgresql.org; Sat, 06 Oct 2018 21:56:19 +0000 Received: from ec2-34-255-138-49.eu-west-1.compute.amazonaws.com ([34.255.138.49] helo=zimbabwe.chezphil.org) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1g8uYa-0005WU-Tu for pgsql-general@lists.postgresql.org; Sat, 06 Oct 2018 21:56:19 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=chezphil.org mappendix.com; s=20171026; h=Content-Type:MIME-Version: Message-ID:Subject:Date:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=hKx+qlZI+om9YGVdN84fC0dL+G6NQiYnIaiyvdJKL+M=; b=KxI0T5O9bGIn1WL/eGONlWoGe mEPfGphtciu4Jlkc7BrvZk591YJAu46GcU34rOhuvqCrVIzR+xt+XKO8rzvohKRWTGuPy9xwf83ES uiPyoQAExbacebAi+0F4H6kcLkQLQgb9+AaqAlshH0WcFmFEzNsoIDZA0vcxM7uIvuIhw=; Received: from localhost ([127.0.0.1] helo=chezphil.org) by zimbabwe.chezphil.org with esmtp (Exim 4.89) (envelope-from ) id 1g8uYZ-0008P6-GD for pgsql-general@lists.postgresql.org; Sat, 06 Oct 2018 22:56:15 +0100 Received: from localhost ([::ffff:127.0.0.1]) by zimbabwe with smtp (dmsmtpd 0.00001); Sat, 06 Oct 2018 22:56:15 +0100 From: "Phil Endecott" To: Date: Sat, 06 Oct 2018 22:56:15 +0100 Subject: Text-indexing UTF-8 bytea, convert_from() immutability, null bytes... Message-ID: <1538862975270@dmwebmail.dmwebmail.chezphil.org> X-Mailer: Decimail Webmail 3alpha16 MIME-Version: 1.0 Content-Type: text/plain; format="fixed"; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Dear Experts, I have a table that contains bytea data which sometimes is UTF-8 text. When it is - and that is indicated by another column - I want to text-search index it. Something like this: db=> create index ix on tbl using gin (to_tsvector('english',body)) where is_utf8_text; (Note my client encoding is UTF-8.) That doesn't work because to_tsvector doesn't take bytea. So I tried to_tsvector('english',body::text) ; that almost works, but CRLFs in the data become \015\012 (i.e. 6 characters) in the text and the tsvector contains things like '12hello'. Next I tried to_tsvector('english',convert_from(body::text,'UTF-8')). That doesn't work because convert_from is not immutable. (This is 9.6; maybe that has changed.) Is there a good reason for that? Maybe because I might change the client encoding? As a hack I tried ALTER FUNCTION to make it immutable, and now I get: ERROR: invalid byte sequence for encoding "UTF8": 0x00 Hmm, as far as I'm aware 0x00 is fine in UTF-8; what's that mean? But actually I'd be more than happy to ignore invalid UTF-8 here, since I'm only using it for text search; there may be some truly invalid UTF-8 in the data. Is there a "permissive" mode for charset conversion? (That error also suggests that the convert_from is not optimising the conversion from UTF-8 to UTF-8 to a no-op.) Anyway: given the problem of creating a text search index over bytea data that contains UTF-8 text, which may include oddities like null bytes, what would you do? Thanks for any suggestions! Phil.