12.6 COPY: Bulk Loading Data from Files

Right, you’ve been dutifully inserting rows one by one or in small batches. It’s fine for a few hundred, maybe a thousand records. But you and I both know that’s not how the real world works. The real world sends you a 12-gigabyte CSV file from some mainframe and says “have this loaded by lunchtime.” You’re not going to do that with a million individual INSERT statements. You’re going to use COPY, PostgreSQL’s built-in data firehose.

12.5 The RETURNING Clause: Getting Inserted Data Back

Right, so you’ve just shoved a bunch of data into a table. Congratulations. But now what? You probably want to know what actually got put in there, right? Maybe you need the auto-generated ID for a new user record to create their first order, or perhaps you just want confirmation that your carefully crafted JSONB payload didn’t get mangled on the way in. You could immediately run a SELECT query, but that’s clunky, inefficient, and frankly, a little desperate. It’s like mailing a package and then driving to the recipient’s house to ask if they got it.

12.4 ON CONFLICT DO UPDATE (UPSERT): Merging on Conflict

Right, so you’ve got data. You want to put it in a table. But here’s the rub: some of it might already be there. The classic, tedious way to handle this is a dance: SELECT to check, then either INSERT or UPDATE. It’s clunky, it’s prone to race conditions, and frankly, it’s beneath you. Enter ON CONFLICT DO UPDATE, PostgreSQL’s glorious gift to humanity, often called an “UPSERT.” It’s a single, atomic operation that says, “Listen, if this insert would violate a unique constraint, just do this update instead.”

12.3 ON CONFLICT DO NOTHING: Silent Duplicate Handling

Right, so you’ve got data to insert. Maybe it’s a list of new users from a signup form, or a batch of sensor readings. You fire off a straightforward INSERT statement, and then… kaboom. A duplicate key violation. The whole operation fails, and you’re left holding the pieces. It’s like trying to seat a dozen people at a dinner table where one already-taken chair causes the entire party to be thrown out of the restaurant. It’s a ridiculous way to run a railroad.

12.2 INSERT ... SELECT: Copying Data Between Tables

Right, so you’ve got data in one table and you need to get it into another. You could write a hundred individual INSERT statements, but you’re not a barbarian. You’re a programmer. You automate things. This is where INSERT ... SELECT comes in, and it’s one of the most powerful tools in your SQL toolbox. It’s the Swiss Army knife for copying or transforming data from one part of your database to another. The basic idea is simple: you take the output of a SELECT statement and you feed it directly into an INSERT.

12.1 Single and Multi-Row INSERT Syntax

Let’s get one thing straight: you’re going to insert data. A lot of it. And while inserting one row at a time is like carefully placing individual bricks, we’re building a cathedral here. We need to move with purpose. The INSERT statement is your workhorse, and understanding its full syntax is the difference between being a casual user and a power user who doesn’t waste cycles. The basic, single-row INSERT is straightforward. You’re telling the database exactly what to put where.

— joke —

...