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