Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eVPHS-0000TT-4A for pgsql-general@arkaria.postgresql.org; Sat, 30 Dec 2017 22:07:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eVPHR-0001Lq-Gh for pgsql-general@arkaria.postgresql.org; Sat, 30 Dec 2017 22:07:01 +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 1eVPHR-0001Lh-6U for pgsql-general@lists.postgresql.org; Sat, 30 Dec 2017 22:07:01 +0000 Received: from tamriel.snowman.net ([2001:470:e38f::11]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eVPHK-00024Q-IM for pgsql-general@lists.postgresql.org; Sat, 30 Dec 2017 22:06:59 +0000 Received: by tamriel.snowman.net (Postfix, from userid 1000) id EA59D5F79C; Sat, 30 Dec 2017 17:06:52 -0500 (EST) Date: Sat, 30 Dec 2017 17:06:52 -0500 From: Stephen Frost To: Melvin Davidson Cc: Alban Hertroys , Jan Wieck , Edson Carlos Ericksson Richter , pgsql-general@lists.postgresql.org Subject: Re: Does PostgreSQL check database integrity at startup? Message-ID: <20171230220652.GK2416@tamriel.snowman.net> References: <42cda197-fead-a7e5-a7e2-750d98bc4eb2@simkorp.com.br> <743808611.5608368.1514484408822@mail.yahoo.com> <20171228182636.GG4628@tamriel.snowman.net> <48a4bcfa-6b85-81ca-9b59-9e47af8fa5af@simkorp.com.br> <20171230011315.GC2416@tamriel.snowman.net> <1B534695-9219-4555-B31E-92A73D390621@gmail.com> <20171230152716.GH2416@tamriel.snowman.net> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="NqNl6FRZtoRUn5bW" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --NqNl6FRZtoRUn5bW Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Greetings, * Melvin Davidson (melvin6925@gmail.com) wrote: > My query works as designed and has done so > for two years. It shows the filenames for schemas and tables in the > database. I'm glad to hear that it works in your specific use-case. Unfortunately, it doesn't work in the general case and therefore isn't a good example. A proper query to return the filename for each user table in the current database is: SELECT quote_ident(nsp.nspname) || '.' || quote_ident(c.relname), s.setting || '/base/' || db.oid || '/' || c.relfilenode FROM pg_settings s JOIN pg_database db on (s.name =3D 'data_directory') JOIN pg_class c on (datname =3D current_database()) JOIN pg_namespace nsp on (c.relnamespace =3D nsp.oid) WHERE relfilenode <> 0 AND nsp.nspname !~ '^pg_' AND nsp.nspname <> 'information_schema'; Note that, as discussed earlier in this thread, this doesn't actually answer what Edson was asking for. Here's the query that would answer his original request: SELECT quote_ident(nsp.nspname) || '.' || quote_ident(c.relname), s.setting || '/base/' || db.oid || '/' || c.relfilenode, (pg_stat_file(s.setting || '/base/' || db.oid || '/' || c.relfilenode)).s= ize as size FROM=20 pg_settings s=20 JOIN pg_database db on (s.name =3D 'data_directory')=20 JOIN pg_class c on (datname =3D current_database())=20 JOIN pg_namespace nsp on (c.relnamespace =3D nsp.oid)=20 WHERE relfilenode <> 0=20 AND nsp.nspname !~ '^pg_' AND nsp.nspname <> 'information_schema'; Technically speaking, while these queries are correct for PG10, in prior versions of PostgreSQL it's possible to have user schemas that begin with 'pg_' and therefore the filtering in the WHERE clause would have to be more specific. Note that both of these need to be run as a superuser in older versions of PG. In PostgreSQL 10, a user could be GRANT'd 'pg_read_all_settings' and be able to run the first query. We don't currently support being able to GRANT a non-superuser the ability to run pg_stat_file(), but that will likely be coming in PG 11. Thanks! Stephen --NqNl6FRZtoRUn5bW Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v1 iQIcBAEBCgAGBQJaSA38AAoJEO1sijiDR2RVPhIQALWpDRV9thcjid3EEC2bsIn1 ToBXhnUFV2TBgsWJnd7Zr93bXB9fzUgEqp9Jq0rwIJxTQ6jkQQm8yn/YjwNnsGyT xGi47iUZlIFYDJosFneS3Mb1msQyEl0nhIK0Q3UagKmoNQR8xjDgDkHunYi80nU0 GHOpEijcEZ66wd1nLCvh9g0d0jXZy9vFiga+PsFeEXg/PdfV/NEU3ktvMj5+rFsr 3v8f3Ml7PU5kv6H5antoTy5ndDQ9UEVKRWkMjRlcA//FhZWuoi2te8E3NGCneC7r U/Pmj/+QYidVAvMH01bKKRNATyDhvSB2Em7JI8//LIfxvcaBxPpYf9THS6B49ID8 jFDaw8wJrgXQ6PQC+8xOV+d9q56vx2Dzd8egO304qxowIaiRfgi4yf0Kk69rqZgd RqJ/FLG2tkTufjCSuhd3D1pkoEZJ1LitOEJY1Hfwj9btpCa16hHytznRV36cLSEa +xY4XkRssPV/WUabfY/7OgBmuXxNtNtEo9XpEcRdZuLsLtk1shk18IikHkPgGXlI 9yKiIOg4zOr930tD93+mhaeZCRGjEj7uAAkfZ+l+QySaxOy1r5+eRX1I5GJkQm6P UP7+AgxGZ1zib8ybWLh3fsfsFdK67s3h6oZN2rZEELyAKytrjNSwjhzVN112FVER HLpwlX+Qcqo7H7776BfO =Sq9D -----END PGP SIGNATURE----- --NqNl6FRZtoRUn5bW--