2

I am using Django with a Postgres backend.

In the Django models, I have to set the max_length attribute on the Charfields, then the Django ORM deals with the database in the backend. In a lot of cases, those are rather small fields (e.g. say names, or city names etc.) that I don't expect would be very long. That application deals with existing data that isn't particularly cleaned. So I don't want to set something like

name = models.Charfield(max_length=50, ...)

Just to realize that someone triple wrote his name in that field and that the field is 75 characters long. On the other hand, I don't want to set huge values and needlessly increase database size just to save me a little bit of trouble.

My current thinking is that Postgres probably reserves some minimal amount of space (e.g. likely that max_length=10 and max_length=15 both take the same storage), so I would set the max_length to that minimal size (or low multiple thereof).

What do you recommend and why?

5
  • VARCHAR values only occupy as much space as the actual strings they contain (plus length, typically two bytes). So, if Django maps Charfield to VARCHAR, no space is wasted. Also, this.
    – mustaccio
    Commented Nov 1, 2021 at 18:02
  • Okay, but I still have the same question about similar test fields in django which may or not map to Varchad( perhaps to Text, or Char....). Commented Nov 1, 2021 at 18:06
  • Interesting reading btw - but yeah, part of my thinking is that I don't want to be too restrictive. But at the same time not want to waste storage either. Commented Nov 1, 2021 at 18:08
  • 1
    @logicOnAbstractions: you are not wasting storage if you use varchar or text - Postgres only stores as many characters as you put into the columns. You should see the length limit as a business constraint, rather than a technical restriction.
    – user1822
    Commented Nov 1, 2021 at 18:13
  • Only use a length restriction to restrict the maximum number of characters - if you need that. It has no bearing whatsoever on storage size. If there is no need to restrict maximum length, just use text. See: dba.stackexchange.com/q/125499/3684, dba.stackexchange.com/a/21496/3684, dba.stackexchange.com/a/89433/3684 Commented Nov 1, 2021 at 23:44

1 Answer 1

4

If you have no length limit imposed by the application logic, use text rather than varchar. This is better than imposing an arbitrary length limit.

text and varchar are the same under the hood in PostgreSQL, and a string will only occupy as much space as it actually needs. You won't lose performance.

1
  • From this & the comments, I realized that I hadn't really given much thoughts about the difference between varchar vs text types (from a db perspective). Partly because the source application I'm migrating from only uses a single type. So yes, I think you are right, I need to ask myself the question on a specific field, I probably just really want to be using a text, not varchar. Thanks for the background on the inner workings - that's what I was fuzzy about, and couldn't find that in the docs. Commented Nov 2, 2021 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.