πΈ A Curated Vault of PostgreSQL Data Modeling Patterns for Banking and Finance π
Explore 65+ real-world, production-ready SQL patterns for PostgreSQL, tailored to modern banking, fintech, and data engineering. Each pattern is a self-contained example, ready for learning or drop-in use.
From basic schema design to advanced indexing, temporal tracking, partitioning, security, and moreβ¦
git clone https://github.com/AishwaryaaP/bank-postgresql-vault.git
cd bank-postgresql-vault
Browse the patterns folder and pick your favorite pattern.
Below are direct links to every pattern for quick access, with descriptions:
| # | Pattern File | Description | π Quick Link |
|---|---|---|---|
| 1 | 01_create_banking_schema.sql | Creates the base banking schema | π View |
| 2 | 02_surrogate_key_identity.sql | Defines tables with surrogate primary keys using IDENTITY columns. | π View |
| 3 | 03_uuid_keys_pgcrypto.sql | Uses UUIDs as primary keys, generated with the pgcrypto extension. | π View |
| 4 | 04_composite_pk_ledger_lines.sql | Introduces composite primary keys for ledger line consistency. | π View |
| 5 | 05_natural_key_bic_swift.sql | Uses natural keys (BIC/SWIFT codes) for bank identification. | π View |
| 6 | 06_unique_constraints_customers.sql | Applies unique constraints to ensure no duplicate customer records. | π View |
| 7 | 07_foreign_keys_on_delete.sql | Demonstrates foreign key constraints with cascading deletes. | π View |
| 8 | 08_soft_delete_flag.sql | Shows soft deletion using a boolean flag instead of hard deletes. | π View |
| 9 | 09_audit_trail_customers.sql | Adds audit trail functionality to track customer changes. | π View |
| 10 | 10_temporal_history_accounts.sql | Implements temporal tables for account history tracking. | π View |
| 11 | 11_optimistic_locking_accounts.sql | Applies optimistic locking with version numbers for concurrent updates. | π View |
| 12 | 12_pessimistic_locking_transfer.sql | Uses pessimistic locking to safely handle money transfers. | π View |
| 13 | 13_upsert_customer_contact.sql | Provides UPSERT logic for customer contact info. | π View |
| 14 | 14_merge_daily_rates.sql | Demonstrates merging daily FX rates using MERGE. | π View |
| 15 | 15_idempotent_insert_transactions.sql | Ensures idempotent inserts of transaction data. | π View |
| 16 | 16_bulk_insert_copy_payments.sql | Loads bulk payment data using COPY. | π View |
| 17 | 17_batch_upsert_cards.sql | Performs batch upserts for card details. | π View |
| 18 | 18_partitioning_range_tx_date.sql | Implements range partitioning by transaction date. | π View |
| 19 | 19_partitioning_list_region.sql | Partitions data by region using list partitioning. | π View |
| 20 | 20_partition_maintenance_template.sql | Provides a template for partition maintenance tasks. | π View |
| 21 | 21_materialized_view_balances.sql | Creates materialized views for customer balances. | π View |
| 22 | 22_security_definer_read_model.sql | Defines security definer functions for read models. | π View |
| 23 | 23_jsonb_kyc_profile.sql | Stores KYC customer profile data in JSONB format. | π View |
| 24 | 24_jsonb_indexing_gin.sql | Creates GIN indexes on JSONB fields for faster queries. | π View |
| 25 | 25_full_text_search_support.sql | Enables full-text search support on text fields. | π View |
| 26 | 26_trigram_search_names.sql | Uses trigram search for fuzzy matching on customer names. | π View |
| 27 | 27_array_account_flags.sql | Stores multiple account flags using PostgreSQL arrays. | π View |
| 28 | 28_hstore_misc_attrs.sql | Uses hstore for flexible key-value storage. | π View |
| 29 | 29_window_functions_ranking.sql | Applies window functions for ranking transactions. | π View |
| 30 | 30_recursive_cte_org_hierarchy.sql | Models organizational hierarchy using recursive CTEs. | π View |
| 31 | 31_lateral_join_top_tx_per_acct.sql | Finds top transactions per account using lateral joins. | π View |
| 32 | 32_anti_join_inactive_customers.sql | Identifies inactive customers using anti-joins. | π View |
| 33 | 33_exists_vs_in_cards.sql | Compares EXISTS vs IN queries for card lookups. | π View |
| 34 | 34_partial_index_active_cards.sql | Creates partial indexes for active cards only. | π View |
| 35 | 35_expression_index_lower_email.sql | Indexes on lowercase emails for case-insensitive lookups. | π View |
| 36 | 36_multicolumn_index_hot_path.sql | Builds multicolumn indexes for performance hot paths. | π View |
| 37 | 37_concurrent_index_large_tx.sql | Demonstrates concurrent index creation on large transaction tables. | π View |
| 38 | 38_postgres_fdw_corebank.sql | Uses Postgres FDW to connect to external core banking DBs. | π View |
| 39 | 39_event_sourcing_ledger.sql | Implements event sourcing pattern for transaction ledger. | π View |
| 40 | 40_cqrs_read_model_mv.sql | Creates CQRS read models using materialized views. | π View |
| 41 | 41_triggers_examples_audit.sql | Shows triggers for auditing changes in key tables. | π View |
| 42 | 42_plpgsql_fn_interest_calc.sql | PL/pgSQL function to calculate account interest. | π View |
| 43 | 43_stored_procedure_transfer.sql | Stored procedure for executing money transfers. | π View |
| 44 | 44_roles_privileges_least.sql | Implements least privilege role-based security. | π View |
| 45 | 45_row_level_security_per_tenant.sql | Sets row-level security policies per tenant. | π View |
| 46 | 46_monthly_partition_do_block.sql | Automates monthly partitions with DO blocks. | π View |
| 47 | 47_backup_restore_notes.sql | Provides SQL notes for backup and restore best practices. | π View |
| 48 | 48_maintenance_vacuum_analyze.sql | Demonstrates table maintenance with VACUUM and ANALYZE. | π View |
| 49 | 49_sequences_and_setval.sql | Manages sequences and resets them with setval. | π View |
| 50 | 50_gapless_ticket_numbers.sql | Ensures gapless ticket numbers using sequences. | π View |
| 51 | 51_advisory_locks_dedup.sql | Uses advisory locks to prevent duplicate inserts. | π View |
| 52 | 52_explain_analyze_examples.sql | Shows query plans with EXPLAIN and ANALYZE. | π View |
| 53 | 53_query_rewrite_tips.sql | Provides query rewrite strategies for performance. | π View |
| 54 | 54_refresh_mv_concurrently.sql | Refreshes materialized views concurrently. | π View |
| 55 | 55_temp_tables_etl.sql | Uses temporary tables in ETL workflows. | π View |
| 56 | 56_pg_cron_example.sql | Schedules tasks with pg_cron extension. | π View |
| 57 | 57_copy_to_from_csv.sql | Imports and exports data using COPY with CSV. | π View |
| 58 | 58_cdc_wal2json_overview.sql | Demonstrates Change Data Capture with wal2json. | π View |
| 59 | 59_generic_search_function.sql | Defines a generic SQL search function. | π View |
| 60 | 60_schema_migrations_table.sql | Creates schema migrations table for versioning. | π View |
| 61 | 61_deferrable_constraints.sql | Shows deferrable constraints for transaction control. | π View |
| 62 | 62_citext_case_insensitive.sql | Uses citext type for case-insensitive text fields. | π View |
| 63 | 63_isolation_levels_examples.sql | Illustrates different transaction isolation levels. | π View |
| 64 | 64_generated_columns_demo.sql | Demonstrates generated columns in PostgreSQL. | π View |
| 65 | 65_check_constraints_business_rules.sql | Implements check constraints for business rules. | π View |
βοΈ If you find this helpful, leave a star and share your thoughts!
Aishwarya Yuvaraj Phirke
π GitHub: @AishwaryaaP
π Connect on LinkedIn
Made with β€οΈ, β and PostgreSQL β Happy Coding!