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 1nN1GY-0001nD-TM for pgsql-general@arkaria.postgresql.org; Wed, 23 Feb 2022 23:41:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nN1G2-00079n-2e for pgsql-general@arkaria.postgresql.org; Wed, 23 Feb 2022 23:41:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nN1G1-00076b-Bf for pgsql-general@lists.postgresql.org; Wed, 23 Feb 2022 23:41:17 +0000 Received: from mail-1.server.selfnet.de ([2001:7c7:2100:400::37]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nN1Ft-0001vG-FM for pgsql-general@lists.postgresql.org; Wed, 23 Feb 2022 23:41:16 +0000 Received: from [127.0.0.1] (localhost [127.0.0.1]) by localhost (Mailerdaemon) with ESMTPSA id C861F4256D for ; Thu, 24 Feb 2022 00:41:03 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=selfnet.de; s=selfnet; t=1645659664; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=giY404n0PGfKw6ssV9HEEdm1gIYEp70WsTbcxSWwSOs=; b=jNoS4Q52Eb+1QmXsf3W6l2jID0nroVLN3IzXuUSSoVynsDETU8dCTsYNOC/YWSK2K8caDp VGqrfBqTTpKFKA4cTFZowWKrSsZ6Z8mRx6gZ37wDdoEXT4vyVSiDUZdxJNWEoNG9ThG/pX Lo1UEJHMVMf/Oxxa+K6AEhZFC2RfVHhQ2SCuAzNGFbS+f00vFdRh0ekY5PhI1n0pIwKMb7 hJTXYo1Qeh9uNbpm32IUMKtgIrK81vprHRishBFc9ql4eZ6VHQ3PWr0zvL3Z0/G9hCSDr/ bYq5idmjIaGG6OwrMJD4QIrrOM61RR2ghT2rqx30nZuWCEVY+DPsrVbSM2K1wQ== Message-ID: <54593f15-6b3b-086e-ce1e-69f0af9d38a9@selfnet.de> Date: Thu, 24 Feb 2022 00:41:01 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.6.1 Subject: Re: tsvector string representation and parsing Content-Language: en-US To: pgsql-general@lists.postgresql.org References: <9e1f312a-92cf-e002-9bfd-728aa450ee09@selfnet.de> <1259438.1645655411@sss.pgh.pa.us> From: =?UTF-8?Q?Johannes_Gra=c3=abn?= In-Reply-To: <1259438.1645655411@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Authentication-Results: mail-1.server.selfnet.de; auth=pass smtp.auth=johannes smtp.mailfrom=johannes@selfnet.de List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Thanks, Tom. On 23/02/2022 23.30, Tom Lane wrote: > =?UTF-8?Q?Johannes_Gra=c3=abn?= writes: >> This is a minimal example that goes wrong (but shouldn't IMHO: > >>> SELECT format('%L:1', '\:')::tsvector > > format(%L) is designed to produce a SQL literal, which does not > have the same requirements as a tsvector element ... yeah, they're > close, but not close enough. In this particular example, > what you get is > > =# SELECT format('%L:1', '\:'); > format > ---------- > E'\\:':1 > (1 row) > > because format() adds an E prefix for the avoidance of doubt about > what to do with the backslashes. tsvector doesn't like that. I see. > I don't think we have any prefab function that does what you're > looking for here, and TBH I'm not sure I see the point of it. > Pretty much any tsvector you'd be dealing with in practice is > going to have come from one of the to_tsvector family of > functions, and those tend to drop punctuation. Applied to normal texts in a standard language, I believe that's true. I'd like to use FTS in a setting where I need to control the positional attributes as I'm specifying more than one lexeme at the same position (which works great btw). That's why I can't use to_tsvector() but need to cast it from a string to tsvector. >> My understanding is that everything inside >> single quotes is taken as a lexeme. From the documentation: > >>> To represent lexemes containing whitespace or punctuation, surround them with quotes > > See also the next bit about having to double quotes and backslashes > within those quotes. So what you'd actually need is > > =# select $$'\\:':1$$::tsvector; > tsvector > ---------- > '\\:':1 > (1 row) > > If you write just one backslash, it has the effect of quoting > the next character, which in this case doesn't need quoting. Before using format(), I tried just generating those strings by doubling any single quote or backslash and enclosing the whole string in single quotes, but that didn't seem a safe way, though it works in principle: > SELECT format($$'%s':%s$$, replace(replace(s, $$'$$, $$''$$), '\', '\\'), i)::tsvector > FROM (SELECT $$\:$$ s, 1 i) x; Would that be the way to go if to_tsvector is not an option? Regards Johannes