postgres alter table hangs with connection pooling - sql

Executing simple alter table add column hangs, this could be due to my application uses connection pooling which opens connections and probably locking tables. is there any way we can still execute alter table command in postgres while application is still running ?

Check the view pg_locks to see which concurrent transaction holds a lock the blocks the ALTER TABLE.
If your connection pool keeps connections hanging in the state “idle in transaction” (check pg_stat_activity), there is a bug in the connection pool or the application. You should fix that, because it causes other problems too, like table bloat because VACUUM cannot do its job.
A collection of statements to monitor locks is available in the Postgres Wiki:
https://wiki.postgresql.org/wiki/Lock_Monitoring
https://wiki.postgresql.org/wiki/Lock_dependency_information

Related

Can't Enable Always on Support for SSISDB

I need to set up Always on Support for SSISDB and I am receiving this message:
The operation cannot be performed on database "SSISDB" because it is involved in a database mirroring session or an availability group. Some operations are not allowed on a database that is participating in a database mirroring session or in an availability group.
I Did have this set up no problem before, but we had to delete the Catalogue and recreate it. And once we have done this, it wont turn back on.
We have AG Set up for all of our other databases and trying to add SSISDB into the AG works but then says we need Always on Support turned on, and this is when we receive the error message.
The operation cannot be performed on database "SSISDB" because it is involved in a database mirroring session or an availability group. Some operations are not allowed on a database that is participating in a database mirroring session or in an availability group.
As error suggest the cause can be mirroring session and the main database remains accessible when a mirroring session is paused or removed.
To Suspend mirroring session use following command:
ALTER DATABASE _database_name_ SET PARTNER SUSPEND
where database_name denotes the mirrored database whose active session you want to pause.
The mirror database no longer keeps up with the principal database when mirroring is pausing, which results in the principal database running exposed.
Also see, Configure Integration Services Catalog Database SSISDB by Rajendra Gupta for more information.

Making queries in HSQLDB during transaction

I'm running a test using HSQLDB and I want to be able making select queries on database while this test to see what is changing. I suppose transaction or something else is blocking the database while test and I'm not able to make queries ...
Is it possible to configure hsql server in that way to be able to do these queries?
This answer is valid if you are starting a HyperSQL Server and connecting with the jdbc:hsqldb:hsql:xxxx connection URL.
The default transaction isolation model is LOCKS. In this model, the tables are locked when their rows are modified during a transaction. You need to use the MVCC model for the database to allow other connections to access the table.
However, when you modify the rows, these changes are only visible to the transaction that makes the change until it commits the data and the changes become visible to other connections.

Query to get active processes in Oracle

I am trying to see what queries are currently running in an oracle DB. However, when I try using the table v$session it gives me an error:
What's the cause of this and what would be the correct way to get the active processes that are running?
I'm looking to get the necessary information to be able to cancel a query for a given user. Let me give an example:
1) User executes query in the application. We add in a comment so we can 'track' that query:
/* Query-ID-1283849 */ select * from mytable
2) Now, if the user clicks the "Cancel" button while the query is running (let's say the query is taking a very long time to respond), we allow the user to cancel that query, given that the user will probably NOT be a sys user but a 'normal' user with read-only privileges.
How could this be done?
At the fundamental database level, you can't kill individual queries. You kill individual sessions. I'm inferring from your question that your specific use case is inside an application, not a tool like Sql Developer or Sql Plus.
Session killing can be done by users that have special database privileges to kill sessions. If you are inside an application running multiple queries in one session, killing the session will effectively kill your application and require either a) an application restart or b) gracefully programatically handling the dropped session.
If you are using an n-tier ORM framework that handles database interactions for you, you may be in a position where killing the session won't have any affect on your application other than the currently running statement.
Another way in your app to handle isolating sessions and queries is to run a multi-threaded application. Each query spawns a new thread, and the thread can be killed without necessarily killing the session.
Basically, the short answer is you can kill a query only by killing its session. Your approach of looking at v$session is correct and necessary to find the session id for any givel sql statement, you just need to have your DBA grant your privileges to the v$session and v$sql synonyms.
Update specific to Sql Developer, based on OP's comment for clarification:
Sql Developer has an option to allow running parallel queries, taking advantage of threads and multiple connections. That setting is found at Tools > Preferences > Database > Worksheet. Regardless of the setting, when you click the query cancel button, the app is still sending a session kill request. The GUI will usually gracefully start a new session and the end user is none the wiser about it. But sometimes things don't work out and the GUI freezes or you end up with no connection and have to manually reconnect.
To add to the complexity, the behavior depends on the driver/client used by your application. OCI, thick clients, and thin clients have shown different behaviors in the past when it comes to kill requests. In fact, in Sql Developer, you have an option to force it to use OCI or a thick driver so that you can avoid certain behaviors.
I'd highly recommend getting rights to view v$session and play around with this. It's fun to learn more about how Oracle manages sessions.
I just discovered that the latest version, Oracle 18c, allows killing an individual query within a session. I'm on 12c so I have not tried this. https://docs.oracle.com/en/database/oracle/oracle-database/18/admin/managing-processes.html#GUID-7D8E5E00-515D-4338-8B86-C2044F6D2957
Relevant parts from the documentation.
5.10.5 Cancelling a SQL Statement in a Session You can cancel a SQL statement in a session using the ALTER SYSTEM CANCEL SQL statement.
Instead of terminating a session, you can cancel a high-load SQL
statement in a session. When you cancel a DML statement, the statement
is rolled back.
The following clauses are required in an ALTER SYSTEM CANCEL SQL
statement:
SID – Session ID
SERIAL – Session serial number
The following clauses are optional in an ALTER SYSTEM CANCEL SQL
statement:
INST_ID – Instance ID
SQL_ID – SQL ID of the SQL statement
You can view this information for a session by querying the GV$SESSION
view.
The following is the syntax for cancelling a SQL statement:
ALTER SYSTEM CANCEL SQL 'SID, SERIAL, #INST_ID, SQL_ID';

