DEFAULT values and expressions in PostgreSQL - Time & Space Complexity
We want to understand how the time to insert data with DEFAULT values changes as we add more rows.
How does the database handle setting default values when many rows are inserted?
Analyze the time complexity of this INSERT statement using DEFAULT values.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username TEXT NOT NULL,
created_at TIMESTAMP DEFAULT now()
);
INSERT INTO users (username) VALUES ('alice'), ('bob'), ('carol');
This code creates a table with a default timestamp and inserts multiple rows without specifying the timestamp.
Look for repeated actions during the insert process.
- Primary operation: For each row inserted, the database evaluates the DEFAULT expression (now()) to set the timestamp.
- How many times: Once per row inserted, so the number of evaluations grows with the number of rows.
As you insert more rows, the database must run the DEFAULT expression for each one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 evaluations of now() |
| 100 | 100 evaluations of now() |
| 1000 | 1000 evaluations of now() |
Pattern observation: The work grows directly with the number of rows inserted.
Time Complexity: O(n)
This means the time to insert rows with DEFAULT values grows linearly with how many rows you add.
[X] Wrong: "DEFAULT expressions run only once no matter how many rows are inserted."
[OK] Correct: Each row needs its own value, so the DEFAULT expression runs separately for every row inserted.
Understanding how DEFAULT values affect insert performance shows you know how databases handle data behind the scenes.
"What if the DEFAULT expression was a constant value instead of a function call? How would the time complexity change?"
Practice
DEFAULT keyword do in a PostgreSQL table column definition?Solution
Step 1: Understand the purpose of DEFAULT
The DEFAULT keyword assigns a value automatically if no value is given during an insert operation.Step 2: Compare options with this behavior
Only Sets a value automatically when no value is provided during insert correctly describes this automatic assignment behavior.Final Answer:
Sets a value automatically when no value is provided during insert -> Option AQuick Check:
DEFAULT = automatic value assignment [OK]
- Thinking DEFAULT deletes or modifies columns
- Confusing DEFAULT with NOT NULL constraint
- Assuming DEFAULT requires manual input
score in PostgreSQL?Solution
Step 1: Recall ALTER TABLE syntax for setting default
In PostgreSQL, to set a default value, use: ALTER TABLE table ALTER COLUMN column SET DEFAULT value;Step 2: Match syntax with options
Only ALTER TABLE mytable ALTER COLUMN score SET DEFAULT 100; matches the correct syntax exactly.Final Answer:
ALTER TABLE mytable ALTER COLUMN score SET DEFAULT 100; -> Option AQuick Check:
ALTER COLUMN ... SET DEFAULT = correct syntax [OK]
- Using SET DEFAULT before column name
- Using CHANGE instead of ALTER COLUMN
- Trying to ADD DEFAULT as a separate clause
CREATE TABLE orders ( id SERIAL PRIMARY KEY, order_date DATE DEFAULT CURRENT_DATE, status TEXT DEFAULT 'pending' );
What will be the result of this query?
INSERT INTO orders (status) VALUES ('shipped') RETURNING *;Solution
Step 1: Understand DEFAULT behavior on insert
Sinceorder_datehas DEFAULT CURRENT_DATE, if not provided, it uses today's date automatically.Step 2: Analyze the insert statement
The insert providesstatus = 'shipped', so it overrides the default 'pending'. Theorder_dateis omitted, so default applies.Final Answer:
A new row with current date as order_date and status 'shipped' -> Option CQuick Check:
DEFAULT fills missing columns, provided columns override [OK]
- Assuming missing columns become NULL instead of default
- Expecting error when default exists
- Confusing default value with NOT NULL constraint
CREATE TABLE users ( id SERIAL PRIMARY KEY, username TEXT NOT NULL, created_at TIMESTAMP DEFAULT NOW() );
Which of the following INSERT statements will cause an error?
Solution
Step 1: Check NOT NULL column requirements
Theusernamecolumn is NOT NULL and must be provided in inserts.Step 2: Analyze each insert
INSERT INTO users (created_at) VALUES (NOW()); inserts onlycreated_atbut omitsusername, causing a NOT NULL violation error.Final Answer:
INSERT INTO users (created_at) VALUES (NOW()); -> Option DQuick Check:
NOT NULL columns must be provided [OK]
- Assuming DEFAULT VALUES inserts NULL for NOT NULL columns
- Thinking DEFAULT can replace NOT NULL columns
- Ignoring NOT NULL constraints
products where the created_at column defaults to the current timestamp, and the price column defaults to 9.99 if no price is given. Which of the following CREATE TABLE statements correctly implements this?Solution
Step 1: Check default for price column
Price should default to numeric 9.99 without quotes (numeric literal).Step 2: Check default for created_at column
UseNOW()function call for current timestamp, notCURRENT_TIMESTAMPorCURRENT_DATE(date only).Step 3: Validate syntax correctness
CREATE TABLE products ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, price NUMERIC DEFAULT 9.99, created_at TIMESTAMP DEFAULT NOW() ); usesprice NUMERIC DEFAULT 9.99andcreated_at TIMESTAMP DEFAULT NOW(), which is correct syntax and behavior.Final Answer:
CREATE TABLE products ( id SERIAL PRIMARY KEY, name TEXT NOT NULL, price NUMERIC DEFAULT 9.99, created_at TIMESTAMP DEFAULT NOW() ); -> Option BQuick Check:
Use NOW() for timestamp, numeric literals without quotes [OK]
- Using quotes around numeric default values
- Using CURRENT_DATE instead of CURRENT_TIMESTAMP for timestamps
- Omitting parentheses in NOW() function
Ready to prove it?
Pass the test and this pattern moves unknown
