12

My first question is, will these update statements work as intended? Very likely, but not certain. SQL Server guarantees it will honour the semantics of the query, and the level of ACID compliance determined by the effective isolation level. Beyond that, all is implementation detail (including what type(s) of locks are taken, when, and for how long they ...


8

If you're using the default isolation level in SQL Server (Read Committed), then you certainly can run into all sorts of issues around inconsistent reads. Paul White describes the problems here. If you want your read queries to read data which is fully consistent to how it looked at a given point in time, I'd recommend that you consider Read Committed ...


8

Since we've established that no other transaction holds any of the locks the current one holds, it logically follows that no other transaction would be attempting to update or lock any of the records with those same object ids in the personal inventory table. You should know that this: no other transaction holds any of the locks the current one ...


5

You've got two different indexes that can satisfy that query. So two queries, running two different plans could each lock a key on a different index. Try forcing the index in the query.


5

Getting the same row from different indexes As David mentions in his answer, you can get the same row from multiple sessions if you happen to access that row via different indexes. The UPDLOCK hint only applies to the specific access method. Having a nonclustered index row U locked does not prevent another query acquiring a U lock on a different index (...


4

The given schedule S is: T1 T2 ---- ----- R(X) W(X) W(X) To be view serializable W2(X) must move before R1(X) or after W1(X). However, the first would violate the "initial read" rule and the second would violate the "last write" rule. T2 starts after T1 so has a higher timestamp. When T1 comes to write X it sees that T2's timestamp on it. By ...


3

The problem is that the HOLDLOCK is creating the "Shared" lock (the Mode: S locks) on that resource for the duration of the transaction. This does not prevent other processes (such as the same proc executed in another session) from placing their own "Shared" lock on the same resource. But then both sessions get to the UPDATE statement which is trying to ...


2

Not sure if that's the best approach, but it seems to be a quick and reliable as long as application[s] consistently uses it. Create a new table, access_attempt (user_id integer not null primary key). Modify you workflow : begin ; insert into attempt_access (user_id) values (:user_id); -- do you verification, raise an error if needed insert into access (...


2

Combining the comment from @Rory and info in @Zaytsev Dmitry's answer: The CREATE INDEX CONCURRENTLY will not return until the index has finished building. So you know the index is done when your query returns. However if you're building a large index you may wonder if it is 'really' still running. You can use the query for 'invalid' indexes: SELECT * ...


2

The tables are being locked due to the trigger: [iduSalesOrderDetail] on [Sales].[SalesOrderDetail] The trigger launches if any of these actions occur: IF UPDATE([ProductID]) OR UPDATE([OrderQty]) OR UPDATE([UnitPrice]) OR UPDATE([UnitPriceDiscount]) And UPDATE([OrderQty]) is one of these actions. This trigger will then update Person.Person two times. ...


2

Your statement modifies several rows. Each of these rows is locked when it is updated. It is well possible that a statement in a concurrent transaction has already locked one of these rows, blocking your UPDATE. If the concurrent transaction then tries to lock one of the rows that your UPDATE has already locked, you get a deadlock.


2

DEFERRED, IMMEDIATE, and EXCLUSIVE are not isolation levels. All three modes ensure the serializable isolation level. (SQLite's transcations are always serializable because it actually serializes them; there is a single, database-wide lock.) IMMEDIATE takes the equivalent of a write lock at the beginning of the transaction; this prevents a deadlock when two ...


1

Your assessment is spot on, and there is no better way of doing it. As an alternative, you could do without the sort, but delete in smaller batches, each in its own transaction. That is still susceptible to deadlock, but the likelihood is smaller, and re-running the transaction doesn't hurt quite as much.


1

Laurenz explained the mechanism that can lead to deadlocks, and you already included a link yourself to a more detailed explanation by Kevin: Deadlocks in PostgreSQL when running UPDATE Here are a step-by-step instructions how to replicate a deadlock - works with plain UPDATE the same way as it does with SELECT .. FOR UPDATE: How to simulate deadlock in ...


1

You need to think about how the workload as a whole is running, how your set of statements sees work done by others and how you see theirs. You should also consider the possible timings of actions from two concurrently running streams of work. It is not clear from your question but I'm going to assume the query ("Here is my SQL query") runs in one session (...


1

For SQL Server: But what's about single statements? In SQL Server, there is 4 transaction isolation levels (in pessimistic locking model). Default transaction isolation level is Read Committed, and locks are placed on a statement level. If you have a transaction that has 2 statements inside it that retrieve same data, and in the middle of that ...


1

Serializable means that there is some order the transactions can be run in without overlapping and we'll end up with the same answers and the same state of the data base as we get by running the transactions in parallel with serializable isolation level. Given two transactions, A and B, the only valid states of the system are All of A followed by all of B, ...


1

The problem is that there could be values present at the end of A's transaction - the ones inserted by B - that were not present when A first checked. This is known as a phantom A phantom is a row that matches the search criteria but is not initially seen. The way to prevent phantoms is to use the Serializable isolation level. This is the only isolation ...


1

If there are multiple queries in a batch then these queries will be executed in the order they appear in the batch. After the execution of Query 1 is finished then the update query will be executed. You can run debugger to see the sequential execution.


1

Option 1: Use COPY from a valid dump of the tables in order to avoid any foreign key conflict (Copy is the fastest way i've found to load bulk data) Option 2: Create the schema without foreign keys, load the dumps, and recreate the foreign keys after that: ALTER TABLE table2 DROP CONSTRAINT fkey; ALTER TABLE table2 ADD CONSTRAINT fkey; FOREIGN KEY (...


Only top voted, non community-wiki answers of a minimum length are eligible