pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedFormat generation_expression
2+ messages / 2 participants
[nested] [flat]
* Format generation_expression
@ 2020-07-25 13:48 Nils Bergmann <nilsbergmann@noim.io>
2020-07-27 20:03 ` Re: Format generation_expression Michael Lewis <mlewis@entrata.com>
0 siblings, 1 reply; 2+ messages in thread
From: Nils Bergmann @ 2020-07-25 13:48 UTC (permalink / raw)
To: pgsql-general
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
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Format generation_expression
2020-07-25 13:48 Format generation_expression Nils Bergmann <nilsbergmann@noim.io>
@ 2020-07-27 20:03 ` Michael Lewis <mlewis@entrata.com>
0 siblings, 0 replies; 2+ messages in thread
From: Michael Lewis @ 2020-07-27 20:03 UTC (permalink / raw)
To: Nils Bergmann <nilsbergmann@noim.io>; +Cc: pgsql-general
Are you caching the definition some other place in the application stack
and checking it later to compare? If so, I would likely alter the table and
in the same transaction read the definition as recorded in
generation_expression from information_schema.columns. Seems simple enough,
but not sure of your overall process.
>
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2020-07-27 20:03 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-07-25 13:48 Format generation_expression Nils Bergmann <nilsbergmann@noim.io>
2020-07-27 20:03 ` Michael Lewis <mlewis@entrata.com>
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