Skip to content

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

feat: Show a badge if the pgbouncer hasn't been removed. (#20830)

Ivan Vasilov3f75f791a688
diff --git a/apps/studio/components/interfaces/Settings/Database/UsePoolerCheckbox.tsx b/apps/studio/components/interfaces/Settings/Database/UsePoolerCheckbox.tsx
index e3be18abd05..400b1859500 100644
--- a/apps/studio/components/interfaces/Settings/Database/UsePoolerCheckbox.tsx
+++ b/apps/studio/components/interfaces/Settings/Database/UsePoolerCheckbox.tsx
@@ -17,6 +17,7 @@ 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'
+import { usePgBouncerStatus } from 'data/config/pgbouncer-enabled-query'
interface UsePoolerCheckboxInterface {
id: string
@@ -34,11 +35,14 @@ export const UsePoolerCheckbox = ({
onSelectPoolingMode,
}: UsePoolerCheckboxInterface) => {
const { ref: projectRef } = useParams()
- const [open, setOpen] = useState(false)
const snap = useDatabaseSettingsStateSnapshot()
const { data, isLoading, isSuccess } = usePoolingConfigurationQuery({ projectRef })
const { data: settings, isSuccess: isSuccessSettings } = useProjectSettingsQuery({ projectRef })
+ const { data: pgBouncerStatus } = usePgBouncerStatus({
+ projectRef: projectRef!,
+ tld: settings ? settings.project.db_host.split('.').pop() : 'co',
+ })
const resolvesToIpV6 = settings?.project.db_ip_addr_config === 'ipv6'
@@ -140,6 +144,7 @@ export const UsePoolerCheckbox = ({
: 'Will resolve to IPv6'}
</Badge>
)}
+ {pgBouncerStatus && <Badge color="amber">PgBouncer pending removal</Badge>}
</div>
</div>
</div>
diff --git a/apps/studio/data/config/keys.ts b/apps/studio/data/config/keys.ts
index b6776176300..ab20e0a678e 100644
--- a/apps/studio/data/config/keys.ts
+++ b/apps/studio/data/config/keys.ts
@@ -1,4 +1,6 @@
export const configKeys = {
+ pgBouncerStatus: (projectRef: string | undefined) =>
+ ['projects', projectRef, 'pgbouncer'] as const,
settings: (projectRef: string | undefined) => ['projects', projectRef, 'settings'] as const,
api: (projectRef: string | undefined) => ['projects', projectRef, 'settings', 'api'] as const,
postgrest: (projectRef: string | undefined) => ['projects', projectRef, 'postgrest'] as const,
diff --git a/apps/studio/data/config/pgbouncer-enabled-query.ts b/apps/studio/data/config/pgbouncer-enabled-query.ts
new file mode 100644
index 00000000000..f4652117d3f
--- /dev/null
+++ b/apps/studio/data/config/pgbouncer-enabled-query.ts
@@ -0,0 +1,42 @@
+import { useQuery, UseQueryOptions } from '@tanstack/react-query'
+import { configKeys } from './keys'
+import { ResponseError } from 'types'
+import { get } from 'lib/common/fetch'
+import { API_URL, BASE_PATH } from 'lib/constants'
+
+type PgBouncerVariables = {
+ projectRef: string
+ tld?: string
+}
+
+// This API call describes whether PgBouncer is still present on the instance. Eventually PgBouncer will be removed.
+export async function getPgBouncerStatus(
+ { projectRef, tld }: PgBouncerVariables,
+ signal?: AbortSignal
+) {
+ if (!tld) {
+ throw new Error('tld is required')
+ }
+
+ const response = await get(`${BASE_PATH}/api/database/${projectRef}/pg-bouncer?tld=${tld}`, {
+ signal,
+ })
+
+ return !!response
+}
+
+export type PgBouncerStatusData = boolean
+export type PgBouncerStatusError = ResponseError
+
+export const usePgBouncerStatus = <TData = PgBouncerStatusData>(
+ { projectRef, tld }: PgBouncerVariables,
+ { enabled, ...options }: UseQueryOptions<PgBouncerStatusData, PgBouncerStatusError, TData> = {}
+) =>
+ useQuery<PgBouncerStatusData, PgBouncerStatusError, TData>(
+ configKeys.pgBouncerStatus(projectRef),
+ ({ signal }) => getPgBouncerStatus({ projectRef, tld }, signal),
+ {
+ enabled: enabled && typeof projectRef !== 'undefined' && typeof tld !== 'undefined',
+ ...options,
+ }
+ )
diff --git a/apps/studio/pages/api/database/[ref]/pg-bouncer.ts b/apps/studio/pages/api/database/[ref]/pg-bouncer.ts
new file mode 100644
index 00000000000..45a39ac7e11
--- /dev/null
+++ b/apps/studio/pages/api/database/[ref]/pg-bouncer.ts
@@ -0,0 +1,41 @@
+import { NextApiRequest, NextApiResponse } from 'next'
+import apiWrapper from 'lib/api/apiWrapper'
+
+export default (req: NextApiRequest, res: NextApiResponse) => apiWrapper(req, res, handler)
+
+async function handler(req: NextApiRequest, res: NextApiResponse) {
+ const { method } = req
+
+ switch (method) {
+ case 'GET':
+ return handleGetAll(req, res)
+ default:
+ res.setHeader('Allow', ['GET'])
+ res.status(405).json({ data: null, error: { message: `Method ${method} Not Allowed` } })
+ }
+}
+
+const handleGetAll = async (req: NextApiRequest, res: NextApiResponse) => {
+ const { ref, tld } = req.query
+ try {
+ // [Joshen] Additional safety checks on project ref just to be sure
+ if ((ref ?? '').length !== 20 || !/^[a-zA-Z]+$/.test(ref as string)) {
+ throw new Error('Invalid project ref')
+ }
+ if (!['co', 'red'].includes(tld as string)) {
+ throw new Error('Invalid TLD')
+ }
+
+ const host = `db.${ref}.supabase.${tld}`
+ const url = `http://${host}:6543`
+ await fetch(url)
+ } catch (error) {
+ // the response is text which make fetch fail because it's trying to parse it as JSON. But if there's a response,
+ // it means the pgBouncer is still active
+ if ((error as any).cause.data) {
+ return res.status(200).json(true)
+ }
+ }
+
+ return res.status(200).json(false)
+}