-- =============================================================================
-- AgenciaPsi — Indexes
-- =============================================================================

CREATE INDEX extensions_tenant_external_id_index ON _realtime.extensions USING btree (tenant_external_id);


--
-- Name: extensions_tenant_external_id_type_index; Type: INDEX; Schema: _realtime; Owner: supabase_admin
--

CREATE UNIQUE INDEX extensions_tenant_external_id_type_index ON _realtime.extensions USING btree (tenant_external_id, type);


--
-- Name: tenants_external_id_index; Type: INDEX; Schema: _realtime; Owner: supabase_admin
--

CREATE UNIQUE INDEX tenants_external_id_index ON _realtime.tenants USING btree (external_id);


--
-- Name: audit_logs_instance_id_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);


--
-- Name: confirmation_token_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE UNIQUE INDEX confirmation_token_idx ON auth.users USING btree (confirmation_token) WHERE ((confirmation_token)::text !~ '^[0-9 ]*$'::text);


--
-- Name: email_change_token_current_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE UNIQUE INDEX email_change_token_current_idx ON auth.users USING btree (email_change_token_current) WHERE ((email_change_token_current)::text !~ '^[0-9 ]*$'::text);


--
-- Name: email_change_token_new_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE UNIQUE INDEX email_change_token_new_idx ON auth.users USING btree (email_change_token_new) WHERE ((email_change_token_new)::text !~ '^[0-9 ]*$'::text);


--
-- Name: factor_id_created_at_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX factor_id_created_at_idx ON auth.mfa_factors USING btree (user_id, created_at);


--
-- Name: flow_state_created_at_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX flow_state_created_at_idx ON auth.flow_state USING btree (created_at DESC);


--
-- Name: identities_email_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX identities_email_idx ON auth.identities USING btree (email text_pattern_ops);


--
-- Name: INDEX identities_email_idx; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--

COMMENT ON INDEX auth.identities_email_idx IS 'Auth: Ensures indexed queries on the email column';


--
-- Name: identities_user_id_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX identities_user_id_idx ON auth.identities USING btree (user_id);


--
-- Name: idx_auth_code; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX idx_auth_code ON auth.flow_state USING btree (auth_code);


--
-- Name: idx_oauth_client_states_created_at; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX idx_oauth_client_states_created_at ON auth.oauth_client_states USING btree (created_at);


--
-- Name: idx_user_id_auth_method; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX idx_user_id_auth_method ON auth.flow_state USING btree (user_id, authentication_method);


--
-- Name: mfa_challenge_created_at_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX mfa_challenge_created_at_idx ON auth.mfa_challenges USING btree (created_at DESC);


--
-- Name: mfa_factors_user_friendly_name_unique; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE UNIQUE INDEX mfa_factors_user_friendly_name_unique ON auth.mfa_factors USING btree (friendly_name, user_id) WHERE (TRIM(BOTH FROM friendly_name) <> ''::text);


--
-- Name: mfa_factors_user_id_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX mfa_factors_user_id_idx ON auth.mfa_factors USING btree (user_id);


--
-- Name: oauth_auth_pending_exp_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX oauth_auth_pending_exp_idx ON auth.oauth_authorizations USING btree (expires_at) WHERE (status = 'pending'::auth.oauth_authorization_status);


--
-- Name: oauth_clients_deleted_at_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX oauth_clients_deleted_at_idx ON auth.oauth_clients USING btree (deleted_at);


--
-- Name: oauth_consents_active_client_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX oauth_consents_active_client_idx ON auth.oauth_consents USING btree (client_id) WHERE (revoked_at IS NULL);


--
-- Name: oauth_consents_active_user_client_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX oauth_consents_active_user_client_idx ON auth.oauth_consents USING btree (user_id, client_id) WHERE (revoked_at IS NULL);


--
-- Name: oauth_consents_user_order_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX oauth_consents_user_order_idx ON auth.oauth_consents USING btree (user_id, granted_at DESC);


--
-- Name: one_time_tokens_relates_to_hash_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX one_time_tokens_relates_to_hash_idx ON auth.one_time_tokens USING hash (relates_to);


--
-- Name: one_time_tokens_token_hash_hash_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX one_time_tokens_token_hash_hash_idx ON auth.one_time_tokens USING hash (token_hash);


--
-- Name: one_time_tokens_user_id_token_type_key; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE UNIQUE INDEX one_time_tokens_user_id_token_type_key ON auth.one_time_tokens USING btree (user_id, token_type);


--
-- Name: reauthentication_token_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE UNIQUE INDEX reauthentication_token_idx ON auth.users USING btree (reauthentication_token) WHERE ((reauthentication_token)::text !~ '^[0-9 ]*$'::text);


--
-- Name: recovery_token_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE UNIQUE INDEX recovery_token_idx ON auth.users USING btree (recovery_token) WHERE ((recovery_token)::text !~ '^[0-9 ]*$'::text);


--
-- Name: refresh_tokens_instance_id_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);


--
-- Name: refresh_tokens_instance_id_user_id_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);


--
-- Name: refresh_tokens_parent_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX refresh_tokens_parent_idx ON auth.refresh_tokens USING btree (parent);


--
-- Name: refresh_tokens_session_id_revoked_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX refresh_tokens_session_id_revoked_idx ON auth.refresh_tokens USING btree (session_id, revoked);


--
-- Name: refresh_tokens_updated_at_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX refresh_tokens_updated_at_idx ON auth.refresh_tokens USING btree (updated_at DESC);


--
-- Name: saml_providers_sso_provider_id_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX saml_providers_sso_provider_id_idx ON auth.saml_providers USING btree (sso_provider_id);


--
-- Name: saml_relay_states_created_at_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX saml_relay_states_created_at_idx ON auth.saml_relay_states USING btree (created_at DESC);


--
-- Name: saml_relay_states_for_email_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX saml_relay_states_for_email_idx ON auth.saml_relay_states USING btree (for_email);


--
-- Name: saml_relay_states_sso_provider_id_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX saml_relay_states_sso_provider_id_idx ON auth.saml_relay_states USING btree (sso_provider_id);


