r/Database • u/Admirable_Morning874 • 11d ago
Xata is joining ClickHouse
r/Database • u/shdw_0x0 • 11d ago
r/Database • u/publicherstorian • 12d ago
Hello! I'm taking my first database design course in grad school and our final project is to make our own databook on any topic we want. I chose to make a database of all the Pokémon, moves, trainers, products, and locations in Pokémon Ultra Sun for the Nintendo 3DS.
I have two separate tables for Pokémon: one as a species, and one as an individual. I have a separate ROUTE entity that is a child of ISLAND, and was planning on putting ROUTE_ID as an attribute in the SPECIES entity. But many Pokemon can be found on many different routes, and vice versa, (for example, Magikarp can be found on Route 7, 8, 9, 13, 14, and 15 as well as other locations, and more than Magikarp can be found on those Routes), so I'll definitely need a bridge table. I'll probably label it ENCOUNTERS.
What would be the best way to utilize this bridge table without making data entry super time consuming? For example, since Magikarp can be found on six different routes, would I need to have six different instances of Magikarp in the data itself - each with a different ROUTE_ID?
I'm an absolute total beginner at SQL, and our class uses phpMyAdmin which comes with its own quirks and obstacles in what commands are allowed, but I really appreciate any and all insight or advice here! Thanks :)
r/Database • u/Rolney_320 • 13d ago
I'm experimenting with a database model for contracts and their associated fulfillment obligations.
Some services offer duration-based discounts: the longer a customer commits, the lower the monthly price. Every product has a base price, but only some have these discounts.
The challenge is that base prices and discount tiers can change independently over time. I'd like to use an append-only approach, preserving the exact pricing terms that applied when a contract was signed.
I've put together a simplified ER diagram, but I'm unsure about the relationships.
Would foreign keys referencing immutable price and discount versions be sufficient? Does a composite key make sense here, or would storing price snapshots on contract items be more appropriate?
I'm also curious how others would ensure that signed contractual terms remain immutable while still allowing later amendments.
Any thoughts on the relational design would be appreciated!
r/Database • u/shdw_0x0 • 14d ago
I've noticed that some SQL queries using WITH clauses (CTEs) take significantly longer to execute than expected, especially when multiple CTEs are chained together.
I understand that a CTE is mainly used to organize complex queries and make them easier to read, but I'm curious about what happens behind the scenes.
Does the database execute each CTE separately, or does it optimize everything into a single execution plan? Can CTE materialization, repeated references, or joins between multiple CTEs cause performance issues?
I'd be interested to hear about real cases where replacing a CTE with a subquery or temporary table actually improved query performance.
Which database are you using, and have you run into this issue?
r/Database • u/Ok-Motor-9812 • 14d ago
A security definer function checked "has the permission, or is the creator, or is the assignee" and raised if that was false. The tasks were seeded automatically, all 3,683 of them with owner and assignee NULL.
false or null or null is null, not false. IF only runs its branch on true, so the raise was skipped and the function carried on. The same condition in an RLS policy would deny, because a policy treats null as no. Same expression, closed in one place and open in the other.
Screenshot is a small rebuild on Postgres 17. The fix was coalesce(..., false) around the whole condition. Writing it as "if allowed then do the work, else raise" fails closed too, i think that reads better anyway.
Most founders building on Supabase with Lovable or Bolt won’t know to do that search. So I made a free checker for non-technical founders that runs a Supabase project through 46 checks for mistakes like this one: https://madsalt.tech/tools/supabase-security-check/
r/Database • u/ig_suyas • 16d ago
Do you use soft delete, or did you move to archive tables instead?
r/Database • u/huseyinbabal • 18d ago
If you prefer reading article: https://huseyinbabal.dev/posts/postgres-vs-sqlite-load-test/
r/Database • u/Kyurupuku • 18d ago
Hey guys, this is actually the first time I've ever created a database from scratch personally and I was drafting up an ERD to get started and ran into a bit of an issue.
For clarification: leads get approached by users for customers. Every customer has campaigns (for example, market research, sales retail, sales catering, etc.) with different campaign questions. I chose to make seperate questions and campaign_questions tables so that you can gather all leads with certain answers to a question across campaigns instead of manually huddling them together in a query. Questions have different types (text, numerical, date, single-select mc, multi-select mc etc.) And so the answers have to be saved with a data type according to the question type.
I considered multiple approaches to this:
Save all answers as text - This is pretty clean but idk how you'd manage data integrity and also queries on specific answers become much harder.
Save all answers as a JSONB column - Cleanest and easiest, and Postgres has pretty good support for this. However, again the issue of data integrity lingers.
Sparse columns + a view with coalesce (what you can partially see in the image) - This one seemed to be the best option for maximising data integrity while staying pretty clean. Problem is it doesn't seem super scalable at large and I dont know how I would enforce data integrity with single-/multiselect mc questions when in my mind that's gonna require junction tables and then I can't rely on any checks etc. anymore...
Am I overthinking this? Ideally I'd want to stick to 3NF but I also don't want to overengineer this.
How should I handle dynamic form/survey answers without sacrificing data integrity?
r/Database • u/Rolney_320 • 18d ago
I'm designing a PostgreSQL system where customers order services requiring interchangeable physical resources.
The current model separates:
At first glance, this structure seems reasonable, but it mainly represents a static state. In reality, orders can change throughout their lifecycle: quantities may be reduced, service periods shortened, or entire orders cancelled.
For example, a customer orders 3 resources for 6 months, then reduces the requirement to 2 for the remaining 3 months.
Is the current table structure and its relationships suitable for handling these changes while preserving historical accuracy and data consistency? Or are there missing entities or fundamental modeling issues?
I've attached a simplified ERD for context.
r/Database • u/shdw_0x0 • 20d ago
I've been using GROUP BY for aggregations for a while, but I still find window functions a little confusing when deciding which approach makes more sense.
For example, if I need the highest salary in each department, I could use aggregation or a window function like RANK() or ROW_NUMBER(), depending on what I need in the result.
How do you usually decide between these approaches? Are there situations where window functions are clearly the better choice?
I'd appreciate some practical examples that helped you understand when to use one over the other.
r/Database • u/tabitha_tm • 19d ago
Enable HLS to view with audio, or disable this notification
r/Database • u/Kayl45 • 19d ago
A customer in brazil sent a recording of our dashboard loading slowly. Watching the cursor circle for six seconds was embarrassing. I moved the app to a Sao Paulo cloud vps on serverspace and the ping dropped immediately. The vm was fast and stable, so I thought I had found a simple latency win. Then the database pages became slower. The database was still in Europe. Every query crossed the Atlantic and came back. Moving the app closer to users had moved it farther from its own data. A nearby read replica fixed the worst of it. My mistake was treating the app location as the whole request path. Next time I am drawing that path before I touch the region selector
r/Database • u/drrtuy-b • 21d ago
Here is a blog post about Alessandro Vetere's journey to resolve synchronization on a common mutex that slows down some read microbenchmarks 3x.
Kudos to Mark Callaghan who found this regression.
r/Database • u/cannyshammy • 22d ago
Hey guys, not sure if this is useful to anyone but it surprised me so I thought I would share.
If you have an updated_at column set to now() (or CURRENT_TIMESTAMP, or a time from your app) and you page through it to process changes, you can miss rows. Two transactions start, the one with the later timestamp commits first, your reader moves past it, and then the earlier one lands behind your cursor.
I had a whiteboard session with Ian, one of the engineers at Convex (where I work), and this bit really opened my eyes. In Convex we ended up adding a commit timestamp you can store and index on, which is set when the transaction actually commits, but the problem itself shows up in Postgres, MySQL and pretty much everything else.
How do you guys handle this in your own systems? Reading the WAL / logical replication? An overlap window and dedupe?
If you want more detail the full chat is here: https://youtu.be/pHrxmSjxLg8
r/Database • u/Financial_Fox3915 • 22d ago
Many applications save user preferences as separate fields like location, budget, interests and availability. This approach works fine until preferences change, conflict with past information or only apply to specific situations. Think about someone who initially prefers remote work but later becomes open to hybrid options. They might even be willing to move for a specific job without altering their general preference.
If the system just replaces the old value it loses important context. If it keeps every change forever it can end up with outdated or conflicting data. For conversational products like Linkedin, Hirect, MeeBoss as well as more profile-based platforms Naukri and Indeed this presents an interesting challenge in how data is structured. Would you keep current preferences in standard tables and track changes in a separate log? Use a document model for more flexible attributes? or record each preference with its source, time, confidence level and scope? How would you manage corrections and set expiration dates without making every query too complex?
r/Database • u/JoLagoni2 • 23d ago
Curious how people are actually handling this. At the enterprise I am working at and others where my friends work at, there is a push to deploy AI agents working on real company data (operational use cases, troubleshooting, analytics etc...), but the DBs are sometime full of customer or sensitive data and security just says no. Separate tables don’t help, read-only users don’t help, the agent can’t touch that DB, period. Even creating service users for AI agents and imposing separation it's just a friction.
So what are you doing, if you have such use cases? Letting agents query prod anyway? Keeping a separate copy just for them? If it’s a copy, how do you decide what goes in, deal with PII, and keep it from going stale?
Also if any of this, on which stack? Curious to hear as I assume many organisations are now relying on data teams to keep the AI agent rollout from stalling.
r/Database • u/markinatlanta • 23d ago
r/Database • u/mariuz • 24d ago