-- Indexes
-- Gerado automaticamente em 2026-05-11T16:53:50.934Z
-- Total: 372

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

CREATE INDEX dal_documento_idx ON public.document_access_logs USING btree (documento_id, acessado_em DESC);

CREATE INDEX dal_tenant_idx ON public.document_access_logs USING btree (tenant_id, acessado_em DESC);

CREATE INDEX dal_user_idx ON public.document_access_logs USING btree (user_id, acessado_em DESC);

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

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

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

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

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

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

CREATE INDEX dg_gerado_por_idx ON public.document_generated USING btree (gerado_por, gerado_em DESC);

CREATE INDEX dg_patient_idx ON public.document_generated USING btree (patient_id, gerado_em DESC);

CREATE INDEX dg_template_idx ON public.document_generated USING btree (template_id);

CREATE INDEX dg_tenant_idx ON public.document_generated USING btree (tenant_id, gerado_em DESC);

CREATE INDEX docs_active_idx ON public.documents USING btree (patient_id, uploaded_at DESC) WHERE (deleted_at IS NULL);

CREATE INDEX docs_nome_trgm_idx ON public.documents USING gin (nome_original public.gin_trgm_ops);

CREATE INDEX docs_owner_idx ON public.documents USING btree (owner_id);

CREATE INDEX docs_patient_idx ON public.documents USING btree (patient_id);

CREATE INDEX docs_tags_idx ON public.documents USING gin (tags);

CREATE INDEX docs_tenant_idx ON public.documents USING btree (tenant_id);

CREATE INDEX docs_tipo_idx ON public.documents USING btree (patient_id, tipo_documento);

CREATE INDEX docs_uploaded_at_idx ON public.documents USING btree (patient_id, uploaded_at DESC);

CREATE INDEX ds_documento_idx ON public.document_signatures USING btree (documento_id, ordem);

CREATE INDEX ds_status_idx ON public.document_signatures USING btree (documento_id, status);

CREATE INDEX ds_tenant_idx ON public.document_signatures USING btree (tenant_id);

CREATE INDEX dsl_documento_idx ON public.document_share_links USING btree (documento_id);

CREATE INDEX dsl_expira_idx ON public.document_share_links USING btree (expira_em) WHERE (ativo = true);

CREATE INDEX dsl_token_idx ON public.document_share_links USING btree (token) WHERE (ativo = true);

CREATE INDEX dt_global_idx ON public.document_templates USING btree (is_global) WHERE (is_global = true);

CREATE INDEX dt_nome_trgm_idx ON public.document_templates USING gin (nome_template public.gin_trgm_ops);

CREATE INDEX dt_owner_idx ON public.document_templates USING btree (owner_id);

CREATE INDEX dt_tenant_idx ON public.document_templates USING btree (tenant_id);

CREATE INDEX dt_tipo_idx ON public.document_templates USING btree (tipo);

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

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

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

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

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

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

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

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

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

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

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

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

CREATE INDEX idx_agenda_eventos_titulo_custom_trgm ON public.agenda_eventos USING gin (titulo_custom public.gin_trgm_ops) WHERE (titulo_custom IS NOT NULL);

CREATE INDEX idx_agenda_eventos_titulo_trgm ON public.agenda_eventos USING gin (titulo public.gin_trgm_ops) WHERE (titulo IS NOT NULL);

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

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

CREATE INDEX idx_audit_logs_changed_fields ON public.audit_logs USING gin (changed_fields);

CREATE INDEX idx_audit_logs_entity ON public.audit_logs USING btree (entity_type, entity_id);

CREATE INDEX idx_audit_logs_tenant_created ON public.audit_logs USING btree (tenant_id, created_at DESC);

CREATE INDEX idx_audit_logs_user_created ON public.audit_logs USING btree (user_id, created_at DESC) WHERE (user_id IS NOT NULL);

CREATE INDEX idx_autoreply_log_cooldown ON public.conversation_autoreply_log USING btree (tenant_id, thread_key, sent_at DESC);

CREATE INDEX idx_bot_sessions_tenant_status ON public.conversation_bot_sessions USING btree (tenant_id, status, started_at DESC);