--
-- Name: sessions_not_after_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX sessions_not_after_idx ON auth.sessions USING btree (not_after DESC);


--
-- Name: sessions_oauth_client_id_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX sessions_oauth_client_id_idx ON auth.sessions USING btree (oauth_client_id);


--
-- Name: sessions_user_id_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX sessions_user_id_idx ON auth.sessions USING btree (user_id);


--
-- Name: sso_domains_domain_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE UNIQUE INDEX sso_domains_domain_idx ON auth.sso_domains USING btree (lower(domain));


--
-- Name: sso_domains_sso_provider_id_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX sso_domains_sso_provider_id_idx ON auth.sso_domains USING btree (sso_provider_id);


--
-- Name: sso_providers_resource_id_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE UNIQUE INDEX sso_providers_resource_id_idx ON auth.sso_providers USING btree (lower(resource_id));


--
-- Name: sso_providers_resource_id_pattern_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX sso_providers_resource_id_pattern_idx ON auth.sso_providers USING btree (resource_id text_pattern_ops);


--
-- Name: unique_phone_factor_per_user; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE UNIQUE INDEX unique_phone_factor_per_user ON auth.mfa_factors USING btree (user_id, phone);


--
-- Name: user_id_created_at_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX user_id_created_at_idx ON auth.sessions USING btree (user_id, created_at);


--
-- Name: users_email_partial_key; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE UNIQUE INDEX users_email_partial_key ON auth.users USING btree (email) WHERE (is_sso_user = false);


--
-- Name: INDEX users_email_partial_key; Type: COMMENT; Schema: auth; Owner: supabase_auth_admin
--

COMMENT ON INDEX auth.users_email_partial_key IS 'Auth: A partial unique index that applies only when is_sso_user is false';


--
-- Name: users_instance_id_email_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX users_instance_id_email_idx ON auth.users USING btree (instance_id, lower((email)::text));


--
-- Name: users_instance_id_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX users_instance_id_idx ON auth.users USING btree (instance_id);


--
-- Name: users_is_anonymous_idx; Type: INDEX; Schema: auth; Owner: supabase_auth_admin
--

CREATE INDEX users_is_anonymous_idx ON auth.users USING btree (is_anonymous);


--
-- Name: agenda_bloqueios_owner_data_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_bloqueios_owner_data_idx ON public.agenda_bloqueios USING btree (owner_id, data_inicio, data_fim);


--
-- Name: agenda_bloqueios_owner_id_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_bloqueios_owner_id_idx ON public.agenda_bloqueios USING btree (owner_id);


--
-- Name: agenda_bloqueios_recorrente_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_bloqueios_recorrente_idx ON public.agenda_bloqueios USING btree (owner_id, dia_semana) WHERE (recorrente = true);


--
-- Name: agenda_bloqueios_tenant_id_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_bloqueios_tenant_id_idx ON public.agenda_bloqueios USING btree (tenant_id);


--
-- Name: agenda_configuracoes_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_configuracoes_tenant_idx ON public.agenda_configuracoes USING btree (tenant_id);


--
-- Name: agenda_configuracoes_tenant_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_configuracoes_tenant_owner_idx ON public.agenda_configuracoes USING btree (tenant_id, owner_id);


--
-- Name: agenda_eventos_billing_contract_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_eventos_billing_contract_idx ON public.agenda_eventos USING btree (billing_contract_id) WHERE (billing_contract_id IS NOT NULL);


--
-- Name: agenda_eventos_insurance_plan_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_eventos_insurance_plan_idx ON public.agenda_eventos USING btree (insurance_plan_id);


--
-- Name: agenda_eventos_owner_inicio_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_eventos_owner_inicio_idx ON public.agenda_eventos USING btree (owner_id, inicio_em);


--
-- Name: agenda_eventos_owner_terapeuta_inicio_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_eventos_owner_terapeuta_inicio_idx ON public.agenda_eventos USING btree (owner_id, terapeuta_id, inicio_em);


--
-- Name: agenda_eventos_recurrence_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_eventos_recurrence_idx ON public.agenda_eventos USING btree (recurrence_id) WHERE (recurrence_id IS NOT NULL);


--
-- Name: agenda_eventos_tenant_inicio_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_eventos_tenant_inicio_idx ON public.agenda_eventos USING btree (tenant_id, inicio_em);


--
-- Name: agenda_eventos_tenant_owner_inicio_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_eventos_tenant_owner_inicio_idx ON public.agenda_eventos USING btree (tenant_id, owner_id, inicio_em);


--
-- Name: agenda_excecoes_owner_data_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_excecoes_owner_data_idx ON public.agenda_excecoes USING btree (owner_id, data);


--
-- Name: agenda_excecoes_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_excecoes_tenant_idx ON public.agenda_excecoes USING btree (tenant_id);


--
-- Name: agenda_excecoes_tenant_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_excecoes_tenant_owner_idx ON public.agenda_excecoes USING btree (tenant_id, owner_id);


--
-- Name: agenda_online_slots_owner_weekday_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_online_slots_owner_weekday_idx ON public.agenda_online_slots USING btree (owner_id, weekday);


--
-- Name: agenda_online_slots_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_online_slots_tenant_idx ON public.agenda_online_slots USING btree (tenant_id);


--
-- Name: agenda_online_slots_tenant_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_online_slots_tenant_owner_idx ON public.agenda_online_slots USING btree (tenant_id, owner_id);


--
-- Name: agenda_regras_semanais_owner_dia_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_regras_semanais_owner_dia_idx ON public.agenda_regras_semanais USING btree (owner_id, dia_semana);


--
-- Name: agenda_regras_semanais_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_regras_semanais_tenant_idx ON public.agenda_regras_semanais USING btree (tenant_id);


--
-- Name: agenda_regras_semanais_tenant_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_regras_semanais_tenant_owner_idx ON public.agenda_regras_semanais USING btree (tenant_id, owner_id);


