Prevention Triggers

Raise trigger for duplicate prevention

First : you forget a ROLLBACK statement to cancel the transaction

Second : you forget to count (HAVING)

Third : you do no have the right syntax for RAISERROR

The code must be :

CREATE OR ALTER TRIGGER prevent_recast 
ON movie_cast$
AFTER INSERT, UPDATE
AS
SET NOCOUNT ON
IF NOT EXISTS (SELECT *
FROM movie_cast$ as t
JOIN inserted i
ON i.mov_id = t.mov_id
AND i.act_id = t.act_id
AND i.role = t.role
HAVING COUNT(*) = 1)
RETURN;
ROLLBACK;
RAISERROR('Duplicate Data : this actor is already cast for this movie.', 16, 1);
GO

Of course as @Larnu says, this is a stupid thing to do a cancel on a transaction that is made of interpreted code (Transact SQL) and runs after the INSERT, instead of using a UNIQUE constraints that runs in C language and acts before the insert !

The constraint will be as simple as:

ALTER TABLE movie_cast$
ADD UNIQUE (actor_id, mov_id, role_name);

Please DO NOT modify my code... Just suggests some corections

Is there a way to prevent Postgres triggers from triggering before Java/EclipseLink transaction commits

I ended up having to add this line:

<property name="eclipselink.persistence-context.flush-mode" value="COMMIT" />

to the persistence.xml file as documented in persistence-context.flush-mode and this answer.

Prevent Deadlock Errors with Trigger on high concurrent write table

Going out on a limb, the typical root cause for deadlocks is that the order of written (locked) rows is inconsistent among concurrent transactions.

Imagine two exactly concurrent transactions:

T1:


INSERT INTO sales(clothing_id, price, timestamp) VALUES
(1, 11, '2000-1-1')
, (2, 22, '2000-2-1');

T2:

INSERT INTO sales(clothing_id, price, timestamp) VALUES
(2, 23, '2000-2-1')
, (1, 12, '2000-1-1');
T1 locks the row with `clothing_id = 1` in `sales` and `clothes`.

T2 locks the row with `clothing_id = 2` in `sales` and `clothes`.

T1 waits for T2 to release locks for `clothing_id = 2`.

T2 waits for T1 to release locks for `clothing_id = 1`.

Deadlock.

Typically, deadlocks are still extremely unlikely as the time window is so narrow, but with bigger sets / more concurrent transaction / longer transactions / more expensive writes / added cycles for triggers (!) etc. it gets more likely.

The trigger itself is not the cause in this scenario (unless it introduces writes out of order!), it only increases the probability of a deadlock actually happening.

The cure is to insert rows in consistent sort order within the same transaction. Most importantly within the same command. Then the next transaction will wait in line until the first one finishes (COMMIT or ROLLBACK) and releases its locks. The manual:

The best defense against deadlocks is generally to avoid them by being
certain that all applications using a database acquire locks on
multiple objects in a consistent order.

See:

  • How to simulate deadlock in PostgreSQL?

Long-running transactions typically add to the problem. See:

  • Table Locking in PostgreSQL

Aside, you use:

ON CONFLICT (clothing_id) DO UPDATE set last_price = NEW.price ... 

You may want to use EXCLUDED instead of NEW here:

ON CONFLICT (clothing_id) DO UPDATE set last_price = EXCLUDED.price ... 

Subtle difference: this way, effects of possible triggers ON INSERT are carried over, while pasting NEW again overwrites that. Related:

  • How to UPSERT multiple rows with individual values in one statement?

Is there a way to prevent Postgres triggers from triggering before Java/EclipseLink transaction commits

I ended up having to add this line:

<property name="eclipselink.persistence-context.flush-mode" value="COMMIT" />

to the persistence.xml file as documented in persistence-context.flush-mode and this answer.

Vue click prevent triggers

The problem is all the <li>s are bound to the same foo variable, so editing one reference affects all others.

The solution is to create separate variables for each <li>. For example, you could change foo to be an array, and bind each individually:

<template>
<li>
<button @click="foo[0] = !foo[0]">FOO</button>
</li>
<li>
<button @click="foo[1] = !foo[1]">FOO</button>
</li>
<template>

<script>
export default {
data() {
return {
foo: []
}
}
}
</script>

Trigger prevent update query to affect it's update in postgreSQL

finally I found the issue.
It was because of the "return" clause in the function.
return null prevent the insert or the delete.
see PostgreSQL documentation

Set Search Params into URL triggers Root loader. How do I Prevent this?

Use the unstable_shouldReload function in your root route.

https://remix.run/docs/en/v1/api/conventions#unstable_shouldreload



Related Topics



Leave a reply



Submit