CREATE INDEX idx_contact_email_types_tenant ON public.contact_email_types USING btree (tenant_id, "position");

CREATE INDEX idx_contact_emails_email ON public.contact_emails USING btree (tenant_id, email);

CREATE INDEX idx_contact_emails_entity ON public.contact_emails USING btree (tenant_id, entity_type, entity_id, "position");

CREATE INDEX idx_contact_phones_entity ON public.contact_phones USING btree (tenant_id, entity_type, entity_id, "position");

CREATE INDEX idx_contact_phones_number ON public.contact_phones USING btree (tenant_id, number);

CREATE INDEX idx_contact_types_tenant ON public.contact_types USING btree (tenant_id, "position");

CREATE INDEX idx_conv_assign_patient ON public.conversation_assignments USING btree (patient_id) WHERE (patient_id IS NOT NULL);

CREATE INDEX idx_conv_assign_tenant_user ON public.conversation_assignments USING btree (tenant_id, assigned_to) WHERE (assigned_to IS NOT NULL);

CREATE INDEX idx_conv_msg_delivery_status ON public.conversation_messages USING btree (tenant_id, delivery_status) WHERE (direction = 'outbound'::text);

CREATE INDEX idx_conv_msg_from_number ON public.conversation_messages USING btree (tenant_id, from_number);

CREATE INDEX idx_conv_msg_kanban ON public.conversation_messages USING btree (tenant_id, kanban_status, priority DESC, created_at DESC);

CREATE INDEX idx_conv_msg_patient ON public.conversation_messages USING btree (patient_id, created_at DESC) WHERE (patient_id IS NOT NULL);

CREATE INDEX idx_conv_msg_provider_msg_id ON public.conversation_messages USING btree (provider_message_id) WHERE (provider_message_id IS NOT NULL);

CREATE INDEX idx_conv_msg_tenant_created ON public.conversation_messages USING btree (tenant_id, created_at DESC);

CREATE INDEX idx_conv_notes_created_by ON public.conversation_notes USING btree (created_by, created_at DESC) WHERE (deleted_at IS NULL);

CREATE INDEX idx_conv_notes_patient ON public.conversation_notes USING btree (patient_id, created_at DESC) WHERE ((deleted_at IS NULL) AND (patient_id IS NOT NULL));

CREATE INDEX idx_conv_notes_tenant_thread ON public.conversation_notes USING btree (tenant_id, thread_key, created_at DESC) WHERE (deleted_at IS NULL);

CREATE INDEX idx_conv_optout_kw_tenant ON public.conversation_optout_keywords USING btree (tenant_id) WHERE (enabled = true);

CREATE INDEX idx_conv_optouts_patient ON public.conversation_optouts USING btree (patient_id) WHERE (patient_id IS NOT NULL);

CREATE INDEX idx_conv_optouts_tenant_phone ON public.conversation_optouts USING btree (tenant_id, phone);

CREATE INDEX idx_conv_tags_tenant ON public.conversation_tags USING btree (tenant_id, "position");

CREATE INDEX idx_conv_thread_tags_tag ON public.conversation_thread_tags USING btree (tag_id);

CREATE INDEX idx_conv_thread_tags_tenant_thread ON public.conversation_thread_tags USING btree (tenant_id, thread_key);

CREATE INDEX idx_dev_auditoria_items_categoria ON public.dev_auditoria_items USING btree (categoria);

CREATE INDEX idx_dev_auditoria_items_ordem ON public.dev_auditoria_items USING btree (ordem);

CREATE INDEX idx_dev_auditoria_items_severidade ON public.dev_auditoria_items USING btree (severidade);

CREATE INDEX idx_dev_auditoria_items_status ON public.dev_auditoria_items USING btree (status);

CREATE INDEX idx_dev_ccs_comp ON public.dev_comparison_competitor_status USING btree (competitor_id);

CREATE INDEX idx_dev_ccs_comparison ON public.dev_comparison_competitor_status USING btree (comparison_id);

CREATE INDEX idx_dev_comparison_matrix_dominio ON public.dev_comparison_matrix USING btree (dominio);