--
-- Name: agenda_slots_bloqueados_semanais_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_slots_bloqueados_semanais_tenant_idx ON public.agenda_slots_bloqueados_semanais USING btree (tenant_id);


--
-- Name: agenda_slots_bloqueados_semanais_tenant_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_slots_bloqueados_semanais_tenant_owner_idx ON public.agenda_slots_bloqueados_semanais USING btree (tenant_id, owner_id);


--
-- Name: agenda_slots_regras_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_slots_regras_tenant_idx ON public.agenda_slots_regras USING btree (tenant_id);


--
-- Name: agenda_slots_regras_tenant_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agenda_slots_regras_tenant_owner_idx ON public.agenda_slots_regras USING btree (tenant_id, owner_id);


--
-- Name: agendador_cfg_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agendador_cfg_tenant_idx ON public.agendador_configuracoes USING btree (tenant_id);


--
-- Name: agendador_sol_data_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agendador_sol_data_idx ON public.agendador_solicitacoes USING btree (data_solicitada, hora_solicitada);


--
-- Name: agendador_sol_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agendador_sol_owner_idx ON public.agendador_solicitacoes USING btree (owner_id, status);


--
-- Name: agendador_sol_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX agendador_sol_tenant_idx ON public.agendador_solicitacoes USING btree (tenant_id);


--
-- Name: billing_contracts_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX billing_contracts_owner_idx ON public.billing_contracts USING btree (owner_id);


--
-- Name: billing_contracts_patient_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX billing_contracts_patient_idx ON public.billing_contracts USING btree (patient_id);


--
-- Name: billing_contracts_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX billing_contracts_tenant_idx ON public.billing_contracts USING btree (tenant_id);


--
-- Name: commitment_services_commitment_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX commitment_services_commitment_idx ON public.commitment_services USING btree (commitment_id);


--
-- Name: commitment_services_service_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX commitment_services_service_idx ON public.commitment_services USING btree (service_id);


--
-- Name: commitment_time_logs_calendar_event_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX commitment_time_logs_calendar_event_idx ON public.commitment_time_logs USING btree (calendar_event_id);


--
-- Name: commitment_time_logs_commitment_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX commitment_time_logs_commitment_idx ON public.commitment_time_logs USING btree (commitment_id, created_at DESC);


--
-- Name: commitment_time_logs_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX commitment_time_logs_tenant_idx ON public.commitment_time_logs USING btree (tenant_id, created_at DESC);


--
-- Name: determined_commitment_fields_commitment_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX determined_commitment_fields_commitment_idx ON public.determined_commitment_fields USING btree (commitment_id, sort_order);


--
-- Name: determined_commitment_fields_key_uniq; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX determined_commitment_fields_key_uniq ON public.determined_commitment_fields USING btree (commitment_id, key);


--
-- Name: determined_commitment_fields_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX determined_commitment_fields_tenant_idx ON public.determined_commitment_fields USING btree (tenant_id);


--
-- Name: determined_commitments_active_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX determined_commitments_active_idx ON public.determined_commitments USING btree (tenant_id, active);


--
-- Name: determined_commitments_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX determined_commitments_tenant_idx ON public.determined_commitments USING btree (tenant_id);


--
-- Name: determined_commitments_tenant_name_uniq; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX determined_commitments_tenant_name_uniq ON public.determined_commitments USING btree (tenant_id, lower(name));


--
-- Name: feriados_global_unique; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX feriados_global_unique ON public.feriados USING btree (data, nome) WHERE (tenant_id IS NULL);


--
-- Name: financial_exceptions_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX financial_exceptions_owner_idx ON public.financial_exceptions USING btree (owner_id);


--
-- Name: financial_exceptions_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX financial_exceptions_tenant_idx ON public.financial_exceptions USING btree (tenant_id);


--
-- Name: idx_addon_credits_tenant; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_addon_credits_tenant ON public.addon_credits USING btree (tenant_id) WHERE (is_active = true);


--
-- Name: idx_addon_credits_type; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_addon_credits_type ON public.addon_credits USING btree (addon_type) WHERE (is_active = true);


--
-- Name: idx_addon_products_active; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_addon_products_active ON public.addon_products USING btree (is_active, is_visible) WHERE (deleted_at IS NULL);


--
-- Name: idx_addon_products_type; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_addon_products_type ON public.addon_products USING btree (addon_type) WHERE (deleted_at IS NULL);


--
-- Name: idx_addon_tx_created; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_addon_tx_created ON public.addon_transactions USING btree (created_at DESC);


--
-- Name: idx_addon_tx_queue; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_addon_tx_queue ON public.addon_transactions USING btree (queue_id) WHERE (queue_id IS NOT NULL);


--
-- Name: idx_addon_tx_tenant; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_addon_tx_tenant ON public.addon_transactions USING btree (tenant_id, addon_type);


--
-- Name: idx_addon_tx_type; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_addon_tx_type ON public.addon_transactions USING btree (type);


--
-- Name: idx_agenda_eventos_determined_commitment_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_agenda_eventos_determined_commitment_id ON public.agenda_eventos USING btree (determined_commitment_id);


--
-- Name: idx_agenda_excecoes_owner_data; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_agenda_excecoes_owner_data ON public.agenda_excecoes USING btree (owner_id, data);


--
-- Name: idx_agenda_slots_regras_owner_dia; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_agenda_slots_regras_owner_dia ON public.agenda_slots_regras USING btree (owner_id, dia_semana);


--
-- Name: idx_email_templates_global_domain; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_email_templates_global_domain ON public.email_templates_global USING btree (domain) WHERE (is_active = true);


--
-- Name: idx_email_templates_global_key; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_email_templates_global_key ON public.email_templates_global USING btree (key) WHERE (is_active = true);


--
-- Name: idx_email_templates_tenant_lookup; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_email_templates_tenant_lookup ON public.email_templates_tenant USING btree (tenant_id, template_key) WHERE (enabled = true);


--
-- Name: idx_email_templates_tenant_owner; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_email_templates_tenant_owner ON public.email_templates_tenant USING btree (owner_id, template_key) WHERE ((enabled = true) AND (owner_id IS NOT NULL));


