Skip to content

Commit

Permalink
Merge pull request #293 from cybertec-postgresql/fix-max-ident-length
Browse files Browse the repository at this point in the history
[*] use system max identifier length instead of hard-coded value
  • Loading branch information
pashagolub authored Oct 25, 2023
2 parents 3c5f75b + 74cf663 commit 28206d7
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/db/sql/metric/ensure_partition_postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ DECLARE
l_part_end date;
l_sql text;
ideal_length int;
l_template_table text := 'admin.metrics_template';
l_unlogged text := '';
l_template_table text := 'admin.metrics_template';
MAX_IDENT_LEN CONSTANT integer := current_setting('max_identifier_length')::int;
BEGIN

PERFORM pg_advisory_xact_lock(regexp_replace( md5(metric) , E'\\D', '', 'g')::varchar(10)::int8);
Expand All @@ -51,9 +52,9 @@ BEGIN

l_part_name_2nd := metric || '_' || dbname;

IF char_length(l_part_name_2nd) > 63 -- use "dbname" hash instead of name for overly long ones
IF char_length(l_part_name_2nd) > MAX_IDENT_LEN -- use "dbname" hash instead of name for overly long ones
THEN
ideal_length = 63 - char_length(format('%s_', metric));
ideal_length = MAX_IDENT_LEN - char_length(format('%s_', metric));
l_part_name_2nd := metric || '_' || substring(md5(dbname) from 1 for ideal_length);
END IF;

Expand Down Expand Up @@ -88,9 +89,9 @@ BEGIN

l_part_name_3rd := format('%s_%s_y%sd%s', metric, dbname, l_year, to_char(l_doy, 'fm000' ));

IF char_length(l_part_name_3rd) > 63 -- use "dbname" hash instead of name for overly long ones
IF char_length(l_part_name_3rd) > MAX_IDENT_LEN -- use "dbname" hash instead of name for overly long ones
THEN
ideal_length = 63 - char_length(format('%s__y%sd%s', metric, l_year, to_char(l_doy, 'fm000')));
ideal_length = MAX_IDENT_LEN - char_length(format('%s__y%sd%s', metric, l_year, to_char(l_doy, 'fm000')));
l_part_name_3rd := format('%s_%s_y%sd%s', metric, substring(md5(dbname) from 1 for ideal_length), l_year, to_char(l_doy, 'fm000' ));
END IF;
ELSE
Expand All @@ -110,9 +111,9 @@ BEGIN

l_part_name_3rd := format('%s_%s_y%sw%s', metric, dbname, l_year, to_char(l_week, 'fm00' ));

IF char_length(l_part_name_3rd) > 63 -- use "dbname" hash instead of name for overly long ones
IF char_length(l_part_name_3rd) > MAX_IDENT_LEN -- use "dbname" hash instead of name for overly long ones
THEN
ideal_length = 63 - char_length(format('%s__y%sw%s', metric, l_year, to_char(l_week, 'fm00')));
ideal_length = MAX_IDENT_LEN - char_length(format('%s__y%sw%s', metric, l_year, to_char(l_week, 'fm00')));
l_part_name_3rd := format('%s_%s_y%sw%s', metric, substring(md5(dbname) from 1 for ideal_length), l_year, to_char(l_week, 'fm00' ));
END IF;
END IF;
Expand Down

0 comments on commit 28206d7

Please sign in to comment.