PostgreSQL Best Practices
speakdatascience.com
speakdatascience.com
So a simple filter in the sense of "omit anything too similar to X" would just omit the mean result within your given deviation. It's effectively asking, "What are some truly insane ways to use PostgreSQL", which is an interesting thought experiment, though if it actually produces useful results then you've basically just written a unit-test (Domain test?) for when AI slop evolves into full on AI jumping the shark.
If you're doing it based on cross-linking (source-citing), you're basically doing Page-Rank for AI.
If you time gate familiarity to posts only up to the NLP/General AI explosion in 2022 or so, well that might still be useful today, but for how long?
If you were to write a "Smart" filter, you're basically just writing the "PostgreSQL Best Practices" article yourself, but writing it for machines instead of humans. And I don't know what to make of that, but frankly I was lead to believe that if nothing else, the robopocalypse would be more interesting than this.
Recommended by whom, based on what?
Everything in this article seems to be blogspam, with zero links, sources, or rationales for the decisions provided.
Re partial indexes - those are great, though you have to be careful to keep them updated as your queries evolve. E.g. if you have a partial index on a set of enum values, you should make sure to check and potentially update it whenever you add a new enum variant.
create table post_vote (
post_id bigint not null references posts (id),
user_id bigint not null references users (id),
amount smallint not null,
primary key (post_id, user_id) include (amount)
);
select sum(amount) from post_vote where post_id = :'post_id';
Upvotes are 1, downvotes are -1. This will be a nearly instantaneous index-only scan summing the amount values.I just wanted to point out that you really have to think about these types of optimizations because they might make one query faster but slow everything else down to the point where you are spending more time overall and using more storage.
PG always does a delete + create for updates. It will have no additional overhead at all
I suppose additional columns could be slower to update too (more data structure traversal), but I'm guessing not by nearly as much as an additional index.
It's a very confused article that (IMO) is AI Slop.
1. Naming conventions is a weird one to start with, but okay. For the most part you don't need to worry about this with PKs, FKs, Indices etc. Those PG will automatically generate for you with the correct syntax.
2. Performance optimization, yes indices are create. But don't name them manually let PG name them for you. Also, where possible _always_ create the index concurrently. This does not lock the table. Important if you have any kind of scale.
3. Security bit of a weird jump as you've gone from app tier concern to PG management concerns. But I would say RLS isn't worth it. And the best security is going to be tightly control what can read and write. Point your reads to a read only replica.
4. pg_dump was never meant to be a backup method; see https://www.postgresql.org/message-id/flat/70b48475-7706-426...
5. You don't need to schedule VACUUM or ANALYZE. PG has the AUTO version of both.
6. Jesus, never use a DEFAULT value on a large table that causes a table rewrite which will cause you downtime. Don't use varchar(n) https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use...
This is an awful article.
If you care about catching stuff like unique constraint violations in your code then you should know what the indexes are named.
Always name your indexes and constraints. They're part of your code. How would you catch an exception that you don't know the name of?
https://www.postgresql.org/docs/current/errcodes-appendix.ht...
That is much more flexible than hard coding the name.
Always name your indexes. Always name your constraints.
> if you have that many violations that points to other problems
No, it doesn't. This is a sophisticated design choice. You might be out of your depth here.
Those are very recent emails that don't say anything about what it was originally meant for, just that nowadays there's better options.
Though really the part they quote called it a backup tool, that does imply it was originally meant to be a backup method.
The article is objectively bad; the subject is clearly something a lot of people think about, myself included.
I'll typically use a "vw_" prefix prior to a name which would be similar to table naming. "v_" is also an option, but I also write a lot of DB functions and stored procedures and with all the types, tables, and column names that get used, indicating variables and parameters can be helpful... so "v_" indicates a variable name in the function context which is why views get "vw_".
I generally find Hungarian notation silly, however, there's a compelling argument to have them in functions and procedures — perhaps more than for views. The type of the variable matters just as much as where/how it was declared, such as in/out params.
That's the 'steelman' argument for them, as I understand it.
On the flip side... perhaps using techniques from the 80s with a programming language and paradigm also from the 80s... and that hasn't changed all that much... isn't all that crazy.
At the end of the day: the evaluation of a given technique such as I described, should be measured against if it makes the development experience simpler and more comprehensible. Being able to instantly tell if a given reference in a procedural query comes from a table a view, a variable, or a parameter, I find helpful since we're typically using many names from these different origins and contexts and frequently side-by-side, such as in a complex query driving the procedural code.
Of course, If you don't like that, or simply don't find it sufficiently "modern" or "fashionable" (my God, what might others think!?)... again, I invite you to do you.
I think consistency is more important than perfection. If you use something like vw_ as recommended in the sibling comment, that's fine, then try to apply it to all views. (Without being overly strict; sometimes there are good reasons to defy conventions. They're conventions, not laws!)
Just keep in mind that a view is both code and a data contract just like a table definition is. All the usual best practices around versioning, automated deployments, and smooth upgrade paths apply. As soon as an application or downstream view, function, etc relies on that view, changing it risks disruption or breakage. Loud breakage if you're lucky, silent data corruption if you're not!
Thinking about the most general norms across database vendors, consider a table name for User Posts: You could write that as UserPosts, but in a lot of databases you're going to end up with userposts, since typically SQL and identifiers are naturally case insensitive; you can preserve case by quoting that "UserPosts", but now you have extra characters just to get the case sensitivity. Finally, you can use snake case: user_posts... true you still end up with extra characters, but ultimately I think a little less shifting. Snake case also keeps you in line with much historical precedence in writing names for the database as well.
SELECT * FROM "MyTable"
You can even use reserved SQL keywords in table/column names as long as they are double-quoted.Alternatively, you can use camelCase without quotes in your SQL, and ignore the fact that Postgres will lowercase it.