--
-- Name: idx_financial_categories_user_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_financial_categories_user_id ON public.financial_categories USING btree (user_id);


--
-- Name: idx_financial_records_active; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_financial_records_active ON public.financial_records USING btree (owner_id, paid_at DESC) WHERE (deleted_at IS NULL);


--
-- Name: idx_financial_records_agenda_evento_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_financial_records_agenda_evento_id ON public.financial_records USING btree (agenda_evento_id) WHERE (agenda_evento_id IS NOT NULL);


--
-- Name: idx_financial_records_category_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_financial_records_category_id ON public.financial_records USING btree (category_id) WHERE (category_id IS NOT NULL);


--
-- Name: idx_financial_records_due_date; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_financial_records_due_date ON public.financial_records USING btree (due_date);


--
-- Name: idx_financial_records_installment_group; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_financial_records_installment_group ON public.financial_records USING btree (installment_group) WHERE (installment_group IS NOT NULL);


--
-- Name: idx_financial_records_owner_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_financial_records_owner_id ON public.financial_records USING btree (owner_id);


--
-- Name: idx_financial_records_paid_at; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_financial_records_paid_at ON public.financial_records USING btree (paid_at DESC);


--
-- Name: idx_financial_records_patient_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_financial_records_patient_id ON public.financial_records USING btree (patient_id);


--
-- Name: idx_financial_records_status; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_financial_records_status ON public.financial_records USING btree (status) WHERE (deleted_at IS NULL);


--
-- Name: idx_financial_records_tenant_active; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_financial_records_tenant_active ON public.financial_records USING btree (tenant_id, paid_at DESC) WHERE ((deleted_at IS NULL) AND (tenant_id IS NOT NULL));


--
-- Name: idx_financial_records_tenant_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_financial_records_tenant_id ON public.financial_records USING btree (tenant_id);


--
-- Name: idx_financial_records_type_status; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_financial_records_type_status ON public.financial_records USING btree (type, status);


--
-- Name: idx_global_notices_active_priority; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_global_notices_active_priority ON public.global_notices USING btree (is_active, priority DESC, starts_at, ends_at);


--
-- Name: idx_intakes_converted_patient_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_intakes_converted_patient_id ON public.patient_intake_requests USING btree (converted_patient_id);


--
-- Name: idx_intakes_owner_cpf; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_intakes_owner_cpf ON public.patient_intake_requests USING btree (owner_id, cpf);


--
-- Name: idx_intakes_owner_created; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_intakes_owner_created ON public.patient_intake_requests USING btree (owner_id, created_at DESC);


--
-- Name: idx_intakes_owner_status_created; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_intakes_owner_status_created ON public.patient_intake_requests USING btree (owner_id, status, created_at DESC);


--
-- Name: idx_intakes_status_created; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_intakes_status_created ON public.patient_intake_requests USING btree (status, created_at DESC);


--
-- Name: idx_notice_dismissals_user; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notice_dismissals_user ON public.notice_dismissals USING btree (user_id, notice_id);


--
-- Name: idx_notif_channels_owner_active; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_channels_owner_active ON public.notification_channels USING btree (owner_id, channel) WHERE ((is_active = true) AND (deleted_at IS NULL));


--
-- Name: idx_notif_channels_tenant; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_channels_tenant ON public.notification_channels USING btree (tenant_id) WHERE (deleted_at IS NULL);


--
-- Name: idx_notif_logs_owner_date; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_logs_owner_date ON public.notification_logs USING btree (owner_id, created_at DESC);


--
-- Name: idx_notif_logs_patient; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_logs_patient ON public.notification_logs USING btree (patient_id, created_at DESC);


--
-- Name: idx_notif_logs_provider_msg; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_logs_provider_msg ON public.notification_logs USING btree (provider_message_id) WHERE (provider_message_id IS NOT NULL);


--
-- Name: idx_notif_logs_status; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_logs_status ON public.notification_logs USING btree (status, created_at DESC);


--
-- Name: idx_notif_logs_tenant_date; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_logs_tenant_date ON public.notification_logs USING btree (tenant_id, created_at DESC);


--
-- Name: idx_notif_prefs_owner; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_prefs_owner ON public.notification_preferences USING btree (owner_id) WHERE (deleted_at IS NULL);


--
-- Name: idx_notif_prefs_patient; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_prefs_patient ON public.notification_preferences USING btree (patient_id) WHERE (deleted_at IS NULL);


--
-- Name: idx_notif_prefs_whatsapp_active; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_prefs_whatsapp_active ON public.notification_preferences USING btree (owner_id, patient_id) WHERE ((whatsapp_opt_in = true) AND (deleted_at IS NULL));


--
-- Name: idx_notif_queue_evento; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_queue_evento ON public.notification_queue USING btree (agenda_evento_id) WHERE (status = ANY (ARRAY['pendente'::text, 'processando'::text]));


--
-- Name: idx_notif_queue_patient; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_queue_patient ON public.notification_queue USING btree (patient_id, channel) WHERE (status = 'pendente'::text);


--
-- Name: idx_notif_queue_pending; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_queue_pending ON public.notification_queue USING btree (scheduled_at) WHERE (status = 'pendente'::text);


--
-- Name: idx_notif_queue_processing; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_queue_processing ON public.notification_queue USING btree (status, updated_at) WHERE (status = 'processando'::text);


--
-- Name: idx_notif_queue_retry; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_queue_retry ON public.notification_queue USING btree (next_retry_at) WHERE ((status = 'pendente'::text) AND (attempts > 0));


--
-- Name: idx_notif_queue_tenant; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_queue_tenant ON public.notification_queue USING btree (tenant_id, created_at DESC);


--
-- Name: idx_notif_schedules_owner_active; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_schedules_owner_active ON public.notification_schedules USING btree (owner_id, event_type) WHERE ((is_active = true) AND (deleted_at IS NULL));