CREATE INDEX idx_dev_comparison_matrix_status ON public.dev_comparison_matrix USING btree (nosso_status);

CREATE INDEX idx_dev_competitor_features_cat ON public.dev_competitor_features USING btree (categoria);

CREATE INDEX idx_dev_competitor_features_comp ON public.dev_competitor_features USING btree (competitor_id);

CREATE INDEX idx_dev_competitor_features_destaque ON public.dev_competitor_features USING btree (destaque);

CREATE INDEX idx_dev_competitor_features_ordem ON public.dev_competitor_features USING btree (competitor_id, ordem);

CREATE INDEX idx_dev_competitors_ativo ON public.dev_competitors USING btree (ativo);

CREATE INDEX idx_dev_competitors_pais ON public.dev_competitors USING btree (pais);

CREATE INDEX idx_dev_generation_log_created ON public.dev_generation_log USING btree (created_at DESC);

CREATE INDEX idx_dev_generation_log_tipo ON public.dev_generation_log USING btree (tipo);

CREATE INDEX idx_dev_roadmap_items_ordem ON public.dev_roadmap_items USING btree (phase_id, ordem);

CREATE INDEX idx_dev_roadmap_items_phase ON public.dev_roadmap_items USING btree (phase_id);

CREATE INDEX idx_dev_roadmap_items_prior ON public.dev_roadmap_items USING btree (prioridade);

CREATE INDEX idx_dev_roadmap_items_status ON public.dev_roadmap_items USING btree (status);

CREATE INDEX idx_dev_roadmap_phases_ordem ON public.dev_roadmap_phases USING btree (ordem);

CREATE INDEX idx_dev_roadmap_phases_status ON public.dev_roadmap_phases USING btree (status);

CREATE INDEX idx_dev_test_items_area ON public.dev_test_items USING btree (area);

CREATE INDEX idx_dev_test_items_ordem ON public.dev_test_items USING btree (area, ordem);

CREATE INDEX idx_dev_test_items_status ON public.dev_test_items USING btree (status);

CREATE INDEX idx_dev_verificacoes_area ON public.dev_verificacoes_items USING btree (area);

CREATE INDEX idx_dev_verificacoes_ordem ON public.dev_verificacoes_items USING btree (area, ordem);

CREATE INDEX idx_dev_verificacoes_severidade ON public.dev_verificacoes_items USING btree (severidade);

CREATE INDEX idx_dev_verificacoes_status ON public.dev_verificacoes_items USING btree (status);

CREATE INDEX idx_documents_content_sha256 ON public.documents USING btree (content_sha256) WHERE (content_sha256 IS NOT NULL);

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

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

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

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));

CREATE INDEX idx_features_is_active ON public.features USING btree (is_active) WHERE (is_active = false);

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

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

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

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

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

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

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

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

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

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

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));

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

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

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

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

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

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

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

CREATE INDEX idx_intakes_progress_pending ON public.patient_intake_requests USING btree (last_progress_at) WHERE (status = 'in_progress'::text);

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

CREATE INDEX idx_mc_expires ON public.math_challenges USING btree (expires_at);

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

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));

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

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

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

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

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

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

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

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

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));

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

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

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

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

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

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

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));

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));

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

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));

CREATE INDEX idx_notification_channels_twilio_subaccount_sid ON public.notification_channels USING btree (twilio_subaccount_sid) WHERE (twilio_subaccount_sid IS NOT NULL);

CREATE INDEX idx_patient_contacts_patient ON public.patient_contacts USING btree (patient_id);

CREATE INDEX idx_patient_contacts_tenant ON public.patient_contacts USING btree (tenant_id);

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

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

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

CREATE INDEX idx_patient_intake_requests_nome_trgm ON public.patient_intake_requests USING gin (nome_completo public.gin_trgm_ops) WHERE (status = 'new'::text);

CREATE INDEX idx_patient_invite_attempts_created ON public.patient_invite_attempts USING btree (created_at DESC);

CREATE INDEX idx_patient_invite_attempts_ok ON public.patient_invite_attempts USING btree (ok) WHERE (ok = false);

