Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix(pam): fix stale transaction state for multi-statement SQL and query errors

Bernie Gandin2b5e3017b4e0
diff --git a/backend/src/ee/services/pam-web-access/pam-postgres-session-handler.ts b/backend/src/ee/services/pam-web-access/pam-postgres-session-handler.ts
index 0d40c36b63..2fa159d98d 100644
--- a/backend/src/ee/services/pam-web-access/pam-postgres-session-handler.ts
+++ b/backend/src/ee/services/pam-web-access/pam-postgres-session-handler.ts
@@ -83,6 +83,10 @@ export const handlePostgresSession = async (
}
};
+ // Server-side transaction state — updated after every query so the client
+ // always receives the authoritative value, including for multi-statement SQL.
+ let isInTransaction = false;
+
// Shared error handler for correlated query messages
const sendQueryError = async (id: string, err: unknown) => {
const pgErr = err as { message?: string; detail?: string; hint?: string };
@@ -95,6 +99,8 @@ export const handlePostgresSession = async (
// ROLLBACK fails if there was no active transaction — safe to ignore.
}
+ isInTransaction = false;
+
sendResponse({
type: PostgresServerMessageType.Error,
id,
@@ -237,7 +243,16 @@ export const handlePostgresSession = async (
| pg.QueryResult<Record<string, unknown>>[];
const executionTimeMs = Math.round(performance.now() - startTime);
const MAX_ROWS = 1000;
- const result = Array.isArray(rawResult) ? rawResult[rawResult.length - 1] : rawResult;
+ // Scan all results to track transaction state accurately for multi-statement SQL.
+ // e.g. "BEGIN; INSERT INTO foo VALUES (1);" — last command is INSERT, not BEGIN,
+ // so checking only the last result would leave isInTransaction false incorrectly.
+ const allResults = Array.isArray(rawResult) ? rawResult : [rawResult];
+ for (const r of allResults) {
+ const cmd = (r.command ?? "").toUpperCase();
+ if (cmd === "BEGIN" || cmd === "START") isInTransaction = true;
+ if (cmd === "COMMIT" || cmd === "ROLLBACK") isInTransaction = false;
+ }
+ const result = allResults[allResults.length - 1];
const allRows = result.rows ?? [];
const isTruncated = allRows.length > MAX_ROWS;
sendResponse({
@@ -247,6 +262,7 @@ export const handlePostgresSession = async (
fields: (result.fields ?? []).map((f) => ({ name: f.name })),
rowCount: result.rowCount,
isTruncated,
+ transactionOpen: isInTransaction,
command: result.command ?? "",
executionTimeMs
});
diff --git a/backend/src/ee/services/pam-web-access/pam-postgres-ws-types.ts b/backend/src/ee/services/pam-web-access/pam-postgres-ws-types.ts
index 4456b46250..bda4395a9e 100644
--- a/backend/src/ee/services/pam-web-access/pam-postgres-ws-types.ts
+++ b/backend/src/ee/services/pam-web-access/pam-postgres-ws-types.ts
@@ -140,6 +140,7 @@ const QueryResultResponseSchema = CorrelatedBaseSchema.extend({
),
rowCount: z.number().nullable(),
isTruncated: z.boolean(),
+ transactionOpen: z.boolean(),
command: z.string(),
executionTimeMs: z.number()
});
diff --git a/frontend/src/pages/pam/PamDataExplorerPage/components/QueryPanel.tsx b/frontend/src/pages/pam/PamDataExplorerPage/components/QueryPanel.tsx
index e901ba7535..8d74d3c56d 100644
--- a/frontend/src/pages/pam/PamDataExplorerPage/components/QueryPanel.tsx
+++ b/frontend/src/pages/pam/PamDataExplorerPage/components/QueryPanel.tsx
@@ -13,6 +13,7 @@ type QueryResult = {
fields: FieldInfo[];
rowCount: number | null;
isTruncated: boolean;
+ transactionOpen: boolean;
command: string;
executionTimeMs: number;
};
@@ -25,9 +26,6 @@ type Props = {
onSqlChange: (sql: string) => void;
};
-const TRANSACTION_START_COMMANDS = new Set(["BEGIN", "START"]);
-const TRANSACTION_END_COMMANDS = new Set(["COMMIT", "ROLLBACK"]);
-
export function QueryPanel({ tab, executeQuery, cancelQuery, tableDetail, onSqlChange }: Props) {
const [isRunning, setIsRunning] = useState(false);
const [result, setResult] = useState<QueryResult | null>(null);
@@ -52,11 +50,10 @@ export function QueryPanel({ tab, executeQuery, cancelQuery, tableDetail, onSqlC
setError(null);
try {
const res = await executeQuery(sqlToRun);
- const cmd = res.command.toUpperCase();
- if (TRANSACTION_START_COMMANDS.has(cmd)) setIsInTransaction(true);
- if (TRANSACTION_END_COMMANDS.has(cmd)) setIsInTransaction(false);
+ setIsInTransaction(res.transactionOpen);
setResult(res);
} catch (err) {
+ setIsInTransaction(false);
setError(err instanceof Error ? err.message : String(err));
setResult(null);
} finally {
diff --git a/frontend/src/pages/pam/PamDataExplorerPage/data-explorer-types.ts b/frontend/src/pages/pam/PamDataExplorerPage/data-explorer-types.ts
index 98151f05a6..6ed264e1c8 100644
--- a/frontend/src/pages/pam/PamDataExplorerPage/data-explorer-types.ts
+++ b/frontend/src/pages/pam/PamDataExplorerPage/data-explorer-types.ts
@@ -17,6 +17,7 @@ export type DataExplorerServerMessage =
fields: FieldInfo[];
rowCount: number | null;
isTruncated: boolean;
+ transactionOpen: boolean;
command: string;
executionTimeMs: number;
}
diff --git a/frontend/src/pages/pam/PamDataExplorerPage/use-data-explorer-session.ts b/frontend/src/pages/pam/PamDataExplorerPage/use-data-explorer-session.ts
index c28c630068..3c1894b3a4 100644
--- a/frontend/src/pages/pam/PamDataExplorerPage/use-data-explorer-session.ts
+++ b/frontend/src/pages/pam/PamDataExplorerPage/use-data-explorer-session.ts
@@ -407,6 +407,7 @@ export const useDataExplorerSession = ({
fields: FieldInfo[];
rowCount: number | null;
isTruncated: boolean;
+ transactionOpen: boolean;
command: string;
executionTimeMs: number;
}> => {
@@ -419,6 +420,7 @@ export const useDataExplorerSession = ({
fields: resp.fields,
rowCount: resp.rowCount,
isTruncated: resp.isTruncated,
+ transactionOpen: resp.transactionOpen,
command: resp.command,
executionTimeMs: resp.executionTimeMs
};