Skip to content

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

Chore/update pgbouncer status check (#20862)

Joshen Lim0c155ec55267
diff --git a/apps/studio/components/interfaces/Settings/Database/UsePoolerCheckbox.tsx b/apps/studio/components/interfaces/Settings/Database/UsePoolerCheckbox.tsx
index 400b1859500..50ffa07284e 100644
--- a/apps/studio/components/interfaces/Settings/Database/UsePoolerCheckbox.tsx
+++ b/apps/studio/components/interfaces/Settings/Database/UsePoolerCheckbox.tsx
@@ -39,10 +39,7 @@ export const UsePoolerCheckbox = ({
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 { data: pgBouncerStatus } = usePgBouncerStatus({ projectRef: projectRef })
const resolvesToIpV6 = settings?.project.db_ip_addr_config === 'ipv6'
@@ -144,7 +141,7 @@ export const UsePoolerCheckbox = ({
: 'Will resolve to IPv6'}
</Badge>
)}
- {pgBouncerStatus && <Badge color="amber">PgBouncer pending removal</Badge>}
+ {pgBouncerStatus?.active && <Badge color="amber">PgBouncer pending removal</Badge>}
</div>
</div>
</div>
diff --git a/apps/studio/data/api.d.ts b/apps/studio/data/api.d.ts
index 5657cb98345..6a49f4a8f83 100644
--- a/apps/studio/data/api.d.ts
+++ b/apps/studio/data/api.d.ts
@@ -491,6 +491,10 @@ export interface paths {
/** Gets project health check */
get: operations['HealthCheckController_projectHealthCheck']
}
+ '/platform/projects/{ref}/load-balancers': {
+ /** Gets non-removed databases of a specified project */
+ get: operations['LoadBalancersController_getLoadBalancers']
+ }
'/platform/projects/{ref}/api/rest': {
/** Gets project OpenApi */
get: operations['ApiController_projectOpenApi']
@@ -585,6 +589,10 @@ export interface paths {
/** Updates project's pgbouncer config */
patch: operations['PgbouncerConfigController_updatePgbouncerConfig']
}
+ '/platform/projects/{ref}/config/pgbouncer/status': {
+ /** Gets project's pgbouncer status */
+ get: operations['PgbouncerConfigController_getPgbouncerStatus']
+ }
'/platform/projects/{ref}/config/postgrest': {
/** Gets project's postgrest config */
get: operations['PostgrestConfigController_getPostgRESTConfig']
@@ -607,6 +615,10 @@ export interface paths {
/** Updates project's storage config */
patch: operations['StorageConfigController_updateConfig']
}
+ '/platform/projects/{ref}/config/supavisor': {
+ /** Gets project's supavisor config */
+ get: operations['SupavisorConfigController_getSupavisorConfig']
+ }
'/platform/projects/{ref}/billing/addons': {
/** Gets project addons */
get: operations['ProjectAddonController_getProjectAddons']
@@ -1247,6 +1259,10 @@ export interface paths {
/** Gets project health check */
get: operations['HealthCheckController_projectHealthCheck']
}
+ '/v0/projects/{ref}/load-balancers': {
+ /** Gets non-removed databases of a specified project */
+ get: operations['LoadBalancersController_getLoadBalancers']
+ }
'/v0/projects/{ref}/api/rest': {
/** Gets project OpenApi */
get: operations['ApiController_projectOpenApi']
@@ -1325,6 +1341,10 @@ export interface paths {
/** Updates project's pgbouncer config */
patch: operations['PgbouncerConfigController_updatePgbouncerConfig']
}
+ '/v0/projects/{ref}/config/pgbouncer/status': {
+ /** Gets project's pgbouncer status */
+ get: operations['PgbouncerConfigController_getPgbouncerStatus']
+ }
'/v0/projects/{ref}/config/postgrest': {
/** Gets project's postgrest config */
get: operations['PostgrestConfigController_getPostgRESTConfig']
@@ -1347,6 +1367,10 @@ export interface paths {
/** Updates project's storage config */
patch: operations['StorageConfigController_updateConfig']
}
+ '/v0/projects/{ref}/config/supavisor': {
+ /** Gets project's supavisor config */
+ get: operations['SupavisorConfigController_getSupavisorConfig']
+ }
'/v0/projects/{ref}/billing/addons': {
/** Gets project addons */
get: operations['ProjectAddonController_getProjectAddons']
@@ -2397,8 +2421,8 @@ export interface components {
id: number
slug: string
name: string
- billing_email: string
- stripe_customer_id: string
+ billing_email?: string
+ stripe_customer_id?: string
opt_in_tags: string[]
}
CustomerResponse: {
@@ -3029,7 +3053,7 @@ export interface components {
/** @enum {string} */
behavior: 'VOLATILE' | 'STABLE' | 'IMMUTABLE'
security_definer: boolean
- config_params: Record<string, unknown> | null
+ config_params: unknown
}
CreateFunctionBody: {
slug: string
@@ -3452,7 +3476,7 @@ export interface components {
| 'tenant:Sql:Write:Update'
| 'write:Update'
)[]
- condition: Record<string, unknown> | null
+ condition: unknown
organization_id: number
resources: string[]
}
@@ -3489,7 +3513,21 @@ export interface components {
first_name: string
last_name: string
}
+ /** @enum {string} */
+ DbInstanceSize:
+ | 'nano'
+ | 'micro'
+ | 'small'
+ | 'medium'
+ | 'large'
+ | 'xlarge'
+ | '2xlarge'
+ | '4xlarge'
+ | '8xlarge'
+ | '12xlarge'
+ | '16xlarge'
ProjectInfo: {
+ infra_compute_size?: components['schemas']['DbInstanceSize']
cloud_provider: string
id: number
inserted_at: string
@@ -3552,6 +3590,7 @@ export interface components {
kps_enabled?: boolean
}
CreateProjectResponse: {
+ infra_compute_size?: components['schemas']['DbInstanceSize']
cloud_provider: string
id: number
inserted_at: string
@@ -3680,6 +3719,16 @@ export interface components {
UpdatePasswordBody: {
password: string
}
+ Database: {
+ identifier: string
+ /** @enum {string} */
+ type: 'PRIMARY' | 'READ_REPLICA'
+ status: string
+ }
+ LoadBalancerDetailResponse: {
+ endpoint: string
+ databases: components['schemas']['Database'][]
+ }
Buffer: Record<string, never>
ResizeBody: {
volume_size_gb: number
@@ -3690,6 +3739,7 @@ export interface components {
'supabase-postgres': string
}
ProjectDetailResponse: {
+ infra_compute_size?: components['schemas']['DbInstanceSize']
cloud_provider: string
db_host: string
id: number
@@ -3867,6 +3917,9 @@ export interface components {
pgbouncer_status: 'COMING_DOWN' | 'COMING_UP' | 'DISABLED' | 'ENABLED' | 'RELOADING'
connectionString: string
}
+ PgbouncerStatusResponse: {
+ active: boolean
+ }
UpdatePgbouncerConfigBody: {
default_pool_size?: number
max_client_conn?: number | null
@@ -3942,6 +3995,21 @@ export interface components {
UpdateStorageConfigResponse: {
fileSizeLimit: number
}
+ SupavisorConfigResponse: {
+ identifier: string
+ /** @enum {string} */
+ database_type: 'PRIMARY' | 'READ_REPLICA'
+ is_using_scram_auth: boolean
+ db_user: string
+ db_host: string
+ db_port: number
+ db_name: string
+ connectionString: string
+ default_pool_size: number | null
+ max_client_conn: number | null
+ /** @enum {string|null} */
+ pool_mode: 'transaction' | 'session' | 'statement' | null
+ }
AvailableAddonResponse: {
type: components['schemas']['ProjectAddonType']
name: string
@@ -8693,6 +8761,22 @@ export interface operations {
}
}
}
+ /** Gets non-removed databases of a specified project */
+ LoadBalancersController_getLoadBalancers: {
+ parameters: {
+ path: {
+ /** @description Project ref */
+ ref: string
+ }
+ }
+ responses: {
+ 200: {
+ content: {
+ 'application/json': components['schemas']['LoadBalancerDetailResponse'][]
+ }
+ }
+ }
+ }
/** Gets project OpenApi */
ApiController_projectOpenApi: {
parameters: {
@@ -9250,6 +9334,26 @@ export interface operations {
}
}
}
+ /** Gets project's pgbouncer status */
+ PgbouncerConfigController_getPgbouncerStatus: {
+ parameters: {
+ path: {
+ /** @description Project ref */
+ ref: string
+ }
+ }
+ responses: {
+ 200: {
+ content: {
+ 'application/json': components['schemas']['PgbouncerStatusResponse']
+ }
+ }
+ /** @description Failed to retrieve project's pgbouncer status */
+ 500: {
+ content: never
+ }
+ }
+ }
/** Gets project's postgrest config */
PostgrestConfigController_getPostgRESTConfig: {
parameters: {
@@ -9422,6 +9526,26 @@ export interface operations {
}
}
}
+ /** Gets project's supavisor config */
+ SupavisorConfigController_getSupavisorConfig: {
+ parameters: {
+ path: {
+ /** @description Project ref */
+ ref: string
+ }
+ }
+ responses: {
+ 200: {
+ content: {
+ 'application/json': components['schemas']['SupavisorConfigResponse'][]
+ }
+ }
+ /** @description Failed to retrieve project's supavisor config */
+ 500: {
+ content: never
+ }
+ }
+ }
/** Gets project addons */
ProjectAddonController_getProjectAddons: {
parameters: {
@@ -11140,11 +11264,6 @@ export interface operations {
'x-vercel-signature': string
}
}
- requestBody: {
- content: {
- 'application/json': components['schemas']['Buffer']
- }
- }
responses: {
201: {
content: never
@@ -11164,11 +11283,6 @@ export interface operations {
'x-hub-signature-256': string
}
}
- requestBody: {
- content: {
- 'application/json': components['schemas']['Buffer']
- }
- }
responses: {
201: {
content: never
diff --git a/apps/studio/data/config/pgbouncer-enabled-query.ts b/apps/studio/data/config/pgbouncer-enabled-query.ts
index f4652117d3f..6a7b84ac92a 100644
--- a/apps/studio/data/config/pgbouncer-enabled-query.ts
+++ b/apps/studio/data/config/pgbouncer-enabled-query.ts
@@ -1,42 +1,37 @@
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'
+import { get } from 'data/fetchers'
+import { IS_PLATFORM } from 'lib/constants'
type PgBouncerVariables = {
- projectRef: string
- tld?: string
+ projectRef?: 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')
- }
+export async function getPgBouncerStatus({ projectRef }: PgBouncerVariables, signal?: AbortSignal) {
+ if (projectRef === undefined) throw new Error('Project ref is required')
- const response = await get(`${BASE_PATH}/api/database/${projectRef}/pg-bouncer?tld=${tld}`, {
- signal,
+ const { data, error } = await get('/platform/projects/{ref}/config/pgbouncer/status', {
+ params: { path: { ref: projectRef } },
})
- return !!response
+ if (error) throw error
+ return data
}
-export type PgBouncerStatusData = boolean
+export type PgBouncerStatusData = Awaited<ReturnType<typeof getPgBouncerStatus>>
export type PgBouncerStatusError = ResponseError
export const usePgBouncerStatus = <TData = PgBouncerStatusData>(
- { projectRef, tld }: PgBouncerVariables,
+ { projectRef }: PgBouncerVariables,
{ enabled, ...options }: UseQueryOptions<PgBouncerStatusData, PgBouncerStatusError, TData> = {}
) =>
useQuery<PgBouncerStatusData, PgBouncerStatusError, TData>(
configKeys.pgBouncerStatus(projectRef),
- ({ signal }) => getPgBouncerStatus({ projectRef, tld }, signal),
+ ({ signal }) => getPgBouncerStatus({ projectRef }, signal),
{
- enabled: enabled && typeof projectRef !== 'undefined' && typeof tld !== 'undefined',
+ enabled: enabled && IS_PLATFORM && typeof projectRef !== 'undefined',
...options,
}
)
diff --git a/apps/studio/pages/api/database/[ref]/pg-bouncer.ts b/apps/studio/pages/api/database/[ref]/pg-bouncer.ts
deleted file mode 100644
index 45a39ac7e11..00000000000
--- a/apps/studio/pages/api/database/[ref]/pg-bouncer.ts
+++ /dev/null
@@ -1,41 +0,0 @@
-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)
-}