TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Postgres: Boundless `Text` and Back Again

99 pointsby timfover 3 years ago

12 comments

radiowaveover 3 years ago
If you&#x27;re at all likely to be defining views that contain any of these columns, I would strongly advocate using text with the check constraint, rather than varchar. The reason being: if you ever need to increase the column&#x27;s maximum length, in the case of varchar you will be prohibited from making this change because it violates the data type dependency between the table and the view.<p>To cope with this, in the same transaction that you&#x27;re increasing the length of the varchar, you will also need to drop and recreate all such dependent views (and any other views that depend upon <i>those</i> views, etc).<p>This is not fun.<p>In comparison: increase the maximum length of the check constraint, job done.
评论 #28487609 未加载
the_dukeover 3 years ago
The way I like to solve this is to still define the columns as `TEXT`, but just add a custom <i>named</i> CHECK constraint that validates the maximum length.<p>Eg: `ALTER TABLE users ADD CONSTRAINT username_max_len CHECK LENGTH(username) &lt;= 100 NOT VALID;`<p>The NOT_VALID option skips checking the existing data, so the migration is quasi-instant.<p>That way the constraint can easily be swapped out by removing the old one and adding a new one, and you have maximum flexibility going forward.<p>This also doesn&#x27;t require an index rebuild, and better models what&#x27;s actually happening internally.
评论 #28485582 未加载
jphover 3 years ago
We had similar experiences, and now use varchar 99, varchar 999, varchar 9999. The purpose of the &quot;9&quot; digits is to highlight that it&#x27;s our plausible guesstimate for &quot;good enough&quot; and can be increased later, rather than any definite unchangeable limit.
评论 #28485534 未加载
perlgeekover 3 years ago
When thinking about length limits, always try to come up with use cases for the data that limit the length.<p>If you have a legal name, you might want to write that person a letter (an invoice, injunction, notification of breach, whatever), so it must fit into the address field. If you allow a 2000 character name, it likely <i>won&#x27;t</i> fit. Same for street names and the likes.<p>Not fitting a full name into a field sucks, but at least it gives the one filling it the choice how to shorten it.<p>If the data goes into some kind of webpage, does it even render properly if it&#x27;s very large? If it goes into a hover text, or in a box that&#x27;s too small for a proper scroll bar, it becomes unreadable it too big.<p>If text becomes <i>really</i> big (and it might be gigabytes if there&#x27;s no limit), storage cost becomes an issue.<p>From this perspective, you basically never want unlimited TEXT fields anywhere, at least not exposed to customers. Maybe it&#x27;s OK for internal some internal, at least if you can yell at your users if they abuse it too much.
评论 #28488136 未加载
jonplackettover 3 years ago
I recently discovered the joy of PostgreSQL via Supabase.io (which is very awesome too, anyone else using it?)<p>The text thing is awesome but I never thought I could be so excited about Policies. It makes everything feel so much more relaxing knowing you can’t accidentally give someone access to something they shouldn’t be able to edit.
hermanradtkeover 3 years ago
I am on this journey as well. We are using constraints that check the length of a text field instead of using varchar. I like the idea of setting a few standard lengths though.<p>A word of caution when using domains: many SaaS offerings that replicate your Postgres database to a data warehouse do not support domains.
评论 #28485262 未加载
tyingqover 3 years ago
&gt;varchar(200) for shorter-length strings like names, addresses, email addresses, etc...The idea is to pick liberal numbers that are easily long enough<p>I&#x27;m not convinced 200 is liberal enough for some of that.<p>For email, a 64 character local part is RFC compliant, then 1 for the &#x27;@&#x27;, and the domain can be 253 characters.<p>Probably similar edge cases for addresses.
评论 #28485947 未加载
评论 #28488168 未加载
评论 #28486602 未加载
评论 #28485502 未加载
espadrineover 3 years ago
I agree with the “order of magnitude” text size limit. I tend to put 10x of what I would consider the expected average. A bit like cryptographers put 2x the number of rounds they think may be eventually broken.<p>Why should the database be the input sanitizer, though? Intermediary systems can break or degrade before it reaches PostgreSQL.
评论 #28486131 未加载
tuatoruover 3 years ago
Chesterton&#x27;s Fence strikes again.<p>It&#x27;s almost like those people back in the 60s and 70s had thought about this.
actuallyalysover 3 years ago
I thought of the DOMAIN solution as I was reading the article (although I didn&#x27;t know DOMAIN was the right mechanism), but the naming convention I had in mind mimicked Rust&#x2F;stdint and put the character limit in the name—something like varchar200 or char200.
faqinghereover 3 years ago
I believe that Postgres, following the SQL standard, silently truncates longer strings when using varchar(n), which is probably not a desirable result if you&#x27;re using it for validation.
评论 #28490456 未加载
评论 #28488824 未加载
iammiscover 3 years ago
My goodness... This has nothing to do with postures and just bad security models when programming.
评论 #28485540 未加载