Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cYghN-0005qx-C7 for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Jan 2017 22:14:49 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cYghM-0006sC-VF for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Jan 2017 22:14:49 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cYgfw-0003ux-QT for pgsql-hackers@postgresql.org; Tue, 31 Jan 2017 22:13:20 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cYgfu-0002uF-5x for pgsql-hackers@postgresql.org; Tue, 31 Jan 2017 22:13:19 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id v0VMD6oi031786; Tue, 31 Jan 2017 17:13:06 -0500 From: Tom Lane To: "Daniel Verite" cc: "Rahila Syed" , "Stephen Frost" , "Ashutosh Bapat" , "pgsql-hackers" , "Andrew Dunstan" Subject: Re: Improvements in psql hooks for variables In-reply-to: <9831.1485896832@sss.pgh.pa.us> References: <0451dd1e-aad1-4f9c-855e-6cc6584d4b54@manitou-mail.org> <30629.1485881533@sss.pgh.pa.us> <9831.1485896832@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Tue, 31 Jan 2017 16:07:12 -0500" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <31721.1485900676.0@sss.pgh.pa.us> Date: Tue, 31 Jan 2017 17:13:06 -0500 Message-ID: <31785.1485900786@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <31721.1485900676.1@sss.pgh.pa.us> 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 ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="keep-psql-variable-list-sorted-by-name.patch"; charset="us-ascii" Content-ID: <31721.1485900676.2@sss.pgh.pa.us> Content-Description: keep-psql-variable-list-sorted-by-name.patch Content-Transfer-Encoding: quoted-printable 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 _varia= ble * 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 **** =20=20 for (current =3D space->next; current; current =3D current->next) { ! if (strcmp(current->name, name) =3D=3D 0) { /* this is correct answer when value is NULL, too */ return current->value; } } =20=20 return NULL; --- 77,91 ---- =20=20 for (current =3D space->next; current; current =3D current->next) { ! int cmp =3D strcmp(current->name, name); !=20 ! if (cmp =3D=3D 0) { /* this is correct answer when value is NULL, too */ return current->value; } + if (cmp > 0) + break; /* it's not there */ } =20=20 return NULL; *************** SetVariable(VariableSpace space, const c *** 247,253 **** current; previous =3D current, current =3D current->next) { ! if (strcmp(current->name, name) =3D=3D 0) { /* * Found entry, so update, unless assign hook returns false. --- 254,262 ---- current; previous =3D current, current =3D current->next) { ! int cmp =3D strcmp(current->name, name); !=20 ! if (cmp =3D=3D 0) { /* * Found entry, so update, unless assign hook returns false. *************** SetVariable(VariableSpace space, const c *** 293,298 **** --- 302,309 ---- =20=20 return confirmed; } + if (cmp > 0) + break; /* it's not there */ } =20=20 /* not present, make new entry ... unless we were asked to delete */ *************** SetVariable(VariableSpace space, const c *** 303,309 **** current->value =3D pg_strdup(value); current->substitute_hook =3D NULL; current->assign_hook =3D NULL; ! current->next =3D NULL; previous->next =3D current; } return true; --- 314,320 ---- current->value =3D pg_strdup(value); current->substitute_hook =3D NULL; current->assign_hook =3D NULL; ! current->next =3D previous->next; previous->next =3D current; } return true; *************** SetVariableHooks(VariableSpace space, co *** 343,349 **** current; previous =3D current, current =3D current->next) { ! if (strcmp(current->name, name) =3D=3D 0) { /* found entry, so update */ current->substitute_hook =3D shook; --- 354,362 ---- current; previous =3D current, current =3D current->next) { ! int cmp =3D strcmp(current->name, name); !=20 ! if (cmp =3D=3D 0) { /* found entry, so update */ current->substitute_hook =3D shook; *************** SetVariableHooks(VariableSpace space, co *** 354,359 **** --- 367,374 ---- (void) (*ahook) (current->value); return; } + if (cmp > 0) + break; /* it's not there */ } =20=20 /* not present, make new entry */ *************** SetVariableHooks(VariableSpace space, co *** 362,368 **** current->value =3D NULL; current->substitute_hook =3D shook; current->assign_hook =3D ahook; ! current->next =3D NULL; previous->next =3D current; if (shook) current->value =3D (*shook) (current->value); --- 377,383 ---- current->value =3D NULL; current->substitute_hook =3D shook; current->assign_hook =3D ahook; ! current->next =3D previous->next; previous->next =3D current; if (shook) current->value =3D (*shook) (current->value); ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers ------- =_aaaaaaaaaa0--