Skip to content

Latest commit

 

History

History
57 lines (46 loc) · 2.84 KB

File metadata and controls

57 lines (46 loc) · 2.84 KB

conditional_stack_push

Location

src/fe_utils/conditional.c:53-68

Overview

Pushes a new conditional state onto the conditional stack, representing entry into a new nested conditional block.

Definition

void
conditional_stack_push(ConditionalStack cstack, ifState new_state)

Detailed Description

This function creates a new conditional branch by pushing a new IfStackElem onto the top of the conditional stack. It allocates memory for a new stack element, initializes it with the provided conditional state, and links it to the existing stack structure. The function implements a typical stack push operation using a linked list, where new elements are added at the head. The query_len and paren_depth fields are initialized to -1, indicating they need to be set later using appropriate setter functions when the actual values become available.

Parameters / Member Variables

  • cstack: ConditionalStack pointer to the stack where the new state should be pushed
  • new_state: ifState enum value representing the state of the new conditional block (IFSTATE_TRUE, IFSTATE_FALSE, IFSTATE_IGNORED, etc.)

Dependencies

Notes and Other Information

  • Creates a new IfStackElem on the heap, so each push must eventually have a corresponding pop to avoid memory leaks
  • The query_len and paren_depth are initialized to -1 and should be set using conditional_stack_set_query_len and conditional_stack_set_paren_depth
  • Used when entering \if, \elif, or \else blocks in psql and pgbench scripts
  • The new state determines whether the current conditional block should execute or be ignored
  • Part of the nested conditional handling system that allows for complex conditional logic in frontend scripts

Simplified Source

void conditional_stack_push(ConditionalStack cstack, ifState new_state) {
    // Allocate new stack element
    IfStackElem *elem = pg_malloc(sizeof(IfStackElem));

    // Initialize the element
    elem->if_state = new_state;
    elem->query_len = -1;        // To be set later
    elem->paren_depth = -1;      // To be set later

    // Add to stack head (push operation)
    elem->next = cstack->head;
    cstack->head = elem;
}