--
-- Name: idx_notif_templates_default; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_templates_default ON public.notification_templates USING btree (channel, event_type) WHERE ((is_default = true) AND (deleted_at IS NULL) AND (tenant_id IS NULL));


--
-- Name: idx_notif_templates_lookup; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_templates_lookup ON public.notification_templates USING btree (channel, event_type, is_active) WHERE (deleted_at IS NULL);


--
-- Name: idx_notif_templates_tenant; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_notif_templates_tenant ON public.notification_templates USING btree (tenant_id, channel, event_type) WHERE ((deleted_at IS NULL) AND (is_active = true));


--
-- Name: idx_patient_group_patient_group_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_patient_group_patient_group_id ON public.patient_group_patient USING btree (patient_group_id);


--
-- Name: idx_patient_groups_owner; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_patient_groups_owner ON public.patient_groups USING btree (owner_id);


--
-- Name: idx_patient_groups_owner_system_nome; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_patient_groups_owner_system_nome ON public.patient_groups USING btree (owner_id, is_system, nome);


--
-- Name: idx_patient_tags_owner; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_patient_tags_owner ON public.patient_tags USING btree (owner_id);


--
-- Name: idx_patients_created_at; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_patients_created_at ON public.patients USING btree (created_at DESC);


--
-- Name: idx_patients_last_attended; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_patients_last_attended ON public.patients USING btree (last_attended_at DESC);


--
-- Name: idx_patients_owner_email_principal; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_patients_owner_email_principal ON public.patients USING btree (owner_id, email_principal);


--
-- Name: idx_patients_owner_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_patients_owner_id ON public.patients USING btree (owner_id);


--
-- Name: idx_patients_owner_nome; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_patients_owner_nome ON public.patients USING btree (owner_id, nome_completo);


--
-- Name: idx_patients_responsible_member; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_patients_responsible_member ON public.patients USING btree (responsible_member_id);


--
-- Name: idx_patients_status; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_patients_status ON public.patients USING btree (status);


--
-- Name: idx_patients_tenant; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_patients_tenant ON public.patients USING btree (tenant_id);


--
-- Name: idx_patients_tenant_email_norm; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_patients_tenant_email_norm ON public.patients USING btree (tenant_id, lower(TRIM(BOTH FROM email_principal)));


--
-- Name: idx_pgp_group; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_pgp_group ON public.patient_group_patient USING btree (patient_group_id);


--
-- Name: idx_pgp_patient; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_pgp_patient ON public.patient_group_patient USING btree (patient_id);


--
-- Name: idx_ppt_patient; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_ppt_patient ON public.patient_patient_tag USING btree (patient_id);


--
-- Name: idx_ppt_tag; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_ppt_tag ON public.patient_patient_tag USING btree (tag_id);


--
-- Name: idx_slots_bloq_owner_dia; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_slots_bloq_owner_dia ON public.agenda_slots_bloqueados_semanais USING btree (owner_id, dia_semana);


--
-- Name: idx_subscription_intents_plan_interval; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_subscription_intents_plan_interval ON public.subscription_intents_legacy USING btree (plan_key, "interval");


--
-- Name: idx_subscription_intents_status; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_subscription_intents_status ON public.subscription_intents_legacy USING btree (status);


--
-- Name: idx_subscription_intents_user_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_subscription_intents_user_id ON public.subscription_intents_legacy USING btree (user_id);


--
-- Name: idx_tenant_features_tenant; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_tenant_features_tenant ON public.tenant_features USING btree (tenant_id);


--
-- Name: idx_tenant_invites_tenant; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_tenant_invites_tenant ON public.tenant_invites USING btree (tenant_id);


--
-- Name: idx_tenant_invites_token; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_tenant_invites_token ON public.tenant_invites USING btree (token);


--
-- Name: idx_therapist_payout_records_financial_record_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_therapist_payout_records_financial_record_id ON public.therapist_payout_records USING btree (financial_record_id);


--
-- Name: idx_therapist_payouts_owner_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_therapist_payouts_owner_id ON public.therapist_payouts USING btree (owner_id);


--
-- Name: idx_therapist_payouts_period; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_therapist_payouts_period ON public.therapist_payouts USING btree (period_start, period_end);


--
-- Name: idx_therapist_payouts_status; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_therapist_payouts_status ON public.therapist_payouts USING btree (status) WHERE (status = 'pending'::text);


--
-- Name: idx_therapist_payouts_tenant_id; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX idx_therapist_payouts_tenant_id ON public.therapist_payouts USING btree (tenant_id);


--
-- Name: insurance_plans_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX insurance_plans_owner_idx ON public.insurance_plans USING btree (owner_id);


--
-- Name: insurance_plans_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX insurance_plans_tenant_idx ON public.insurance_plans USING btree (tenant_id);


--
-- Name: ix_plan_prices_plan; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX ix_plan_prices_plan ON public.plan_prices USING btree (plan_id);


--
-- Name: ix_plan_public_bullets_plan; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX ix_plan_public_bullets_plan ON public.plan_public_bullets USING btree (plan_id);


--
-- Name: ix_plan_public_sort; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX ix_plan_public_sort ON public.plan_public USING btree (sort_order);


--
-- Name: notifications_owner_created; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX notifications_owner_created ON public.notifications USING btree (owner_id, created_at DESC);


--
-- Name: notifications_owner_unread; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX notifications_owner_unread ON public.notifications USING btree (owner_id, read_at) WHERE (read_at IS NULL);


--
-- Name: patient_discounts_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_discounts_owner_idx ON public.patient_discounts USING btree (owner_id);


--
-- Name: patient_discounts_patient_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_discounts_patient_idx ON public.patient_discounts USING btree (patient_id);


--
-- Name: patient_discounts_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_discounts_tenant_idx ON public.patient_discounts USING btree (tenant_id);


--
-- Name: patient_group_patient_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_group_patient_tenant_idx ON public.patient_group_patient USING btree (tenant_id);


--
-- Name: patient_groups_owner_nome_uniq; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX patient_groups_owner_nome_uniq ON public.patient_groups USING btree (owner_id, nome);


