forked from awslabs/amazon-redshift-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv_get_tbl_reads_and_writes.sql
73 lines (71 loc) · 2.82 KB
/
v_get_tbl_reads_and_writes.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
--DROP VIEW admin.v_get_tbl_reads_and_writes;
/**********************************************************************************************
Purpose: View to get the READ and WRITE operations per table for specific transactions.
This view should be used with a filter that limits the output for transaction IDs or query IDs.
This view will help to see what tables are operated on by transactions and to see how transactions
have dependencies between each other. The operation will be one of the
following:
- R if it is a read operation that was done
- W if it is a write operation that was done
- A if the query statement got aborted this could be due to a serializable isolation violation
the user will need to check the query manually.
Another output is the transaction actions (tx_action) which will be:
- R if the transaction is rolled back
- C if the transaction is committed
History:
2016-11-03 pvbouwel Created
**********************************************************************************************/
CREATE OR REPLACE VIEW admin.v_get_tbl_reads_and_writes
AS
WITH v_operations AS
( SELECT query, tbl, 'R' AS operation FROM stl_scan WHERE type=2 GROUP BY query, tbl
UNION
SELECT query, tbl, 'W' AS operation FROM stl_delete GROUP BY query, tbl
UNION
SELECT query, tbl, 'W' AS operation FROM stl_insert GROUP BY query, tbl
UNION
SELECT sq.query AS query, stc.table_id AS tbl, 'A' AS operation FROM stl_query sq LEFT JOIN stl_tr_conflict stc on sq.xid = stc.xact_id where aborted=1 GROUP BY query, tbl
),
v_end_of_transaction AS
( SELECT xid, MAX(tx_action) AS tx_action, MAX(endtime) as endtime FROM
( SELECT xid, 'R' AS tx_action, endtime FROM stl_utilitytext WHERE text ILIKE '%rollback%' OR text ILIKE '%aborted%'
UNION ALL
SELECT xid, 'C' AS tx_action, endtime FROM stl_utilitytext WHERE text ILIKE '%commit%' OR text ILIKE '%end%'
UNION ALL
SELECT xid, 'C' AS tx_action, endtime from stl_commit_stats WHERE node=-1
) GROUP BY xid
)
SELECT
xid
,query
,tbl
,operation
,statement_starttime
,statement_endtime
,transaction_endtime
,transaction_action
FROM (
SELECT
sq.xid as xid
,sq.query as query
,vo.tbl as tbl
,vo.operation as operation
,sq.starttime as statement_starttime
,sq.endtime as statement_endtime
,ve.endtime as transaction_endtime
,ve.tx_action as transaction_action
FROM stl_query sq
LEFT JOIN v_operations vo ON sq.query=vo.query
LEFT JOIN v_end_of_transaction ve ON sq.xid=ve.xid
UNION ALL
SELECT
xid as xid
,null as query
,null as tbl
,'C' as operation
,startqueue as statement_starttime
,endtime as statement_endtime
,endtime as transaction_endtime
,'C' as transaction_action
FROM stl_commit_stats where node=-1
) order by statement_starttime;