docker-library / postgres

Docker Official Image packaging for Postgres
http://www.postgresql.org
MIT License
2.16k stars 1.13k forks source link

Add `less` or replace `more` with `less` (at least for Debian) #1115

Closed dvaerum closed 8 months ago

dvaerum commented 1 year ago

I was wondering, hoping, that it would be possible to make it so that the Postgres container would use the less command instead of more as it is the default for Postgres (at least if I install the less command into a container, it will be used instead of more)

I assume that the reason for picking more instead of less has to do with the container size. If that is the case, I would if it is possible to just make the change for the Debian based container because I would assume that the ones which are concerned with the size of the container would be using the Alpine-based container anyway.

If there are some other reasons for using more over less I would like to know 😊

tianon commented 9 months ago

It's actually a simpler explanation than that -- we didn't explicitly pick one or the other (if you look at our Dockerfile, we install neither less nor more).

dvaerum commented 9 months ago

It's actually a simpler explanation than that -- we didn't explicitly pick one or the other (if you look at our Dockerfile, we install neither less nor more).

Okay, so some other packages must be pulling in the more package 🤔 would it be possible to get the less package to be part of the Postgres container?

dvv@IT-04027 ~> sudo docker run -it postgres bash
Unable to find image 'postgres:latest' locally
latest: Pulling from library/postgres
1f7ce2fa46ab: Pull complete 
8a0c088137b8: Pull complete 
11be68f68a2e: Pull complete 
19f13c4e1d96: Pull complete 
43187fdc5ebc: Pull complete 
a84cb0803492: Pull complete 
b50a897e2632: Pull complete 
7bc6d5552c52: Pull complete 
c8161286a3f1: Pull complete 
e36f0ab546af: Pull complete 
c2a71678092b: Pull complete 
7c23bdcac538: Pull complete 
16648961c661: Pull complete 
Digest: sha256:a2282ad0db623c27f03bab803975c9e3942a24e974f07142d5d69b6b8eaaf9e2
Status: Downloaded newer image for postgres:latest

root@c17d93b14848:/# more
more: bad usage
Try 'more --help' for more information.

root@c17d93b14848:/# which more
/usr/bin/more
tianon commented 9 months ago

If we can (with minimal effort/breakage) prevent more from being pulled in to begin with, that'd be my top preference.

Barring that, I would only consider less if the final image is smaller (and does not then still pull in more; ie, whatever's pulling it in has an alternative that less satisfies).

tianon commented 9 months ago

Ah, more is part of util-linux, and is thus "essential" and not something we can remove.

Edit: to better illustrate:

$ docker run -it --rm debian:bookworm-slim more --version
more from util-linux 2.38.1
tianon commented 9 months ago

Is there any core functionality of PostgreSQL itself that uses less and especially an explicit upstream-voiced preference for less (like documentation that says something like "we recommend less" or similar)?

dvaerum commented 8 months ago

Is there any core functionality of PostgreSQL itself that uses less and especially an explicit upstream-voiced preference for less (like documentation that says something like "we recommend less" or similar)?

Sorry, don't am really unsure about the question here

tianon commented 8 months ago

Apologies! Let me rephrase. Is there a PostgreSQL provided command or instruction which invokes less directly, or are you looking for less because you're doing something like psql --help | less?

tianon commented 8 months ago

I did some poking around in the upstream source code (which wasn't easy, given the large number of times the word less appears there :sob:), but did manage to find a few things:

$ git grep -nP "['\"|]"'[[:space:]]*less\b(?![[:space:]-]*(than|equal))'
...
src/include/fe_utils/print.h:25:#define DEFAULT_PAGER "less"

$ git grep -nC4 'DEFAULT_PAGER'
src/fe_utils/print.c-3111-                      pagerprog = getenv("PSQL_PAGER");
src/fe_utils/print.c-3112-                      if (!pagerprog)
src/fe_utils/print.c-3113-                              pagerprog = getenv("PAGER");
src/fe_utils/print.c-3114-                      if (!pagerprog)
src/fe_utils/print.c:3115:                              pagerprog = DEFAULT_PAGER;
src/fe_utils/print.c-3116-                      else
src/fe_utils/print.c-3117-                      {
src/fe_utils/print.c-3118-                              /* if PAGER is empty or all-white-space, don't use pager */
src/fe_utils/print.c-3119-                              if (strspn(pagerprog, " \t\r\n") == strlen(pagerprog))
--
src/include/fe_utils/print.h-19-
src/include/fe_utils/print.h-20-
src/include/fe_utils/print.h-21-/* This is not a particularly great place for this ... */
src/include/fe_utils/print.h-22-#ifndef __CYGWIN__
src/include/fe_utils/print.h:23:#define DEFAULT_PAGER "more"
src/include/fe_utils/print.h-24-#else
src/include/fe_utils/print.h:25:#define DEFAULT_PAGER "less"
src/include/fe_utils/print.h-26-#endif
src/include/fe_utils/print.h-27-
src/include/fe_utils/print.h-28-enum printFormat
src/include/fe_utils/print.h-29-{

When I run psql --help=variables, there's a pager variable that appears to be related, but I can't seem to trigger it doing anything interesting (with or without less installed).

dvaerum commented 8 months ago

Is there a PostgreSQL provided command or instruction which invokes less directly

Yes (ish maybe :sweat_smile: ) Postgres (psql) uses less as the pager when it is available (perhaps there are exceptions, but I have not encountered them) one can change this behavior by defining the environment variable PAGER and SQL_PAGER. The same goes for the PostgreSQL container, if less is installed it will be used as the pager when querying

Source for more info: https://www.postgresql.org/docs/current/app-psql.html#APP-PSQL-META-COMMAND-PSET-PAGER

Hope this makes it more clear, why I am interested in having less available. If the argument is size, maybe less could just be added to the Debian based containers

tianon commented 8 months ago

Looks like less would add ~1.5MiB to the total image size on disk -- compared to the over 400MiB for the Debian variants and it being used as pager by default if it's available, I think that's probably compelling enough. :sweat_smile:

dvaerum commented 8 months ago

@tianon thanks a lot 😁 🎉