--
-- Name: patient_groups_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_groups_tenant_idx ON public.patient_groups USING btree (tenant_id);


--
-- Name: patient_intake_owner_id_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_intake_owner_id_idx ON public.patient_intake_requests USING btree (owner_id);


--
-- Name: patient_intake_requests_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_intake_requests_tenant_idx ON public.patient_intake_requests USING btree (tenant_id);


--
-- Name: patient_intake_status_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_intake_status_idx ON public.patient_intake_requests USING btree (status);


--
-- Name: patient_intake_token_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_intake_token_idx ON public.patient_intake_requests USING btree (token);


--
-- Name: patient_invites_one_active_per_owner; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX patient_invites_one_active_per_owner ON public.patient_invites USING btree (owner_id) WHERE (active = true);


--
-- Name: patient_invites_owner_id_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_invites_owner_id_idx ON public.patient_invites USING btree (owner_id);


--
-- Name: patient_invites_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_invites_tenant_idx ON public.patient_invites USING btree (tenant_id);


--
-- Name: patient_invites_token_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_invites_token_idx ON public.patient_invites USING btree (token);


--
-- Name: patient_patient_tag_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_patient_tag_tenant_idx ON public.patient_patient_tag USING btree (tenant_id);


--
-- Name: patient_tags_owner_name_uq; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX patient_tags_owner_name_uq ON public.patient_tags USING btree (owner_id, lower(nome));


--
-- Name: patient_tags_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX patient_tags_tenant_idx ON public.patient_tags USING btree (tenant_id);


--
-- Name: payment_settings_tenant_id_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX payment_settings_tenant_id_idx ON public.payment_settings USING btree (tenant_id);


--
-- Name: ppt_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX ppt_owner_idx ON public.patient_patient_tag USING btree (owner_id);


--
-- Name: ppt_patient_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX ppt_patient_idx ON public.patient_patient_tag USING btree (patient_id);


--
-- Name: ppt_tag_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX ppt_tag_idx ON public.patient_patient_tag USING btree (tag_id);


--
-- Name: professional_pricing_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX professional_pricing_tenant_idx ON public.professional_pricing USING btree (tenant_id);


--
-- Name: profiles_work_description_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX profiles_work_description_idx ON public.profiles USING btree (work_description) WHERE (work_description IS NOT NULL);


--
-- Name: recurrence_exceptions_rule_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX recurrence_exceptions_rule_idx ON public.recurrence_exceptions USING btree (recurrence_id);


--
-- Name: recurrence_exceptions_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX recurrence_exceptions_tenant_idx ON public.recurrence_exceptions USING btree (tenant_id);


--
-- Name: recurrence_rule_services_rule_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX recurrence_rule_services_rule_idx ON public.recurrence_rule_services USING btree (rule_id);


--
-- Name: recurrence_rule_services_service_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX recurrence_rule_services_service_idx ON public.recurrence_rule_services USING btree (service_id);


--
-- Name: recurrence_rules_active_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX recurrence_rules_active_idx ON public.recurrence_rules USING btree (owner_id, status) WHERE (status = 'ativo'::text);


--
-- Name: recurrence_rules_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX recurrence_rules_owner_idx ON public.recurrence_rules USING btree (owner_id);


--
-- Name: recurrence_rules_patient_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX recurrence_rules_patient_idx ON public.recurrence_rules USING btree (patient_id);


--
-- Name: recurrence_rules_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX recurrence_rules_tenant_idx ON public.recurrence_rules USING btree (tenant_id);


--
-- Name: saas_doc_votos_doc_id_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX saas_doc_votos_doc_id_idx ON public.saas_doc_votos USING btree (doc_id);


--
-- Name: saas_doc_votos_user_id_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX saas_doc_votos_user_id_idx ON public.saas_doc_votos USING btree (user_id);


--
-- Name: saas_docs_categoria_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX saas_docs_categoria_idx ON public.saas_docs USING btree (categoria);


--
-- Name: saas_docs_exibir_no_faq_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX saas_docs_exibir_no_faq_idx ON public.saas_docs USING btree (exibir_no_faq) WHERE (exibir_no_faq = true);


--
-- Name: saas_docs_path_ativo_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX saas_docs_path_ativo_idx ON public.saas_docs USING btree (pagina_path, ativo);


--
-- Name: saas_faq_ativo_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX saas_faq_ativo_idx ON public.saas_faq USING btree (ativo);


--
-- Name: saas_faq_categoria_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX saas_faq_categoria_idx ON public.saas_faq USING btree (categoria);


--
-- Name: saas_faq_fts_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX saas_faq_fts_idx ON public.saas_faq USING gin (to_tsvector('portuguese'::regconfig, ((COALESCE(pergunta, ''::text) || ' '::text) || COALESCE(conteudo, ''::text))));


--
-- Name: saas_faq_itens_ativo_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX saas_faq_itens_ativo_idx ON public.saas_faq_itens USING btree (ativo);


--
-- Name: saas_faq_itens_doc_id_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX saas_faq_itens_doc_id_idx ON public.saas_faq_itens USING btree (doc_id);


--
-- Name: saas_faq_pagina_path_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX saas_faq_pagina_path_idx ON public.saas_faq USING btree (pagina_path);


--
-- Name: saas_faq_publico_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX saas_faq_publico_idx ON public.saas_faq USING btree (publico);


--
-- Name: saas_faq_votos_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX saas_faq_votos_idx ON public.saas_faq USING btree (votos DESC);


--
-- Name: services_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX services_owner_idx ON public.services USING btree (owner_id);


--
-- Name: services_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX services_tenant_idx ON public.services USING btree (tenant_id);


--
-- Name: sint_personal_created_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX sint_personal_created_idx ON public.subscription_intents_personal USING btree (created_at DESC);


--
-- Name: sint_personal_status_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX sint_personal_status_idx ON public.subscription_intents_personal USING btree (status);


--
-- Name: sint_tenant_created_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX sint_tenant_created_idx ON public.subscription_intents_tenant USING btree (created_at DESC);


