Yes, you can use INSERT … ON CONFLICT DO UPDATE with a partial unique index like the one you defined in PostgreSQL, but there are some important considerations to keep in mind.
Key Points
Partial Unique Index:
Your index nt_stop_group_uniq_code is a partial unique index because it includes a WHERE clause (WHERE NOT sgr_deleted). This means the uniqueness constraint only applies to rows where sgr_deleted is false. Rows where sgr_deleted is true are excluded from the index and can have duplicate sgr_code values without causing a conflict.
Using ON CONFLICT with Partial Indexes:
The ON CONFLICT clause can reference a unique index, including a partial unique index, but you must ensure that the conflict target (the column or expression in the index) is explicitly specified in the ON CONFLICT clause. For your case, the conflict target would be the sgr_code column, and the partial condition (NOT sgr_deleted) is implicitly enforced by the index.
Syntax:
When using ON CONFLICT DO UPDATE, you need to specify the conflict target (the column or index) and the action to take when a conflict occurs. The conflict target must match the columns or expressions in the unique index.
Example
Here’s an example of how you can use INSERT … ON CONFLICT DO UPDATE with your partial unique index:
sql
INSERT INTO nt_stop_group (sgr_code, sgr_name, sgr_deleted)
VALUES ('CODE123', 'Stop Group Name', false)
ON CONFLICT ON CONSTRAINT nt_stop_group_uniq_code
DO UPDATE SET
sgr_name = EXCLUDED.sgr_name,
sgr_deleted = EXCLUDED.sgr_deleted;
Alternatively, if you prefer not to reference the index by name, you can specify the conflict target directly using the column(s) involved in the index:
sql
INSERT INTO nt_stop_group (sgr_code, sgr_name, sgr_deleted)
VALUES ('CODE123', 'Stop Group Name', false)
ON CONFLICT (sgr_code) WHERE NOT sgr_deleted
DO UPDATE SET
sgr_name = EXCLUDED.sgr_name,
sgr_deleted = EXCLUDED.sgr_deleted;
Explanation of the Example
ON CONFLICT ON CONSTRAINT nt_stop_group_uniq_code: This references the unique index by its name. This is the preferred approach when working with named constraints or indexes.
ON CONFLICT (sgr_code) WHERE NOT sgr_deleted: This explicitly specifies the conflict target (sgr_code) and the partial condition (WHERE NOT sgr_deleted) that matches the index definition. This approach is useful if you want to avoid hardcoding the index name or if the index name might change.
DO UPDATE: Specifies the action to take when a conflict occurs. In this case, it updates the sgr_name and sgr_deleted columns with the values from the EXCLUDED row (the row that was attempted to be inserted).
Important Notes
Matching the Partial Condition:
The partial condition (WHERE NOT sgr_deleted) in the index means that conflicts will only be detected for rows where sgr_deleted is false. If you attempt to insert a row with a duplicate sgr_code but sgr_deleted is true in the existing row, no conflict will occur, and the new row will be inserted.
Behavior of DO UPDATE:
When a conflict occurs, the DO UPDATE action will update the existing row. Be cautious with how you handle the sgr_deleted flag in the update. For example, if the update sets sgr_deleted = true, the row will no longer be subject to the uniqueness constraint (since it will be excluded from the index), and subsequent inserts with the same sgr_code will not trigger a conflict.
Performance:
Using a partial index can improve performance by reducing the number of rows that need to be indexed, but ensure that your queries and ON CONFLICT clauses are designed to take advantage of the index.
PostgreSQL Version:
The ON CONFLICT feature is available in PostgreSQL 9.5 and later. Ensure you are using a supported version.
Testing Your Setup
To verify that your INSERT … ON CONFLICT DO UPDATE works as expected, you can test it with a few scenarios:
Insert a new row (no conflict):
sql
INSERT INTO nt_stop_group (sgr_code, sgr_name, sgr_deleted)
VALUES ('CODE123', 'Stop Group 1', false)
ON CONFLICT (sgr_code) WHERE NOT sgr_deleted
DO UPDATE SET
sgr_name = EXCLUDED.sgr_name,
sgr_deleted = EXCLUDED.sgr_deleted;
This should insert the row without any conflict.
Insert a duplicate row (conflict, triggers update):
sql
INSERT INTO nt_stop_group (sgr_code, sgr_name, sgr_deleted)
VALUES ('CODE123', 'Updated Stop Group 1', false)
ON CONFLICT (sgr_code) WHERE NOT sgr_deleted
DO UPDATE SET
sgr_name = EXCLUDED.sgr_name,
sgr_deleted = EXCLUDED.sgr_deleted;
This should update the existing row instead of inserting a new one.
Insert a duplicate row with sgr_deleted = true in the existing row (no conflict):
First, update the existing row to set sgr_deleted = true:
sql
UPDATE nt_stop_group SET sgr_deleted = true WHERE sgr_code = 'CODE123';
Then, insert a new row with the same sgr_code:
sql
INSERT INTO nt_stop_group (sgr_code, sgr_name, sgr_deleted)
VALUES ('CODE123', 'Stop Group 2', false)
ON CONFLICT (sgr_code) WHERE NOT sgr_deleted
DO UPDATE SET
sgr_name = EXCLUDED.sgr_name,
sgr_deleted = EXCLUDED.sgr_deleted;
This should insert a new row because the existing row with sgr_code = 'CODE123' has sgr_deleted = true and is excluded from the index.
Conclusion
Yes, you can use INSERT … ON CONFLICT DO UPDATE with your partial unique index, as long as you correctly specify the conflict target and handle the partial condition appropriately. Be sure to test your implementation thoroughly to ensure it behaves as expected in all scenarios.