Kill Db2 select statement after the declared time - select

In Db2 LUW 9.7, how to implement a query timeout?
When performing queries, is there is a 'timeout' parameter that I can declare/implement somehow that will make a given query abort after a certain time?
So far, I can only consider the potentially unsafe practice of killing the process that performs the select query in Unix.

This question is not about programming, it is about administration.
A query will never "kill itself". But there are tools available which (after suitable configuration) will limit the resources that a query can consume at run time, and will cause the query to return an error (if desired) if such resources are exceeded.
You should never kill a process that is owned by a Db2-instance, doing that carries risk that you trash your database.
To control the resources (time, memory, CPU, disk, etc...) that a query can use, it is possible on V9.7 to use Workload-Manager. This requires configuration and experience to properly use it.
IBM also offered a Query-Patroller tool before IBM introduced the Workload Manager (WLM). This was a different solution than WLM. I think Query Patroller was also available in V9.7. It has since been replaced by WLM.

In principle there are two ways to achieve what you need, depending on whether you have the control over the application or the database.
From application end one can use SQL_ATTR_QUERY_TIMEOUT keyword with CLI/ODBC or setQueryTimeout with JDBC (there should be an equivalent for other interfaces). I will attach CLI example at the end.
If you have only control over database then one could consider creating a threshold with ACTIVITYTOTALTIME that would automatically interrupt long running queries.
If you are concerned with Db2 CLP client, then interrupting execution with ctrl+c will propagate interrupt to the database, i.e. you should no longer see statement as executing inside the database. If you however break it more "forcefully" e.g. run kill against db2bp process associated with the shell, statement will continue executing for some time, but it will be eventually interrupted (one will see AgentBreathingPoint Warning in db2diag.log, when Db2 engine periodically checks whether client is still around and it happens to be gone).
Db2 CLI example:
I will use
db2cli executable - CLI to run Db2 CLI commands
"bad query" that is expected to run for a long time:
select t1.*, t2.* from sysibm.syscolumns t1, sysibm.syscolumns t2, sysibm.syscolumns t3, sysibm.syscolumns t4, sysibm.syscolumns t5 order by t5.name desc, t4.name asc fetch first 10 rows only
set the timeout to 10 seconds
code:
SQLAllocEnv 1
SQLAllocConnect 1 1
SQLConnect 1 SAMPLE sql_nts db2v115 sql_nts **** sql_nts
SQLAllocStmt 1 1
SQLSetStmtAttr 1 SQL_ATTR_QUERY_TIMEOUT 10
SQLPrepare 1 "select t1.*, t2.* from sysibm.syscolumns t1, sysibm.syscolumns t2, sysibm.syscolumns t3, sysibm.syscolumns t4, sysibm.syscolumns t5 order by t5.name desc, t4.name asc fetch first 10 rows only" sql_nts
SQLExecute 1
SQLFetch 1
SQLError 1 1 1
it is SQLFetch 1 that will fail, full output including SQLCODEs below:
$ db2cli
IBM DATABASE 2 Interactive CLI Sample Program
(C) COPYRIGHT International Business Machines Corp. 1993,1996
All Rights Reserved
Licensed Materials - Property of IBM
US Government Users Restricted Rights - Use, duplication or
disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
> SQLAllocEnv 1
SQLAllocConnect 1 1
SQLConnect 1 SAMPLE sql_nts db2v115 sql_nts *** sql_nts
SQLAllocStmt 1 1
SQLSetStmtAttr 1 SQL_ATTR_QUERY_TIMEOUT 10
SQLAllocEnv: rc = 0 (SQL_SUCCESS)
CLI henv = 1, Test Driver henv = 1
> SQLAllocConnect: rc = 0 (SQL_SUCCESS)
CLI hdbc = 1, Test Driver hdbc = 1
> SQLConnect: rc = 0 (SQL_SUCCESS)
> SQLAllocStmt: rc = 0 (SQL_SUCCESS)
CLI hstmt = 1, Test Driver hstmt = 1
> SQLSetStmtAttr: rc = 0 (SQL_SUCCESS)
> SQLPrepare 1 "select t1.*, t2.* from sysibm.syscolumns t1, sysibm.syscolumns t2, sysibm.syscolumns t3, sysibm.syscolumns t4, sysibm.syscolumns t5 order by t5.name desc, t4.name asc fetch first 10 rows only" sql_nts
SQLPrepare: rc = 0 (SQL_SUCCESS)
> SQLExecute 1
SQLExecute: rc = 0 (SQL_SUCCESS)
> SQLFetch 1
SQLFetch: rc = -1 (SQL_ERROR)
> SQLError 1 1 1
SQLError: rc = 0 (SQL_SUCCESS)
SQLError: SQLState : S1008
fNativeError : -952
szErrorMsg : [IBM][CLI Driver][DB2/6000] SQL0952N Processing was cancelled due to an interrupt. SQLSTATE=57014
cbErrorMsg : 100

