Scenario 04: Feature Development

Scenario 04: Feature Development

Developer understanding existing code to add new features.

Quick Start

# Select Feature Development Scenario
/select 04

Understanding Existing Code

> Find functions related to transaction handling

╭─────────────── Related Code ────────────────────────────────╮
│                                                              │
│  Transaction Functions:                                      │
│                                                              │
│  Entry Points:                                               │
│    StartTransaction()      src/backend/access/transam/xact.c │
│    CommitTransaction()     src/backend/access/transam/xact.c │
│    AbortTransaction()      src/backend/access/transam/xact.c │
│                                                              │
│  Support Functions:                                          │
│    AssignTransactionId()   src/backend/access/transam/xact.c │
│    GetCurrentTransactionId() src/backend/access/transam/xact.c │
│                                                              │
│  Related Modules:                                            │
│    src/backend/access/heap/  - Heap operations               │
│    src/backend/storage/lmgr/ - Lock manager                  │
│                                                              │
╰──────────────────────────────────────────────────────────────╯

Understanding Call Chains

> Show the call path from parser to executor

╭─────────────── Call Chain ──────────────────────────────────╮
│                                                              │
│  Parser → Executor Flow:                                     │
│                                                              │
│  1. pg_parse_query()                                         │
│       ↓                                                      │
│  2. pg_analyze_and_rewrite()                                 │
│       ↓                                                      │
│  3. pg_plan_queries()                                        │
│       ↓                                                      │
│  4. PortalRun()                                              │
│       ↓                                                      │
│  5. ExecutorStart() → ExecutorRun() → ExecutorFinish()       │
│       ↓                                                      │
│  6. ExecProcNode() [dispatches to specific nodes]            │
│                                                              │
╰──────────────────────────────────────────────────────────────╯

Integration Points

Finding Extension Points

> Where can I add a new node type?

╭─────────────── Extension Points ────────────────────────────╮
                                                              
  Adding New Executor Node Type:                              
                                                              
  1. Define node structure in:                                
     src/include/nodes/execnodes.h                            
                                                              
  2. Implement node operations:                               
     src/backend/executor/nodeXXX.c                           
     - ExecInitXXX()                                          
     - ExecXXX()                                              
     - ExecEndXXX()                                           
                                                              
  3. Register in dispatcher:                                  
     src/backend/executor/execProcnode.c                      
     - ExecInitNode()                                         
     - ExecProcNode()                                         
                                                              
╰──────────────────────────────────────────────────────────────╯

Understanding Dependencies

> What modules does the buffer manager depend on?

╭─────────────── Dependencies ────────────────────────────────╮
│                                                              │
│  Buffer Manager Dependencies:                                │
│                                                              │
│  Upstream (uses):                                            │
│    - storage/smgr     (Storage manager interface)            │
│    - storage/lmgr     (Lock manager)                         │
│    - utils/memutils   (Memory contexts)                      │
│                                                              │
│  Downstream (used by):                                       │
│    - access/heap      (Heap access methods)                  │
│    - access/index     (Index access methods)                 │
│    - executor/*       (Query execution)                      │
│                                                              │
│  Interfaces:                                                 │
│    - ReadBuffer()     (Main read interface)                  │
│    - ReleaseBuffer()  (Release interface)                    │
│                                                              │
╰──────────────────────────────────────────────────────────────╯

Code Patterns

Finding Similar Implementations

> Show examples of error handling in executor

╭─────────────── Error Handling Patterns ─────────────────────╮
                                                              
  Common Patterns in Executor:                                
                                                              
  1. ereport with ERROR level:                                
     ereport(ERROR,                                           
         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),             
          errmsg("feature X not supported")));                
                                                              
  2. Exception handling with PG_TRY:                          
     PG_TRY();                                                
     {                                                        
         /* risky operation */                                
     }                                                        
     PG_CATCH();                                              
     {                                                        
         /* cleanup */                                        
         PG_RE_THROW();                                       
     }                                                        
     PG_END_TRY();                                            
                                                              
╰──────────────────────────────────────────────────────────────╯

Placement Recommendations

> Where should I place new cache invalidation feature?

╭─────────────── Recommended Placement ─────────────────────╮
│                                                            │
│  Subsystem: cache manager                                  │
│  File: src/backend/utils/cache.c                           │
│  Near method: cache_lookup() (line 145)                    │
│  Confidence: 75%                                           │
│                                                            │
│  Related methods in this area:                             │
│    - cache_insert()                                        │
│    - cache_remove()                                        │
│    - cache_invalidate_all()                                │
│                                                            │
╰────────────────────────────────────────────────────────────╯

Pattern Examples

> Show pattern examples for executor subsystem

╭─────────────── Pattern Examples ──────────────────────────╮
│                                                            │
│  Examples from the target subsystem:                       │
│                                                            │
│  Method          | File          | Pattern        | CC    │
│  init_executor() | execMain.c:42 | Initialization | 3     │
│  exec_scan()     | execScan.c:88 | Handler        | 5     │
│  get_plan_node() | execUtils.c:15| Query/lookup   | 2     │
│  check_perms()   | execPerms.c:7 | Validation     | 4     │
│  end_executor()  | execMain.c:200| Cleanup        | 1     │
│                                                            │
╰────────────────────────────────────────────────────────────╯

Example Questions

  • “How does [feature_name] work?”
  • “What functions are involved in [subsystem]?”
  • “Where should I place new [feature] in [module]?”
  • “Show similar implementations to [existing_feature]”
  • “What’s the data flow for [operation]?”
  • “Find integration points for [component]”
  • “Show pattern examples for [subsystem]”
  • “Recommend placement for new [feature_type]”