Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Chore/update connection string settings kaizen (#20658)

Joshen Lim401d6ad6e8da
diff --git a/apps/studio/components/interfaces/Settings/Database/ConnectionPooling/ConnectionPooling.tsx b/apps/studio/components/interfaces/Settings/Database/ConnectionPooling/ConnectionPooling.tsx
index 3c6a46adff4..e2022989fc1 100644
--- a/apps/studio/components/interfaces/Settings/Database/ConnectionPooling/ConnectionPooling.tsx
+++ b/apps/studio/components/interfaces/Settings/Database/ConnectionPooling/ConnectionPooling.tsx
@@ -31,6 +31,8 @@ import { usePoolingConfigurationUpdateMutation } from 'data/database/pooling-con
import { useProjectAddonsQuery } from 'data/subscriptions/project-addons-query'
import { useCheckPermissions, useStore } from 'hooks'
import { POOLING_OPTIMIZATIONS } from './ConnectionPooling.constants'
+import { SESSION_MODE_DESCRIPTION, TRANSACTION_MODE_DESCRIPTION } from '../Database.constants'
+import { useDatabaseSettingsStateSnapshot } from 'state/database-settings'
const formId = 'connection-pooling-form'
@@ -58,7 +60,8 @@ const FormSchema = z.object({
export const ConnectionPooling = () => {
const { ui } = useStore()
const { ref: projectRef } = useParams()
- const { project, isLoading: projectIsLoading } = useProjectContext()
+ const { project } = useProjectContext()
+ const snap = useDatabaseSettingsStateSnapshot()
const { data: addons } = useProjectAddonsQuery({ projectRef })
const computeInstance = addons?.selected_addons.find((addon) => addon.type === 'compute_instance')
@@ -74,9 +77,6 @@ export const ConnectionPooling = () => {
const connectionPoolingUnavailable =
!poolingInfo?.pgbouncer_enabled && poolingInfo?.pool_mode === null
- // [Joshen] TODO this needs to be obtained from BE as 26th Jan is when we'll start - projects will be affected at different rates
- const resolvesToIpV6 = !poolingInfo?.supavisor_enabled && false // Number(new Date()) > Number(dayjs.utc('01-26-2024', 'MM-DD-YYYY').toDate())
-
const canUpdateConnectionPoolingConfiguration = useCheckPermissions(
PermissionAction.UPDATE,
'projects',
@@ -245,42 +245,27 @@ export const ConnectionPooling = () => {
>
<p>Transaction mode</p>
<p className="text-xs text-foreground-lighter">
- Connection is assigned to the client for the duration of a
- transaction. Some session-based Postgres features such as prepared
- statements are not available with this option.
+ {TRANSACTION_MODE_DESCRIPTION}
</p>
</Listbox.Option>
<Listbox.Option key="session" label="Session" value="session">
<p>Session mode</p>
<p className="text-xs text-foreground-lighter">
- When a new client connects, a connection is assigned to the client
- until it disconnects. All Postgres features can be used with this
- option.
+ {SESSION_MODE_DESCRIPTION}
</p>
</Listbox.Option>
</Listbox>
</FormControl_Shadcn_>
<FormDescription_Shadcn_ className="col-start-5 col-span-8 flex flex-col gap-y-2">
<p>
- Specify when a connection can be returned to the pool. Please refer to
- our{' '}
- <a
- href="https://supabase.com/docs/guides/database/connecting-to-postgres#how-connection-pooling-works"
- target="_blank"
- rel="noreferrer"
- className="underline"
+ Specify when a connection can be returned to the pool.{' '}
+ <span
+ tabIndex={0}
+ onClick={() => snap.setShowPoolingModeHelper(true)}
+ className="cursor-pointer underline underline-offset-2"
>
- documentation
- </a>{' '}
- to find out the most suitable mode for your use case.
- </p>
- <p>
- If you're using{' '}
- <span className="text-foreground">prepared statements</span> in your
- database, you will need to either use the{' '}
- <span className="text-foreground">Session</span> pool mode or use port{' '}
- <span className="text-foreground">5432</span> in the connection
- string.
+ Unsure which pooling mode to use?
+ </span>
</p>
</FormDescription_Shadcn_>
<FormMessage_Shadcn_ className="col-start-5 col-span-8" />
diff --git a/apps/studio/components/interfaces/Settings/Database/Database.constants.ts b/apps/studio/components/interfaces/Settings/Database/Database.constants.ts
new file mode 100644
index 00000000000..6318c6148c9
--- /dev/null
+++ b/apps/studio/components/interfaces/Settings/Database/Database.constants.ts
@@ -0,0 +1,5 @@
+export const TRANSACTION_MODE_DESCRIPTION =
+ 'Connection is assigned to the client for the duration of a transaction. Some session-based Postgres features such as prepared statements are not available with this option.'
+
+export const SESSION_MODE_DESCRIPTION =
+ 'When a new client connects, a connection is assigned to the client until it disconnects. All Postgres features can be used with this option.'
diff --git a/apps/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseConnectionString.tsx b/apps/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseConnectionString.tsx
index 40cb5bac8b7..e4880f6187c 100644
--- a/apps/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseConnectionString.tsx
+++ b/apps/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseConnectionString.tsx
@@ -2,7 +2,7 @@ import * as Tooltip from '@radix-ui/react-tooltip'
import { useParams, useTelemetryProps } from 'common'
import { useRouter } from 'next/router'
import { useEffect, useRef, useState } from 'react'
-import { Button, IconExternalLink, Input, Separator, Tabs } from 'ui'
+import { Button, IconChevronDown, IconExternalLink, Input, Separator, Tabs } from 'ui'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import AlertError from 'components/ui/AlertError'
@@ -46,6 +46,10 @@ export const DatabaseConnectionString = () => {
const connectionStringsRef = useRef<HTMLDivElement>(null)
const [usePoolerConnection, setUsePoolerConnection] = useState(true)
+ const [poolingMode, setPoolingMode] = useState<'transaction' | 'session' | 'statement'>(
+ 'transaction'
+ )
+ const [showUriSyntax, setShowUriSyntax] = useState(false)
const [selectedTab, setSelectedTab] = useState<
'uri' | 'psql' | 'golang' | 'jdbc' | 'dotnet' | 'nodejs' | 'php' | 'python'
>('uri')
@@ -119,7 +123,9 @@ export const DatabaseConnectionString = () => {
region: isProjectLoading ? '' : project?.region || '',
tld: usePoolerConnection ? poolerTld : connectionTld,
portNumber: usePoolerConnection
- ? poolingInfo.db_port.toString()
+ ? poolingMode === 'transaction'
+ ? poolingInfo.db_port.toString()
+ : '5432'
: connectionInfo.db_port.toString(),
})
: []
@@ -135,6 +141,12 @@ export const DatabaseConnectionString = () => {
}
}, [connectionString])
+ useEffect(() => {
+ if (poolingInfo?.pool_mode !== undefined) {
+ setPoolingMode(poolingInfo.pool_mode)
+ }
+ }, [poolingInfo?.pool_mode])
+
return (
<div id="connection-string">
<Panel
@@ -177,7 +189,9 @@ export const DatabaseConnectionString = () => {
<UsePoolerCheckbox
id="connection-string"
checked={usePoolerConnection}
+ poolingMode={poolingMode}
onCheckedChange={setUsePoolerConnection}
+ onSelectPoolingMode={setPoolingMode}
/>
{!usePoolerConnection && <IPv4DeprecationNotice />}
<Input
@@ -185,12 +199,35 @@ export const DatabaseConnectionString = () => {
readOnly
disabled
className="input-mono [&>div>div>div>input]:text-xs [&>div>div>div>input]:opacity-100"
- value={connectionStrings[selectedTab]}
+ value={
+ poolingMode === 'transaction'
+ ? connectionStrings[selectedTab]
+ : connectionStrings[selectedTab].replace('6543', '5432')
+ }
onCopy={() => handleCopy(selectedTab)}
/>
- {poolerConnStringSyntax.length > 0 && poolingInfo?.supavisor_enabled && (
- <div className="flex flex-col gap-y-1 text-foreground-light">
- <p className="text-sm">
+ </div>
+ )}
+ </Panel.Content>
+ {poolerConnStringSyntax.length > 0 && poolingInfo?.supavisor_enabled && (
+ <>
+ <Separator />
+ <Panel.Content className="!py-3 space-y-2">
+ <div
+ className="flex items-center gap-x-2 transition cursor-pointer opacity-75 hover:opacity-100"
+ onClick={() => setShowUriSyntax(!showUriSyntax)}
+ >
+ <p className="text-xs text-foreground">
+ How to connect to a different database or switch to another user
+ </p>
+ <IconChevronDown
+ strokeWidth={1.5}
+ className={`transition ${showUriSyntax ? '-rotate-180' : ''}`}
+ />
+ </div>
+ {showUriSyntax && (
+ <div className="text-foreground-light">
+ <p className="text-xs">
You can use the following URI format to switch to a different database or user
{usePoolerConnection ? ' when using connection pooling' : ''}.
</p>
@@ -230,9 +267,9 @@ export const DatabaseConnectionString = () => {
</p>
</div>
)}
- </div>
- )}
- </Panel.Content>
+ </Panel.Content>
+ </>
+ )}
</Panel>
</div>
)
diff --git a/apps/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseSettings.tsx b/apps/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseSettings.tsx
index fffecfaa928..b067539b2ef 100644
--- a/apps/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseSettings.tsx
+++ b/apps/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseSettings.tsx
@@ -36,6 +36,9 @@ const DatabaseSettings = () => {
const showReadReplicasUI = readReplicasEnabled && selectedProject?.is_read_replicas_enabled
const connectionStringsRef = useRef<HTMLDivElement>(null)
const [usePoolerConnection, setUsePoolerConnection] = useState(true)
+ const [poolingMode, setPoolingMode] = useState<'transaction' | 'session' | 'statement'>(
+ 'transaction'
+ )
const {
data: poolingInfo,
@@ -112,6 +115,12 @@ const DatabaseSettings = () => {
}
}, [connectionString])
+ useEffect(() => {
+ if (poolingInfo?.pool_mode !== undefined) {
+ setPoolingMode(poolingInfo.pool_mode)
+ }
+ }, [poolingInfo?.pool_mode])
+
return (
<>
<section id="direct-connection">
@@ -144,7 +153,9 @@ const DatabaseSettings = () => {
<UsePoolerCheckbox
id="connection-params"
checked={usePoolerConnection}
+ poolingMode={poolingMode}
onCheckedChange={setUsePoolerConnection}
+ onSelectPoolingMode={setPoolingMode}
/>
{!usePoolerConnection && <IPv4DeprecationNotice />}
{isMd5 && (
@@ -188,7 +199,7 @@ const DatabaseSettings = () => {
readOnly
copy
disabled
- value={connectionInfo.db_port}
+ value={poolingMode === 'transaction' ? connectionInfo.db_port : '5432'}
label="Port"
/>
{isMd5 && (
diff --git a/apps/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseSettings.utils.ts b/apps/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseSettings.utils.ts
index a345fb287b8..bedff4cfde1 100644
--- a/apps/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseSettings.utils.ts
+++ b/apps/studio/components/interfaces/Settings/Database/DatabaseSettings/DatabaseSettings.utils.ts
@@ -58,6 +58,12 @@ export const getConnectionStrings = (
}
}
+const DB_USER_DESC = 'Database user (e.g postgres)'
+const DB_PASS_DESC = 'Database password'
+const DB_NAME_DESC = 'Database name (e.g postgres)'
+const PROJECT_REF_DESC = "Project's reference ID"
+const PORT_NUMBER_DESC = 'Port number (Use 5432 if using prepared statements)'
+
// [Joshen] This is to the best of interpreting the syntax from the API response
// // There's different format for PG13 (depending on authentication method being md5) and PG14
export const constructConnStringSyntax = (
@@ -89,7 +95,7 @@ export const constructConnStringSyntax = (
]
const dbHostDetails = [
{ value: 'db.', tooltip: undefined },
- { value: ref, tooltip: "Project's reference ID" },
+ { value: ref, tooltip: PROJECT_REF_DESC },
{ value: `.supabase.${tld}`, tooltip: undefined },
]
@@ -97,40 +103,40 @@ export const constructConnStringSyntax = (
if (isMd5) {
return [
{ value: 'postgres://', tooltip: undefined },
- { value: '[user]', tooltip: 'Database user (e.g postgres)' },
+ { value: '[user]', tooltip: DB_USER_DESC },
{ value: ':', tooltip: undefined },
- { value: '[password]', tooltip: 'Database password' },
+ { value: '[password]', tooltip: DB_PASS_DESC },
{ value: '@', tooltip: undefined },
...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
{ value: ':', tooltip: undefined },
- { value: portNumber, tooltip: 'Port number (Use 5432 if using prepared statements)' },
+ { value: portNumber, tooltip: PORT_NUMBER_DESC },
{ value: '/', tooltip: undefined },
- { value: '[db-name]', tooltip: 'Database name (e.g postgres)' },
+ { value: '[db-name]', tooltip: DB_NAME_DESC },
...(usePoolerConnection
? [
{ value: `?options=reference%3D`, tooltip: undefined },
- { value: ref, tooltip: "Project's reference ID" },
+ { value: ref, tooltip: PROJECT_REF_DESC },
]
: []),
]
} else {
return [
{ value: 'postgres://', tooltip: undefined },
- { value: '[user]', tooltip: 'Database user (e.g postgres)' },
+ { value: '[user]', tooltip: DB_USER_DESC },
...(usePoolerConnection
? [
{ value: '.', tooltip: undefined },
- { value: ref, tooltip: "Project's reference ID" },
+ { value: ref, tooltip: PROJECT_REF_DESC },
]
: []),
{ value: ':', tooltip: undefined },
- { value: '[password]', tooltip: 'Database password' },
+ { value: '[password]', tooltip: DB_PASS_DESC },
{ value: '@', tooltip: undefined },
...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
{ value: ':', tooltip: undefined },
- { value: portNumber, tooltip: 'Port number (Use 5432 if using prepared statements)' },
+ { value: portNumber, tooltip: PORT_NUMBER_DESC },
{ value: '/', tooltip: undefined },
- { value: '[db-name]', tooltip: 'Database name (e.g postgres)' },
+ { value: '[db-name]', tooltip: DB_NAME_DESC },
]
}
}
@@ -139,19 +145,19 @@ export const constructConnStringSyntax = (
if (isMd5) {
return [
{ value: 'psql "postgresql://', tooltip: undefined },
- { value: '[user]', tooltip: 'Database user (e.g postgres)' },
+ { value: '[user]', tooltip: DB_USER_DESC },
{ value: ':', tooltip: undefined },
- { value: '[password]', tooltip: 'Database password' },
+ { value: '[password]', tooltip: DB_PASS_DESC },
{ value: '@', tooltip: undefined },
...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
{ value: ':', tooltip: undefined },
- { value: portNumber, tooltip: 'Port number (Use 5432 if using prepared statements)' },
+ { value: portNumber, tooltip: PORT_NUMBER_DESC },
{ value: '/', tooltip: undefined },
- { value: '[db-name]', tooltip: 'Database name (e.g postgres)' },
+ { value: '[db-name]', tooltip: DB_NAME_DESC },
...(usePoolerConnection
? [
{ value: '?options=reference%3D', tooltip: undefined },
- { value: ref, tooltip: "Project's reference ID" },
+ { value: ref, tooltip: PROJECT_REF_DESC },
]
: []),
]
@@ -160,15 +166,15 @@ export const constructConnStringSyntax = (
{ value: 'psql -h ', tooltip: undefined },
...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
{ value: ' -p ', tooltip: undefined },
- { value: portNumber, tooltip: 'Port number (Use 5432 if using prepared statements)' },
+ { value: portNumber, tooltip: PORT_NUMBER_DESC },
{ value: ' -d ', tooltip: undefined },
- { value: '[db-name]', tooltip: 'Database name (e.g postgres)' },
+ { value: '[db-name]', tooltip: DB_NAME_DESC },
{ value: ' -U ', tooltip: undefined },
- { value: '[user]', tooltip: 'Database user (e.g postgres)' },
+ { value: '[user]', tooltip: DB_USER_DESC },
...(usePoolerConnection
? [
{ value: '.', tooltip: undefined },
- { value: ref, tooltip: "Project's reference ID" },
+ { value: ref, tooltip: PROJECT_REF_DESC },
]
: []),
]
@@ -179,40 +185,40 @@ export const constructConnStringSyntax = (
if (isMd5) {
return [
{ value: 'user=', tooltip: undefined },
- { value: '[user]', tooltip: 'Database user (e.g postgres)' },
+ { value: '[user]', tooltip: DB_USER_DESC },
{ value: ' password=', tooltip: undefined },
- { value: '[password]', tooltip: 'Database password' },
+ { value: '[password]', tooltip: DB_PASS_DESC },
{ value: ' host=', tooltip: undefined },
...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
{ value: ' port=', tooltip: undefined },
- { value: portNumber, tooltip: 'Port number (Use 5432 if using prepared statements)' },
+ { value: portNumber, tooltip: PORT_NUMBER_DESC },
{ value: ' dbname=', tooltip: undefined },
- { value: '[db-name]', tooltip: 'Database name (e.g postgres)' },
+ { value: '[db-name]', tooltip: DB_NAME_DESC },
...(usePoolerConnection
? [
{ value: ' options=reference=', tooltip: undefined },
- { value: ref, tooltip: "Project's reference ID" },
+ { value: ref, tooltip: PROJECT_REF_DESC },
]
: []),
]
} else {
return [
{ value: 'user=', tooltip: undefined },
- { value: '[user]', tooltip: 'Database user (e.g postgres)' },
+ { value: '[user]', tooltip: DB_USER_DESC },
...(usePoolerConnection
? [
{ value: '.', tooltip: undefined },
- { value: ref, tooltip: "Project's reference ID" },
+ { value: ref, tooltip: PROJECT_REF_DESC },
]
: []),
{ value: ' password=', tooltip: undefined },
- { value: '[password]', tooltip: 'Database password' },
+ { value: '[password]', tooltip: DB_PASS_DESC },
{ value: ' host=', tooltip: undefined },
...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
{ value: ' port=', tooltip: undefined },
- { value: portNumber, tooltip: 'Port number (Use 5432 if using prepared statements)' },
+ { value: portNumber, tooltip: PORT_NUMBER_DESC },
{ value: ' dbname=', tooltip: undefined },
- { value: '[db-name]', tooltip: 'Database name (e.g postgres)' },
+ { value: '[db-name]', tooltip: DB_NAME_DESC },
]
}
}
@@ -223,17 +229,17 @@ export const constructConnStringSyntax = (
{ value: 'jdbc:postgresql://', tooltip: undefined },
...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
{ value: ':', tooltip: undefined },
- { value: portNumber, tooltip: 'Port number (Use 5432 if using prepared statements)' },
+ { value: portNumber, tooltip: PORT_NUMBER_DESC },
{ value: '/', tooltip: undefined },
- { value: '[db-name]', tooltip: 'Database name (e.g postgres)' },
+ { value: '[db-name]', tooltip: DB_NAME_DESC },
{ value: '?user=', tooltip: undefined },
- { value: '[user]', tooltip: 'Database user (e.g postgres)' },
+ { value: '[user]', tooltip: DB_USER_DESC },
{ value: '&password=', tooltip: undefined },
- { value: '[password]', tooltip: 'Database password' },
+ { value: '[password]', tooltip: DB_PASS_DESC },
...(usePoolerConnection
? [
{ value: '&options=reference%3D', tooltip: undefined },
- { value: ref, tooltip: "Project's reference ID" },
+ { value: ref, tooltip: PROJECT_REF_DESC },
]
: []),
]
@@ -242,19 +248,19 @@ export const constructConnStringSyntax = (
{ value: 'jdbc:postgresql://', tooltip: undefined },
...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
{ value: `:`, tooltip: undefined },
- { value: portNumber, tooltip: 'Port number (Use 5432 if using prepared statements)' },
+ { value: portNumber, tooltip: PORT_NUMBER_DESC },
{ value: '/', tooltip: undefined },
- { value: '[db-name]', tooltip: 'Database name (e.g postgres)' },
+ { value: '[db-name]', tooltip: DB_NAME_DESC },
{ value: '?user=', tooltip: undefined },
- { value: '[user]', tooltip: 'Database user (e.g postgres)' },
+ { value: '[user]', tooltip: DB_USER_DESC },
...(usePoolerConnection
? [
{ value: '.', tooltip: undefined },
- { value: ref, tooltip: "Project's reference ID" },
+ { value: ref, tooltip: PROJECT_REF_DESC },
]
: []),
{ value: '&password=', tooltip: undefined },
- { value: '[password]', tooltip: 'Database password' },
+ { value: '[password]', tooltip: DB_PASS_DESC },
]
}
}
@@ -263,19 +269,19 @@ export const constructConnStringSyntax = (
if (isMd5) {
return [
{ value: 'User Id=', tooltip: undefined },
- { value: '[user]', tooltip: 'Database user (e.g postgres)' },
+ { value: '[user]', tooltip: DB_USER_DESC },
{ value: ';Password=', tooltip: undefined },
- { value: '[password]', tooltip: 'Database password' },
+ { value: '[password]', tooltip: DB_PASS_DESC },
{ value: ';Server=', tooltip: undefined },
...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
{ value: ';Port=', tooltip: undefined },
- { value: portNumber, tooltip: 'Port number (Use 5432 if using prepared statements)' },
+ { value: portNumber, tooltip: PORT_NUMBER_DESC },
{ value: ';Database=', tooltip: undefined },
- { value: '[db-name]', tooltip: 'Database name (e.g postgres)' },
+ { value: '[db-name]', tooltip: DB_NAME_DESC },
...(usePoolerConnection
? [
{ value: ";Options='reference=", tooltip: undefined },
- { value: ref, tooltip: "Project's reference ID" },
+ { value: ref, tooltip: PROJECT_REF_DESC },
{ value: "'", tooltip: undefined },
]
: []),
@@ -283,21 +289,21 @@ export const constructConnStringSyntax = (
} else {
return [
{ value: 'User Id=', tooltip: undefined },
- { value: '[user]', tooltip: 'Database user (e.g postgres)' },
+ { value: '[user]', tooltip: DB_USER_DESC },
...(usePoolerConnection
? [
{ value: '.', tooltip: undefined },
- { value: ref, tooltip: "Project's reference ID" },
+ { value: ref, tooltip: PROJECT_REF_DESC },
]
: []),
{ value: ';Password=', tooltip: undefined },
- { value: '[password]', tooltip: 'Database password' },
+ { value: '[password]', tooltip: DB_PASS_DESC },
{ value: ';Server=', tooltip: undefined },
...(usePoolerConnection ? poolerHostDetails : dbHostDetails),
{ value: ';Port=', tooltip: undefined },
- { value: portNumber, tooltip: 'Port number (Use 5432 if using prepared statements)' },
+ { value: portNumber, tooltip: PORT_NUMBER_DESC },
{ value: ';Database=', tooltip: undefined },
- { value: '[db-name]', tooltip: 'Database name (e.g postgres)' },
+ { value: '[db-name]', tooltip: DB_NAME_DESC },
]
}
}
diff --git a/apps/studio/components/interfaces/Settings/Database/PoolingModesModal.tsx b/apps/studio/components/interfaces/Settings/Database/PoolingModesModal.tsx
new file mode 100644
index 00000000000..1d0485dcfa6
--- /dev/null
+++ b/apps/studio/components/interfaces/Settings/Database/PoolingModesModal.tsx
@@ -0,0 +1,101 @@
+import { useParams } from 'common'
+import { Markdown } from 'components/interfaces/Markdown'
+import { usePoolingConfigurationQuery } from 'data/database/pooling-configuration-query'
+import { AlertTriangleIcon } from 'lucide-react'
+import { useDatabaseSettingsStateSnapshot } from 'state/database-settings'
+import {
+ AlertDescription_Shadcn_,
+ AlertTitle_Shadcn_,
+ Alert_Shadcn_,
+ Button,
+ IconExternalLink,
+ Modal,
+} from 'ui'
+
+export const PoolingModesModal = () => {
+ const { ref: projectRef } = useParams()
+ const snap = useDatabaseSettingsStateSnapshot()
+
+ const { data } = usePoolingConfigurationQuery({ projectRef: projectRef })
+
+ const navigateToPoolerSettings = () => {
+ const el = document.getElementById('connection-pooler')
+ if (el) el.scrollIntoView({ behavior: 'smooth', block: 'center' })
+ }
+
+ return (
+ <Modal
+ hideFooter
+ size="xlarge"
+ visible={snap.showPoolingModeHelper}
+ header={
+ <div className="w-full flex items-center justify-between">
+ <p>Which pooling mode should I use?</p>
+ <Button asChild type="default" icon={<IconExternalLink strokeWidth={1.5} />}>
+ <a
+ href="https://supabase.com/docs/guides/database/connecting-to-postgres#how-connection-pooling-works"
+ target="_blank"
+ rel="noreferrer"
+ >
+ Documentation
+ </a>
+ </Button>
+ </div>
+ }
+ onCancel={() => snap.setShowPoolingModeHelper(false)}
+ >
+ <Modal.Content className="py-4 text-sm flex flex-col gap-y-4">
+ <Markdown
+ className="max-w-full [&>h3]:text-sm"
+ content={`
+A connection pooler is a system (external to Postgres) which manages Postgres connections
+by allocating connections whenever clients make requests. Each pooling mode handles
+connections differently.
+
+### Transaction mode
+This mode is recommended if you are connecting from *serverless environments*. A connection is assigned to the client for the duration of a transaction. Two consecutive transactions from the same client could be executed over two different connections. Some session-based Postgres features such as prepared statements are *not available* with this option.
+
+### Session mode
+This mode is similar to connecting to your database directly. There is full support for prepared statements in this mode. When a new client connects, a connection is assigned to the client until it disconnects. You *might run into pooler connection limits* since the connection is held till the client disconnects.
+
+### Using session and transaction modes at the same time
+ ${
+ data?.pool_mode === 'transaction'
+ ? 'You can use the session mode connection string (port 5432) and transaction mode connection string (port 6543) in your application.'
+ : 'To get the best of both worlds, as a starting point, we recommend using session mode just when you need support for prepared statements and transaction mode in other cases.'
+ }
+`}
+ />
+ {data?.pool_mode === 'session' && (
+ <Alert_Shadcn_ variant="warning">
+ <AlertTriangleIcon strokeWidth={2} />
+ <AlertTitle_Shadcn_>
+ Pooling mode is currently configured to use session mode
+ </AlertTitle_Shadcn_>
+ <AlertDescription_Shadcn_>
+ To use transaction mode concurrently with session mode, change the pooling mode to
+ transaction first in the{' '}
+ <span
+ tabIndex={0}
+ className="text-foreground cursor-pointer underline underline-offset-2"
+ onClick={() => {
+ snap.setShowPoolingModeHelper(false)
+ navigateToPoolerSettings()
+ }}
+ >
+ connection pooling settings
+ </span>
+ . After this, you can use transaction mode on port 6543 and session mode on port 5432.
+ </AlertDescription_Shadcn_>
+ </Alert_Shadcn_>
+ )}
+ </Modal.Content>
+ <Modal.Separator />
+ <Modal.Content className="flex items-center justify-end pb-2">
+ <Button type="default" onClick={() => snap.setShowPoolingModeHelper(false)}>
+ Close
+ </Button>
+ </Modal.Content>
+ </Modal>
+ )
+}
diff --git a/apps/studio/components/interfaces/Settings/Database/UsePoolerCheckbox.tsx b/apps/studio/components/interfaces/Settings/Database/UsePoolerCheckbox.tsx
index 8a3a7b2c292..39dc5c8e03f 100644
--- a/apps/studio/components/interfaces/Settings/Database/UsePoolerCheckbox.tsx
+++ b/apps/studio/components/interfaces/Settings/Database/UsePoolerCheckbox.tsx
@@ -1,56 +1,187 @@
+import * as Tooltip from '@radix-ui/react-tooltip'
import { useParams } from 'common'
-import { Badge, Checkbox_Shadcn_ } from 'ui'
+import { useState } from 'react'
+import {
+ Badge,
+ Button,
+ Checkbox_Shadcn_,
+ CommandGroup_Shadcn_,
+ CommandItem_Shadcn_,
+ CommandList_Shadcn_,
+ Command_Shadcn_,
+ IconChevronDown,
+ PopoverContent_Shadcn_,
+ PopoverTrigger_Shadcn_,
+ Popover_Shadcn_,
+ Separator,
+} from 'ui'
-import { Markdown } from 'components/interfaces/Markdown'
-import { usePoolingConfigurationQuery } from 'data/database/pooling-configuration-query'
+import ShimmeringLoader from 'components/ui/ShimmeringLoader'
import { useProjectSettingsQuery } from 'data/config/project-settings-query'
+import { usePoolingConfigurationQuery } from 'data/database/pooling-configuration-query'
+import { useDatabaseSettingsStateSnapshot } from 'state/database-settings'
interface UsePoolerCheckboxInterface {
id: string
checked: boolean
+ poolingMode: 'transaction' | 'session' | 'statement'
onCheckedChange: (value: boolean) => void
+ onSelectPoolingMode: (mode: 'transaction' | 'session') => void
}
-export const UsePoolerCheckbox = ({ id, checked, onCheckedChange }: UsePoolerCheckboxInterface) => {
+export const UsePoolerCheckbox = ({
+ id,
+ checked,
+ poolingMode = 'transaction',
+ onCheckedChange,
+ onSelectPoolingMode,
+}: UsePoolerCheckboxInterface) => {
const { ref: projectRef } = useParams()
- const { data, isSuccess } = usePoolingConfigurationQuery({ projectRef })
+ const [open, setOpen] = useState(false)
+ const snap = useDatabaseSettingsStateSnapshot()
+
+ const { data, isLoading, isSuccess } = usePoolingConfigurationQuery({ projectRef })
const { data: settings, isSuccess: isSuccessSettings } = useProjectSettingsQuery({ projectRef })
const resolvesToIpV6 = !data?.supavisor_enabled && settings?.project.db_ip_addr_config === 'ipv6'
return (
- <div className="flex gap-x-3">
- <Checkbox_Shadcn_
- id={`use-pooler-${id}`}
- checked={checked}
- onCheckedChange={() => onCheckedChange(!checked)}
- />
- <div className="-mt-[2px] flex flex-col gap-y-1 w-full">
- <label htmlFor={`use-pooler-${id}`} className="text-sm cursor-pointer">
- Use connection pooling
- {isSuccess && checked && data.supavisor_enabled && (
- <Badge color="scale" className="ml-2">
- Supavisor
- </Badge>
- )}
- {isSuccessSettings && (
- <Badge color="scale" className="ml-2">
- {checked
- ? 'Resolves to IPv4'
- : resolvesToIpV6
- ? 'Resolves to IPv6'
- : 'Will resolve to IPv6'}
- </Badge>
- )}
- </label>
- <Markdown
- extLinks
- className="[&>p]:m-0 space-y-1 text-foreground-lighter max-w-full"
- content={`
-IPv4 and IPv6 connections will resolve while using connection pooling\n
-A connection pooler is useful for managing a large number of temporary connections. [Learn more](https://supabase.com/docs/guides/database/connecting-to-postgres#connection-pooler)`}
- />
+ <>
+ <div className="flex flex-col gap-y-1">
+ <div className="flex gap-x-3">
+ <Checkbox_Shadcn_
+ id={`use-pooler-${id}`}
+ checked={checked}
+ onCheckedChange={() => onCheckedChange(!checked)}
+ />
+ <div className="-mt-[2px] flex flex-col gap-y-1 w-full">
+ <div className="flex items-center gap-x-4">
+ <div className="flex items-center gap-x-2">
+ <label htmlFor={`use-pooler-${id}`} className="text-sm cursor-pointer">
+ Use connection pooling
+ </label>
+ {checked && (
+ <>
+ <div className="flex items-center gap-x-2">
+ {isLoading && <ShimmeringLoader className="w-[100px] py-2.5" />}
+ {isSuccess && (
+ <div className="flex items-center gap-x-1">
+ <Popover_Shadcn_ open={open} onOpenChange={setOpen} modal={false}>
+ <PopoverTrigger_Shadcn_ asChild>
+ <div className="flex items-center space-x-2 cursor-pointer">
+ <Button
+ type="outline"
+ className="py-0.5 pr-1.5"
+ iconRight={<IconChevronDown strokeWidth={1.5} />}
+ >
+ <span className="capitalize">Mode: {poolingMode}</span>
+ </Button>
+ </div>
+ </PopoverTrigger_Shadcn_>
+ <PopoverContent_Shadcn_
+ className="p-0 w-52"
+ side="bottom"
+ align="start"
+ >
+ <Command_Shadcn_>
+ <CommandList_Shadcn_>
+ <CommandGroup_Shadcn_>
+ <CommandItem_Shadcn_
+ disabled={data.pool_mode === 'session'}
+ value="transaction"
+ className={`w-full text-foreground ${
+ data.pool_mode === 'session'
+ ? 'cursor-default'
+ : 'cursor-pointer'
+ }`}
+ onSelect={() => {
+ onSelectPoolingMode('transaction')
+ setOpen(false)
+ }}
+ onClick={() => {
+ onSelectPoolingMode('transaction')
+ setOpen(false)
+ }}
+ >
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger asChild>
+ <span className="w-full">Transaction mode</span>
+ </Tooltip.Trigger>
+ {data.pool_mode === 'session' && (
+ <Tooltip.Portal>
+ <Tooltip.Content side="right">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'rounded bg-alternative py-1 px-2 leading-none shadow',
+ 'border border-background w-[380px]',
+ ].join(' ')}
+ >
+ <span className="text-xs text-foreground">
+ Pooling mode is currently configured to use
+ session mode. If you want to use both session mode
+ and transaction mode at the same time, change the
+ pooling mode to transaction first in the pooler
+ configuration settings. You can then connect to
+ session mode on port 5432 and transaction mode on
+ port 6543
+ </span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Portal>
+ )}
+ </Tooltip.Root>
+ </CommandItem_Shadcn_>
+ <CommandItem_Shadcn_
+ value="session"
+ className="cursor-pointer w-full text-foreground"
+ onSelect={() => {
+ onSelectPoolingMode('session')
+ setOpen(false)
+ }}
+ onClick={() => {
+ onSelectPoolingMode('session')
+ setOpen(false)
+ }}
+ >
+ Session mode
+ </CommandItem_Shadcn_>
+ </CommandGroup_Shadcn_>
+ </CommandList_Shadcn_>
+ </Command_Shadcn_>
+ <Separator />
+ <p
+ className="px-3 py-2 text-xs text-foreground-light hover:text-foreground cursor-pointer transition"
+ onClick={() => snap.setShowPoolingModeHelper(true)}
+ >
+ How to choose pooling modes?
+ </p>
+ </PopoverContent_Shadcn_>
+ </Popover_Shadcn_>
+ </div>
+ )}
+ </div>
+ </>
+ )}
+ </div>
+ <div className="flex items-center gap-x-1">
+ {isSuccess && checked && data.supavisor_enabled && (
+ <Badge color="scale">Supavisor</Badge>
+ )}
+ {isSuccessSettings && (
+ <Badge color="scale">
+ {checked
+ ? 'Resolves to IPv4'
+ : resolvesToIpV6
+ ? 'Resolves to IPv6'
+ : 'Will resolve to IPv6'}
+ </Badge>
+ )}
+ </div>
+ </div>
+ </div>
+ </div>
</div>
- </div>
+ </>
)
}
diff --git a/apps/studio/pages/project/[ref]/settings/database.tsx b/apps/studio/pages/project/[ref]/settings/database.tsx
index 4e7dbfc4e3b..f52bcd36543 100644
--- a/apps/studio/pages/project/[ref]/settings/database.tsx
+++ b/apps/studio/pages/project/[ref]/settings/database.tsx
@@ -8,10 +8,11 @@ import { observer } from 'mobx-react-lite'
import { NextPageWithLayout } from 'types'
import BannedIPs from 'components/interfaces/Settings/Database/BannedIPs'
+import { DatabaseReadOnlyAlert } from 'components/interfaces/Settings/Database/DatabaseReadOnlyAlert'
import { DatabaseConnectionString } from 'components/interfaces/Settings/Database/DatabaseSettings/DatabaseConnectionString'
import DiskSizeConfiguration from 'components/interfaces/Settings/Database/DiskSizeConfiguration'
+import { PoolingModesModal } from 'components/interfaces/Settings/Database/PoolingModesModal'
import SSLConfiguration from 'components/interfaces/Settings/Database/SSLConfiguration'
-import { DatabaseReadOnlyAlert } from 'components/interfaces/Settings/Database/DatabaseReadOnlyAlert'
const ProjectSettings: NextPageWithLayout = () => {
return (
@@ -32,6 +33,7 @@ const ProjectSettings: NextPageWithLayout = () => {
<BannedIPs />
</div>
</div>
+ <PoolingModesModal />
</div>
)
}
diff --git a/apps/studio/state/database-settings.tsx b/apps/studio/state/database-settings.tsx
new file mode 100644
index 00000000000..93ce2c7723d
--- /dev/null
+++ b/apps/studio/state/database-settings.tsx
@@ -0,0 +1,36 @@
+import { PropsWithChildren, createContext, useContext } from 'react'
+import { proxy, useSnapshot } from 'valtio'
+
+import { useConstant } from 'common'
+
+export function createDatabaseSettingsState() {
+ const state = proxy({
+ showPoolingModeHelper: false,
+ setShowPoolingModeHelper: (value: boolean) => {
+ state.showPoolingModeHelper = value
+ },
+ })
+
+ return state
+}
+
+export type DatabaseSettingsState = ReturnType<typeof createDatabaseSettingsState>
+
+export const DatabaseSettingsStateContext = createContext<DatabaseSettingsState>(
+ createDatabaseSettingsState()
+)
+
+export const DatabaseSettingsStateContextProvider = ({ children }: PropsWithChildren) => {
+ const state = useConstant(createDatabaseSettingsState)
+
+ return (
+ <DatabaseSettingsStateContext.Provider value={state}>
+ {children}
+ </DatabaseSettingsStateContext.Provider>
+ )
+}
+
+export function useDatabaseSettingsStateSnapshot(options?: Parameters<typeof useSnapshot>[1]) {
+ const state = useContext(DatabaseSettingsStateContext)
+ return useSnapshot(state, options)
+}