--
-- Name: sint_tenant_status_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX sint_tenant_status_idx ON public.subscription_intents_tenant USING btree (status);


--
-- Name: sint_tenant_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX sint_tenant_tenant_idx ON public.subscription_intents_tenant USING btree (tenant_id);


--
-- Name: subscription_events_created_at_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX subscription_events_created_at_idx ON public.subscription_events USING btree (created_at DESC);


--
-- Name: subscription_events_owner_ref_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX subscription_events_owner_ref_idx ON public.subscription_events USING btree (owner_type, owner_ref);


--
-- Name: subscription_events_sub_created_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX subscription_events_sub_created_idx ON public.subscription_events USING btree (subscription_id, created_at DESC);


--
-- Name: subscription_events_subscription_id_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX subscription_events_subscription_id_idx ON public.subscription_events USING btree (subscription_id);


--
-- Name: subscriptions_one_active_per_tenant; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX subscriptions_one_active_per_tenant ON public.subscriptions USING btree (tenant_id) WHERE (status = 'active'::text);


--
-- Name: subscriptions_one_active_per_user; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX subscriptions_one_active_per_user ON public.subscriptions USING btree (user_id) WHERE (status = 'active'::text);


--
-- Name: subscriptions_one_active_per_user_personal; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX subscriptions_one_active_per_user_personal ON public.subscriptions USING btree (user_id) WHERE ((tenant_id IS NULL) AND (status = 'active'::text));


--
-- Name: subscriptions_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX subscriptions_owner_idx ON public.subscriptions USING btree (user_id);


--
-- Name: subscriptions_plan_key_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX subscriptions_plan_key_idx ON public.subscriptions USING btree (plan_key);


--
-- Name: subscriptions_status_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX subscriptions_status_idx ON public.subscriptions USING btree (status);


--
-- Name: subscriptions_tenant_id_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX subscriptions_tenant_id_idx ON public.subscriptions USING btree (tenant_id);


--
-- Name: subscriptions_tenant_period_end_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX subscriptions_tenant_period_end_idx ON public.subscriptions USING btree (tenant_id, current_period_end);


--
-- Name: subscriptions_tenant_status_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX subscriptions_tenant_status_idx ON public.subscriptions USING btree (tenant_id, status);


--
-- Name: subscriptions_user_status_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX subscriptions_user_status_idx ON public.subscriptions USING btree (user_id, status, created_at DESC);


--
-- Name: support_sessions_expires_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX support_sessions_expires_idx ON public.support_sessions USING btree (expires_at);


--
-- Name: support_sessions_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX support_sessions_tenant_idx ON public.support_sessions USING btree (tenant_id);


--
-- Name: support_sessions_token_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX support_sessions_token_idx ON public.support_sessions USING btree (token);


--
-- Name: tenant_members_tenant_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX tenant_members_tenant_idx ON public.tenant_members USING btree (tenant_id);


--
-- Name: tenant_members_user_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX tenant_members_user_idx ON public.tenant_members USING btree (user_id);


--
-- Name: tenant_modules_owner_idx; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE INDEX tenant_modules_owner_idx ON public.tenant_modules USING btree (owner_id);


--
-- Name: unique_member_per_tenant; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX unique_member_per_tenant ON public.tenant_members USING btree (tenant_id, user_id);


--
-- Name: uq_patients_tenant_user; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX uq_patients_tenant_user ON public.patients USING btree (tenant_id, user_id) WHERE (user_id IS NOT NULL);


--
-- Name: uq_plan_price_active; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX uq_plan_price_active ON public.plan_prices USING btree (plan_id, "interval", currency) WHERE ((is_active = true) AND (active_to IS NULL));


--
-- Name: uq_plan_prices_active; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX uq_plan_prices_active ON public.plan_prices USING btree (plan_id, "interval") WHERE (is_active = true);


--
-- Name: uq_subscriptions_active_by_tenant; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX uq_subscriptions_active_by_tenant ON public.subscriptions USING btree (tenant_id) WHERE ((tenant_id IS NOT NULL) AND (status = 'active'::text));


--
-- Name: uq_subscriptions_active_personal_by_user; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX uq_subscriptions_active_personal_by_user ON public.subscriptions USING btree (user_id) WHERE ((tenant_id IS NULL) AND (status = 'active'::text));


--
-- Name: uq_tenant_invites_pending; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX uq_tenant_invites_pending ON public.tenant_invites USING btree (tenant_id, lower(email), role) WHERE ((accepted_at IS NULL) AND (revoked_at IS NULL));


--
-- Name: uq_tenant_members_tenant_user; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX uq_tenant_members_tenant_user ON public.tenant_members USING btree (tenant_id, user_id);


--
-- Name: ux_subscriptions_active_per_personal_user; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX ux_subscriptions_active_per_personal_user ON public.subscriptions USING btree (user_id) WHERE ((status = 'active'::text) AND (tenant_id IS NULL));


--
-- Name: ux_subscriptions_active_per_tenant; Type: INDEX; Schema: public; Owner: supabase_admin
--

CREATE UNIQUE INDEX ux_subscriptions_active_per_tenant ON public.subscriptions USING btree (tenant_id) WHERE ((status = 'active'::text) AND (tenant_id IS NOT NULL));


--
-- Name: ix_realtime_subscription_entity; Type: INDEX; Schema: realtime; Owner: supabase_admin
--

CREATE INDEX ix_realtime_subscription_entity ON realtime.subscription USING btree (entity);


--
-- Name: messages_inserted_at_topic_index; Type: INDEX; Schema: realtime; Owner: supabase_realtime_admin
--

CREATE INDEX messages_inserted_at_topic_index ON ONLY realtime.messages USING btree (inserted_at DESC, topic) WHERE ((extension = 'broadcast'::text) AND (private IS TRUE));


--
-- Name: messages_2026_03_20_inserted_at_topic_idx; Type: INDEX; Schema: realtime; Owner: supabase_admin
--

CREATE INDEX messages_2026_03_20_inserted_at_topic_idx ON realtime.messages_2026_03_20 USING btree (inserted_at DESC, topic) WHERE ((extension = 'broadcast'::text) AND (private IS TRUE));


