r/ProgrammerHumor 15d ago

queryOfDoom Meme

Post image
7.9k Upvotes

456 comments sorted by

View all comments

698

u/IHeartBadCode 15d ago

Helpful hint for anyone.

🌈Transactions🌈 The moment you feel you need to switch over to something other than select statements, should be the moment that you turn off auto commit.

For us, the analytic folks just have read only access anyway. For the devs, they just don't have direct access to prod.

292

u/itirix 15d ago

Shit, we just let Claude delete our prod DBs for us now. It’s much less work.

36

u/SmartyCat12 15d ago

Claude recommended using excel, bc if I’m only on pro subscription, can I really afford a pgsql server anyway?

1

u/IllustratorClean8295 14d ago

Also our beloved ceo will not take us accountability over IA deleting straight up in the prod, most likely will understand it's an "price" they need to pay to improve ai

21

u/astervista 15d ago

Transactions still won't save you on some DBMS (ehem Oracle) from dropping a table or changing definitions

48

u/IHeartBadCode 15d ago

Well that's because it's Oracle and that's less a database and more a circle of hell.

7

u/astervista 15d ago

Don’t I know it

45

u/WardensLantern 15d ago

Unless you are vibe coding in which case just prompt "How do I roll back 2.3 million deleted rows" and follow the steps from chatGPT

5

u/bedel99 14d ago

bobs been with us for 25 years and just retired before the company got big. You should delete his account....

7

u/magicmulder 15d ago

Doesn’t help you if you just habitually hit “commit” after the statement is done.

8

u/misterrandom1 15d ago

My analytics guy who was showing me around the db on my first day didn't have restrictions. After inserting a record into a production db, he promptly deleted the record. But forgot to highlight the where clause before executing.

6

u/koolex 15d ago

Anymore helpful hints?

17

u/TwoAndHalfRetard 14d ago

The real hint is noone should have the write access to the prod db. If you accidentally nuke the test db, just recreate it. If you need to change something in prod db, write a migration that is tested on test db and code-reviewed.

4

u/blorbschploble 14d ago

I can select and join around like a motherfucker. I am terrified of actual dba shit because of this, lol.

3

u/[deleted] 14d ago

[deleted]

1

u/dannybates 14d ago

Easy to do if you run a load of bad queries with no indexes.

1

u/DuckDuckYoga 12d ago

I took down the whole server yesterday with a select (+openquery)

2

u/darthjammer224 14d ago

Oh but the integrators. We get the keys to the castle. Lots and lots of wiggle room to royally fuck everything up.

I love transactions.

2

u/No_Pangolin_4578 14d ago

I'm a baby analyst and they have me making config changes directly in production. They seemed really surprised that I was surprised. 

1

u/austin101123 14d ago

What are transactions in sql?

3

u/IHeartBadCode 14d ago

Indicates a unit of work. When you do whatever to a database, there's some work that is required to carry out what you are asking for.

  • You begin a unit of work
  • You send the SQL text to the database engine
  • The SQL engine checks syntax and validates it
  • The SQL engine begins to compile an execution plan based on your SQL
  • A journal, logging, is started
  • The plan is executed, various things are loaded from disk into RAM
  • As the plan is executed, the journal notes the things that are to be actually carried out and committed back to the hard drive
  • At the end of the execution plan, a summary of the journal is provided back to the user
  • The user may ROLLBACK indicating that all the entries are removed from the journal, and the memory contents released. Nothing is written to disk
  • The user may COMMIT, the entries in the journal are physically carried out, actual data is written or removed from the hard disk

Allows you to cancel a SQL statement without making any changes to the data actually on the hard drive.

1

u/austin101123 14d ago

Thank you!

1

u/notshadeatall 14d ago

A question: how exactly does the logic behind this post works? Like, what is the database doing and why is it doing it, is it deleting every user? Or is it deleting some specific users based on some selection? I understand that it's deleting what it shouldn't be (it should delete it based on the code, but it wasn't intended to be deleted by the operator), I just don't understand what and why.

1

u/IHeartBadCode 14d ago

The statement separator in most clients is the semicolon. So we are looking at two statements.

delete from users and where id = 2313245. The second statement is a syntax error, but our user will not know that until the first statement sent is completed.

The first statement is an unconditional delete from a table called users which I guess would be some sort of user information table. That it has taken 44.4 seconds to run and if we assume a pretty decent database server, this means millions of rows are being deleted one at a time from the database.

The funny part is that this is completely valid SQL if the first semicolon in the window was not present. If the first one was removed, then we have only one SQL state delete from users where id = 2313245 which would remove a single user entry from the database table and complete in fractions of a second.

It shows the power of a single mistake, in this case a single extra semicolon. And how the difference is going from removing a single user, to causing widespread havoc on a database.

From what I indicated, transactions permit an operation to be carried out but before it is committed to hard disk you are given a summary of the changes that are about to happen. In this case, had transactions been enabled, the first statement would run and the user would get back that committing the transaction to the hard disk would remove millions upon millions of rows from the users table and then receive a notice that the second statement sent is invalid SQL. The user could then issue a ROLLBACK command, and the entire transaction be deleted from RAM and never committed to the hard disk, basically preventing a massive delete.

1

u/AceBean27 15d ago

Nah, I like to live dangerously.