pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Nils Bergmann <nilsbergmann@noim.io>
To: pgsql-general@postgresql.org
Subject: Format generation_expression
Date: Sat, 25 Jul 2020 15:48:54 +0200
Message-ID: <03e76a96-bf36-4773-8272-cc29bd786386@Spark> (raw)
References: <b668c0b4-c913-4d2d-bee6-5d5f4c9a0547@Spark>

Hi,

Currently, I have a problem. I am working on a project with generated columns. I try to implement the generated column feature in the typeorm project. The thing is, I don't want to drop and add a generated column every time the application starts. Therefore I need a way to check whether a generated column expression has changed.

With select generation_expression from information_schema."columns" c where c.generation_expression is not null; I can retrieve the saved generation expression. However, It seems like Postgres optimizes or changes it. Therefore, it is different.

Example:

I add a column like this:

alter table company add tsv tsvector generated always as (setweight(to_tsvector('german', coalesce(name, '')), 'A') || setweight(jsonb_to_tsvector('german', keywords::jsonb, '["string"]'),'A')) stored;

Now if I try to retrieve the expression with select c.generation_expression from information_schema."columns" c where c.column_name = 'tsv' and c.table_name = 'company'; it is different.

Input expression:
setweight(to_tsvector('german', coalesce(title, '')), 'A') || setweight(to_tsvector('german', coalesce(description, '')), 'B');
What I got from postgres:
(setweight(to_tsvector('german'::regconfig, (COALESCE(name, ''::character varying))::text), 'A'::"char") || setweight(jsonb_to_tsvector('german'::regconfig, keywords, '["string"]'::jsonb), 'A'::"char"))

Postgres formats the query in some way. Is there any way to do this manually?
Like:
magic_function_to_format_sql(`setweight(to_tsvector('german', coalesce(title, '')), 'A') || setweight(to_tsvector('german', coalesce(description, '')), 'B')`) Returns: (setweight(to_tsvector('german'::regconfig, (COALESCE(name, ''::character varying))::text), 'A'::"char") || setweight(jsonb_to_tsvector('german'::regconfig, keywords, '["string"]'::jsonb), 'A'::"char"))

Then I could check if the saved expression is equal to the formatted one. I tried to find information in the documentation. But I had no luck.

Best regards,

Nils Bergmann

view thread (2+ messages)  latest in thread

Message-ID: <03e76a96-bf36-4773-8272-cc29bd786386@Spark>
Permalink:  ../03e76a96-bf36-4773-8272-cc29bd786386@Spark/
Also on:    postgresql.org/message-id/03e76a96-bf36-4773-8272-cc29bd786386@Spark

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: nilsbergmann@noim.io
  Subject: Re: Format generation_expression
  In-Reply-To: <03e76a96-bf36-4773-8272-cc29bd786386@Spark>
  References: <b668c0b4-c913-4d2d-bee6-5d5f4c9a0547@Spark> <03e76a96-bf36-4773-8272-cc29bd786386@Spark>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox