4 SELECT oid AS datoid FROM pg_database WHERE datname = current_database() \gset
7 pg_advisory_xact_lock(1), pg_advisory_xact_lock_shared(2),
8 pg_advisory_xact_lock(1, 1), pg_advisory_xact_lock_shared(2, 2);
9 pg_advisory_xact_lock | pg_advisory_xact_lock_shared | pg_advisory_xact_lock | pg_advisory_xact_lock_shared
10 -----------------------+------------------------------+-----------------------+------------------------------
14 SELECT locktype, classid, objid, objsubid, mode, granted
15 FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid
16 ORDER BY classid, objid, objsubid;
17 locktype | classid | objid | objsubid | mode | granted
18 ----------+---------+-------+----------+---------------+---------
19 advisory | 0 | 1 | 1 | ExclusiveLock | t
20 advisory | 0 | 2 | 1 | ShareLock | t
21 advisory | 1 | 1 | 2 | ExclusiveLock | t
22 advisory | 2 | 2 | 2 | ShareLock | t
25 -- pg_advisory_unlock_all() shouldn't release xact locks
26 SELECT pg_advisory_unlock_all();
27 pg_advisory_unlock_all
28 ------------------------
32 SELECT count(*) FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid;
38 -- can't unlock xact locks
40 pg_advisory_unlock(1), pg_advisory_unlock_shared(2),
41 pg_advisory_unlock(1, 1), pg_advisory_unlock_shared(2, 2);
42 WARNING: you don't own a lock of type ExclusiveLock
43 WARNING: you don't own a lock of type ShareLock
44 WARNING: you don't own a lock of type ExclusiveLock
45 WARNING: you don't own a lock of type ShareLock
46 pg_advisory_unlock | pg_advisory_unlock_shared | pg_advisory_unlock | pg_advisory_unlock_shared
47 --------------------+---------------------------+--------------------+---------------------------
51 -- automatically release xact locks at commit
53 SELECT count(*) FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid;
60 -- holding both session and xact locks on the same objects, xact first
62 pg_advisory_xact_lock(1), pg_advisory_xact_lock_shared(2),
63 pg_advisory_xact_lock(1, 1), pg_advisory_xact_lock_shared(2, 2);
64 pg_advisory_xact_lock | pg_advisory_xact_lock_shared | pg_advisory_xact_lock | pg_advisory_xact_lock_shared
65 -----------------------+------------------------------+-----------------------+------------------------------
69 SELECT locktype, classid, objid, objsubid, mode, granted
70 FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid
71 ORDER BY classid, objid, objsubid;
72 locktype | classid | objid | objsubid | mode | granted
73 ----------+---------+-------+----------+---------------+---------
74 advisory | 0 | 1 | 1 | ExclusiveLock | t
75 advisory | 0 | 2 | 1 | ShareLock | t
76 advisory | 1 | 1 | 2 | ExclusiveLock | t
77 advisory | 2 | 2 | 2 | ShareLock | t
81 pg_advisory_lock(1), pg_advisory_lock_shared(2),
82 pg_advisory_lock(1, 1), pg_advisory_lock_shared(2, 2);
83 pg_advisory_lock | pg_advisory_lock_shared | pg_advisory_lock | pg_advisory_lock_shared
84 ------------------+-------------------------+------------------+-------------------------
89 SELECT locktype, classid, objid, objsubid, mode, granted
90 FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid
91 ORDER BY classid, objid, objsubid;
92 locktype | classid | objid | objsubid | mode | granted
93 ----------+---------+-------+----------+---------------+---------
94 advisory | 0 | 1 | 1 | ExclusiveLock | t
95 advisory | 0 | 2 | 1 | ShareLock | t
96 advisory | 1 | 1 | 2 | ExclusiveLock | t
97 advisory | 2 | 2 | 2 | ShareLock | t
100 -- unlocking session locks
102 pg_advisory_unlock(1), pg_advisory_unlock(1),
103 pg_advisory_unlock_shared(2), pg_advisory_unlock_shared(2),
104 pg_advisory_unlock(1, 1), pg_advisory_unlock(1, 1),
105 pg_advisory_unlock_shared(2, 2), pg_advisory_unlock_shared(2, 2);
106 WARNING: you don't own a lock of type ExclusiveLock
107 WARNING: you don't own a lock of type ShareLock
108 WARNING: you don't own a lock of type ExclusiveLock
109 WARNING: you don't own a lock of type ShareLock
110 pg_advisory_unlock | pg_advisory_unlock | pg_advisory_unlock_shared | pg_advisory_unlock_shared | pg_advisory_unlock | pg_advisory_unlock | pg_advisory_unlock_shared | pg_advisory_unlock_shared
111 --------------------+--------------------+---------------------------+---------------------------+--------------------+--------------------+---------------------------+---------------------------
112 t | f | t | f | t | f | t | f
115 SELECT count(*) FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid;
122 -- holding both session and xact locks on the same objects, session first
124 pg_advisory_lock(1), pg_advisory_lock_shared(2),
125 pg_advisory_lock(1, 1), pg_advisory_lock_shared(2, 2);
126 pg_advisory_lock | pg_advisory_lock_shared | pg_advisory_lock | pg_advisory_lock_shared
127 ------------------+-------------------------+------------------+-------------------------
131 SELECT locktype, classid, objid, objsubid, mode, granted
132 FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid
133 ORDER BY classid, objid, objsubid;
134 locktype | classid | objid | objsubid | mode | granted
135 ----------+---------+-------+----------+---------------+---------
136 advisory | 0 | 1 | 1 | ExclusiveLock | t
137 advisory | 0 | 2 | 1 | ShareLock | t
138 advisory | 1 | 1 | 2 | ExclusiveLock | t
139 advisory | 2 | 2 | 2 | ShareLock | t
143 pg_advisory_xact_lock(1), pg_advisory_xact_lock_shared(2),
144 pg_advisory_xact_lock(1, 1), pg_advisory_xact_lock_shared(2, 2);
145 pg_advisory_xact_lock | pg_advisory_xact_lock_shared | pg_advisory_xact_lock | pg_advisory_xact_lock_shared
146 -----------------------+------------------------------+-----------------------+------------------------------
151 SELECT locktype, classid, objid, objsubid, mode, granted
152 FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid
153 ORDER BY classid, objid, objsubid;
154 locktype | classid | objid | objsubid | mode | granted
155 ----------+---------+-------+----------+---------------+---------
156 advisory | 0 | 1 | 1 | ExclusiveLock | t
157 advisory | 0 | 2 | 1 | ShareLock | t
158 advisory | 1 | 1 | 2 | ExclusiveLock | t
159 advisory | 2 | 2 | 2 | ShareLock | t
162 -- releasing all session locks
163 SELECT pg_advisory_unlock_all();
164 pg_advisory_unlock_all
165 ------------------------
169 SELECT count(*) FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid;
176 -- grabbing txn locks multiple times
178 pg_advisory_xact_lock(1), pg_advisory_xact_lock(1),
179 pg_advisory_xact_lock_shared(2), pg_advisory_xact_lock_shared(2),
180 pg_advisory_xact_lock(1, 1), pg_advisory_xact_lock(1, 1),
181 pg_advisory_xact_lock_shared(2, 2), pg_advisory_xact_lock_shared(2, 2);
182 pg_advisory_xact_lock | pg_advisory_xact_lock | pg_advisory_xact_lock_shared | pg_advisory_xact_lock_shared | pg_advisory_xact_lock | pg_advisory_xact_lock | pg_advisory_xact_lock_shared | pg_advisory_xact_lock_shared
183 -----------------------+-----------------------+------------------------------+------------------------------+-----------------------+-----------------------+------------------------------+------------------------------
187 SELECT locktype, classid, objid, objsubid, mode, granted
188 FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid
189 ORDER BY classid, objid, objsubid;
190 locktype | classid | objid | objsubid | mode | granted
191 ----------+---------+-------+----------+---------------+---------
192 advisory | 0 | 1 | 1 | ExclusiveLock | t
193 advisory | 0 | 2 | 1 | ShareLock | t
194 advisory | 1 | 1 | 2 | ExclusiveLock | t
195 advisory | 2 | 2 | 2 | ShareLock | t
199 SELECT count(*) FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid;
205 -- grabbing session locks multiple times
207 pg_advisory_lock(1), pg_advisory_lock(1),
208 pg_advisory_lock_shared(2), pg_advisory_lock_shared(2),
209 pg_advisory_lock(1, 1), pg_advisory_lock(1, 1),
210 pg_advisory_lock_shared(2, 2), pg_advisory_lock_shared(2, 2);
211 pg_advisory_lock | pg_advisory_lock | pg_advisory_lock_shared | pg_advisory_lock_shared | pg_advisory_lock | pg_advisory_lock | pg_advisory_lock_shared | pg_advisory_lock_shared
212 ------------------+------------------+-------------------------+-------------------------+------------------+------------------+-------------------------+-------------------------
216 SELECT locktype, classid, objid, objsubid, mode, granted
217 FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid
218 ORDER BY classid, objid, objsubid;
219 locktype | classid | objid | objsubid | mode | granted
220 ----------+---------+-------+----------+---------------+---------
221 advisory | 0 | 1 | 1 | ExclusiveLock | t
222 advisory | 0 | 2 | 1 | ShareLock | t
223 advisory | 1 | 1 | 2 | ExclusiveLock | t
224 advisory | 2 | 2 | 2 | ShareLock | t
228 pg_advisory_unlock(1), pg_advisory_unlock(1),
229 pg_advisory_unlock_shared(2), pg_advisory_unlock_shared(2),
230 pg_advisory_unlock(1, 1), pg_advisory_unlock(1, 1),
231 pg_advisory_unlock_shared(2, 2), pg_advisory_unlock_shared(2, 2);
232 pg_advisory_unlock | pg_advisory_unlock | pg_advisory_unlock_shared | pg_advisory_unlock_shared | pg_advisory_unlock | pg_advisory_unlock | pg_advisory_unlock_shared | pg_advisory_unlock_shared
233 --------------------+--------------------+---------------------------+---------------------------+--------------------+--------------------+---------------------------+---------------------------
234 t | t | t | t | t | t | t | t
237 SELECT count(*) FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid;
243 -- .. and releasing them all at once
245 pg_advisory_lock(1), pg_advisory_lock(1),
246 pg_advisory_lock_shared(2), pg_advisory_lock_shared(2),
247 pg_advisory_lock(1, 1), pg_advisory_lock(1, 1),
248 pg_advisory_lock_shared(2, 2), pg_advisory_lock_shared(2, 2);
249 pg_advisory_lock | pg_advisory_lock | pg_advisory_lock_shared | pg_advisory_lock_shared | pg_advisory_lock | pg_advisory_lock | pg_advisory_lock_shared | pg_advisory_lock_shared
250 ------------------+------------------+-------------------------+-------------------------+------------------+------------------+-------------------------+-------------------------
254 SELECT locktype, classid, objid, objsubid, mode, granted
255 FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid
256 ORDER BY classid, objid, objsubid;
257 locktype | classid | objid | objsubid | mode | granted
258 ----------+---------+-------+----------+---------------+---------
259 advisory | 0 | 1 | 1 | ExclusiveLock | t
260 advisory | 0 | 2 | 1 | ShareLock | t
261 advisory | 1 | 1 | 2 | ExclusiveLock | t
262 advisory | 2 | 2 | 2 | ShareLock | t
265 SELECT pg_advisory_unlock_all();
266 pg_advisory_unlock_all
267 ------------------------
271 SELECT count(*) FROM pg_locks WHERE locktype = 'advisory' AND database = :datoid;