Can entity framework select block the table?

I heard that SQL Server SELECT statements causing blocking.
So I have MVC application with EF and SQL Server 2008 and it shares DB with another application which is very frequently writes some data. And MVC application generates some real-time reports based that data which comes from another application.
So given that scenario is it possible that while generating a report it will block some tables where another application will try to write data?
I tried to make some manual inserts and updates while report is generated and it handled fine. Am I misunderstood something?
This is one of the reasons why in Entity Framework 6 for Sql Server a default in database creation has changed:
EF is now aligned with a “best practice” for SQL Server databases, which is to configure the database’s READ_COMMITTED_SNAPSHOT setting to ON. This means that, by default, the database will create a snapshot of itself every time a change is made. Queries will be performed on the snapshot while updates are performed on the actual database.
So with databases created by EF 5 and lower, READ_COMMITTED_SNAPSHOT is OFF which means that
the Database Engine uses shared locks to prevent other transactions from modifying rows while the current transaction is running a read operation.
Of course you can always change the setting yourself:
ALTER DATABASE MyDb SET READ_COMMITTED_SNAPSHOT ON
READ UNCOMMITTED should help, because it doesn't issue shared locks on data that are being retrieved. So, it doesn't bother your other application that intensively updates the data. Another option is to use SNAPSHOT isolation leven on your long-running SELECT. This approach preserves data integrity for selected data, but for a cost of higher CPU, and more intense tempdb usage.

Does merge replication lock subscriber database?

I need to configure merge replication between 2 databases. These databases have foreign key integrity, which makes the replication not work, so I resorted to:
Dropping all FKs on subscriber database,
Replicating, and
Recreating the FKs.
This however leaves the subscriber database vulnerable to FK violations.
So my questions are:
Does the replication lock the subscriber database, raise a transaction, and render the database unusable in any way during the process?
If not, could I initiate such a lock manually through TSQL?
If none of the above is possible, is there something I'm missing?
don't know about lock initiated by replication, but for the time of your maintenance you can set the whole database to single_user or restricted_user.
ALTER DATABASE SET RESTRICTED_USER
i recommend the second one as it allows access to the database to all users who are, quote:
members of the db_owner fixed database role and dbcreator and
sysadmin fixed server roles
(see here: http://msdn.microsoft.com/en-us/library/aa933082%28SQL.80%29.aspx)
, only regular users are restricted. it will wait until all regular user connections are done
ALTER DATABASE SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE
will kill all such connections immediately. This
select DATABASEPROPERTYEX ('ocon_reportdb','UserAccess') DATABASEPROPERTYEX_UserAccess
reads the current status
UPDATE:
there are maintainance activities such as statistics performed by the database engine. using WITH ROLLBACK IMMEDIATE will also kill those connections, so be careful
UPDATE2: specs to have acces in restricted_user-mode