--
-- Name: messages_2026_03_21_inserted_at_topic_idx; Type: INDEX; Schema: realtime; Owner: supabase_admin
--

CREATE INDEX messages_2026_03_21_inserted_at_topic_idx ON realtime.messages_2026_03_21 USING btree (inserted_at DESC, topic) WHERE ((extension = 'broadcast'::text) AND (private IS TRUE));


--
-- Name: messages_2026_03_22_inserted_at_topic_idx; Type: INDEX; Schema: realtime; Owner: supabase_admin
--

CREATE INDEX messages_2026_03_22_inserted_at_topic_idx ON realtime.messages_2026_03_22 USING btree (inserted_at DESC, topic) WHERE ((extension = 'broadcast'::text) AND (private IS TRUE));


--
-- Name: messages_2026_03_23_inserted_at_topic_idx; Type: INDEX; Schema: realtime; Owner: supabase_admin
--

CREATE INDEX messages_2026_03_23_inserted_at_topic_idx ON realtime.messages_2026_03_23 USING btree (inserted_at DESC, topic) WHERE ((extension = 'broadcast'::text) AND (private IS TRUE));


--
-- Name: messages_2026_03_24_inserted_at_topic_idx; Type: INDEX; Schema: realtime; Owner: supabase_admin
--

CREATE INDEX messages_2026_03_24_inserted_at_topic_idx ON realtime.messages_2026_03_24 USING btree (inserted_at DESC, topic) WHERE ((extension = 'broadcast'::text) AND (private IS TRUE));


--
-- Name: messages_2026_03_25_inserted_at_topic_idx; Type: INDEX; Schema: realtime; Owner: supabase_admin
--

CREATE INDEX messages_2026_03_25_inserted_at_topic_idx ON realtime.messages_2026_03_25 USING btree (inserted_at DESC, topic) WHERE ((extension = 'broadcast'::text) AND (private IS TRUE));


--
-- Name: messages_2026_03_26_inserted_at_topic_idx; Type: INDEX; Schema: realtime; Owner: supabase_admin
--

CREATE INDEX messages_2026_03_26_inserted_at_topic_idx ON realtime.messages_2026_03_26 USING btree (inserted_at DESC, topic) WHERE ((extension = 'broadcast'::text) AND (private IS TRUE));


--
-- Name: subscription_subscription_id_entity_filters_key; Type: INDEX; Schema: realtime; Owner: supabase_admin
--

CREATE UNIQUE INDEX subscription_subscription_id_entity_filters_key ON realtime.subscription USING btree (subscription_id, entity, filters);


--
-- Name: bname; Type: INDEX; Schema: storage; Owner: supabase_storage_admin
--

CREATE UNIQUE INDEX bname ON storage.buckets USING btree (name);


--
-- Name: bucketid_objname; Type: INDEX; Schema: storage; Owner: supabase_storage_admin
--

CREATE UNIQUE INDEX bucketid_objname ON storage.objects USING btree (bucket_id, name);


--
-- Name: buckets_analytics_unique_name_idx; Type: INDEX; Schema: storage; Owner: supabase_storage_admin
--

CREATE UNIQUE INDEX buckets_analytics_unique_name_idx ON storage.buckets_analytics USING btree (name) WHERE (deleted_at IS NULL);


--
-- Name: idx_iceberg_namespaces_bucket_id; Type: INDEX; Schema: storage; Owner: supabase_storage_admin
--

CREATE UNIQUE INDEX idx_iceberg_namespaces_bucket_id ON storage.iceberg_namespaces USING btree (catalog_id, name);


--
-- Name: idx_iceberg_tables_location; Type: INDEX; Schema: storage; Owner: supabase_storage_admin
--

CREATE UNIQUE INDEX idx_iceberg_tables_location ON storage.iceberg_tables USING btree (location);


--
-- Name: idx_iceberg_tables_namespace_id; Type: INDEX; Schema: storage; Owner: supabase_storage_admin
--

CREATE UNIQUE INDEX idx_iceberg_tables_namespace_id ON storage.iceberg_tables USING btree (catalog_id, namespace_id, name);


--
-- Name: idx_multipart_uploads_list; Type: INDEX; Schema: storage; Owner: supabase_storage_admin
--

CREATE INDEX idx_multipart_uploads_list ON storage.s3_multipart_uploads USING btree (bucket_id, key, created_at);


--
-- Name: idx_objects_bucket_id_name; Type: INDEX; Schema: storage; Owner: supabase_storage_admin
--

CREATE INDEX idx_objects_bucket_id_name ON storage.objects USING btree (bucket_id, name COLLATE "C");


--
-- Name: idx_objects_bucket_id_name_lower; Type: INDEX; Schema: storage; Owner: supabase_storage_admin
--

CREATE INDEX idx_objects_bucket_id_name_lower ON storage.objects USING btree (bucket_id, lower(name) COLLATE "C");


--
-- Name: name_prefix_search; Type: INDEX; Schema: storage; Owner: supabase_storage_admin
--

CREATE INDEX name_prefix_search ON storage.objects USING btree (name text_pattern_ops);


--
-- Name: vector_indexes_name_bucket_id_idx; Type: INDEX; Schema: storage; Owner: supabase_storage_admin
--

CREATE UNIQUE INDEX vector_indexes_name_bucket_id_idx ON storage.vector_indexes USING btree (name, bucket_id);


--
-- Name: supabase_functions_hooks_h_table_id_h_name_idx; Type: INDEX; Schema: supabase_functions; Owner: supabase_functions_admin
--

CREATE INDEX supabase_functions_hooks_h_table_id_h_name_idx ON supabase_functions.hooks USING btree (hook_table_id, hook_name);


--
-- Name: supabase_functions_hooks_request_id_idx; Type: INDEX; Schema: supabase_functions; Owner: supabase_functions_admin
--

CREATE INDEX supabase_functions_hooks_request_id_idx ON supabase_functions.hooks USING btree (request_id);