ODBC/CLI: QueryTimeout
JDBC: The commandTimeout property.
Common IBM Data Server Driver for JDBC and SQLJ properties for all supported database products
if I kill the process that performs the select query in Unix then the query should also be aborted
Yes.

create a threshold on ACTIVITYTOTALTIME or UOWTOTALTIME and set action on stop execution
https://www.ibm.com/support/knowledgecenter/en/SSEPGG_9.7.0/com.ibm.db2.luw.sql.ref.doc/doc/r0050563.html

Related

PostgreSQL statement timeout error on a function when "statement_timeout = 0"

I'm using PGWatch2 monitoring for a master(write) and two replication(read) servers. This monitoring uses this function to get query information. There is no errors on master server but replications have this error:
pgwatch2#database ERROR: canceling statement due to statement timeout
pgwatch2#database STATEMENT:
with q_data as (
select
...
This query takes about 7s on master and
both replication servers and master has statement_timeout=0:
statement_timeout
-------------------
0
(1 row)
I'm using PostgreSQL 12.9 on UBUNTU 20.04.1

SQLSTATE=23514 : Facing error on Set integrity

Can someone suggest me what is the problem, why I am not able to enable integrity, although no constraint found with name of CATEGORY.
create function FCOK_ACCOUNT_CATEGORY_C2(xmlrecord XML) returns integer language sql contains sql no external action deterministic return xmlcast(xmlquery('$d/row/c2' passing xmlrecord as "d") as integer);
reorg table FCOK_ACCOUNT_TEST inplace
db2pd -db admindb –reorg
Database Member 0 -- Database ADMINDB -- Active -- Up 10 days 03:14:17 -- Date 2021-07-26-19.28.05.648688
Table Reorg Information:
Address TbspaceID TableID PartID MasterTbs MasterTab TableName Type IndexID TempSpaceID
0x0A001F00174DF588 4 21566 n/a n/a n/a FCOK_ACCOUNT Offline 0 4
0x0A001F0027981508 3 26880 n/a n/a n/a FCOK_ACCOUNT_TEST Online 0 3
Table Reorg Stats:
Address TableName Start End PhaseStart MaxPhase Phase CurCount MaxCount Status Completion
0x0A001F00174DF588 FCOK_ACCOUNT 07/16/2021 18:38:46 07/16/2021 18:43:15 07/16/2021 18:39:40 3 IdxRecreat 0 0 Done 0
0x0A001F0027981508 FCOK_ACCOUNT_TEST 07/26/2021 18:13:16 07/26/2021 18:14:38 n/a n/a n/a 0 0 Done 0
bash-4.2$
set integrity for FCOK_ACCOUNT_TEST off;
ALTER TABLE FCOK_ACCOUNT_TEST ADD CATEGORY INTEGER generated always as (FCOK_ACCOUNT_CATEGORY_C2(XMLRECORD))
set integrity for FCOK_ACCOUNT_TEST immediate checked;
db2 "set integrity for DB2ADMIN.FCOK_ACCOUNT_TEST immediate checked"
DB21034E The command was processed as an SQL statement because it was not a
valid Command Line Processor command. During SQL processing it returned:
SQL3603N Integrity processing through the SET INTEGRITY statement has found
an integrity violation involving a constraint, a unique index, a generated
column, or an index over an XML column. The associated object is identified by
"DB2ADMIN.FCOK_ACCOUNT_TEST.CATEGORY". SQLSTATE=23514
bash-4.2$
bash-4.2$ db2 "select TYPE, ENFORCED from SYSCAT.TABCONST where CONSTNAME='CATEGORY'"
TYPE ENFORCED
0 record(s) selected.
bash-4.2$ db2 "select COLSEQ,COLNAME from SYSCAT.KEYCOLUSE where CONSTNAME='CATEGORY'"
COLSEQ COLNAME
0 record(s) selected.
bash-4.2$ db2 "reorg table db2admin.FCOK_ACCOUNT_TEST inplace"
SQL0668N Operation not allowed for reason code "1" on table
"DB2ADMIN.FCOK_ACCOUNT_TEST". SQLSTATE=57016

Staus NOTRUN in Automatic task scheduler

I created a DB2 task to run my stored procedure automatically at a specific time, I created the task using the ADMIN_TASK_ADD procedure:
CALL SYSPROC.ADMIN_TASK_ADD ( 'WR_AM_ADT_AUTO_CNRRM_SCHDLR',
NULL,
NULL,
NULL,
'05 16 * * *',
'ASPECT',
'WR_AM_ADT_AUTO_CNRRM',
'81930',NULL,NULL);
COMMIT;
I want to run my scheduled task every day at 04:05 PM, but it didn't work and giving the status as
NOTRUN, SQLCODE -104
.
So can anyone please tell me what am I doing wrong?
I also checked my scheduler in task list using following command:
SELECT * from SYSTOOLS.ADMIN_TASK_LIST
I am using DB2 9.7 version on Windows.
The status of the task NOTRUN means an error prevented the scheduler from calling the task's procedure. The SQLCODE indicates the type of error.
I suggest you the followings;
Confirm scheduler is enabled.
db2 > db2set
DB2_ATS_ENABLE=YES
ATS depends on the SYSTOOLSPACE tablespace to store historical data and configuration information. You can check if the tablespace exists in your system with the following query.
db2 select TBSPACE from SYSCAT.TABLESPACES where TBSPACE = 'SYSTOOLSPACE'
You can test stored procedure in isolation
CALL WR_AM_ADT_AUTO_CNRRM()
Then run your task in schedular!

PostgreSQL hangs while executing DDL without log message

I have Postgresql-9.2.10 on CentOS.
I experience the following error:
DETAIL: Multiple failures --- write error might be permanent.
ERROR: could not open file "pg_tblspc / 143862353 / PG_9.2_201204301 / 16439 / 199534370_fsm": No such file or directory
This happens since I stopped the PostgreSQL service, ran pg_resetxlog and started the service. The logs in pg_log look good, and the service is listed without any problem.
DML works well , but not a DDL statement like CREATE TABLE, otherwise an error message is thrown or nothing is visible in the logs in pg_log.
If I try to create a table, there is no reaction, and it looks like the statement is blocked by a lock.
So I tried the following query to look for locks:
SELECT blocked_locks.pid AS blocked_pid,
blocked_activity.usename AS blocked_user,
blocking_locks.pid AS blocking_pid,
blocking_activity.usename AS blocking_user,
blocked_activity.query AS blocked_statement,
blocking_activity.query AS blocking_statement
FROM pg_catalog.pg_locks blocked_locks
JOIN pg_catalog.pg_stat_activity blocked_activity ON blocked_activity.pid = blocked_locks.pid
JOIN pg_catalog.pg_locks blocking_locks
ON blocking_locks.locktype = blocked_locks.locktype
AND blocking_locks.DATABASE IS NOT DISTINCT FROM blocked_locks.DATABASE
AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation
AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page
AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple
AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid
AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid
AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid
AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid
AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid
AND blocking_locks.pid != blocked_locks.pid
JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid
WHERE NOT blocked_locks.granted;
You probably corrupted the PostgreSQL cluster with pg_resetxlog. How exactly did you run the command?
I would restore from the last good backup.

"Lost connection to MySQL server during query" in Google Cloud SQL

I am having a weird, recurring but not constant, error where I get "2013, 'Lost connection to MySQL server during query'". These are the premises:
a Python app runs around 15-20minutes every hour and then stops (hourly scheduled by cron)
the app is on a GCE n1-highcpu-2 instance, the db is on a D1 with a per package pricing plan and the following mysql flags
max_allowed_packet 1073741824
slow_query_log on
log_output TABLE
log_queries_not_using_indexes on
the database is accessed only by this app and this app only so the usage is the same, around 20 consecutive minutes per hour and then nothing at all for the other 40 minutes
the first query it does is
SELECT users.user_id, users.access_token, users.access_token_secret, users.screen_name, metadata.last_id
FROM users
LEFT OUTER JOIN metadata ON users.user_id = metadata.user_id
WHERE users.enabled = 1
the above query joins two tables that are each around 700 lines longs and do not have indexes
after this query (which takes 0.2 seconds when it runs without problems) the app starts without any issues
Looking at the logs I see that each time this error presents itself the interval between the start of the query and the error is 15 minutes.
I've also enabled the slow query log and those query are registered like this:
start_time: 2014-10-27 13:19:04
query_time: 00:00:00
lock_time: 00:00:00
rows_sent: 760
rows_examined: 1514
db: foobar
last_insert_id: 0
insert_id: 0
server_id: 1234567
sql_text: ...
Any ideas?
If your connection is idle for the 15 minute gap the you are probably seeing GCE disconnect your idle TCP connection, as described at https://cloud.google.com/compute/docs/troubleshooting#communicatewithinternet. Try the workaround that page suggests:
sudo /sbin/sysctl -w net.ipv4.tcp_keepalive_time=60 net.ipv4.tcp_keepalive_intvl=60 net.ipv4.tcp_keepalive_probes=5
(You may need to put this configuration into /etc/sysctl.conf to make it permanent)