66import time
77from typing import Dict , List , Optional , Tuple , Union # noqa: F401
88
9- import psycopg
10- from psycopg .rows import dict_row
9+ import psycopg2
10+
11+ from datadog_checks .postgres .cursor import CommenterDictCursor
1112
1213try :
1314 import datadog_agent
6061"""
6162
6263PG_EXTENSIONS_QUERY = """
63- SELECT extname, nspname schemaname
64- FROM pg_extension left join pg_namespace on extnamespace = pg_namespace.oid;
64+ SELECT extname, nspname schemaname FROM pg_extension left join pg_namespace on extnamespace = pg_namespace.oid;
6565"""
6666
6767PG_EXTENSION_LOADER_QUERY = {
@@ -256,7 +256,7 @@ def __init__(self, check, config, shutdown_callback):
256256 enabled = is_affirmative (config .resources_metadata_config .get ("enabled" , True )),
257257 dbms = "postgres" ,
258258 min_collection_interval = config .min_collection_interval ,
259- expected_db_exceptions = (psycopg .errors .DatabaseError ,),
259+ expected_db_exceptions = (psycopg2 .errors .DatabaseError ,),
260260 job_name = "database-metadata" ,
261261 shutdown_callback = shutdown_callback ,
262262 )
@@ -319,7 +319,7 @@ def report_postgres_extensions(self):
319319 @tracked_method (agent_check_getter = agent_check_getter )
320320 def _collect_postgres_extensions (self ):
321321 with self ._check ._get_main_db () as conn :
322- with conn .cursor (row_factory = dict_row ) as cursor :
322+ with conn .cursor (cursor_factory = CommenterDictCursor ) as cursor :
323323 self ._time_since_last_extension_query = time .time ()
324324
325325 # Get loaded extensions
@@ -394,7 +394,7 @@ def _collect_postgres_schemas(self):
394394 continue
395395
396396 with self .db_pool .get_connection (dbname , self ._config .idle_connection_timeout ) as conn :
397- with conn .cursor (row_factory = dict_row ) as cursor :
397+ with conn .cursor (cursor_factory = psycopg2 . extras . DictCursor ) as cursor :
398398 for schema in database ["schemas" ]:
399399 if not self ._should_collect_metadata (schema ["name" ], "schema" ):
400400 continue
@@ -510,7 +510,9 @@ def _collect_schema_info(self):
510510 self ._last_schemas_query_time = time .time ()
511511 return metadata
512512
513- def _query_database_information (self , cursor : psycopg .Cursor , dbname : str ) -> Dict [str , Union [str , int ]]:
513+ def _query_database_information (
514+ self , cursor : psycopg2 .extensions .cursor , dbname : str
515+ ) -> Dict [str , Union [str , int ]]:
514516 """
515517 Collect database info. Returns
516518 description: str
@@ -523,7 +525,7 @@ def _query_database_information(self, cursor: psycopg.Cursor, dbname: str) -> Di
523525 row = cursor .fetchone ()
524526 return row
525527
526- def _query_schema_information (self , cursor : psycopg . Cursor , dbname : str ) -> Dict [str , str ]:
528+ def _query_schema_information (self , cursor : psycopg2 . extensions . cursor , dbname : str ) -> Dict [str , str ]:
527529 """
528530 Collect user schemas. Returns
529531 id: str
@@ -639,7 +641,7 @@ def sort_tables(info):
639641 return table_info [:limit ]
640642
641643 def _query_tables_for_schema (
642- self , cursor : psycopg . Cursor , schema_id : str , dbname : str
644+ self , cursor : psycopg2 . extensions . cursor , schema_id : str , dbname : str
643645 ) -> List [Dict [str , Union [str , Dict ]]]:
644646 """
645647 Collect list of tables for a schema. Returns a list of dictionaries
@@ -670,7 +672,7 @@ def _query_tables_for_schema(
670672 return table_payloads
671673
672674 def _query_table_information (
673- self , cursor : psycopg . Cursor , schema_name : str , table_info : List [Dict [str , Union [str , bool ]]]
675+ self , cursor : psycopg2 . extensions . cursor , schema_name : str , table_info : List [Dict [str , Union [str , bool ]]]
674676 ) -> List [Dict [str , Union [str , Dict ]]]:
675677 """
676678 Collect table information . Returns a dictionary
@@ -750,7 +752,7 @@ def _query_table_information(
750752 def _collect_metadata_for_database (self , dbname ):
751753 metadata = {}
752754 with self .db_pool .get_connection (dbname , self ._config .idle_connection_timeout ) as conn :
753- with conn .cursor (row_factory = dict_row ) as cursor :
755+ with conn .cursor (cursor_factory = psycopg2 . extras . DictCursor ) as cursor :
754756 database_info = self ._query_database_information (cursor , dbname )
755757 metadata .update (
756758 {
@@ -771,7 +773,7 @@ def _collect_metadata_for_database(self, dbname):
771773 @tracked_method (agent_check_getter = agent_check_getter )
772774 def _collect_postgres_settings (self ):
773775 with self ._check ._get_main_db () as conn :
774- with conn .cursor (row_factory = dict_row ) as cursor :
776+ with conn .cursor (cursor_factory = CommenterDictCursor ) as cursor :
775777 # Get loaded extensions
776778 cursor .execute (PG_EXTENSIONS_QUERY )
777779 rows = cursor .fetchall ()
@@ -798,14 +800,6 @@ def _collect_postgres_settings(self):
798800 )
799801 self ._time_since_last_settings_query = time .time ()
800802 cursor .execute (query , (self .pg_settings_ignored_patterns ,))
801- # pg3 returns a set of results for each statement in the multiple statement query
802- # We want to retrieve the last one that actually has the settings results
803- rows = []
804- has_more_results = True
805- while has_more_results :
806- if cursor .pgresult .status == psycopg .pq .ExecStatus .TUPLES_OK :
807- rows = cursor .fetchall ()
808- has_more_results = cursor .nextset ()
809- self ._log .debug ("Loaded %s rows from pg_settings" , rows )
803+ rows = cursor .fetchall ()
810804 self ._log .debug ("Loaded %s rows from pg_settings" , len (rows ))
811- return rows
805+ return [ dict ( row ) for row in rows ]
0 commit comments