CREATE INDEX idx_patient_invite_attempts_owner ON public.patient_invite_attempts USING btree (owner_id);

CREATE INDEX idx_patient_invite_attempts_token ON public.patient_invite_attempts USING btree (token);

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

CREATE INDEX idx_patients_cpf_trgm ON public.patients USING gin (cpf public.gin_trgm_ops) WHERE (cpf IS NOT NULL);

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

CREATE INDEX idx_patients_email_trgm ON public.patients USING gin (email_principal public.gin_trgm_ops) WHERE (email_principal IS NOT NULL);

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

CREATE INDEX idx_patients_nome_trgm ON public.patients USING gin (nome_completo public.gin_trgm_ops);

CREATE INDEX idx_patients_origem ON public.patients USING btree (tenant_id, origem) WHERE (origem IS NOT NULL);

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

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

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

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

CREATE INDEX idx_patients_risco_elevado ON public.patients USING btree (tenant_id, risco_elevado) WHERE (risco_elevado = true);

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

CREATE INDEX idx_patients_status_tenant ON public.patients USING btree (tenant_id, status);

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

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

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

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

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

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

CREATE INDEX idx_psa_endpoint_created ON public.public_submission_attempts USING btree (endpoint, created_at DESC);

CREATE INDEX idx_psa_failed ON public.public_submission_attempts USING btree (created_at DESC) WHERE (success = false);

CREATE INDEX idx_psa_ip_hash_created ON public.public_submission_attempts USING btree (ip_hash, created_at DESC) WHERE (ip_hash IS NOT NULL);

CREATE INDEX idx_psh_patient ON public.patient_status_history USING btree (patient_id, alterado_em DESC);

CREATE INDEX idx_psh_tenant ON public.patient_status_history USING btree (tenant_id, alterado_em DESC);

CREATE INDEX idx_pt_evento_tipo ON public.patient_timeline USING btree (patient_id, evento_tipo);

CREATE INDEX idx_pt_patient_ocorrido ON public.patient_timeline USING btree (patient_id, ocorrido_em DESC);

CREATE INDEX idx_pt_tenant ON public.patient_timeline USING btree (tenant_id, ocorrido_em DESC);

CREATE INDEX idx_services_name_trgm ON public.services USING gin (name public.gin_trgm_ops);

CREATE INDEX idx_session_reminder_tenant_sent ON public.session_reminder_logs USING btree (tenant_id, sent_at DESC);

CREATE INDEX idx_sla_breaches_open ON public.conversation_sla_breaches USING btree (resolved_at) WHERE (resolved_at IS NULL);

CREATE INDEX idx_sla_breaches_tenant_breached ON public.conversation_sla_breaches USING btree (tenant_id, breached_at DESC);

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

CREATE INDEX idx_srl_blocked_until ON public.submission_rate_limits USING btree (blocked_until) WHERE (blocked_until IS NOT NULL);

CREATE INDEX idx_srl_endpoint ON public.submission_rate_limits USING btree (endpoint, last_attempt_at DESC);

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

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

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

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

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

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

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

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

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

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

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

CREATE INDEX idx_twilio_usage_channel ON public.twilio_subaccount_usage USING btree (channel_id, period_start DESC);

CREATE INDEX idx_twilio_usage_tenant_period ON public.twilio_subaccount_usage USING btree (tenant_id, period_start DESC);

CREATE UNIQUE INDEX idx_twilio_usage_unique_period ON public.twilio_subaccount_usage USING btree (channel_id, period_start, period_end);

CREATE INDEX idx_wa_credit_packages_active ON public.whatsapp_credit_packages USING btree (is_active, "position", price_brl) WHERE (is_active = true);

CREATE INDEX idx_wa_credit_purchases_asaas_payment ON public.whatsapp_credit_purchases USING btree (asaas_payment_id) WHERE (asaas_payment_id IS NOT NULL);

CREATE INDEX idx_wa_credit_purchases_status ON public.whatsapp_credit_purchases USING btree (status, created_at DESC);

CREATE INDEX idx_wa_credit_purchases_tenant ON public.whatsapp_credit_purchases USING btree (tenant_id, created_at DESC);

