Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql Patches

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 06:27 AM
Bernd Helmle
 
Posts: n/a
Default psql command aliases support

Folks,

please find attached a patch which implements psql command aliases. They
work the same way as on bash, zsh and others, for example:

#= \alias d \dt+
#= \d
List of relations
Schema | Name | Type | Owner | Description
--------+------+-------+-------+-------------
public | foo | table | bernd |
(1 row)

=# \alias current_query SELECT current_query, NOW() - query_start FROM
pg_stat_activity WHERE current_query NOT LIKE 'IDLE%';

#= \current_query
current_query
| ?column?
-------------------------------------------------------------------------------------------------------+----------
SELECT current_query, NOW() - query_start FROM pg_stat_activity WHERE
current_query NOT LIKE 'IDLE%'; | 00:00:00
(1 row)


#= \unalias d
#= \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | foo | table | bernd
(1 row)


I hope i broke nothing and maybe we find this useful for 8.4, documentation
included.

--
Thanks

Bernd

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 06:27 AM
Tom Lane
 
Posts: n/a
Default Re: psql command aliases support

Bernd Helmle <mailings@oopsware.de> writes:
> please find attached a patch which implements psql command aliases. They
> work the same way as on bash, zsh and others, for example:


> #= \alias d \dt+
> #= \d


Do we really want such a thing? The space of backslash command names
is so densely populated already that it's hard to imagine creating
aliases without conflicting with existing (or future) command names
--- as indeed your example does. It seems like mostly a recipe for
confusion.

regards, tom lane

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 06:27 AM
Peter Eisentraut
 
Posts: n/a
Default Re: psql command aliases support

Am Dienstag, 1. April 2008 schrieb Tom Lane:
> Do we really want such a thing?


Yes!

> The space of backslash command names
> is so densely populated already that it's hard to imagine creating
> aliases without conflicting with existing (or future) command names
> --- as indeed your example does. It seems like mostly a recipe for
> confusion.


This is a standard feature and effect on shells. Shells have even more likely
commands and conflicts, and still aliases are widely used. If people are
concerned about conflicts, they shouldn't use aliases.

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-19-2008, 06:27 AM
Bernd Helmle
 
Posts: n/a
Default Re: psql command aliases support

--On Dienstag, April 01, 2008 11:39:59 -0400 Tom Lane <tgl@sss.pgh.pa.us>
wrote:

> Do we really want such a thing?


Well, i use aliases everytime and everywhere they got implemented and i
found it quite useful to _extend_ existing behavior (integrating additional
functionality in an easy way)

> The space of backslash command names
> is so densely populated already that it's hard to imagine creating
> aliases without conflicting with existing (or future) command names


I often found existing backslash command sometimes overloaded or simply not
providing information i really need (for example, an easy way to get
information about current locales, encoding and user settings). You simply
can't catch all requirements DBA's and users want within a all-catching
implementation. Using this way, users are able to implement their own
command shortcuts

Overriding existing backslash commands (as my first example shows) is only
an implementation-specific detail which could easily forbidden. However,
defining your own shortcuts for your psql-sessions looks quite useful to
me, like my 2nd example tries to illustrate.

> It seems like mostly a recipe for confusion.


So what? This could happen in every shell that supports aliases as well. I
don't get your point...?

--
Thanks

Bernd

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-19-2008, 06:27 AM
Gregory Stark
 
Posts: n/a
Default Re: psql command aliases support

"Peter Eisentraut" <peter_e@gmx.net> writes:

> Am Dienstag, 1. April 2008 schrieb Tom Lane:
>> Do we really want such a thing?

>
> Yes!
>
>> The space of backslash command names
>> is so densely populated already that it's hard to imagine creating
>> aliases without conflicting with existing (or future) command names
>> --- as indeed your example does. It seems like mostly a recipe for
>> confusion.

>
> This is a standard feature and effect on shells. Shells have even more likely
> commands and conflicts, and still aliases are widely used. If people are
> concerned about conflicts, they shouldn't use aliases.


I think you're crazy to think shells are more likely to have conflicts. Shells
require a whole token match, not just the first letter.

In other words, any alias *starting with* the letters c, d, e, f, g, h, i,
o, s, w, z would be a conflict. Just for maximum confusion the list of letters
which cause conflicts when capitalized would be entirely different.

Picture a newbie typoing on their \old alias and trying to figure out where
all their data is going... Hopefully they weren't too attached to whatever was
in their "ldd" file yesterday.

I could see it being handy being able to save commonly executed queries and
access them with a shortcut but I think you need to use a separate namespace
from psql's backslash commands. Perhaps `query` or invent a single backslash
command to execute them like "\query current_query". Actually I like that last
idea the most.

One thing to think about is how to pass arguments to the aliases. Csh put us
in the soup by hacking in arguments in a terrible way. As a result quoting in
csh aliases is awful. Even if it's not implemented right away I think we
should figure out what the syntax would be for passing arguments to be
interpolated into the query before backing ourselves into a corner.

I can't imagine much of a use case for being able to alias backslash commands
themselves. They're already ridiculously terse anyways. How many keystrokes
can you possibly save?

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's On-Demand Production Tuning

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-19-2008, 06:27 AM
Bernd Helmle
 
Posts: n/a
Default Re: psql command aliases support

--On Donnerstag, April 03, 2008 03:13:47 +0100 Gregory Stark
<stark@enterprisedb.com> wrote:

