Re: How to get the size of JSONB in bytes?

Поиск
Список
Период
Сортировка
От Petr Korobeinikov
Тема Re: How to get the size of JSONB in bytes?
Дата
Msg-id CAJL5ff-emqZCzOodvjX3uQ97JhSs1hQTykaeCU_R8GRyfAhhLw@mail.gmail.com
обсуждение исходный текст
Ответ на How to get the size of JSONB in bytes?  (Dmitry Savenko <ds@dsavenko.com>)
Ответы Re: How to get the size of JSONB in bytes?  (Jim Nasby <Jim.Nasby@BlueTreble.com>)
Список pgsql-general
This doesn't work because it can't cast JSONB to 'bytea'. I tried casting to 'text', still no luck. Could anyone please help me?

You can use check-constraint like this:

# create table t (
    jb jsonb
);
# alter table t add constraint jb_length_check CHECK (length(jb::text) < 16); -- 16 is example value
# insert into t values ('{"key":"v"}');
INSERT 0 1
# insert into t values ('{"key":"value"}');
ERROR:  new row for relation "t" violates check constraint "jb_length_check"
DETAIL:  Failing row contains ({"key": "value"}).
 
Also, is there a better approach to impose a size limit, then writing triggers? I need it to be fairly flexible, e.g. 10Kb is not a constant, it may even be different for different documents (rows) in the same table.

The better approach is extract your length-validation logic into your application.

В списке pgsql-general по дате отправления:

Предыдущее
От: Dmitry Savenko
Дата:
Сообщение: How to get the size of JSONB in bytes?
Следующее
От: Craig Ringer
Дата:
Сообщение: Re: BDR