Re: [HACKERS] Improvements in psql hooks for variables - Mailing list pgsql-hackers

From Tom Lane
Subject Re: [HACKERS] Improvements in psql hooks for variables
Date
Msg-id 31785.1485900786@sss.pgh.pa.us
Whole thread Raw
In response to Re: [HACKERS] Improvements in psql hooks for variables  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
BTW ... while I've been fooling with this issue, I've gotten a bit
annoyed at the fact that "\set" prints the variables in, essentially,
creation order.  That makes the list ugly and hard to find things in,
and it exposes some psql implementation details to users.  I propose
the attached simple patch to keep the list in name order instead.

(This is on top of my previous patch, but it'd be pretty trivial
to modify to apply against HEAD.)

            regards, tom lane

diff --git a/src/bin/psql/variables.c b/src/bin/psql/variables.c
index b9b8fcb..9ca1000 100644
*** a/src/bin/psql/variables.c
--- b/src/bin/psql/variables.c
*************** valid_variable_name(const char *name)
*** 43,48 ****
--- 43,51 ----
  /*
   * A "variable space" is represented by an otherwise-unused struct _variable
   * that serves as list header.
+  *
+  * The list entries are kept in name order (according to strcmp).  This
+  * is mainly to make the results of PrintVariables() more pleasing.
   */
  VariableSpace
  CreateVariableSpace(void)
*************** GetVariable(VariableSpace space, const c
*** 74,84 ****

      for (current = space->next; current; current = current->next)
      {
!         if (strcmp(current->name, name) == 0)
          {
              /* this is correct answer when value is NULL, too */
              return current->value;
          }
      }

      return NULL;
--- 77,91 ----

      for (current = space->next; current; current = current->next)
      {
!         int            cmp = strcmp(current->name, name);
!
!         if (cmp == 0)
          {
              /* this is correct answer when value is NULL, too */
              return current->value;
          }
+         if (cmp > 0)
+             break;                /* it's not there */
      }

      return NULL;
*************** SetVariable(VariableSpace space, const c
*** 247,253 ****
           current;
           previous = current, current = current->next)
      {
!         if (strcmp(current->name, name) == 0)
          {
              /*
               * Found entry, so update, unless assign hook returns false.
--- 254,262 ----
           current;
           previous = current, current = current->next)
      {
!         int            cmp = strcmp(current->name, name);
!
!         if (cmp == 0)
          {
              /*
               * Found entry, so update, unless assign hook returns false.
*************** SetVariable(VariableSpace space, const c
*** 293,298 ****
--- 302,309 ----

              return confirmed;
          }
+         if (cmp > 0)
+             break;                /* it's not there */
      }

      /* not present, make new entry ... unless we were asked to delete */
*************** SetVariable(VariableSpace space, const c
*** 303,309 ****
          current->value = pg_strdup(value);
          current->substitute_hook = NULL;
          current->assign_hook = NULL;
!         current->next = NULL;
          previous->next = current;
      }
      return true;
--- 314,320 ----
          current->value = pg_strdup(value);
          current->substitute_hook = NULL;
          current->assign_hook = NULL;
!         current->next = previous->next;
          previous->next = current;
      }
      return true;
*************** SetVariableHooks(VariableSpace space, co
*** 343,349 ****
           current;
           previous = current, current = current->next)
      {
!         if (strcmp(current->name, name) == 0)
          {
              /* found entry, so update */
              current->substitute_hook = shook;
--- 354,362 ----
           current;
           previous = current, current = current->next)
      {
!         int            cmp = strcmp(current->name, name);
!
!         if (cmp == 0)
          {
              /* found entry, so update */
              current->substitute_hook = shook;
*************** SetVariableHooks(VariableSpace space, co
*** 354,359 ****
--- 367,374 ----
                  (void) (*ahook) (current->value);
              return;
          }
+         if (cmp > 0)
+             break;                /* it's not there */
      }

      /* not present, make new entry */
*************** SetVariableHooks(VariableSpace space, co
*** 362,368 ****
      current->value = NULL;
      current->substitute_hook = shook;
      current->assign_hook = ahook;
!     current->next = NULL;
      previous->next = current;
      if (shook)
          current->value = (*shook) (current->value);
--- 377,383 ----
      current->value = NULL;
      current->substitute_hook = shook;
      current->assign_hook = ahook;
!     current->next = previous->next;
      previous->next = current;
      if (shook)
          current->value = (*shook) (current->value);

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

pgsql-hackers by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: [HACKERS] Patch: Write Amplification Reduction Method (WARM)
Next
From: Andres Freund
Date:
Subject: Re: [HACKERS] Patch: Write Amplification Reduction Method (WARM)