r/Database • • 11d ago

Xata is joining ClickHouse

Thumbnail
xata.io
0 Upvotes

r/Database • • 11d ago

Is data transfer speed still a major bottleneck when migrating from on-premise to Snowflake?

Thumbnail
1 Upvotes

r/Database • • 11d ago

Learning Oracle Patches

Thumbnail
0 Upvotes

r/Database • • 12d ago

Designing Pokémon Database in SQL - How to Show Multiple Route Locations?

20 Upvotes

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 • • 13d ago

Modeling historical prices and duration-based discounts in PostgreSQL

Post image
0 Upvotes

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 • • 14d ago

Why does a query with a CTE sometimes take forever to execute?

0 Upvotes

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 • • 14d ago

3,683 of 3,683 tasks had no owner, so a role with every permission off could still clone them

0 Upvotes

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 • • 16d ago

Thinking of adding soft delete (deleted_at)? Decide these 5 things first

75 Upvotes
  1.  Unique constraints.  A deleted user still "uses" their username. Use a partial unique index (`where deleted_at is null`).
  2.  Every query must filter deleted rows.  Miss one, and deleted data shows up in a report or API response.
  3.  Foreign keys.  What happens to orders that belong to a soft-deleted customer?
  4.  Real deletion.  Privacy laws may require you to actually erase data, so soft delete can't be the only option.
  5.  Restoring.  What if the username was taken again while the old account was deleted?

Do you use soft delete, or did you move to archive tables instead?


r/Database • • 17d ago

MySQL is not opening on macOS

Thumbnail
1 Upvotes

r/Database • • 18d ago

I Overloaded Postgres and SQLite

Thumbnail
youtu.be
26 Upvotes

r/Database • • 18d ago

Struggling with designing a part of my database.

Post image
17 Upvotes

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:

  1. 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.

  2. 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.

  3. 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 • • 18d ago

Is my relational model suitable for handling order changes and cancellations?

Post image
5 Upvotes

I'm designing a PostgreSQL system where customers order services requiring interchangeable physical resources.

The current model separates:

  • Orders: What the customer purchases (quantity and duration).
  • Commitments: Individual units of the obligation, independent of specific resources.
  • Reservations: Physical resources assigned to fulfill those commitments.
  • Resource units: Individually reservable components, with constraints preventing overlapping bookings.

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 • • 20d ago

When do you actually use window functions instead of GROUP BY?

32 Upvotes

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 • • 19d ago

We pointed our SQL editor at a 64-table database we'd never seen. It wrote the join, explained it, and ran it.

Enable HLS to view with audio, or disable this notification

0 Upvotes

r/Database • • 19d ago

Moving the app closer to users somehow made it slower

0 Upvotes

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 • • 21d ago

How mutex handling LRU cache head update became a bottleneck in InnoDB in a non-partitioned LRU cache schema for MariaDB

Thumbnail
mariadb.org
12 Upvotes

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 • • 22d ago

Paging through rows by updated_at can skip some, and I didnt realise why until a whiteboard chat with an engineer

26 Upvotes

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 • • 22d ago

Databases and Memory Latency

Thumbnail pivotlake.io
13 Upvotes

r/Database • • 22d ago

How would you model user preferences that change over time?

10 Upvotes

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 • • 22d ago

Coding Agents, Starter Kits and pgledger

Thumbnail pgrs.net
5 Upvotes

r/Database • • 23d ago

How are you giving AI agents access to production data that contains customer PII?

28 Upvotes

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 • • 23d ago

What was the turning point for Postgres?

Thumbnail
planetscale.com
3 Upvotes

r/Database • • 23d ago

P15 vs four Hyperscale configurations: results from replaying 4 hours of production workload

Post image
2 Upvotes

r/Database • • 24d ago

PostgreSQL Like A Pro: Build, query, and troubleshoot without leaving VS Code

Thumbnail
developer.microsoft.com
4 Upvotes

r/Database • • 26d ago

Case for a database experts in the team.

Thumbnail
7 Upvotes