Skip to content

Commit 6ec594e

Browse files
authored
postgresql_query: return a list containing execution time per query in milliseconds (ansible-collections#792)
1 parent b230f49 commit 6ec594e

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- postgresql_query - returns the `execution_time_ms` list containing execution time per query in milliseconds (https://github.com/ansible-collections/community.postgresql/issues/787).

plugins/modules/postgresql_query.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,19 @@
249249
returned: changed
250250
type: int
251251
sample: 5
252+
execution_time_ms:
253+
description:
254+
- A list containing execution time per query in milliseconds.
255+
- The measurements are done right before and after passing
256+
the query to the driver for execution.
257+
returned: success
258+
type: list
259+
sample: [7104]
260+
version_added: '3.10.0'
252261
'''
253262

254263
import re
264+
import time
255265

256266
from ansible.module_utils._text import to_native
257267
from ansible.module_utils.basic import AnsibleModule
@@ -285,6 +295,18 @@
285295
#
286296

287297

298+
def execute_and_return_time(cursor, query, args):
299+
# Measure query execution time in milliseconds as requested in
300+
# https://github.com/ansible-collections/community.postgresql/issues/787
301+
start_time = time.perf_counter()
302+
303+
cursor.execute(query, args)
304+
305+
# Calculate the execution time rounding it to 4 decimal places
306+
exec_time_ms = round((time.perf_counter() - start_time) * 1000, 4)
307+
return cursor, exec_time_ms
308+
309+
288310
def insane_query(string):
289311
for c in string:
290312
if c not in (' ', '\n', '', '\t'):
@@ -367,14 +389,19 @@ def main():
367389
changed = False
368390

369391
query_all_results = []
392+
execution_time_ms = []
370393
rowcount = 0
371394
statusmessage = ''
372395

373396
# Execute query:
374397
for query in query_list:
375398
try:
376399
current_query_txt = cursor.mogrify(query, args)
377-
cursor.execute(query, args)
400+
401+
cursor, exec_time_ms = execute_and_return_time(cursor, query, args)
402+
403+
execution_time_ms.append(exec_time_ms)
404+
378405
statusmessage = cursor.statusmessage
379406
if cursor.rowcount > 0:
380407
rowcount += cursor.rowcount
@@ -444,6 +471,7 @@ def main():
444471
query_result=query_result,
445472
query_all_results=query_all_results,
446473
rowcount=rowcount,
474+
execution_time_ms=execution_time_ms,
447475
)
448476

449477
cursor.close()

tests/integration/targets/postgresql_query/tasks/postgresql_query_initial.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
- result.statusmessage == 'ANALYZE'
4444
- result.query_result == {}
4545
- result.query_all_results == [{}]
46+
- result.execution_time_ms[0] > 0
4647

4748
- name: postgresql_query - simple select query to test_table
4849
become_user: '{{ pg_user }}'
@@ -524,6 +525,7 @@
524525
- assert:
525526
that:
526527
- result.rowcount == 1
528+
- result.execution_time_ms[0] > 0
527529

528530
#############################################################################
529531
# Issue https://github.com/ansible-collections/community.postgresql/issues/47
@@ -575,6 +577,9 @@
575577
- result.rowcount == 3
576578
- result.query_result == [{"?column?": 1}]
577579
- 'result.query_all_results == [[{"?column?": 1}], [{"?column?": 1}], [{"?column?": 1}]]'
580+
- result.execution_time_ms[0] > 0
581+
- result.execution_time_ms[1] > 0
582+
- result.execution_time_ms[2] > 0
578583

579584
- name: Run SHOW query
580585
become_user: '{{ pg_user }}'
@@ -586,4 +591,4 @@
586591

587592
- assert:
588593
that:
589-
- result is not changed
594+
- result is not changed

0 commit comments

Comments
 (0)