-
Notifications
You must be signed in to change notification settings - Fork 470
Apply SELECT
policies if RETURNING
has cols
#19720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,20 +53,25 @@ The `USING` and `WITH CHECK` expressions can reference table columns and use ses | |
|
||
The following table shows which policies are applied to which statement types, with additional considerations listed after the table. | ||
|
||
| Command / clause pattern | `SELECT` policy - `USING` (row that already exists) | `INSERT` policy - `WITH CHECK` (row being added) | `UPDATE` policy - `USING` (row before the change) | `UPDATE` policy - `WITH CHECK` (row after the change) | `DELETE` policy - `USING` (row to be removed) | | ||
|-------------------------------------|-----------------------------------------------------|--------------------------------------------------|---------------------------------------------------|-------------------------------------------------------|-----------------------------------------------| | ||
| `SELECT` | ✓ | — | — | — | — | | ||
| `SELECT ... FOR UPDATE / FOR SHARE` | ✓ | — | ✓ | — | — | | ||
| `INSERT` | — | ✓ | — | — | — | | ||
| `INSERT ... RETURNING` | ✓ | ✓ | — | — | — | | ||
| `UPDATE` | ✓ | — | ✓ | ✓ | — | | ||
| `DELETE` | ✓ | — | — | — | ✓ | | ||
| `INSERT ... ON CONFLICT DO UPDATE` | ✓ | — | ✓ | ✓ | — | | ||
| `UPSERT` | ✓ | — | ✓ | ✓ | — | | ||
| Command / clause pattern | `SELECT` policy - `USING` (row that already exists) | `SELECT` policy - `USING` (row being added) | `INSERT` policy - `WITH CHECK` (row being added) | `UPDATE` policy - `USING` (row before the change) | `UPDATE` policy - `WITH CHECK` (row after the change) | `DELETE` policy - `USING` (row to be removed) | | ||
|--------------------------------------|-----------------------------------------------------|---------------------------------------------|--------------------------------------------------|---------------------------------------------------|-------------------------------------------------------|-----------------------------------------------| | ||
| `SELECT` | ✓ | — | — | — | — | — | | ||
| `SELECT ... FOR UPDATE / FOR SHARE` | ✓ | — | — | ✓ | — | — | | ||
| `INSERT` | — | ✓ | ✓ | — | — | — | | ||
| `INSERT ... RETURNING` | — | ✓(a) | ✓ | — | — | — | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the |
||
| `UPDATE` | ✓ | ✓(a) | — | ✓ | ✓ | — | | ||
| `DELETE` | ✓ | — | — | — | — | ✓ | | ||
| `INSERT ... ON CONFLICT DO UPDATE` | ✓(b) | ✓(b) | — | ✓ | ✓ | — | | ||
| `UPSERT` | ✓(b) | ✓(b) | — | ✓ | ✓ | — | | ||
|
||
- ✓: Always applied when fetching an existing row for `UPDATE` or `DELETE` (CockroachDB uses the `USING` policy to filter rows before modifying or deleting). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use the ✓ in cells for SQL statements that aren't UPDATE or DELETE. Also, it's used in cells for the WITH CHECK expression. I wonder if this should just say something like 'Always applied', without going into detail about the SQL statements and expression type. |
||
- (a): Applied on write statements (`INSERT ... RETURNING`, `UPDATE`, `DELETE`) only when the statement references row columns (e.g., in `WHERE`, `SET`, or `RETURNING`), since those references require reading the row. | ||
- (b): A `USING` policy failure causes the statement to fail. Normally, `USING` filters out rows silently. | ||
|
||
In other words, during writes, the `SELECT` policy is always applied to the existing row. For the new row being inserted or updated, it's only applied if that row is referenced in the `WHERE`, `SET`, or `RETURNING` clause, since those clauses require reading from it. | ||
|
||
Additional considerations include: | ||
|
||
- `SELECT` evaluation: CockroachDB always evaluates `SELECT` (`USING`) policies for `INSERT`, `UPDATE`, and `DELETE`, even when the statement doesn't reference table columns. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we include further details as to when the SELECT policies will be applied during an INSERT/UPDATE/DELETE? SELECT policies are always applied during the fetch of the existing row (for UPDATE or DELETE). If a column is referenced in the WHERE, SET or RETURNING clause, then SELECT policies will also get applied during the write. I think the postgres docs handled this by adding superscript numbers to the above table as an additional addendum. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've taken a shot at addressing this feedback in 8af8a1f tried to incorporate the info from your comment and add some superscripts and more explanation PTAL! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. During writes, the SELECT policy is always applied to the existing row. For the new row being inserted or updated, it's only applied if that row is referenced in the WHERE, SET, or RETURNING clause, since those clauses require reading from it. It might be worth adding a column to the chart for this distinction. The values for chart would be:
For UPSERT and it's variants, we have slightly different behaviour. So, I added (b) which means:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
- {% include {{ page.version.version }}/known-limitations/rls-values-on-conflict-do-nothing.md %} This is a [known limitation](#known-limitations). | ||
|
||
## Examples | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed that we don't apply SELECT policies on this INSERT (3rd column). It can only be applied in the INSERT ... RETURNING case, and only if the column is referenced in the RETURNING clause, which you have covered.