select version(); 
-- PostgreSQL 17.4 (Debian 17.4-1.pgdg110+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit

select * from pg_settings where name ~ 'shared';
shared_buffers	131072	8kB	Resource Usage / Memory	Sets the number of shared memory buffers used by the server.
shared_memory_size	8173	MB

 SELECT * FROM pg_config();
|name             |setting                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|-----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|BINDIR           |/usr/lib/postgresql/17/bin                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
|DOCDIR           |/usr/share/doc/postgresql-doc-17                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
|HTMLDIR          |/usr/share/doc/postgresql-doc-17                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
|INCLUDEDIR       |/usr/include/postgresql                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|PKGINCLUDEDIR    |/usr/include/postgresql                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
|INCLUDEDIR-SERVER|/usr/include/postgresql/17/server                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
|LIBDIR           |/usr/lib/x86_64-linux-gnu                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
|PKGLIBDIR        |/usr/lib/postgresql/17/lib                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
|LOCALEDIR        |/usr/share/locale                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
|MANDIR           |/usr/share/postgresql/17/man                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
|SHAREDIR         |/usr/share/postgresql/17                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
|SYSCONFDIR       |/etc/postgresql-common                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
|PGXS             |/usr/lib/postgresql/17/lib/pgxs/src/makefiles/pgxs.mk                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
|CONFIGURE        | '--build=x86_64-linux-gnu' '--prefix=/usr' '--includedir=${prefix}/include' '--mandir=${prefix}/share/man' '--infodir=${prefix}/share/info' '--sysconfdir=/etc' '--localstatedir=/var' '--disable-option-checking' '--disable-silent-rules' '--libdir=${prefix}/lib/x86_64-linux-gnu' '--runstatedir=/run' '--disable-maintainer-mode' '--disable-dependency-tracking' '--with-tcl' '--with-perl' '--with-python' '--with-pam' '--with-openssl' '--with-libxml' '--with-libxslt' '--mandir=/usr/share/postgresql/17/man' '--docdir=/usr/share/doc/postgresql-doc-17' '--sysconfdir=/etc/postgresql-common' '--datarootdir=/usr/share/' '--datadir=/usr/share/postgresql/17' '--bindir=/usr/lib/postgresql/17/bin' '--libdir=/usr/lib/x86_64-linux-gnu/' '--libexecdir=/usr/lib/postgresql/' '--includedir=/usr/include/postgresql/' '--with-extra-version= (Debian 17.4-1.pgdg110+2)' '--enable-nls' '--enable-thread-safety' '--enable-debug' '--disable-rpath' '--with-uuid=e2fs' '--with-gnu-ld' '--with-gssapi' '--with-ldap' '--with-pgport=5432' '--with-system-tzdata=/usr/share/zoneinfo' 'AWK=mawk' 'MKDIR_P=/bin/mkdir -p' 'PROVE=/usr/bin/prove' 'PYTHON=/usr/bin/python3' 'TAR=/bin/tar' 'XSLTPROC=xsltproc --nonet' 'CFLAGS=-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer' 'LDFLAGS=-Wl,-z,relro -Wl,-z,now' '--enable-tap-tests' '--with-icu' '--with-llvm' 'LLVM_CONFIG=/usr/bin/llvm-config-16' 'CLANG=/usr/bin/clang-16' '--with-lz4' '--with-zstd' '--with-systemd' '--with-selinux' '--enable-dtrace' 'build_alias=x86_64-linux-gnu' 'CPPFLAGS=-Wdate-time -D_FORTIFY_SOURCE=2' 'CXXFLAGS=-g -O2 -fstack-protector-strong -Wformat -Werror=format-security'|
|CC               |gcc                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
|CPPFLAGS         |-Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
|CFLAGS           |-Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -g -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
|CFLAGS_SL        |-fPIC                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
|LDFLAGS          |-Wl,-z,relro -Wl,-z,now -L/usr/lib/llvm-16/lib -Wl,--as-needed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
|LDFLAGS_EX       |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
|LDFLAGS_SL       |                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
|LIBS             |-lpgcommon -lpgport -lselinux -lzstd -llz4 -lxslt -lxml2 -lpam -lssl -lcrypto -lgssapi_krb5 -lz -lreadline -lpthread -lrt -ldl -lm                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
|VERSION          |PostgreSQL 17.4 (Debian 17.4-1.pgdg110+2)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |


select * from pg_database; 
|oid   |datname  |datdba|encoding|datlocprovider|datistemplate|datallowconn|dathasloginevt|datconnlimit|datfrozenxid|datminmxid|dattablespace|datcollate |datctype   |datlocale|daticurules|datcollversion|datacl                             |
|------|---------|------|--------|--------------|-------------|------------|--------------|------------|------------|----------|-------------|-----------|-----------|---------|-----------|--------------|-----------------------------------|
|5     |postgres |10    |6       |c             |false        |true        |false         |-1          |730         |1         |1,663        |en_US.UTF-8|en_US.UTF-8|         |           |2.40          |                                   |
|16,388|demo     |10    |6       |c             |false        |true        |false         |-1          |730         |1         |1,663        |en_US.UTF-8|en_US.UTF-8|         |           |2.40          |                                   |
|1     |template1|10    |6       |c             |true         |true        |false         |-1          |730         |1         |1,663        |en_US.UTF-8|en_US.UTF-8|         |           |2.40          |{=c/postgres,postgres=CTc/postgres}|
|4     |template0|10    |6       |c             |true         |false       |false         |-1          |730         |1         |1,663        |en_US.UTF-8|en_US.UTF-8|         |           |              |{=c/postgres,postgres=CTc/postgres}|


select collname, collprovider, collencoding, collcollate, collctype, colllocale, 
	collversion, collisdeterministic 
FROM pg_collation cll
--where cll.collname in ('C.utf8', 'en_US.utf8', 'ru-RU-x-icu')

|collname   |collprovider|collencoding|collcollate|collctype |colllocale|collversion|collisdeterministic|
|-----------|------------|------------|-----------|----------|----------|-----------|-------------------|
|default    |d           |-1          |           |          |          |           |true               |
|C          |c           |-1          |C          |C         |          |           |true               |
|POSIX      |c           |-1          |POSIX      |POSIX     |          |           |true               |
|ucs_basic  |b           |6           |           |          |C         |1          |true               |
|pg_c_utf8  |b           |6           |           |          |C.UTF-8   |1          |true               |
|unicode    |i           |-1          |           |          |und       |153.14     |true               |
|C.utf8     |c           |6           |C.utf8     |C.utf8    |          |           |true               |
|en_US.utf8 |c           |6           |en_US.utf8 |en_US.utf8|          |2.40       |true               |
|ru_RU.utf8 |c           |6           |ru_RU.utf8 |ru_RU.utf8|          |2.40       |true               |
|en_US      |c           |6           |en_US.utf8 |en_US.utf8|          |2.40       |true               |
|ru_RU      |c           |6           |ru_RU.utf8 |ru_RU.utf8|          |2.40       |true               |
|und-x-icu  |i           |-1          |           |          |und       |153.14     |true               |
|af-x-icu   |i           |-1          |           |          |af        |153.14.37  |true               |
|af-NA-x-icu|i           |-1          |           |          |af-NA     |153.14.37  |true               |
|af-ZA-x-icu|i           |-1          |           |          |af-ZA     |153.14.37  |true               |
...


explain (analyze, costs, buffers, verbose) 
select 
    ('БвЁжЫйяЙSёYz&$' || gen.id) 
from  generate_series(1, 10000) AS gen(id)
order by 1 desc; 
Execution Time: 73.600 ms

explain (analyze, costs, buffers, verbose) 
select 
    ('БвЁжЫйяЙSёYz&$' || gen.id) collate "C" 
from  generate_series(1, 10000) AS gen(id)
order by 1 desc; 
Execution Time: 4.792 ms

explain (analyze, costs, buffers, verbose) 
select 
    ('БвЁжЫйяЙSёYz&$' || gen.id) collate "ucs_basic" 
from  generate_series(1, 10000) AS gen(id)
order by 1 desc; 
Execution Time: 4.796 ms

explain (analyze, costs, buffers, verbose) 
select 
    ('БвЁжЫйяЙSёYz&$' || gen.id) collate "pg_c_utf8" 
from  generate_series(1, 10000) AS gen(id)
order by 1 desc;
Execution Time: 4.863 ms

explain (analyze, costs, buffers, verbose) 
SELECT
    ('БвЁжЫйяЙSёYz&$' || gen.id) collate "C.utf8" 
FROM generate_series(1, 10000) AS gen(id)
order by 1 desc; 
Execution Time: 7.473 ms

explain (analyze, costs, buffers, verbose) 
select 
    ('БвЁжЫйяЙSёYz&$' || gen.id) collate "und-x-icu" 
from  generate_series(1, 10000) AS gen(id)
order by 1 desc; 
Execution Time: 13.282 ms


explain (analyze, costs, buffers, verbose) 
SELECT
    ('БвЁжЫйяЙSёYz&$' || gen.id) collate "en_US.utf8" 
FROM generate_series(1, 10000) AS gen(id)
order by 1 desc; 
Execution Time: 73.068 ms


explain (analyze, costs, buffers, verbose) 
SELECT
    ('БвЁжЫйяЙSёYz&$' || gen.id) collate "ru_RU.utf8" 
FROM generate_series(1, 10000) AS gen(id)
order by 1 ; 
Execution Time: 73.211 ms

explain (analyze, costs, buffers, verbose) 
SELECT
    ('БвЁжЫйяЙSёYz&$' || gen.id) collate "en-US-x-icu" 
FROM generate_series(1, 10000) AS gen(id)
order by 1 ; 
Execution Time: 12.886 ms

explain (analyze, costs, buffers, verbose) 
SELECT
    ('БвЁжЫйяЙSёYz&$' || gen.id) collate "ru-RU-x-icu" 
FROM generate_series(1, 10000) AS gen(id)
order by 1 desc; 
Execution Time: 13.188 ms

CREATE COLLATION ignore_accent_case (provider = icu, deterministic = false, locale = 'und-u-ks-level1');

select 'Ё' = 'е' collate "ignore_accent_case" as "Ё=е ?"
     , 'Й' = 'И' collate "ignore_accent_case" as "Й=и ?";
    
explain (analyze, costs, buffers, verbose) 
SELECT
    ('БвЁжЫйяЙSёYz&$' || gen.id) collate "ignore_accent_case" 
FROM generate_series(1, 10000) AS gen(id)
order by 1 desc; 
Execution Time: 13.184 ms


-- drop table test_coll2;
--truncate test_coll2;
create table test_coll2 (
	txt_C_utf8 		text collate "C.utf8" not null,
	txt_en_US_utf8 	text collate "en_US.utf8" not null,
	txt_ru_RU_x_icu text collate "ru-RU-x-icu" not null
);

INSERT INTO test_coll2 (txt_C_utf8, txt_en_US_utf8,txt_ru_RU_x_icu)
SELECT
    'БвЁжЫйяЙSёYz&$' || gen.id AS txt_C_utf8,
    'БвЁжЫйяЙSёYz&$' || gen.id AS txt_en_US_utf8,
    'БвЁжЫйяЙSёYz&$' || gen.id AS txt_ru_RU_x_icu
FROM generate_series(200000000, 200000000 + 2000000) AS gen(id);

CREATE INDEX test_coll2_txt_C_utf8_idx ON test_coll2 (txt_C_utf8);
CREATE INDEX test_coll2_txt_en_US_utf8_idx ON test_coll2 (txt_en_US_utf8);
CREATE INDEX test_coll2_txt_ru_RU_x_icu_idx ON test_coll2 (txt_ru_RU_x_icu);

vacuum (freeze, analyze, verbose) test_coll2;
checkpoint;

select * from test_coll2 limit 100;

select (select count(*) from public.test_coll2) as cnt,
pg_size_pretty(pg_relation_size('public.test_coll2'::regclass)) as table_size, 
pg_size_pretty(pg_total_relation_size('public.test_coll2'::regclass)) as total_table_size;

|cnt      |table_size|total_table_size|
|---------|----------|----------------|
|2,000,001|256 MB    |594 MB          |


select idx.tablename , idx.indexname, 
	pg_size_pretty(pg_relation_size((idx.schemaname|| '.' ||idx.indexname)::regclass)) as idx_size ,
	pg_relation_size((idx.schemaname|| '.' ||idx.indexname)::regclass) as idx_size_2 
from pg_indexes idx
where schemaname = 'public' and tablename ~ 'test_coll2';

|tablename |indexname                     |idx_size|idx_size_2 |
|----------|------------------------------|--------|-----------|
|test_coll2|test_coll2_txt_c_utf8_idx     |113 MB  |118,128,640|
|test_coll2|test_coll2_txt_en_us_utf8_idx |113 MB  |118,128,640|
|test_coll2|test_coll2_txt_ru_ru_x_icu_idx|113 MB  |118,128,640|


--set jit=on;	

-- Measure speed of text search on  Index Cond: (test_coll2.txt_c_utf8 = ANY (ids.id_arr))
-- Run query ~15 times until table and index cached and exec time stabilized. 

explain (analyze, costs, buffers, verbose)
with ids as 
(select array_agg(xx.id) as id_arr from 
     (select txt_C_utf8 as id from test_coll2 tablesample bernoulli (10) ORDER BY random() FETCH FIRST 100000 ROWS ONLY 
    ) as xx  
)
select max(txt_C_utf8), max(txt_en_US_utf8), max(txt_ru_RU_x_icu) from test_coll2, ids  
where txt_C_utf8 = any(ids.id_arr);        

-- The explain shows index performance like:
        ->  Index Scan using test_coll2_txt_c_utf8_idx on public.test_coll2  (cost=0.55..49.70 rows=10 width=99) (actual time=133.999..358.031 rows=100000 loops=1)
-- 358 ms index search timing
        
-- Same code, but use txt_en_US_utf8       
explain (analyze, costs, buffers, verbose)
with ids as 
(select array_agg(xx.id) as id_arr from 
     (select txt_en_US_utf8 as id from test_coll2 tablesample bernoulli (10) ORDER BY random() FETCH FIRST 100000 ROWS ONLY 
    ) as xx  
)
select max(txt_C_utf8), max(txt_en_US_utf8), max(txt_ru_RU_x_icu) from test_coll2, ids  
where txt_en_US_utf8 = any(ids.id_arr);  

        ->  Index Scan using test_coll2_txt_en_us_utf8_idx on public.test_coll2  (cost=0.55..49.70 rows=10 width=99) (actual time=1061.392..1979.241 rows=100000 loops=1)
-- 2000 ms   en_US_utf8 is approx. 4 times slower then C_utf8 !!!        

-- Same code, but use txt_ru_RU_x_icu        
explain (analyze, costs, buffers, verbose)
with ids as 
(select array_agg(xx.id) as id_arr from 
     (select txt_ru_RU_x_icu as id from test_coll2 tablesample bernoulli (10) ORDER BY random() FETCH FIRST 100000 ROWS ONLY 
    ) as xx  
)
select max(txt_C_utf8), max(txt_en_US_utf8), max(txt_ru_RU_x_icu) from test_coll2, ids  
where txt_ru_RU_x_icu = any(ids.id_arr);         

        ->  Index Scan using test_coll2_txt_ru_ru_x_icu_idx on public.test_coll2  (cost=0.55..49.70 rows=10 width=99) (actual time=213.286..495.744 rows=100000 loops=1)
        -->  Index Scan using test_coll2_txt_ru_ru_x_icu_idx on public.test_coll2  (cost=0.55..49.70 rows=10 width=99) (actual time=222.585..659.931 rows=100000 loops=1)
-- 495 ms , this is slower then C_utf8 because of ICU, but ~ 3 times better than en_US_utf8
 

--drop FUNCTION test_coll_loop_1(int); 
CREATE OR REPLACE FUNCTION test_coll_loop_1(num_runs INT DEFAULT 5)
RETURNS TABLE (
    array_size INT,
    txt_C_utf8_time numeric,
    txt_en_US_utf8_time numeric,
    txt_ru_RU_x_icu_time numeric,
    txt_C_utf8_sort_time numeric,
    txt_en_US_utf8_sort_time numeric,
    txt_ru_RU_x_icu_sort_time numeric    
) AS $$
DECLARE
    arr_size INT;
    start_time TIMESTAMP;
    end_time TIMESTAMP;
    arr_txt_C_utf8 TEXT[]       collate "C.utf8" ;
    arr_txt_en_US_utf8 TEXT[]   collate "en_US.utf8" ;
    arr_txt_ru_RU_x_icu TEXT[]  collate "ru-RU-x-icu" ;
    i INT;
    table_count INT8 := 0;
    table_min INT8 := 0;
    table_max INT8 := 0;
    txt_C_utf8_tmp      TEXT collate "C.utf8" ;
    txt_en_US_utf8_tmp  TEXT collate "en_US.utf8" ;
    txt_ru_RU_x_icu_tmp TEXT collate "ru-RU-x-icu" ;
    rec record; 
BEGIN
    RAISE NOTICE 'test_coll_loop_1 num_runs=%', num_runs;
    -- Fill test arrays
    SELECT 
        array_agg(txt_C_utf8),
        array_agg(txt_en_US_utf8),
        array_agg(txt_ru_RU_x_icu)
    INTO arr_txt_C_utf8,
        arr_txt_en_US_utf8,
        arr_txt_ru_RU_x_icu
    FROM (
        SELECT txt_C_utf8, txt_en_US_utf8, txt_ru_RU_x_icu
        FROM test_coll2 
        TABLESAMPLE BERNOULLI(2) 
        ORDER BY random() 
        FETCH FIRST 100000 ROWS ONLY
    ) AS xx;
    -- Temp table with results
    CREATE TEMP TABLE results (
        array_size INT,
        txt_C_utf8_time numeric,
        txt_en_US_utf8_time numeric,
        txt_ru_RU_x_icu_time numeric,
        txt_C_utf8_sort_time numeric,
        txt_en_US_utf8_sort_time numeric,
        txt_ru_RU_x_icu_sort_time numeric
    );
    FOR arr_size IN SELECT unnest(ARRAY[100000, 10000, 1000]) LOOP
        -- Инициализируем переменные для накопления результатов
        declare
            txt_C_utf8_total numeric := 0;
            txt_en_US_utf8_total numeric := 0;
            txt_ru_RU_x_icu_total numeric := 0;
            txt_C_utf8_sort_total numeric := 0;
            txt_en_US_utf8_sort_total numeric := 0;
            txt_ru_RU_x_icu_sort_total numeric := 0;
            rec_count int8 := 0;
            txt_xx text; 
        BEGIN
            -- Do several runs  
            FOR i IN 1..num_runs LOOP
		        raise notice 'arr_size=% run=%', arr_size, i;    
 -- txt_pg_c_utf8 Worm up cache:
		        foreach txt_C_utf8_tmp in array arr_txt_C_utf8[1:arr_size]
		        loop
		        	select * into rec from test_coll2 where txt_C_utf8 = txt_C_utf8_tmp;
		        end loop;
--              for rec in  
--			    select * from test_coll2 where txt_pg_c_utf8 = any(arr_txt_C_utf8[1:arr_size])
--			    loop
--		          rec_count := rec_count + 1;
--			    end loop;    	       
	            rec_count := 0;
                start_time := clock_timestamp();
		        foreach txt_C_utf8_tmp in array arr_txt_C_utf8[1:arr_size]
		        loop
		        	select * into rec from test_coll2 where txt_C_utf8 = txt_C_utf8_tmp;
		        end loop;             
                end_time := clock_timestamp();
                txt_C_utf8_total := txt_C_utf8_total +  round(EXTRACT(EPOCH FROM (end_time - start_time)) * 1E6); -- Time in microseconds 
 -- txt_en_US_utf8 Worm up cache:
   		        foreach txt_en_US_utf8_tmp in array arr_txt_en_US_utf8[1:arr_size]
		        loop
		        	select * into rec from test_coll2 where txt_en_US_utf8 = txt_en_US_utf8_tmp;
		        end loop;
	            rec_count := 0;
                start_time := clock_timestamp();
   		        foreach txt_en_US_utf8_tmp in array arr_txt_en_US_utf8[1:arr_size]
		        loop
		        	select * into rec from test_coll2 where txt_en_US_utf8 = txt_en_US_utf8_tmp;
		        end loop;          
                end_time := clock_timestamp();
                txt_en_US_utf8_total := txt_en_US_utf8_total +  round(EXTRACT(EPOCH FROM (end_time - start_time)) * 1E6); -- Time in microseconds 
 -- txt_ru_RU_x_icu Worm up cache:
   		        foreach txt_ru_RU_x_icu_tmp in array arr_txt_ru_RU_x_icu[1:arr_size]
		        loop
		        	select * into rec from test_coll2 where txt_ru_RU_x_icu = txt_ru_RU_x_icu_tmp;
		        end loop;               
	            rec_count := 0;
                start_time := clock_timestamp();
   		        foreach txt_ru_RU_x_icu_tmp in array arr_txt_ru_RU_x_icu[1:arr_size]
		        loop
		        	select * into rec from test_coll2 where txt_ru_RU_x_icu = txt_ru_RU_x_icu_tmp;
		        end loop;    
                end_time := clock_timestamp();
                txt_ru_RU_x_icu_total := txt_ru_RU_x_icu_total +  round(EXTRACT(EPOCH FROM (end_time - start_time)) * 1E6); -- Time in microseconds 
-- Sorting tests  :               
                start_time := clock_timestamp();
                select max(xx) into txt_xx FROM unnest(arr_txt_C_utf8[1:arr_size]) as xx ORDER BY 1;
                end_time := clock_timestamp();             
                txt_C_utf8_sort_total := txt_C_utf8_sort_total + EXTRACT(EPOCH FROM (end_time - start_time)) * 1E6;
--
               start_time := clock_timestamp();
                select max(xx) into txt_xx FROM unnest(arr_txt_en_US_utf8[1:arr_size]) as xx ORDER BY 1;
                end_time := clock_timestamp();             
                txt_en_US_utf8_sort_total := txt_en_US_utf8_sort_total + EXTRACT(EPOCH FROM (end_time - start_time)) * 1E6;
--
                start_time := clock_timestamp();
                select max(xx) into txt_xx FROM unnest(arr_txt_ru_RU_x_icu[1:arr_size]) as xx ORDER BY 1;
                end_time := clock_timestamp();             
                txt_ru_RU_x_icu_sort_total := txt_ru_RU_x_icu_sort_total + EXTRACT(EPOCH FROM (end_time - start_time)) * 1E6;
            END LOOP;
            -- save average values
            INSERT INTO results VALUES (
                arr_size,
                round(txt_C_utf8_total / num_runs, 0),
                round(txt_en_US_utf8_total / num_runs, 0),
                round(txt_ru_RU_x_icu_total / num_runs, 0),
                round(txt_C_utf8_sort_total / num_runs, 0),
                round(txt_en_US_utf8_sort_total / num_runs, 0),
                round(txt_ru_RU_x_icu_sort_total / num_runs, 0)
            );
        end; 
    END LOOP;
    -- Возвращаем результаты
    RETURN QUERY SELECT * FROM results;
    -- Удаляем временную таблицу
    DROP TABLE results;
END;
$$ LANGUAGE plpgsql;
        
select * from test_coll_loop_1(5);        

PG17:
|array_size|txt_c_utf8_time|txt_en_us_utf8_time|txt_ru_ru_x_icu_time|txt_c_utf8_sort_time|txt_en_us_utf8_sort_time|txt_ru_ru_x_icu_sort_time|
|----------|---------------|-------------------|--------------------|--------------------|------------------------|-------------------------|
|100,000   |236,537        |751,191            |280,172             |7,475               |25,583                  |8,859                    |
|10,000    |55,348         |181,438            |67,831              |1,885               |6,371                   |2,220                    |
|1,000     |4,643          |16,695             |5,664               |262                 |686                     |266                      |

|array_size|txt_c_utf8_time|txt_en_us_utf8_time|txt_ru_ru_x_icu_time|txt_c_utf8_sort_time|txt_en_us_utf8_sort_time|txt_ru_ru_x_icu_sort_time|
|----------|---------------|-------------------|--------------------|--------------------|------------------------|-------------------------|
|100,000   |232,102        |724,397            |275,933             |7,129               |25,066                  |8,609                    |
|10,000    |55,811         |180,333            |67,036              |1,864               |6,378                   |2,209                    |
|1,000     |4,665          |16,726             |5,613               |248                 |684                     |266                      |



PG15:
|array_size|txt_c_utf8_time|txt_en_us_utf8_time|txt_ru_ru_x_icu_time|txt_c_utf8_sort_time|txt_en_us_utf8_sort_time|txt_ru_ru_x_icu_sort_time|
|----------|---------------|-------------------|--------------------|--------------------|------------------------|-------------------------|
|100,000   |233,928        |717,514            |286,568             |7,319               |25,074                  |8,864                    |
|10,000    |55,941         |181,211            |69,827              |1,871               |6,363                   |2,243                    |
|1,000     |4,683          |16,969             |5,837               |247                 |687                     |269                      |

|array_size|txt_c_utf8_time|txt_en_us_utf8_time|txt_ru_ru_x_icu_time|txt_c_utf8_sort_time|txt_en_us_utf8_sort_time|txt_ru_ru_x_icu_sort_time|
|----------|---------------|-------------------|--------------------|--------------------|------------------------|-------------------------|
|100,000   |238,141        |729,475            |292,190             |7,094               |25,282                  |8,905                    |
|10,000    |56,204         |181,163            |70,090              |1,836               |6,353                   |2,245                    |
|1,000     |4,692          |16,775             |5,880               |245                 |690                     |268                      |