CREATE INDEX idx_wa_credits_tx_kind ON public.whatsapp_credits_transactions USING btree (tenant_id, kind, created_at DESC);

CREATE INDEX idx_wa_credits_tx_tenant_created ON public.whatsapp_credits_transactions USING btree (tenant_id, created_at DESC);

CREATE INDEX idx_wa_incidents_open ON public.whatsapp_connection_incidents USING btree (resolved_at) WHERE (resolved_at IS NULL);

CREATE INDEX idx_wa_incidents_tenant_started ON public.whatsapp_connection_incidents USING btree (tenant_id, started_at DESC);

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

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

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

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

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

CREATE INDEX medicos_especialidade_idx ON public.medicos USING btree (especialidade);

CREATE INDEX medicos_nome_idx ON public.medicos USING btree (nome);

CREATE INDEX medicos_nome_trgm_idx ON public.medicos USING gin (nome public.gin_trgm_ops);

CREATE INDEX medicos_owner_idx ON public.medicos USING btree (owner_id);

CREATE INDEX medicos_tenant_idx ON public.medicos USING btree (tenant_id);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

CREATE INDEX patients_convenio_id_idx ON public.patients USING btree (convenio_id);

CREATE INDEX patients_etnia_idx ON public.patients USING btree (etnia);

CREATE INDEX patients_pronomes_idx ON public.patients USING btree (pronomes);

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

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

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

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

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

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

CREATE INDEX psc_owner_idx ON public.patient_support_contacts USING btree (owner_id);

CREATE INDEX psc_patient_idx ON public.patient_support_contacts USING btree (patient_id);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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));

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

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

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

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

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

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

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

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

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

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

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

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

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

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

CREATE UNIQUE INDEX uq_bot_sessions_active_per_thread ON public.conversation_bot_sessions USING btree (tenant_id, thread_key) WHERE (status = 'active'::text);

CREATE UNIQUE INDEX uq_contact_email_types_system_slug ON public.contact_email_types USING btree (slug) WHERE (tenant_id IS NULL);

CREATE UNIQUE INDEX uq_contact_email_types_tenant_slug ON public.contact_email_types USING btree (tenant_id, slug) WHERE (tenant_id IS NOT NULL);

CREATE UNIQUE INDEX uq_contact_emails_primary ON public.contact_emails USING btree (entity_type, entity_id) WHERE (is_primary = true);

CREATE UNIQUE INDEX uq_contact_phones_primary ON public.contact_phones USING btree (entity_type, entity_id) WHERE (is_primary = true);

CREATE UNIQUE INDEX uq_contact_types_system_slug ON public.contact_types USING btree (slug) WHERE (tenant_id IS NULL);

CREATE UNIQUE INDEX uq_contact_types_tenant_slug ON public.contact_types USING btree (tenant_id, slug) WHERE (tenant_id IS NOT NULL);

CREATE UNIQUE INDEX uq_conv_optouts_active ON public.conversation_optouts USING btree (tenant_id, phone) WHERE (opted_back_in_at IS NULL);

CREATE UNIQUE INDEX uq_conv_tags_system_slug ON public.conversation_tags USING btree (slug) WHERE (tenant_id IS NULL);

CREATE UNIQUE INDEX uq_conv_tags_tenant_slug ON public.conversation_tags USING btree (tenant_id, slug) WHERE (tenant_id IS NOT NULL);

CREATE UNIQUE INDEX uq_patient_contacts_primario ON public.patient_contacts USING btree (patient_id) WHERE ((is_primario = true) AND (ativo = true));

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

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));

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

CREATE UNIQUE INDEX uq_session_reminder_event_type ON public.session_reminder_logs USING btree (event_id, reminder_type);

CREATE UNIQUE INDEX uq_sla_breaches_open_per_thread ON public.conversation_sla_breaches USING btree (tenant_id, thread_key) WHERE (resolved_at IS NULL);

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));

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));

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));

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

CREATE UNIQUE INDEX uq_wa_incidents_open_per_channel ON public.whatsapp_connection_incidents USING btree (channel_id) WHERE (resolved_at IS NULL);

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));

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));