> I think you're crazy to think shells are more likely to have conflicts.
> Shells require a whole token match, not just the first letter.
>
> In other words, any alias *starting with* the letters c, d, e, f, g, h, i,
> o, s, w, z would be a conflict. Just for maximum confusion the list of
> letters which cause conflicts when capitalized would be entirely
> different.
>
> Picture a newbie typoing on their \old alias and trying to figure out
> where all their data is going... Hopefully they weren't too attached to
> whatever was in their "ldd" file yesterday.


Of course, the patch doesn't work this way. Only complete tokens delivered
by the parser are substituted, illustrated here with your example:


#= \alias old SELECT version();
#= \old
version
---------------------------------------------------------------------------------------------------
PostgreSQL 8.4devel on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC)
4.2.3 (Debian 4.2.3-2)
(1 row)

#= \o foo
#= \old
#=
zsh: suspended psql
% cat foo
version
---------------------------------------------------------------------------------------------------
PostgreSQL 8.4devel on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC)
4.2.3 (Debian 4.2.3-2)
(1 row)

--
Thanks

Bernd

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-19-2008, 06:27 AM
Gregory Stark
 
Posts: n/a
Default Re: psql command aliases support

"Bernd Helmle" <mailings@oopsware.de> writes:

>> Picture a newbie typoing on their \old alias and trying to figure out
>> where all their data is going... Hopefully they weren't too attached to
>> whatever was in their "ldd" file yesterday.

>
> Of course, the patch doesn't work this way. Only complete tokens delivered by
> the parser are substituted, illustrated here with your example:


To be more explicit what I meant was someone doing

#= \alias old select version();
....
#= \oldd
<oops>
#= \old
#= select 'where is all my output going?'
#= select 'what happened to my ldd file?'




--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's RemoteDBA services!

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-19-2008, 06:27 AM
Peter Eisentraut
 
Posts: n/a
Default Re: psql command aliases support

Am Donnerstag, 3. April 2008 schrieb Gregory Stark:
> To be more explicit what I meant was someone doing
>
> #= \alias old select version();
> ...
> #= \oldd
> <oops>
> #= \old
> #= select 'where is all my output going?'
> #= select 'what happened to my ldd file?'


This is a valid concern, but it is orthogonal to the alias feature. You have
the same problem already if you mistype

\oo instead of \o
\ofoo instead of \obar
\o instead of \p
\oset instead of \pset

or even more amusingly

\o foo instead of \i foo -- check your keyboard layout

and so on.

If you want to guard against typos, you need to remove the \o command in its
current form. Prohibiting the addition of new commands, by whichever means,
is not going to help. And we have never had a policy or real analysis
whether new or existing commands are typo-proof.

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-19-2008, 06:27 AM
Gregory Stark
 
Posts: n/a
Default Re: psql command aliases support

"Peter Eisentraut" <peter_e@gmx.net> writes:

> This is a valid concern, but it is orthogonal to the alias feature. You have
> the same problem already if you mistype
>
> \oo instead of \o
> \ofoo instead of \obar


Not really. In these cases you know what \o is going to do, you've just typo'd
the filename. The case I was saying was a "conflict" was because you were
using an entirely different feature and might not never have even met \o.

> \o instead of \p
> \oset instead of \pset


Sure, if you typo \o instead of select * from pg_class you might be surprised
too. You can't protect against typing an entirely different command than
intended. At least you can then go look up what \o does and figure out what
happened.

If you type \ofoo instead of \obar I think there's a big difference between
having it accidentally do what you wanted it to do but to the wrong file and
having it do something entirely unrelated to what you intended and end up
overwriting a file instead of run a simple select.

> or even more amusingly
>
> \o foo instead of \i foo -- check your keyboard layout


The point is here you've typed a different command entirely. Unsurprisingly
it's going to do something different.

\old means something *today*. In the proposed syntax by creating the alias
you're changing what it means. You're not changing other \ofoo arguments
though which is where the possibility for confusion arises.

Consider instead if Debian decided to include a convenient \deb alias for a
select query against the package repository. Now if I happen to have an "eb"
table I will be surprised when \deb doesn't work.

And lengthening the alias doesn't really help (aside from defeating the
purpose of having aliases). If they define \debian they would still be
interfering if I should have a table named "ebian".

As rule of thumb, I think if you try to execute an alias which doesn't exist,
you should get an "alias does not exist" command. You should not get an
"Invalid command" nor "Did not find relation" and certainly not some random
command you've never met before being run reading or overwriting random files.


I repeat my suggestion of having a \query command so you could do:

\setquery deb select * from debian_packages where packagename like :1
\setquery bbdb select * from bbdb where name like :1

\query deb postgres
\query bbdb peter

to run a saved query. I'm not attached to "setquery" though.

--
Gregory Stark
EnterpriseDB http://www.enterprisedb.com
Ask me about EnterpriseDB's RemoteDBA services!

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-19-2008, 06:27 AM
Peter Eisentraut
 
Posts: n/a
Default Re: psql command aliases support

Am Donnerstag, 3. April 2008 schrieb Gregory Stark:
> \old means something *today*. In the proposed syntax by creating the alias
> you're changing what it means.


Yes. There are two complementary responses to that:

1. That's an intentional, useful feature of aliases.
2. If you don't like it, don't use it.

By your line of argument, we couldn't ever add any new commands in psql
anyway.

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:28 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585