uranusjr commented on code in PR #65618:
URL: https://github.com/apache/airflow/pull/65618#discussion_r3975804374


##########
providers/postgres/src/airflow/providers/postgres/hooks/postgres.py:
##########
@@ -296,16 +296,47 @@ def get_conn(self) -> CompatConnection:
             if arg_name not in self.ignored_extra_options:
                 conn_args[arg_name] = arg_val
 
-        raw_cursor = conn.extra_dejson.get("cursor")
+        return conn_args, conn
 
+    def get_conn(self) -> CompatConnection:
+        """Establish a connection to a postgres database."""
+        conn_args, conn = self._build_conn_args()
+        raw_cursor = conn.extra_dejson.get("cursor")
         if raw_cursor:
             key, value = self._get_cursor_config(raw_cursor)
             conn_args[key] = value
-
         self.conn = self._create_connection(conn_args)
-
         return self.conn
 
+    async def aget_conn(self) -> Any:
+        """Establish an async connection to a postgres database."""
+        if not USE_PSYCOPG3:
+            raise NotImplementedError("Async connections for PostgresHook 
require psycopg3.")
+        from psycopg import AsyncConnection
+
+        conn_args, conn = self._build_conn_args()
+
+        raw_cursor = conn.extra_dejson.get("cursor")
+        if raw_cursor:
+            conn_args["row_factory"] = self._get_cursor(raw_cursor)
+
+        # Use Any type for the connection args to avoid type conflicts

Review Comment:
   ```suggestion
   ```



##########
providers/postgres/src/airflow/providers/postgres/hooks/postgres.py:
##########
@@ -296,16 +296,47 @@ def get_conn(self) -> CompatConnection:
             if arg_name not in self.ignored_extra_options:
                 conn_args[arg_name] = arg_val
 
-        raw_cursor = conn.extra_dejson.get("cursor")
+        return conn_args, conn
 
+    def get_conn(self) -> CompatConnection:
+        """Establish a connection to a postgres database."""
+        conn_args, conn = self._build_conn_args()
+        raw_cursor = conn.extra_dejson.get("cursor")
         if raw_cursor:
             key, value = self._get_cursor_config(raw_cursor)
             conn_args[key] = value
-
         self.conn = self._create_connection(conn_args)
-
         return self.conn
 
+    async def aget_conn(self) -> Any:
+        """Establish an async connection to a postgres database."""
+        if not USE_PSYCOPG3:
+            raise NotImplementedError("Async connections for PostgresHook 
require psycopg3.")
+        from psycopg import AsyncConnection
+
+        conn_args, conn = self._build_conn_args()
+
+        raw_cursor = conn.extra_dejson.get("cursor")
+        if raw_cursor:
+            conn_args["row_factory"] = self._get_cursor(raw_cursor)
+
+        # Use Any type for the connection args to avoid type conflicts
+        connection = await AsyncConnection.connect(**cast("Any", conn_args))
+
+        # Register JSON handlers for both json and jsonb types
+        # This ensures JSON data is properly decoded from bytes to Python 
objects
+        register_default_adapters(connection)
+
+        # Add the notice handler AFTER the connection is established

Review Comment:
   ```suggestion
   ```



##########
providers/postgres/src/airflow/providers/postgres/hooks/postgres.py:
##########
@@ -296,16 +296,47 @@ def get_conn(self) -> CompatConnection:
             if arg_name not in self.ignored_extra_options:
                 conn_args[arg_name] = arg_val
 
-        raw_cursor = conn.extra_dejson.get("cursor")
+        return conn_args, conn
 
+    def get_conn(self) -> CompatConnection:
+        """Establish a connection to a postgres database."""
+        conn_args, conn = self._build_conn_args()
+        raw_cursor = conn.extra_dejson.get("cursor")
         if raw_cursor:
             key, value = self._get_cursor_config(raw_cursor)
             conn_args[key] = value
-
         self.conn = self._create_connection(conn_args)
-
         return self.conn
 
+    async def aget_conn(self) -> Any:
+        """Establish an async connection to a postgres database."""
+        if not USE_PSYCOPG3:
+            raise NotImplementedError("Async connections for PostgresHook 
require psycopg3.")
+        from psycopg import AsyncConnection
+
+        conn_args, conn = self._build_conn_args()
+
+        raw_cursor = conn.extra_dejson.get("cursor")
+        if raw_cursor:
+            conn_args["row_factory"] = self._get_cursor(raw_cursor)
+
+        # Use Any type for the connection args to avoid type conflicts
+        connection = await AsyncConnection.connect(**cast("Any", conn_args))
+
+        # Register JSON handlers for both json and jsonb types
+        # This ensures JSON data is properly decoded from bytes to Python 
objects

Review Comment:
   ```suggestion
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to