marcin

marcin

Any way to limit an Ecto map / PostgreSQL JSONB size?

Hello.
I’m using a PostgreSQL-backed Ecto schema with a field :fields, :map, default: %{} to store some custom fields. I’d like to limit the size of this map, to have some control over the storage size of this table.
I am not sure where to add this check in an elegant way:

  1. The map is deserialized by a GraphQL/Absinthe custom scalar deserializer. I could check for size limit here, but I would have to return just :error when it’s exceeded, the same as in case of parse error. To the API user this would not be kind - start showing deserialize error after input size crosses a threshold. This approach would also mean i have to define one limit for all JSONBs, regardless of their use.
  2. In resolver, the input is already parsed into a map. That map ends up in changeset - to check it’s size I would have to serialize it to JSON just to check the length of the string, which seems like a waste of CPU cycles.
    3.. In the DB there is no way I am aware of to limit the size of JSONB column.

Any tips on how to overcome this?

First 4 of 4 Posts Switch mode

kip

kip

ex_cldr Core Team

I would implement this as a constraint in the database. Then your application code doesn’t have to do anything. For example:

CREATE TABLE products (
    my_json jsonb CONSTRAINT max_jsonb_size CHECK (pg_column_size(my_json) <= 1024)
);
outlog

outlog

isn’t something like this needed on the changeset? (haven’t kept close track of ecto)

|> check_constraint(:my_json, name: :max_jsonb_size)

kip

kip

ex_cldr Core Team

The check_constraint/3 documentation suggests that its role is to convert the database exception into a changeset error. So I suspect your guidance is spot on.

I also didn’t realise that there is migration support for constraints either. Using my example above it could be:

create constraint("products", :max_jsonb_size, check: "pg_column_size(my_json) <= 1024")
marcin

marcin

Perfect!
I found some stack overflow where they did this with a TRIGGER, and was discouraged as that looked complex. CONSTRAINT CHECK is the proper solution I was looking for. Thanks!

— All posts loaded —

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement