{"id":440426,"date":"2024-11-30T21:01:58","date_gmt":"2024-11-30T21:01:58","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=440426"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=440426","title":{"rendered":"<span>\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446 \u0432 Postgresql \u043d\u0430 SQL<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c\u043e\u0439 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438<\/h3>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0432\u0430\u043c \u043f\u0440\u0438\u0448\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043d\u0430 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u0431\u044d\u043a\u0435\u043d\u0434\u0443. \u0412\u044b \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u0435 \u043a \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0443 \u0441 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u043c\u0438 \u0438 \u0432\u0438\u0434\u0438\u0442\u0435, \u043f\u043e\u043c\u0438\u043c\u043e \u043f\u0440\u043e\u0447\u0435\u0433\u043e, \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 (\u0411\u0414). \u041f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442:<\/p>\n<p><strong>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 1. \u0422\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u043a \u0442\u0430\u0431\u043b\u0438\u0446\u0435 price \u0432 \u0441\u0445\u0435\u043c\u0435 public<\/strong><\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>\u0421\u0442\u043e\u043b\u0431\u0435\u0446<\/p>\n<\/th>\n<th>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/th>\n<th>\n<p>\u0422\u0438\u043f<\/p>\n<\/th>\n<th>\n<p>Not null<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">id<\/p>\n<\/td>\n<td>\n<p align=\"left\">PK<\/p>\n<\/td>\n<td>\n<p align=\"left\">int8<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0414\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">price_value<\/p>\n<\/td>\n<td>\n<p align=\"left\">UNIQUE price_unique<\/p>\n<\/td>\n<td>\n<p align=\"left\">numeric(18, 6)<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0414\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">model_id<\/p>\n<\/td>\n<td>\n<p align=\"left\">UNIQUE price_unique, FOREIGN KEY price_models_fk \u0441\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u043c <strong>id<\/strong> \u0442\u0430\u0431\u043b\u0438\u0446\u044b <strong>models<\/strong><\/p>\n<\/td>\n<td>\n<p align=\"left\">int4<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0414\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">comment<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u200b<\/p>\n<\/td>\n<td>\n<p align=\"left\">varchar(255)<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041d\u0435\u0442<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043e\u0434\u0438\u043d \u0438\u0437 \u0442\u0435\u0441\u0442-\u043a\u0435\u0439\u0441\u043e\u0432 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u044d\u0442\u0438\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c. \u041a \u0442\u0430\u043a\u0438\u043c \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u0441\u043e\u0441\u0442\u0430\u0432 \u0438 \u0442\u0438\u043f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0441\u0432\u044f\u0437\u0435\u0439 \u0438 \u0442.\u0434. \u0412 Postgresql \u0435\u0441\u0442\u044c \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u0430\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/2d0\/a2c\/900\/2d0a2c90059b4658f8f00c0bac0822b4.png\" width=\"854\" height=\"349\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2d0\/a2c\/900\/2d0a2c90059b4658f8f00c0bac0822b4.png\"\/><\/figure>\n<p>\u041a \u0441\u043b\u043e\u0432\u0443, \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u043e\u0431\u0437\u043e\u0440\u0430 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043d\u043e \u0438 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0438 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 SQL.<\/p>\n<p>\u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043f\u0430\u0440\u0443 \u0442\u0430\u0431\u043b\u0438\u0446:<\/p>\n<pre><code class=\"sql\">-- drop table public.models  create table public.models (     id int8 generated by default as identity( increment by 1 minvalue 1 maxvalue 9223372036854775807 start 1 cache 1 no cycle) not null,     \"name\" varchar(50) not null,     manufacturer varchar(50) not null,     constraint models_pk primary key (id),     constraint models_unique unique (name, manufacturer) );<\/code><\/pre>\n<pre><code class=\"sql\">-- drop table public.price  create table public.price (     id int8 generated by default as identity( increment by 1 minvalue 1 maxvalue 9223372036854775807 start 1 cache 1 no cycle) not null,     price_value numeric(18,6) not null,     model_id int4 not null,     comment varchar(255) null,     constraint price_pk primary key (id),     constraint price_models_fk foreign key (model_id) references public.models(id),     constraint price_unique unique (price_value, model_id) );<\/code><\/pre>\n<p>\u0421\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u043c\u0441\u044f \u043d\u0430\u00a0<strong>price<\/strong>. \u0422\u0430\u0431\u043b\u0438\u0446\u0430\u00a0<strong>models<\/strong>\u00a0\u043d\u0443\u0436\u043d\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u00a0<strong>price<\/strong>\u00a0\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 (<strong>constraint<\/strong>) \u043f\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u043c\u0443 \u043a\u043b\u044e\u0447\u0443 (<strong>foreign key<\/strong>). SQL-\u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u044d\u0442\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">with     -- \u041d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441: \u0437\u0430\u0434\u0430\u0435\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b     params(table_name, table_schema) as (         values (             'price',             'public'         )     ),     -- \u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0442\u0430\u0431\u043b\u0438\u0446\u044b     required_columns(column_name, data_type, is_nullable, character_maximum_length) as (         values                         ('id',          'bigint',               'NO', null),             ('price_value', 'numeric',              'NO', 18.6),             ('model_id',    'integer',      'NO',null),             ('comment',     'character varying',    'YES',  255)     ),     -- \u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f     required_constraints(column_name, constraint_type) as (         values                         ('id',          'PRIMARY KEY'),             ('id',    'FOREIGN KEY'),             ('model_id',    'FOREIGN KEY'),             ('price_value', 'UNIQUE'),             ('model_id',    'UNIQUE')     ),     -- \u041d\u0430\u0445\u043e\u0434\u0438\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u0445 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 null'\u043e\u0432     columns_info as (         select             column_name, data_type, is_nullable,             coalesce(numeric_precision, 0)          as numeric_precision,             coalesce(numeric_scale, 0)              as numeric_scale,             coalesce(character_maximum_length, 0)   as character_maximum_length         from information_schema.columns         where             table_name       = (select table_name   from params)             and table_schema = (select table_schema from params)     ),     -- \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u043f\u043e\u0434\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u043d\u0435\u0439     check_table_exist as (         select             case when count_all_fields &lt; 1 then false else true end table_exists,             case when count_all_fields &lt; 1 then 1 else 0 end table_exists_error,             count_all_fields         from (             select count (*) as count_all_fields             from columns_info         ) sq     ),     -- \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0439 \u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043d\u0430\u0431\u043e\u0440\u044b \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u044b     fields_comparison as (         select t.*         from columns_info t         inner join required_columns r             on  t.column_name   = r.column_name             and t.data_type     = r.data_type             and t.is_nullable   = r.is_nullable             and (                 -- \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0446\u0435\u043b\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439                 case                     when t.data_type = 'numeric'                     then t.numeric_precision = trunc(r.character_maximum_length::numeric)                 end                 and                 -- \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0434\u0440\u043e\u0431\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439                 case                     when t.data_type = 'numeric'                     then t.numeric_scale = (r.character_maximum_length::numeric - trunc(r.character_maximum_length::numeric))                     * power(10, length(split_part(r.character_maximum_length::text, '.', 2)))                 end                 or t.character_maximum_length = coalesce(r.character_maximum_length::numeric, 0)            )     ),     -- \u0418\u0449\u0435\u043c \u043b\u0438\u0448\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_unexpected_fields as (         select             count (column_name) as count_unexpected_fields,             string_agg(column_name, ', ') as unexpected_fields         from (             select column_name             from columns_info             except             select column_name             from required_columns         ) sq     ),     -- \u0418\u0449\u0435\u043c \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_missing_fields as (         select             count (column_name) as count_missing_fields,             string_agg(column_name, ', ') as missing_fields         from (             select column_name             from required_columns             except             select column_name             from columns_info         ) sq     ),     -- \u0418\u0449\u0435\u043c \u043d\u0435\u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_invalid_fields as (         select             count (column_name) as count_invalid_fields,             string_agg(column_name, ', ') as invalid_fields         from (             select column_name             from required_columns             except             select column_name             from fields_comparison             except             select string_to_table(missing_fields, ', ')             from check_missing_fields         ) sq     ),     -- \u0418\u0449\u0435\u043c \u0432\u0441\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b     constraints_query as(         select             t1.constraint_type,             t2.column_name,             t3.column_name as foreign_column         from information_schema.table_constraints t1         left join information_schema.constraint_column_usage t2             on t1.constraint_name = t2.constraint_name         left join information_schema.key_column_usage as t3             on t1.constraint_name = t3.constraint_name             and t1.table_schema = t3.table_schema         where             t1.table_name            = (select table_name   from params)             and t1.constraint_schema = (select table_schema from params)             and t2.column_name is not null     ),     -- \u0412\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439 (foreign_column) \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 (column_name)     union_foreign_ref_columns as (         select column_name, constraint_type         from constraints_query         union all         select foreign_column as column_name, constraint_type         from constraints_query     ),     -- \u0418\u0449\u0435\u043c \u043b\u0438\u0448\u043d\u0438\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_unexpected_constraints as (         select             count (column_name) as count_unexpected_constraints,             string_agg(column_name || ' (' || constraint_type || ')', ', ') as unexpected_constraints         from (             select *             from union_foreign_ref_columns             except             select column_name, constraint_type             from required_constraints         ) sq     ),     -- \u0418\u0449\u0435\u043c \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_missing_constraints as (         select             count (column_name) as count_missing_constraints,             string_agg(column_name || ' (' || constraint_type || ')', ', ') as missing_constraints         from (             select column_name, constraint_type             from required_constraints             except             select *             from union_foreign_ref_columns         ) sq     ),     -- \u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435     checks as (         select             -- \u0412\u044b\u0432\u043e\u0434\u0438\u043c \u0432\u0441\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0430\u0445 \u0438 \u0441\u0443\u043c\u043c\u0438\u0440\u0443\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e             table_exists_error + count_unexpected_fields + count_missing_fields + count_invalid_fields +             count_unexpected_constraints + count_missing_constraints as errors,             *         from        check_table_exist         cross join  check_unexpected_fields         cross join  check_missing_fields         cross join  check_invalid_fields         cross join  check_unexpected_constraints         cross join  check_missing_constraints ) select * from checks<\/code><\/pre>\n<p>\u0412\u0430\u0436\u043d\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435: \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u0432\u0430\u0448\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043d\u0443\u0436\u043d\u043e \u0432\u043d\u0435\u0441\u0442\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 CTE\u00a0<strong>params<\/strong>\u00a0(\u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0432\u0430\u0448\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u0441\u0445\u0435\u043c\u044b, \u0433\u0434\u0435 \u043e\u043d\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f),\u00a0<strong>required_columns<\/strong>\u00a0(\u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432\u0430\u0448\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b) \u0438\u00a0<strong>required_constraints<\/strong>\u00a0(\u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u0432\u0430\u0448\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b). \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u043e\u0434 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043c\u0435\u043d\u044f\u0442\u044c.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9fa\/859\/64c\/9fa85964cdd4d482d28f4b28fdcff15e.png\" alt=\"img.2024-11-20 21.46.34.png\" title=\"\" width=\"1050\" height=\"848\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9fa\/859\/64c\/9fa85964cdd4d482d28f4b28fdcff15e.png\"\/><\/figure>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u043b\u0438\u0447\u0438\u0435\u043c \u0432\u043e \u0432\u0441\u0435\u0445 \u0411\u0414 Postgresql \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0445\u0435\u043c\u044b (<strong>information_schema<\/strong>). \u0412 Oracle, \u0435\u0441\u043b\u0438 \u043d\u0435 \u043e\u0448\u0438\u0431\u0430\u044e\u0441\u044c, \u0442\u043e\u0436\u0435 \u0442\u0430\u043a\u0430\u044f \u0435\u0441\u0442\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0430\u043a\u0442\u0443\u0430\u043b\u0435\u043d \u0438 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0421\u0423\u0411\u0414 (\u043f\u043e\u043b\u0430\u0433\u0430\u044e, \u0441 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u043a\u0430\u043c\u0438).<\/p>\n<h3>\u0427\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441?<\/h3>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>\u0421\u0442\u043e\u043b\u0431\u0435\u0446<\/p>\n<\/th>\n<th>\n<p>\u0420\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u043a\u0430<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">errors<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0443\u043c\u043c\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u0448\u0438\u0431\u043e\u043a<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">table_exists<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043b\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">table_exists_error<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u0434\u043b\u044f \u0443\u0447\u0435\u0442\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">count_all_fields<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u0432\u0441\u0435\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">count_unexpected_fields<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u043b\u0438\u0448\u043d\u0438\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">unexpected_fields<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043b\u0438\u0448\u043d\u0438\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 \u0437\u0430\u043f\u044f\u0442\u0443\u044e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">count_missing_fields<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">missing_fields<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 \u0437\u0430\u043f\u044f\u0442\u0443\u044e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">count_invalid_fields<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0435\u0441\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u043b\u0438\u0431\u043e \u043e\u0448\u0438\u0431\u043a\u0438<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">invalid_fields<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 \u0437\u0430\u043f\u044f\u0442\u0443\u044e, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0435\u0441\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u043b\u0438\u0431\u043e \u043e\u0448\u0438\u0431\u043a\u0438<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">count_unexpected_constraints<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u043b\u0438\u0448\u043d\u0438\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">unexpected_constraints<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043b\u0438\u0448\u043d\u0438\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c \u0442\u0438\u043f\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0432 \u0441\u043a\u043e\u0431\u043a\u0430\u0445 \u0447\u0435\u0440\u0435\u0437 \u0437\u0430\u043f\u044f\u0442\u0443\u044e<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">count_missing_constraints<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">missing_constraints<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0441 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435\u043c \u0442\u0438\u043f\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0432 \u0441\u043a\u043e\u0431\u043a\u0430\u0445 \u0447\u0435\u0440\u0435\u0437 \u0437\u0430\u043f\u044f\u0442\u0443\u044e<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9df\/6c7\/e48\/9df6c7e48589043f1f4ec30db0a2f030.png\" alt=\"img.2024-11-27 19.40.40.png\" title=\"\" width=\"1616\" height=\"916\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9df\/6c7\/e48\/9df6c7e48589043f1f4ec30db0a2f030.png\"\/><\/figure>\n<h3>\u0410\u043d\u0430\u043b\u0438\u0437 \u0437\u0430\u043f\u0440\u043e\u0441\u0430<\/h3>\n<p>\u0412 CTE\u00a0<strong>params<\/strong>,\u00a0<strong>required_columns<\/strong>\u00a0\u0438\u00a0<strong>required_constraints<\/strong>\u00a0\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435. \u042d\u0442\u043e &#8212; \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0435 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u044b \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u0421\u00a0<strong>params<\/strong>, \u0434\u0443\u043c\u0430\u044e, \u0432\u0441\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u043e &#8212; \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b, \u0433\u0434\u0435 \u043e\u043d\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f:<\/p>\n<pre><code class=\"sql\">with     -- \u041d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441: \u0437\u0430\u0434\u0430\u0435\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b     params(table_name, table_schema) as (         values (             'price',             'smartphones'         )     ) select * from params<\/code><\/pre>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 CTE, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u044f \u043f\u0438\u0448\u0443, \u0431\u0443\u0434\u0443 \u043e\u0444\u043e\u0440\u043c\u043b\u044f\u0442\u044c \u0441 select&#8217;\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u044d\u0442\u043e\u043c\u0443 CTE. \u0412\u0435\u0441\u043e\u043c\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u0442 &#8212; \u043c\u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \ud83d\ude42<br \/>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0443 \u0441\u0435\u0431\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e. \u041e\u043d \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043a\u0430\u0436\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u043d\u0443\u0442\u0440\u0438 CTE:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/27f\/bfb\/798\/27fbfb798af85d39d711373916d58b48.png\" alt=\"img.2024-11-27 20.01.42.png\" title=\"\" width=\"564\" height=\"358\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/27f\/bfb\/798\/27fbfb798af85d39d711373916d58b48.png\"\/><\/figure>\n<p><strong>required_column<\/strong>\u00a0\u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435:<\/p>\n<pre><code class=\"sql\">with     -- \u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0442\u0430\u0431\u043b\u0438\u0446\u044b     required_columns(column_name, data_type, is_nullable, character_maximum_length) as (         values                         ('id',          'bigint',               'NO',   null),             ('price_value', 'numeric',              'NO',   18.6),             ('model_id',    'integer',      'NO',   null),             ('comment',     'character varying',    'YES',  255)     ) select * from required_columns<\/code><\/pre>\n<p>\u0417\u0434\u0435\u0441\u044c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 4-\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u0445:<\/p>\n<ul>\n<li>\n<p><strong>column_name<\/strong>: \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435;<\/p>\n<\/li>\n<li>\n<p><strong>data_type<\/strong>: \u0442\u0438\u043f \u0441\u0442\u043e\u043b\u0431\u0446\u0430. \u0412 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u043e \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 (<strong>character varying)<\/strong>, \u0430 \u043d\u0435 \u0441\u0438\u043d\u043e\u043d\u0438\u043c (<strong>varchar<\/strong>). \u042d\u0442\u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0437\u0434\u0435\u0441\u044c:<br \/><a href=\"https:\/\/www.postgresql.org\/docs\/8.1\/datatype.html\" rel=\"noopener noreferrer nofollow\">https:\/\/www.postgresql.org\/docs\/8.1\/datatype.html<\/a><\/p>\n<\/li>\n<li>\n<p><strong>is_nullable<\/strong>: \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043b\u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u0430 null;<\/p>\n<\/li>\n<li>\n<p><strong>character_maximum_length<\/strong>: \u0434\u043b\u0438\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0438. \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 (\u0446\u0438\u0444\u0440) \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u0442 &#8212; \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f null.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 CTE\u00a0<strong>required_constraints<\/strong>\u00a0\u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0441\u044f \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043f\u043e 2-\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c:<\/p>\n<ul>\n<li>\n<p><strong>column_name<\/strong>: \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u0430, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435;<\/p>\n<\/li>\n<li>\n<p><strong>constraint_type<\/strong>: \u0442\u0438\u043f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f.<\/p>\n<\/li>\n<\/ul>\n<p>\u0422\u0438\u043f\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 (<strong>constraint_type<\/strong>) \u0431\u044b\u0432\u0430\u044e\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435:<\/p>\n<ul>\n<li>\n<p><strong>CHECK<\/strong>: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0442\u044c \u043a\u0430\u043a\u043e\u043c\u0443-\u043b\u0438\u0431\u043e \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u044e;<\/p>\n<\/li>\n<li>\n<p><strong>NOT NULL<\/strong>: \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0433\u043e\u0432\u043e\u0440\u044f\u0449\u0435\u0435 &#8212; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c NULL;<\/p>\n<\/li>\n<li>\n<p><strong>UNIQUE<\/strong>: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435 \u043d\u0435 \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u043b\u0438\u0447\u0438\u0435<\/p>\n<\/li>\n<li>\n<p><strong>PRIMARY KEY<\/strong>: \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 UNIQUE \u0438 NOT NULL;<\/p>\n<\/li>\n<li>\n<p><strong>FOREIGN KEY<\/strong>: \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435 \u0443 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0432\u0437\u044f\u0442\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0442\u0430\u043a\u0438\u043c \u0436\u0435, \u043a\u0430\u043a \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435 \u0434\u0440\u0443\u0433\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b.<\/p>\n<\/li>\n<\/ul>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435\u00a0<strong>NOT NULL<\/strong>\u00a0\u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c \u0432 CTE\u00a0<strong>required_columns<\/strong>. \u041a\u0430\u043a \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0442\u0430\u043c \u0435\u0441\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446\u00a0<strong>is_nullable<\/strong>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0438\u0441\u0430\u043d \u043f\u043e\u0441\u043b\u0435\u00a0<strong>required_constraints<\/strong>. \u0414\u043e \u044d\u0442\u043e\u0433\u043e CTE \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u043a\u0430\u043a \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043b \u0432\u044b\u0448\u0435, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435. \u0412 CTE\u00a0<strong>columns_info<\/strong>\u00a0\u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u0445 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a\u00a0<strong>columns<\/strong>:<\/p>\n<pre><code class=\"sql\">select * from information_schema.columns<\/code><\/pre>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u043c \u0432\u044b\u0431\u043e\u0440\u043a\u0443 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u0442\u0435\u043c \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0443\u0436\u043d\u044b \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a. null&#8217;\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u0443\u0435\u043c \u0432 0, \u0447\u0442\u043e\u0431\u044b \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c. \u0422\u0430\u043a\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e (\u0432\u00a0<strong>where<\/strong>) \u043f\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0438 \u043f\u043e \u0441\u0445\u0435\u043c\u0435, \u0433\u0434\u0435 \u043e\u043d\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f:<\/p>\n<pre><code class=\"sql\">select     column_name, data_type, is_nullable,     coalesce(numeric_precision, 0)          as numeric_precision,     coalesce(numeric_scale, 0)              as numeric_scale,     coalesce(character_maximum_length, 0)   as character_maximum_length from information_schema.columns where     table_name       = 'price'     and table_schema = 'public'<\/code><\/pre>\n<p>\u0412 select&#8217;\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>\u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u0430<\/p>\n<\/th>\n<th>\n<p>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">column_name<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">data_type<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0422\u0438\u043f \u0441\u0442\u043e\u043b\u0431\u0446\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">is_nullable<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u0435\u043d \u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0434\u043b\u044f \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">numeric_precision<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 (<strong>numeric<\/strong>, <strong>integer<\/strong> \u0438 \u0442.\u0434.). \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0446\u0438\u0444\u0440 \u0446\u0435\u043b\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0447\u0438\u0441\u043b\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">numeric_scale<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0434\u043b\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432. \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0446\u0438\u0444\u0440 \u0434\u0440\u043e\u0431\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0447\u0438\u0441\u043b\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">character_maximum_length<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0434\u043b\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0442\u0438\u043f\u0430 <strong>varchar<\/strong> (<strong>character varying<\/strong>)<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u043f\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0438\u043b\u0438 \u0446\u0438\u0444\u0440 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u0442\u0438\u043f\u0430\u00a0<strong>boolean<\/strong>\u00a0\u0438\u043b\u0438\u00a0<strong>jsonb<\/strong>), \u0442\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0435 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u0443\u0434\u0435\u0442 null.<\/p>\n<p>\u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 CTE \u0438 \u043e\u0431\u0440\u0430\u0442\u0438\u043c\u0441\u044f \u043a \u0437\u0430\u043f\u0438\u0441\u044f\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e \u043d\u0435\u043c\u0443 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f, \u0447\u0435\u0440\u0435\u0437\u00a0<strong>select<\/strong>:<\/p>\n<pre><code class=\"sql\">with columns_info as (     select         column_name, data_type, is_nullable,         coalesce(numeric_precision, 0)          as numeric_precision,         coalesce(numeric_scale, 0)              as numeric_scale,         coalesce(character_maximum_length, 0)   as character_maximum_length     from information_schema.columns     where         table_name       = 'price'         and table_schema = 'public' ) select * from columns_info<\/code><\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043e\u0434 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b. \u0421\u0434\u0435\u043b\u0430\u0435\u043c \u044d\u0442\u043e \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u0434\u0441\u0447\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043e\u043f\u0438\u0441\u0430\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 (\u043c\u0435\u043d\u044f\u0435\u043c\u00a0<code>select *<\/code>\u00a0\u043d\u0430\u00a0<code>select count (*)<\/code>\u00a0):<\/p>\n<pre><code class=\"sql\">with columns_info as (     select         column_name, data_type, is_nullable,         coalesce(numeric_precision, 0)          as numeric_precision,         coalesce(numeric_scale, 0)              as numeric_scale,         coalesce(character_maximum_length, 0)   as character_maximum_length     from information_schema.columns     where         table_name       = 'price'         and table_schema = 'public' ) select count (*) from columns_info<\/code><\/pre>\n<p>\u041b\u043e\u0433\u0438\u043a\u0430 \u043f\u0440\u043e\u0441\u0442\u0430: \u043d\u0435\u0442 \u0442\u0430\u0431\u043b\u0438\u0446\u044b &#8212; \u043d\u0435\u0442 \u0438 \u0437\u0430\u043f\u0438\u0441\u0435\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c, \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u043c \u0432 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0445. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u044e\u0434\u0430\u00a0<strong>case<\/strong>\u00a0\u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 boolean&#8217;\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u0443\u0441 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438. \u0410 \u0442\u0430\u043a\u0436\u0435 \u0441\u0447\u0435\u0442\u0447\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c 1, \u0435\u0441\u043b\u0438 boolean &#8212; true \u0438 0, \u0435\u0441\u043b\u0438 boolean &#8212; false. \u041e\u043d \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430 \u0441\u0443\u043c\u043c\u044b \u043e\u0448\u0438\u0431\u043e\u043a. \u0415\u0449\u0435 \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 &#8212; \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f. \u0412\u0441\u0435 \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u043e\u0437\u044c\u043c\u0435\u043c \u0438\u0437 select&#8217;\u0430, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0448\u0435 \u043f\u043e\u0434 CTE. \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0435\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"sql\">with columns_info as (     select         column_name, data_type, is_nullable,         coalesce(numeric_precision, 0)          as numeric_precision,         coalesce(numeric_scale, 0)              as numeric_scale,         coalesce(character_maximum_length, 0)   as character_maximum_length     from information_schema.columns     where         table_name       = 'price'         and table_schema = 'public' ) select     case when count_all_fields &lt; 1 then false   else true   end table_exists,     case when count_all_fields &lt; 1 then 1       else 0      end table_exists_error,     count_all_fields from (     select count (*) as count_all_fields     from columns_info ) sq<\/code><\/pre>\n<p>\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0431\u043b\u043e\u043a\u0435 \u043a\u043e\u0434\u0430 \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u044c \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 &#8212; \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u0432 CTE\u00a0<strong>required_columns<\/strong>, \u0438 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u0432 CTE\u00a0<strong>columns_info<\/strong>. \u0423\u0431\u0435\u0440\u0435\u043c \u043a\u043e\u0434 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 CTE\u00a0<strong>check_table_exists<\/strong>. \u0422\u0430\u043a\u0436\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u043c 3 CTE \u0441 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u044c \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435. \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b (<strong>table_name<\/strong>) \u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b (<strong>schema_name<\/strong>) \u0434\u043b\u044f\u00a0<strong>columns_info<\/strong>\u00a0\u0442\u0435\u043f\u0435\u0440\u044c \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u044b. \u041f\u0438\u0448\u0435\u043c \u043a\u043e\u0434 \u0434\u0430\u043b\u044c\u0448\u0435:<\/p>\n<pre><code class=\"sql\">with     -- \u041d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441: \u0437\u0430\u0434\u0430\u0435\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b     params(table_name, table_schema) as (         values (             'price',             'public'         )     ),     -- \u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0442\u0430\u0431\u043b\u0438\u0446\u044b     required_columns(column_name, data_type, is_nullable, character_maximum_length) as (         values                         ('id',          'bigint',                       'NO', null),             ('price_value', 'numeric',            'NO', 18.6),             ('model_id',    'integer',  'NO',null),             ('comment',     'character varying',            'YES',255)     ),     -- \u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f     required_constraints(column_name, constraint_type) as (         values                         ('id',          'PRIMARY KEY'),             ('id',    'FOREIGN KEY'),             ('model_id',    'FOREIGN KEY'),             ('price_value', 'UNIQUE'),             ('model_id',    'UNIQUE')     ),     -- \u041d\u0430\u0445\u043e\u0434\u0438\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u0445 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 null'\u043e\u0432     columns_info as (         select             column_name, data_type, is_nullable,             coalesce(numeric_precision, 0)          as numeric_precision,             coalesce(numeric_scale, 0)              as numeric_scale,             coalesce(character_maximum_length, 0)   as character_maximum_length         from information_schema.columns         where             table_name       = (select table_name   from params)             and table_schema = (select table_schema from params)     ),     -- \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u043f\u043e\u0434\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u043d\u0435\u0439     check_table_exist as (         select             case when count_all_fields &lt; 1 then false else true end table_exists,             case when count_all_fields &lt; 1 then 1 else 0 end table_exists_error,             count_all_fields         from (             select count (*) as count_all_fields             from columns_info         ) sq     ) select * from columns_info<\/code><\/pre>\n<p>\u0421\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u044b \u0432 SQL \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438. \u041c\u043d\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b \u0442\u0440\u0438 &#8212; \u0447\u0435\u0440\u0435\u0437:<\/p>\n<ul>\n<li>\n<p>\u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u00a0<strong>except<\/strong>;<\/p>\n<\/li>\n<li>\n<p>\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435\u00a0<strong>join<\/strong>;<\/p>\n<\/li>\n<li>\n<p>\u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0443.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044f \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0441\u044c join&#8217;\u043e\u043c. \u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c \u0432 CTE\u00a0<strong>columns_info<\/strong>. \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0435\u0440\u0432\u044b\u0445 3-\u0445. \u0422\u0430\u0431\u043b\u0438\u0446\u0430\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0430\u043b\u0438\u0430\u0441\u044b\u00a0<strong>t<\/strong>\u00a0\u0438\u00a0<strong>r<\/strong>. \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0437\u0430\u043f\u0438\u0441\u0438 \u0431\u0443\u0434\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u00a0<strong>columns_info<\/strong>. \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0435\u0441\u0442\u044c \u0432 \u0411\u0414. \u041a\u043e\u0434 \u0432\u00a0<strong>with<\/strong>\u00a0\u0438\u0434\u0435\u043d\u0442\u0438\u0447\u0435\u043d \u0431\u043b\u043e\u043a\u0443, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u043c\u0443 \u0432\u044b\u0448\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u0434\u0430\u043b\u0435\u0435 \u0431\u0443\u0434\u0443 \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u0442\u044c \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430:<\/p>\n<pre><code class=\"sql\">with (...) select t.* from columns_info t inner join required_columns r     on  t.column_name               = r.column_name     and t.data_type                 = r.data_type     and t.is_nullable               = r.is_nullable<\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u0435\u0449\u0435 3 \u0441\u0442\u043e\u043b\u0431\u0446\u0430. \u0421 \u043d\u0438\u043c\u0438 \u0441\u043b\u043e\u0436\u043d\u0435\u0435. \u0412 CTE\u00a0<strong>required_columns<\/strong>\u00a0\u0435\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e 4 \u0441\u0442\u043e\u043b\u0431\u0446\u0430, \u0442\u043e\u0433\u0434\u0430 \u0432\u00a0<strong>columns_info<\/strong>\u00a0\u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f 6. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0438 \u0446\u0438\u0444\u0440 \u043c\u044b \u0437\u0430\u0434\u0430\u0435\u043c \u0432 1-\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0435 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0442\u0438\u043f\u043e\u0432. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0435\u0441\u0442\u044c \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043d\u0430 3 \u0441\u0442\u043e\u043b\u0431\u0446\u0430.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e: \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432\u00a0<strong>varchar<\/strong>. null&#8217;\u044b \u0432 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0435 \u0441 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u043d\u0430 0. \u041d\u0430 \u0432\u0441\u044f\u043a\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u043f\u043e\u0432\u0442\u043e\u0440\u044e, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f: null&#8217;\u044b \u0447\u0435\u0440\u0435\u0437 \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f:<\/p>\n<pre><code class=\"sql\">with (...) select t.* from columns_info t inner join required_columns r     on  t.column_name               = r.column_name     and t.data_type                 = r.data_type     and t.is_nullable               = r.is_nullable     and t.character_maximum_length  = coalesce(r.character_maximum_length, 0)<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0446\u0435\u043b\u0443\u044e \u0438 \u0434\u0440\u043e\u0431\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u0438 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439. \u041d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0442\u0438\u043f\u0430\u00a0<strong>numeric<\/strong>\u00a0\u0438\u00a0<strong>decimal<\/strong>, \u0442.\u043a. \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b \u0438\u043c\u0435\u044e\u0442 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u0446\u0435\u043b\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0447\u0438\u0441\u0435\u043b. \u0417\u0434\u0435\u0441\u044c \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u00a0<strong>case<\/strong>\u00a0\u0434\u043b\u044f \u043e\u0442\u0431\u043e\u0440\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0441 \u0442\u0438\u043f\u0430\u043c\u0438\u00a0<strong>numeric<\/strong>\u00a0\u0438\u00a0<strong>decimal<\/strong>. \u0421 \u0443\u0447\u0435\u0442\u043e\u043c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0446\u0435\u043b\u0430\u044f \u0447\u0430\u0441\u0442\u044c, \u0443 \u0447\u0438\u0441\u043b\u0430 \u0438\u0437 \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u00a0<strong>character_maximum_length<\/strong>\u00a0(\u0438\u0437 CTE\u00a0<strong>required_columns<\/strong>) \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0441\u0435\u043a\u0430\u0442\u044c \u0434\u0440\u043e\u0431\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<strong>trunc()<\/strong>. \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0435\u0434\u0438\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0440\u0430\u043d\u0435\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u0443\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0434\u043b\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439\u00a0<strong>varchar<\/strong>\u00a0\u043d\u043e\u0432\u044b\u0439 \u043a\u043e\u0434 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0447\u0435\u0440\u0435\u0437\u00a0<strong>or<\/strong>. \u0412\u0441\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u0432 \u0441\u043a\u043e\u0431\u043a\u0438:<\/p>\n<pre><code class=\"sql\">with (...) select t.* from columns_info t inner join required_columns r     on  t.column_name               = r.column_name     and t.data_type                 = r.data_type     and t.is_nullable               = r.is_nullable     and t.character_maximum_length  = coalesce(r.character_maximum_length::numeric, 0)     and (     case             when t.data_type in ('numeric', 'decimal')             then t.numeric_precision = trunc(r.character_maximum_length::numeric)         end         or t.character_maximum_length = coalesce(r.character_maximum_length::numeric, 0)     )<\/code><\/pre>\n<p>\u0418 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0430\u043c\u043e\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0435: \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0434\u0440\u043e\u0431\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0435\u0451 \u043f\u0440\u043e\u0441\u0442\u043e &#8212; \u043d\u0443\u0436\u043d\u043e \u043b\u0438\u0448\u044c \u0438\u0437 \u0447\u0438\u0441\u043b\u0430 \u0432\u044b\u0447\u0435\u0441\u0442\u044c \u0435\u0433\u043e \u0446\u0435\u043b\u0443\u044e \u0447\u0430\u0441\u0442\u044c. \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e\u0441\u0442\u0438:<\/p>\n<p><em>18,6 &#8212; 18 = 0,6<\/em><\/p>\n<p>\u041d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434\u043e\u043c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u0435\u0435. \u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u00a0<strong>price<\/strong>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043a\u0430\u043a \u043e\u0431\u044a\u0435\u043a\u0442 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0435\u0441\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446\u00a0<strong>price_value<\/strong>. \u041e\u043d \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f\u00a0<strong>numeric<\/strong>\u00a0\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 18.6, \u0442\u043e \u0435\u0441\u0442\u044c 18 \u0446\u0438\u0444\u0440 \u0432 \u0446\u0435\u043b\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0438 6 \u0432 \u0434\u0440\u043e\u0431\u043d\u043e\u0439. \u0422\u043e, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432 select \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u043e\u0441\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f7b\/75d\/b38\/f7b75db38736aa737f92577ce2f7a4b4.png\" alt=\"img.2024-11-21 22.11.48.png\" title=\"\" width=\"782\" height=\"365\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f7b\/75d\/b38\/f7b75db38736aa737f92577ce2f7a4b4.png\"\/><\/figure>\n<p>\u042d\u0442\u043e \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0410 \u0447\u0442\u043e \u0432 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c?<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4be\/8fd\/76e\/4be8fd76e5c2b9809bb50653b282cbe5.png\" alt=\"img.2024-11-21 22.10.37.png\" title=\"\" width=\"732\" height=\"367\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4be\/8fd\/76e\/4be8fd76e5c2b9809bb50653b282cbe5.png\"\/><\/figure>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0441\u0442\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435, \u043d\u0443\u0436\u043d\u043e \u0434\u0440\u043e\u0431\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u0432\u0435\u0441\u0442\u0438 \u0432 \u0446\u0435\u043b\u0443\u044e. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 10. \u041d\u043e \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0431\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0431\u044b\u043b\u043e \u043d\u0435 18.6, \u0430, \u0441\u043a\u0430\u0436\u0435\u043c, 18.15? \u0422\u043e\u0433\u0434\u0430 \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 10 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u044c \u0432\u043e 2-\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438. \u0418 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 &#8212; \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435, \u0440\u0430\u0432\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0437\u043d\u0430\u043a\u043e\u0432 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u044f\u0442\u043e\u0439. \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0434\u043b\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u00a0<strong>price_value<\/strong>, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 18.6, \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u043d\u0430 1 (\u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u0442\u043e\u043b\u044c\u043a\u043e 1 \u0446\u0438\u0444\u0440\u0430 &#8212; 6). \u0410 \u0435\u0441\u043b\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0431\u044b\u043b\u043e 18.15, \u0442\u043e \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u043b\u0430 \u0431\u044b\u0442\u044c 2.<\/p>\n<p>\u0418\u0442\u0430\u043a, \u0440\u0430\u0437\u043d\u043e\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u0430 \u0438 \u0435\u0433\u043e \u0446\u0435\u043b\u043e\u0439 \u0447\u0430\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0435\u0440\u0443\u0442\u0441\u044f \u0438\u0437\u00a0<strong>character_maximum_length<\/strong>\u00a0(CTE\u00a0<strong>required_columns<\/strong>), \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u0430\u044f \u0432 \u0432\u0438\u0434\u0435 \u043a\u043e\u0434\u0430 \u043d\u0430 \u0441\u043a\u0440\u0438\u043d\u0448\u043e\u0442\u0435 \u0432\u044b\u0448\u0435, \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">r.character_maximum_length::numeric - trunc(r.character_maximum_length::numeric)<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0435\u0451 \u043d\u0443\u0436\u043d\u043e \u0443\u043c\u043d\u043e\u0436\u0438\u0442\u044c \u043d\u0430 10 \u0432 \u0441\u0442\u0435\u043f\u0435\u043d\u0438:<\/p>\n<pre><code class=\"sql\">(r.character_maximum_length::numeric - trunc(r.character_maximum_length::numeric)) * power(10, 1)<\/code><\/pre>\n<p>\u041d\u043e \u0435\u0434\u0438\u043d\u0438\u0446\u0430 (1) \u0432\u00a0<strong>power()<\/strong>\u00a0\u043f\u043e\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043d\u0435 \u0432\u0441\u0435 \u0441\u043b\u0443\u0447\u0430\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043e\u0434 \u0434\u043b\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0440\u0430\u0441\u0447\u0435\u0442\u0430:<\/p>\n<pre><code class=\"sql\">(r.character_maximum_length::numeric - trunc(r.character_maximum_length::numeric)) * power(10, length(split_part(r.character_maximum_length::text, '.', 2)))<\/code><\/pre>\n<p>\u041f\u0440\u043e\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e.\u00a0<strong>r.character_maximum_length::text<\/strong>\u00a0&#8212; \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u043e\u0432, \u0442.\u043a. \u044d\u0442\u043e\u0442 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f\u00a0<strong>numeric<\/strong>, \u043d\u043e \u0435\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0431\u0443\u0434\u0443\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0441\u0438\u043c\u0432\u043e\u043b\u044c\u043d\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438. \u0424\u0443\u043d\u043a\u0446\u0438\u044f\u00a0<strong>split_part()<\/strong>\u00a0\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0442\u0431\u043e\u0440\u0430 \u0446\u0438\u0444\u0440 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 (\u043d\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044f \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u043e\u0447\u043a\u0430, \u0442.\u043a. \u0434\u0440\u043e\u0431\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u0432 \u0442\u0430\u043a\u043e\u043c \u0432\u0438\u0434\u0435). \u0410\u00a0<strong>length<\/strong>\u00a0\u043f\u043e\u0434\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u0442\u0438\u0445 \u0446\u0438\u0444\u0440.<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f:<\/p>\n<pre><code class=\"sql\">with(...) select t.* from columns_info t inner join required_columns r     on  t.column_name   = r.column_name     and t.data_type     = r.data_type     and t.is_nullable   = r.is_nullable     and (         -- \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0446\u0435\u043b\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439         case             when t.data_type in ('numeric', 'decimal')             then t.numeric_precision = trunc(r.character_maximum_length::numeric)         end         and         -- \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0434\u0440\u043e\u0431\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439         case             when t.data_type in ('numeric', 'decimal')             then t.numeric_scale = (r.character_maximum_length::numeric - trunc(r.character_maximum_length::numeric))             * power(10, length(split_part(r.character_maximum_length::text, '.', 2)))         end         or t.character_maximum_length = coalesce(r.character_maximum_length::numeric, 0)    )<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u043a\u0430\u0436\u0435\u0442, \u043a\u0430\u043a\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438\u043c\u0435\u044e\u0442 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0435 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435. \u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043e\u0442\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0435 \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0437\u0430\u043f\u0438\u0441\u0438 \u0431\u0443\u0434\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0441 \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u043e\u043c \u0438\u0437 CTE\u00a0<strong>required_columns<\/strong>. \u0422\u0430\u043a \u043c\u044b \u043d\u0430\u0439\u0434\u0435\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u044b, \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043a\u0430\u043a\u0438\u0435-\u043b\u0438\u0431\u043e \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u044d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 CTE \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c\u00a0<strong>fields_comparison<\/strong>:<\/p>\n<pre><code class=\"sql\">-- \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0439 \u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043d\u0430\u0431\u043e\u0440\u044b \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u044b with  (...),     fields_comparison as (         select t.*         from columns_info t         inner join required_columns r             on  t.column_name   = r.column_name             and t.data_type     = r.data_type             and t.is_nullable   = r.is_nullable             and (                 -- \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0446\u0435\u043b\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439                 case                     when t.data_type = 'numeric'                     then t.numeric_precision = trunc(r.character_maximum_length::numeric)                 end                 and                 -- \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0434\u0440\u043e\u0431\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439                 case                     when t.data_type = 'numeric'                     then t.numeric_scale = (r.character_maximum_length::numeric - trunc(r.character_maximum_length::numeric))                     * power(10, length(split_part(r.character_maximum_length::text, '.', 2)))                 end                 or t.character_maximum_length = coalesce(r.character_maximum_length::numeric, 0)            ) ) select * from fields_comparison <\/code><\/pre>\n<p>\u0415\u0434\u0435\u043c \u0434\u0430\u043b\u044c\u0448\u0435. \u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c CTE,\u00a0<strong>check_unexpected_fields<\/strong>, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0438\u0441\u043a \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0435\u0441\u0442\u044c \u0432 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0435, \u043d\u043e \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0432 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u043c (\u0438\u0445 \u0443\u0441\u043b\u043e\u0432\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0437\u0432\u0430\u0442\u044c &#171;\u043b\u0438\u0448\u043d\u0438\u043c\u0438&#187;). \u042d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0432 CTE\u00a0<strong>columns_info<\/strong>\u00a0\u0438 CTE\u00a0<strong>required_columns<\/strong>. \u0417\u0430\u043f\u0440\u043e\u0441 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439:<\/p>\n<pre><code class=\"sql\">select column_name from columns_info except select column_name from required_columns<\/code><\/pre>\n<p>\u041f\u043e \u0442\u0430\u043a\u043e\u043c\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u043a\u0430\u0436\u0434\u043e\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 &#171;\u043b\u0438\u0448\u043d\u0435\u0433\u043e&#187; \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0432\u0435\u0434\u0435\u043d\u043e \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435. \u041e\u0434\u043d\u0430\u043a\u043e \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u044d\u0442\u043e \u043d\u0435 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 CTE, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u0442 \u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0430\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0430, \u0432\u0435\u0440\u043d\u0443\u0432\u0448\u0438\u0435\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0432 \u043e\u0434\u043d\u043e\u0439 \u044f\u0447\u0435\u0439\u043a\u0435. \u042f \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u044e \u0447\u0435\u0440\u0435\u0437 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u0440\u043e\u043a\u0435, \u0441 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044f, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<strong>string_agg()<\/strong>. \u0418 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u0434\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u044e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u0432\u0435\u0440\u043d\u0443\u0432\u0448\u0438\u0445\u0441\u044f \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443:<\/p>\n<pre><code class=\"sql\">with (...),     -- \u0418\u0449\u0435\u043c \u043b\u0438\u0448\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_unexpected_fields as (         select             count (column_name) as count_unexpected_fields,             string_agg(column_name, ', ') as unexpected_fields         from (             select column_name             from columns_info             except             select column_name             from required_columns         ) sq     ) select * from check_unexpected_fields<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0432 CTE\u00a0<strong>check_missing_fields<\/strong>\u00a0\u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442\u0441\u044f \u043f\u043e\u0447\u0442\u0438 \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u0437\u0430\u043f\u0440\u043e\u0441. \u0420\u0430\u0437\u043d\u0438\u0446\u0430 \u043b\u0438\u0448\u044c \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432\u00a0<strong>except<\/strong>\u00a0\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438. \u042d\u0442\u043e\u0442 CTE \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b. \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0442\u0435, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0432 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u043c \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0435, \u043d\u043e \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0432 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c:<\/p>\n<pre><code class=\"sql\">with  (...),     -- \u0418\u0449\u0435\u043c \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_missing_fields as (         select             count (column_name) as count_missing_fields,             string_agg(column_name, ', ') as missing_fields         from (             select column_name             from required_columns             except             select column_name             from columns_info         ) sq     ) select * from check_missing_fields<\/code><\/pre>\n<p>\u0418\u0442\u0430\u043a, \u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0434\u0438\u0438 \u043d\u0430\u043c \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b \u0441\u0442\u043e\u043b\u0431\u0446\u044b:<\/p>\n<ul>\n<li>\n<p>\u041e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0435 (CTE\u00a0<strong>required_columns<\/strong>);<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043e\u0432\u043f\u0430\u0432\u0448\u0438\u0435 \u0441 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c\u0438 (CTE\u00a0<strong>fields_comparison<\/strong>);<\/p>\n<\/li>\n<li>\n<p>\u041b\u0438\u0448\u043d\u0438\u0435 (CTE\u00a0<strong>check_unexpected_fields<\/strong>);<\/p>\n<\/li>\n<li>\n<p>\u041d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0435 (CTE\u00a0<strong>check_missing_fields<\/strong>).<\/p>\n<\/li>\n<\/ul>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0435\u0449\u0435 \u043e\u0434\u043d\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e. \u0412 \u043d\u0435\u043c \u0431\u0443\u0434\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c\u0441\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442 \u0441 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u043e\u043c \u043b\u0438\u0448\u044c \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, CTE \u0441 \u0442\u0430\u043a\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u043d\u0435\u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043b\u043e\u0433\u0438\u0447\u043d\u044b\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u00a0<strong>check_invalid_fields<\/strong>.<\/p>\n<p>\u0417\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0435\u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432 \u0441\u0435\u0431\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u00a0<strong>except<\/strong>, \u043d\u043e \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u0432\u0430\u0436\u0434\u044b. \u0418\u0437 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 (CTE\u00a0<strong>required_columns<\/strong>) \u0432\u044b\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043e\u0432\u043f\u0430\u0432\u0448\u0438\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 (CTE\u00a0<strong>fields_comparison<\/strong>) \u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 (CTE\u00a0<strong>check_missing_fields<\/strong>). \u0414\u043b\u044f CTE\u00a0<strong>check_missing_fields<\/strong>\u00a0\u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0442\u0430\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0443\u0436\u043d\u043e &#171;\u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c&#187; \u0441\u0442\u0440\u043e\u043a\u0443 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u043d\u0430\u0431\u043e\u0440 \u0437\u0430\u043f\u0438\u0441\u0435\u0439. \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u0438\u00a0<strong>string_to_table()<\/strong>. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0442\u0430\u043a\u043e\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"sql\">with  (...),     -- \u0418\u0449\u0435\u043c \u043d\u0435\u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_invalid_fields as (         select             count (column_name) as count_invalid_fields,             string_agg(column_name, ', ') as invalid_fields         from (             select column_name             from required_columns             except             select column_name             from fields_comparison             except             select string_to_table(missing_fields, ', ')             from check_missing_fields         ) sq select * from check_invalid_fields<\/code><\/pre>\n<p>\u0412\u0441\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 &#8212; \u0441\u043e\u0431\u0440\u0430\u043d\u0430. \u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c (<strong>constraints<\/strong>). \u041a \u0441\u043b\u043e\u0432\u0443, \u043a \u043d\u0438\u043c \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445, \u0432 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438, \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0435 \u043a\u043b\u044e\u0447\u0438 (<strong>foreign keys<\/strong>). \u0414\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0445 \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u0439 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0442\u044c \u0438\u0437 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0445\u0435\u043c\u044b \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0442\u0430\u0431\u043b\u0438\u0446:<\/p>\n<ul>\n<li>\n<p><strong>constraint_type<\/strong>\u00a0\u0438\u0437\u00a0<strong>table_constraints<\/strong>;<\/p>\n<\/li>\n<li>\n<p><strong>column_name<\/strong>\u00a0\u0438\u0437\u00a0<strong>constraint_column_usage<\/strong>;<\/p>\n<\/li>\n<li>\n<p><strong>column_name<\/strong>\u00a0\u0438\u0437\u00a0<strong>key_column_usage<\/strong>;<\/p>\n<\/li>\n<\/ul>\n<p>\u0415\u0441\u043b\u0438 \u043a\u0430\u043a\u043e\u0435-\u043b\u0438\u0431\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0442\u043e \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043e\u0448\u0438\u0431\u043a\u0430. \u0412 \u0435\u0451 \u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0435 \u0431\u0443\u0434\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u043d\u0430\u0440\u0443\u0448\u0430\u0435\u043c\u043e\u0433\u043e \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f (<strong>violates constraint<\/strong>):<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/2db\/73b\/041\/2db73b041432121b238b7b0deef32d55.png\" alt=\"img.2024-11-27 18.34.53.png\" title=\"\" width=\"798\" height=\"182\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2db\/73b\/041\/2db73b041432121b238b7b0deef32d55.png\"\/><\/figure>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043f\u043e\u043c\u043e\u0433\u0430\u044e\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445. \u041f\u043e \u044d\u0442\u043e\u0439 \u043f\u0440\u0438\u0447\u0438\u043d\u0435 \u0432\u0430\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0438\u0445 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0438 \u043b\u043e\u0433\u0438\u043a\u0443. \u0418\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u0445 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043f\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0443. \u0415\u0433\u043e \u043e\u043f\u0438\u0448\u0435\u043c \u0432 CTE\u00a0<strong>constraints_query<\/strong>:<\/p>\n<pre><code class=\"sql\">with (...),     -- \u0418\u0449\u0435\u043c \u0432\u0441\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b     constraints_query as(         select             t1.constraint_type,             t2.column_name,             t3.column_name as foreign_column         from information_schema.table_constraints t1         left join information_schema.constraint_column_usage t2             on t1.constraint_name = t2.constraint_name         left join information_schema.key_column_usage as t3             on t1.constraint_name = t3.constraint_name             and t1.table_schema = t3.table_schema         where             t1.table_name            = (select table_name   from params)             and t1.constraint_schema = (select table_schema from params)             and t2.column_name is not null     ) select * from constraints_query<\/code><\/pre>\n<p>\u041f\u043e \u044d\u0442\u043e\u043c\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u0431\u0443\u0434\u0443\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f 3 \u0441\u0442\u043e\u043b\u0431\u0446\u0430:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b98\/c94\/26a\/b98c9426af3367459f210790a47b9251.png\" alt=\"img.2024-11-27 19.06.34.png\" title=\"\" width=\"522\" height=\"315\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b98\/c94\/26a\/b98c9426af3367459f210790a47b9251.png\"\/><\/figure>\n<p>\u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u043c \u0434\u0430\u0442\u0430\u0441\u0435\u0442\u0435, \u0432 CTE\u00a0<strong>required_constraints<\/strong>, \u0443 \u043d\u0430\u0441 \u0432\u0441\u0435\u0433\u043e 2 \u0441\u0442\u043e\u043b\u0431\u0446\u0430:<\/p>\n<pre><code class=\"sql\">with     (...)     -- \u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f     required_constraints(column_name, constraint_type) as (         values                         ('id',          'PRIMARY KEY'),             ('id',    'FOREIGN KEY'),             ('model_id',    'FOREIGN KEY'),             ('price_value', 'UNIQUE'),             ('model_id',    'UNIQUE')     ) select * from required_constraints<\/code><\/pre>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c CTE (\u043d\u0430\u0437\u043e\u0432\u0435\u043c \u0435\u0433\u043e\u00a0<strong>union_foreign_ref_columns<\/strong>) \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432\u00a0<strong>column_name<\/strong>\u00a0\u0438\u00a0<strong>foreign_column<\/strong>\u00a0\u0432 1 \u0441\u0442\u043e\u043b\u0431\u0435\u0446:<\/p>\n<pre><code class=\"sql\">with (...),     -- \u0412\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439 (foreign_column) \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 (column_name)     union_foreign_ref_columns as (         select column_name, constraint_type         from constraints_query         union all         select foreign_column as column_name, constraint_type         from constraints_query     ) select * from union_foreign_ref_columns<\/code><\/pre>\n<p>\u0424\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u00a0<code>as column_name<\/code>\u00a0\u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u0435\u043d, \u043d\u043e \u044f \u0435\u0433\u043e \u043e\u0441\u0442\u0430\u0432\u0438\u043b \u0435\u0433\u043e \u0434\u043b\u044f \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0447\u0438\u0442\u0430\u0435\u043c\u043e\u0441\u0442\u0438 \u043a\u043e\u0434\u0430.<\/p>\n<p>\u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043d\u0430\u0431\u043e\u0440 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u044c \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435. \u042f \u0440\u0435\u0448\u0438\u043b \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 CTE \u043f\u043e\u0434 \u043f\u043e\u0438\u0441\u043a \u043d\u0435\u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439, \u0442.\u043a. \u0437\u0434\u0435\u0441\u044c \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u043e 2 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 (<strong>column_name<\/strong>\u00a0\u0438\u00a0<strong>constraints_type<\/strong>). \u0412 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e &#171;\u043b\u0438\u0448\u043d\u0438\u0435&#187; \u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0432 2-\u0445 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 CTE. \u0418 \u044d\u0442\u043e \u043f\u043e\u0447\u0442\u0438 \u043a\u043e\u043d\u0435\u0446 \u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"sql\">with (...),     -- \u0418\u0449\u0435\u043c \u043b\u0438\u0448\u043d\u0438\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_unexpected_constraints as (         select             count (column_name) as count_unexpected_constraints,             string_agg(column_name || ' (' || constraint_type || ')', ', ') as unexpected_constraints         from (             select *             from union_foreign_ref_columns             except             select column_name, constraint_type             from required_constraints         ) sq     ) select * from check_unexpected_constraints<\/code><\/pre>\n<p>\u0438<\/p>\n<pre><code class=\"sql\">with (...),     -- \u0418\u0449\u0435\u043c \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_missing_constraints as (         select             count (column_name) as count_missing_constraints,             string_agg(column_name || ' (' || constraint_type || ')', ', ') as missing_constraints         from (             select column_name, constraint_type             from required_constraints             except             select *             from union_foreign_ref_columns         ) sq     ) select * from check_missing_constraints<\/code><\/pre>\n<p>\u041b\u043e\u0433\u0438\u043a\u0430 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0437\u0434\u0435\u0441\u044c \u0442\u0430\u043a\u0430\u044f \u0436\u0435, \u043a\u0430\u043a \u043f\u0440\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432. \u041e\u0442\u043b\u0438\u0447\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u044d\u0442\u0438\u043c\u0438 2-\u043c\u044f CTE &#8212; \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u00a0<strong>except<\/strong>: \u0432\u00a0<strong>check_unexpected_constraints<\/strong>\u00a0\u0437\u0430\u043f\u0440\u043e\u0441 \u043a\u00a0<strong>union_foreign_ref_columns<\/strong>\u00a0\u0438\u0434\u0435\u0442 \u0434\u043e\u00a0<strong>except<\/strong>, \u0430 \u0432\u00a0<strong>check_missing_constraints<\/strong>\u00a0&#8212; \u043f\u043e\u0441\u043b\u0435. \u0418\u0437 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e \u0437\u0434\u0435\u0441\u044c \u0440\u0430\u0437\u0432\u0435 \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u0430\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f:<\/p>\n<pre><code class=\"sql\">string_agg(column_name || ' (' || constraint_type || ')', ', ')<\/code><\/pre>\n<p><strong>string_agg()<\/strong>\u00a0\u0437\u0434\u0435\u0441\u044c \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0432 1-\u0439 \u0441\u0442\u0440\u043e\u043a\u0435. \u041d\u043e \u0437\u0434\u0435\u0441\u044c 1-\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0436\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u043a\u043e\u0434\u0435 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432. \u042d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0432 1-\u0439 \u044f\u0447\u0435\u0439\u043a\u0435 \u0443\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a, \u043d\u043e \u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u043f\u043e 2-\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c. \u0412 \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u044d\u0442\u043e\u0439 \u044f\u0447\u0435\u0439\u043a\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4b1\/dff\/986\/4b1dff986c7e062c4f7db2df15baa3fb.png\" alt=\"img.2024-11-27 19.27.36.png\" title=\"\" width=\"906\" height=\"221\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4b1\/dff\/986\/4b1dff986c7e062c4f7db2df15baa3fb.png\"\/><\/figure>\n<p>\u041f\u043e \u044d\u0442\u043e\u043c\u0443 \u0441\u043a\u0440\u0438\u043d\u0448\u043e\u0442\u0443 \u043f\u0440\u043e\u0449\u0435 \u043f\u043e\u043d\u044f\u0442\u044c, \u043f\u043e\u0447\u0435\u043c\u0443 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u0441\u00a0<strong>string_agg()<\/strong>\u00a0\u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a\u0430\u044f, \u043a\u0430\u043a\u0430\u044f \u043e\u043d\u0430 \u0435\u0441\u0442\u044c. \u0412 \u043d\u0435\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0438\u0437 2-\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f 2-\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0431\u0435\u0440\u0435\u0442\u0441\u044f \u0432 \u0441\u043a\u043e\u0431\u043a\u0438 \u0441 \u043f\u0440\u043e\u0431\u0435\u043b\u043e\u043c \u043f\u0435\u0440\u0435\u0434 1-\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c.<\/p>\n<p>\u041d\u0443 \u0438 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c CTE \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 CTE, \u0438 \u0441\u0443\u043c\u043c\u0438\u0440\u0443\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u043e\u0448\u0438\u0431\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u043b\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e\u00a0<strong>count()<\/strong>:<\/p>\n<pre><code class=\"sql\">with (...),     -- \u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435     checks as (         select             -- \u0412\u044b\u0432\u043e\u0434\u0438\u043c \u0432\u0441\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0430\u0445 \u0438 \u0441\u0443\u043c\u043c\u0438\u0440\u0443\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e             table_exists_error + count_unexpected_fields + count_missing_fields + count_invalid_fields +             count_unexpected_constraints + count_missing_constraints as errors,             *         from        check_table_exist         cross join  check_unexpected_fields         cross join  check_missing_fields         cross join  check_invalid_fields         cross join  check_unexpected_constraints         cross join  check_missing_constraints ) select * from checks<\/code><\/pre>\n<p>\u0417\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0432\u044b\u0432\u043e\u0434\u0438\u043c\u044b\u0435 \u044d\u0442\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u0432 \u0441\u0442\u0440\u043e\u043a\u0430\u0445, \u0432\u0438\u0434\u043d\u044b \u0432 \u0441\u0430\u043c\u0438\u0445 \u0441\u0442\u0440\u043e\u043a\u0430\u0445, \u0432 \u0442\u0443\u043b\u0442\u0438\u043f\u0435 (\u0435\u0441\u043b\u0438 \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435), \u0438 \u0438\u0445 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0431\u043b\u043e\u043a\u043d\u043e\u0442:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/acd\/503\/fc2\/acd503fc20796a453df631ed19dda565.gif\" width=\"1614\" height=\"969\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/acd\/503\/fc2\/acd503fc20796a453df631ed19dda565.gif\"\/><\/figure>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443 \u044d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u043b\u0435\u0437\u0435\u043d?<\/h3>\n<ul>\n<li>\n<p>\u041f\u043e \u043c\u043e\u0435\u043c\u0443 \u043e\u043f\u044b\u0442\u0443, \u043f\u043e\u0447\u0435\u043c\u0443-\u0442\u043e \u043d\u043e\u0432\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0411\u0414, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438, \u0447\u0430\u0441\u0442\u043e \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043e \u0432 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u0445. \u0418\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u044d\u0442\u043e\u0433\u043e, \u043f\u0440\u043e\u0449\u0435 \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0435\u0433\u043e \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u0430\u0432\u043e\u043a. \u042d\u0442\u043e \u0441\u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442 \u0432\u0440\u0435\u043c\u044f \u043f\u0440\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442 \u0447\u0435\u043b\u043e\u0432\u0435\u0447\u0435\u0441\u043a\u0438\u0439 \u0444\u0430\u043a\u0442\u043e\u0440. \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0435\u0441\u043b\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u0430\u0432\u043e\u043a \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e;<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u043b\u044c\u0437\u0430 \u043e\u0442 \u0442\u0430\u043a\u0438\u0445 \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u0447\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0442\u0435\u043c \u0432\u044b\u0448\u0435, \u0447\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0442\u0430\u0431\u043b\u0438\u0446 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0438;<\/p>\n<\/li>\n<li>\n<p>\u041f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f \u0434\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 \u043f\u043e\u0441\u043b\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0431\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u0435\u043d\u0434.<\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/862562\/\"> https:\/\/habr.com\/ru\/articles\/862562\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>\u041f\u0440\u0438\u043c\u0435\u0440 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c\u043e\u0439 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438<\/h3>\n<p>\u041f\u0440\u0438\u0432\u0435\u0442.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0432\u0430\u043c \u043f\u0440\u0438\u0448\u043b\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043d\u0430 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u0431\u044d\u043a\u0435\u043d\u0434\u0443. \u0412\u044b \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u0435 \u043a \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0443 \u0441 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u043c\u0438 \u0438 \u0432\u0438\u0434\u0438\u0442\u0435, \u043f\u043e\u043c\u0438\u043c\u043e \u043f\u0440\u043e\u0447\u0435\u0433\u043e, \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 (\u0411\u0414). \u041f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442:<\/p>\n<p><strong>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 1. \u0422\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u043a \u0442\u0430\u0431\u043b\u0438\u0446\u0435 price \u0432 \u0441\u0445\u0435\u043c\u0435 public<\/strong><\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>\u0421\u0442\u043e\u043b\u0431\u0435\u0446<\/p>\n<\/th>\n<th>\n<p>\u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435<\/p>\n<\/th>\n<th>\n<p>\u0422\u0438\u043f<\/p>\n<\/th>\n<th>\n<p>Not null<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">id<\/p>\n<\/td>\n<td>\n<p align=\"left\">PK<\/p>\n<\/td>\n<td>\n<p align=\"left\">int8<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0414\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">price_value<\/p>\n<\/td>\n<td>\n<p align=\"left\">UNIQUE price_unique<\/p>\n<\/td>\n<td>\n<p align=\"left\">numeric(18, 6)<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0414\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">model_id<\/p>\n<\/td>\n<td>\n<p align=\"left\">UNIQUE price_unique, FOREIGN KEY price_models_fk \u0441\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u043c <strong>id<\/strong> \u0442\u0430\u0431\u043b\u0438\u0446\u044b <strong>models<\/strong><\/p>\n<\/td>\n<td>\n<p align=\"left\">int4<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0414\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">comment<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u200b<\/p>\n<\/td>\n<td>\n<p align=\"left\">varchar(255)<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u041d\u0435\u0442<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043e\u0434\u0438\u043d \u0438\u0437 \u0442\u0435\u0441\u0442-\u043a\u0435\u0439\u0441\u043e\u0432 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u044d\u0442\u0438\u0445 \u0442\u0430\u0431\u043b\u0438\u0446 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c. \u041a \u0442\u0430\u043a\u0438\u043c \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u0441\u043e\u0441\u0442\u0430\u0432 \u0438 \u0442\u0438\u043f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0441\u0432\u044f\u0437\u0435\u0439 \u0438 \u0442.\u0434. \u0412 Postgresql \u0435\u0441\u0442\u044c \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u0430\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u044c \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041a \u0441\u043b\u043e\u0432\u0443, \u044d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u043e\u0431\u0437\u043e\u0440\u0430 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043d\u043e \u0438 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0438 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 SQL.<\/p>\n<p>\u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043f\u0430\u0440\u0443 \u0442\u0430\u0431\u043b\u0438\u0446:<\/p>\n<pre><code class=\"sql\">-- drop table public.models  create table public.models (     id int8 generated by default as identity( increment by 1 minvalue 1 maxvalue 9223372036854775807 start 1 cache 1 no cycle) not null,     \"name\" varchar(50) not null,     manufacturer varchar(50) not null,     constraint models_pk primary key (id),     constraint models_unique unique (name, manufacturer) );<\/code><\/pre>\n<pre><code class=\"sql\">-- drop table public.price  create table public.price (     id int8 generated by default as identity( increment by 1 minvalue 1 maxvalue 9223372036854775807 start 1 cache 1 no cycle) not null,     price_value numeric(18,6) not null,     model_id int4 not null,     comment varchar(255) null,     constraint price_pk primary key (id),     constraint price_models_fk foreign key (model_id) references public.models(id),     constraint price_unique unique (price_value, model_id) );<\/code><\/pre>\n<p>\u0421\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u043c\u0441\u044f \u043d\u0430\u00a0<strong>price<\/strong>. \u0422\u0430\u0431\u043b\u0438\u0446\u0430\u00a0<strong>models<\/strong>\u00a0\u043d\u0443\u0436\u043d\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u00a0<strong>price<\/strong>\u00a0\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 (<strong>constraint<\/strong>) \u043f\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u043c\u0443 \u043a\u043b\u044e\u0447\u0443 (<strong>foreign key<\/strong>). SQL-\u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u044d\u0442\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">with     -- \u041d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441: \u0437\u0430\u0434\u0430\u0435\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0441\u0445\u0435\u043c\u044b     params(table_name, table_schema) as (         values (             'price',             'public'         )     ),     -- \u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0442\u0430\u0431\u043b\u0438\u0446\u044b     required_columns(column_name, data_type, is_nullable, character_maximum_length) as (         values                         ('id',          'bigint',               'NO', null),             ('price_value', 'numeric',              'NO', 18.6),             ('model_id',    'integer',      'NO',null),             ('comment',     'character varying',    'YES',  255)     ),     -- \u041e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f     required_constraints(column_name, constraint_type) as (         values                         ('id',          'PRIMARY KEY'),             ('id',    'FOREIGN KEY'),             ('model_id',    'FOREIGN KEY'),             ('price_value', 'UNIQUE'),             ('model_id',    'UNIQUE')     ),     -- \u041d\u0430\u0445\u043e\u0434\u0438\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u0445 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u0435\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 null'\u043e\u0432     columns_info as (         select             column_name, data_type, is_nullable,             coalesce(numeric_precision, 0)          as numeric_precision,             coalesce(numeric_scale, 0)              as numeric_scale,             coalesce(character_maximum_length, 0)   as character_maximum_length         from information_schema.columns         where             table_name       = (select table_name   from params)             and table_schema = (select table_schema from params)     ),     -- \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u043f\u043e\u0434\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432 \u043d\u0435\u0439     check_table_exist as (         select             case when count_all_fields &lt; 1 then false else true end table_exists,             case when count_all_fields &lt; 1 then 1 else 0 end table_exists_error,             count_all_fields         from (             select count (*) as count_all_fields             from columns_info         ) sq     ),     -- \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0439 \u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043d\u0430\u0431\u043e\u0440\u044b \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u044b     fields_comparison as (         select t.*         from columns_info t         inner join required_columns r             on  t.column_name   = r.column_name             and t.data_type     = r.data_type             and t.is_nullable   = r.is_nullable             and (                 -- \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0446\u0435\u043b\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439                 case                     when t.data_type = 'numeric'                     then t.numeric_precision = trunc(r.character_maximum_length::numeric)                 end                 and                 -- \u0421\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0434\u0440\u043e\u0431\u043d\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439                 case                     when t.data_type = 'numeric'                     then t.numeric_scale = (r.character_maximum_length::numeric - trunc(r.character_maximum_length::numeric))                     * power(10, length(split_part(r.character_maximum_length::text, '.', 2)))                 end                 or t.character_maximum_length = coalesce(r.character_maximum_length::numeric, 0)            )     ),     -- \u0418\u0449\u0435\u043c \u043b\u0438\u0448\u043d\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_unexpected_fields as (         select             count (column_name) as count_unexpected_fields,             string_agg(column_name, ', ') as unexpected_fields         from (             select column_name             from columns_info             except             select column_name             from required_columns         ) sq     ),     -- \u0418\u0449\u0435\u043c \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_missing_fields as (         select             count (column_name) as count_missing_fields,             string_agg(column_name, ', ') as missing_fields         from (             select column_name             from required_columns             except             select column_name             from columns_info         ) sq     ),     -- \u0418\u0449\u0435\u043c \u043d\u0435\u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_invalid_fields as (         select             count (column_name) as count_invalid_fields,             string_agg(column_name, ', ') as invalid_fields         from (             select column_name             from required_columns             except             select column_name             from fields_comparison             except             select string_to_table(missing_fields, ', ')             from check_missing_fields         ) sq     ),     -- \u0418\u0449\u0435\u043c \u0432\u0441\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b     constraints_query as(         select             t1.constraint_type,             t2.column_name,             t3.column_name as foreign_column         from information_schema.table_constraints t1         left join information_schema.constraint_column_usage t2             on t1.constraint_name = t2.constraint_name         left join information_schema.key_column_usage as t3             on t1.constraint_name = t3.constraint_name             and t1.table_schema = t3.table_schema         where             t1.table_name            = (select table_name   from params)             and t1.constraint_schema = (select table_schema from params)             and t2.column_name is not null     ),     -- \u0412\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u043a\u043b\u044e\u0447\u0435\u0439 (foreign_column) \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 (column_name)     union_foreign_ref_columns as (         select column_name, constraint_type         from constraints_query         union all         select foreign_column as column_name, constraint_type         from constraints_query     ),     -- \u0418\u0449\u0435\u043c \u043b\u0438\u0448\u043d\u0438\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_unexpected_constraints as (         select             count (column_name) as count_unexpected_constraints,             string_agg(column_name || ' (' || constraint_type || ')', ', ') as unexpected_constraints         from (             select *             from union_foreign_ref_columns             except             select column_name, constraint_type             from required_constraints         ) sq     ),     -- \u0418\u0449\u0435\u043c \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u044e\u0449\u0438\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0438 \u0441\u0447\u0438\u0442\u0430\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e     check_missing_constraints as (         select             count (column_name) as count_missing_constraints,             string_agg(column_name || ' (' || constraint_type || ')', ', ') as missing_constraints         from (             select column_name, constraint_type             from required_constraints             except             select *             from union_foreign_ref_columns         ) sq     ),     -- \u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435     checks as (         select             -- \u0412\u044b\u0432\u043e\u0434\u0438\u043c \u0432\u0441\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0430\u0445 \u0438 \u0441\u0443\u043c\u043c\u0438\u0440\u0443\u0435\u043c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e             table_exists_error + count_unexpected_fields + count_missing_fields + count_invalid_fields +             count_unexpected_constraints + count_missing_constraints as errors,             *         from        check_table_exist         cross join  check_unexpected_fields         cross join  check_missing_fields         cross join  check_invalid_fields         cross join  check_unexpected_constraints         cross join  check_missing_constraints ) select * from checks<\/code><\/pre>\n<p>\u0412\u0430\u0436\u043d\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435: \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u0432\u0430\u0448\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043d\u0443\u0436\u043d\u043e \u0432\u043d\u0435\u0441\u0442\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 CTE\u00a0<strong>params<\/strong>\u00a0(\u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0432\u0430\u0448\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u0441\u0445\u0435\u043c\u044b, \u0433\u0434\u0435 \u043e\u043d\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f),\u00a0<strong>required_columns<\/strong>\u00a0(\u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0432\u0430\u0448\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b) \u0438\u00a0<strong>required_constraints<\/strong>\u00a0(\u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u0432\u0430\u0448\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b). \u041e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u043e\u0434 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043c\u0435\u043d\u044f\u0442\u044c.<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0439 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u043b\u0438\u0447\u0438\u0435\u043c \u0432\u043e \u0432\u0441\u0435\u0445 \u0411\u0414 Postgresql \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0445\u0435\u043c\u044b (<strong>information_schema<\/strong>). \u0412 Oracle, \u0435\u0441\u043b\u0438 \u043d\u0435 \u043e\u0448\u0438\u0431\u0430\u044e\u0441\u044c, \u0442\u043e\u0436\u0435 \u0442\u0430\u043a\u0430\u044f \u0435\u0441\u0442\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0430\u043a\u0442\u0443\u0430\u043b\u0435\u043d \u0438 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0421\u0423\u0411\u0414 (\u043f\u043e\u043b\u0430\u0433\u0430\u044e, \u0441 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u043a\u0430\u043c\u0438).<\/p>\n<h3>\u0427\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441?<\/h3>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p>\u0421\u0442\u043e\u043b\u0431\u0435\u0446<\/p>\n<\/th>\n<th>\n<p>\u0420\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u043a\u0430<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">errors<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0443\u043c\u043c\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u0448\u0438\u0431\u043e\u043a<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">table_exists<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043b\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u0430<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">table_exists_error<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u0434\u043b\u044f \u0443\u0447\u0435\u0442\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">count_all_fields<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u0421\u0447\u0435\u0442\u0447\u0438\u043a \u0432\u0441\u0435\u0445 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">coun<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-440426","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/440426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=440426"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/440426\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=440426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=440426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=440426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}