1 /*
2  * lock_graph.h
3  *
4  * Data structures and functions for gathering lock graphs between
5  * distributed transactions.
6  *
7  * Copyright (c) Citus Data, Inc.
8  *
9  *-------------------------------------------------------------------------
10  */
11 
12 #ifndef LOCK_GRAPH_H
13 #define LOCK_GRAPH_H
14 
15 
16 #include "postgres.h"
17 #include "libpq-fe.h"
18 
19 #include "datatype/timestamp.h"
20 #include "distributed/backend_data.h"
21 #include "storage/lock.h"
22 
23 
24 /*
25  * Describes an edge in a waiting-for graph of locks.  This isn't used for
26  * deadlock-checking directly, but to gather the information necessary to
27  * do so.
28  *
29  * The datatypes here are a bit looser than strictly necessary, because
30  * they're transported as the return type from an SQL function.
31  */
32 typedef struct WaitEdge
33 {
34 	int waitingPid;
35 	int waitingNodeId;
36 	int64 waitingTransactionNum;
37 	TimestampTz waitingTransactionStamp;
38 
39 	int blockingPid;
40 	int blockingNodeId;
41 	int64 blockingTransactionNum;
42 	TimestampTz blockingTransactionStamp;
43 
44 	/* blocking transaction is also waiting on a lock */
45 	bool isBlockingXactWaiting;
46 } WaitEdge;
47 
48 
49 /*
50  * WaitGraph represent a graph of wait edges as an adjacency list.
51  */
52 typedef struct WaitGraph
53 {
54 	int localNodeId;
55 	int allocatedSize;
56 	int edgeCount;
57 	WaitEdge *edges;
58 } WaitGraph;
59 
60 
61 extern WaitGraph * BuildGlobalWaitGraph(void);
62 extern bool IsProcessWaitingForLock(PGPROC *proc);
63 extern bool IsInDistributedTransaction(BackendData *backendData);
64 extern TimestampTz ParseTimestampTzField(PGresult *result, int rowIndex, int colIndex);
65 extern int64 ParseIntField(PGresult *result, int rowIndex, int colIndex);
66 
67 /* some utility function to parse results */
68 extern int64 ParseIntField(PGresult *result, int rowIndex, int colIndex);
69 extern bool ParseBoolField(PGresult *result, int rowIndex, int colIndex);
70 extern TimestampTz ParseTimestampTzField(PGresult *result, int rowIndex, int colIndex);
71 
72 
73 #endif /* LOCK_GRAPH_H */
74