datistemplate of pg_database does not behave as per description in documentation

Lists: pgsql-hackers
From: Rajeev rastogi <rajeev(dot)rastogi(at)huawei(dot)com>
To: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: datistemplate of pg_database does not behave as per description in documentation
Date: 2014-03-27 08:12:38
Message-ID: BF2827DCCE55594C8D7A8F7FFD3AB7713DDDD1A7@SZXEML508-MBX.china.huawei.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

As per the documentation, datistemplate of pg_database is used in following way:

datistemplate

Bool

If true then this database can be used in the TEMPLATE clause of CREATE DATABASE to create a new database as a clone of this one

But current code does not behave in this manner. Even if dbistemplate of database is false, still it allows to be used as template database.

postgres=# select datname, datistemplate from pg_database;
datname | datistemplate
-----------+---------------
template1 | t
template0 | t
postgres | f
(3 rows)

postgres=# create database tempdb template postgres; ---Actually this should fail.
CREATE DATABASE

Though I am not sure if we have to modify source code to align the behavior with documentation or we need to change the documentation itself.
To me it looks like code change will be better, so I am attaching the current patch with source code change. After modification, result will be as follows:

postgres=# create database newtempdb template postgres;
ERROR: DB name "postgres" given as template is not a template database

Please provide your feedback.

Thanks and Regards,
Kumar Rajeev Rastogi

Attachment Content-Type Size
datistemplate_issue.patch application/octet-stream 764 bytes

From: Magnus Hagander <magnus(at)hagander(dot)net>
To: Rajeev rastogi <rajeev(dot)rastogi(at)huawei(dot)com>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: datistemplate of pg_database does not behave as per description in documentation
Date: 2014-03-27 08:38:32
Message-ID: CABUevEwOHzwSPnzXKk5dy3hQP9GOsd=b7s9cK1VwewywEYqBtA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Thu, Mar 27, 2014 at 9:12 AM, Rajeev rastogi
<rajeev(dot)rastogi(at)huawei(dot)com>wrote:

> As per the documentation, datistemplate of pg_database is used in
> following way:
>
>
>
> datistemplate
>
> Bool
>
> If true then this database can be used in the TEMPLATE clause of CREATE
> DATABASE to create a new database as a clone of this one
>
>
>
> But current code does not behave in this manner. Even if dbistemplate of
> database is false, still it allows to be used as template database.
>
>
>
> postgres=# select datname, datistemplate from pg_database;
>
> datname | datistemplate
>
> -----------+---------------
>
> template1 | t
>
> template0 | t
>
> *postgres | f*
>
> (3 rows)
>
>
>
> *postgres=# create database tempdb template postgres;
> ---Actually this should fail.*
>
> *CREATE DATABASE*
>
>
>
> Though I am not sure if we have to modify source code to align the
> behavior with documentation or we need to change the documentation itself.
>
> To me it looks like code change will be better, so I am attaching the
> current patch with source code change. After modification, result will be
> as follows:
>
>
>
> *postgres=# create database newtempdb template postgres;*
>
> *ERROR: DB name "postgres" given as template is not a template database*
>
>
>
> Please provide your feedback.
>
>
>
AFAICT, the *only* thing datistemplate is used is to set parameters in
autovacuum.

So clearly we should do something. Changing the code that way carries the
risk of breaking applications (or at least DBA scripts) for no apparent
reason. I think it's better to document it.

However, that also raises a third option. We could just drop the idea if
datistemplate completely, and remove the column. Since clearly it's not
actually doing anything, and people seem to have been happy with that for a
while, why do we need it in the first place?

--
Magnus Hagander
Me: http://www.hagander.net/
Work: http://www.redpill-linpro.com/


From: Stephen Frost <sfrost(at)snowman(dot)net>
To: Magnus Hagander <magnus(at)hagander(dot)net>
Cc: Rajeev rastogi <rajeev(dot)rastogi(at)huawei(dot)com>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: datistemplate of pg_database does not behave as per description in documentation
Date: 2014-03-27 11:24:35
Message-ID: 20140327112435.GG4582@tamriel.snowman.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

* Magnus Hagander (magnus(at)hagander(dot)net) wrote:
> However, that also raises a third option. We could just drop the idea if
> datistemplate completely, and remove the column. Since clearly it's not
> actually doing anything, and people seem to have been happy with that for a
> while, why do we need it in the first place?

It's a bit of extra meta-data which can be nice to have (I know we use
that field for our own purposes..). On the flip side, I've never liked
that you have to update pg_database to change it, so perhaps we should
get rid of it and remove that temptation.

The other field we regularly update in pg_database is datallowconn...
Would love to see that as an actual ALTER DATABASE command instead...

Thanks,

Stephen


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Magnus Hagander <magnus(at)hagander(dot)net>
Cc: Rajeev rastogi <rajeev(dot)rastogi(at)huawei(dot)com>, "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: datistemplate of pg_database does not behave as per description in documentation
Date: 2014-03-27 11:45:52
Message-ID: 31029.1395920752@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Magnus Hagander <magnus(at)hagander(dot)net> writes:
> On Thu, Mar 27, 2014 at 9:12 AM, Rajeev rastogi
> <rajeev(dot)rastogi(at)huawei(dot)com>wrote:
>> But current code does not behave in this manner. Even if dbistemplate of
>> database is false, still it allows to be used as template database.

> AFAICT, the *only* thing datistemplate is used is to set parameters in
> autovacuum.

Huh? The code comment is perfectly clear:

/*
* Permission check: to copy a DB that's not marked datistemplate, you
* must be superuser or the owner thereof.
*/
if (!src_istemplate)
{
if (!pg_database_ownercheck(src_dboid, GetUserId()))
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("permission denied to copy database \"%s\"",
dbtemplate)));
}

I agree we need to make the docs match the code, but changing behavior
that's been like that for ten or fifteen years isn't the answer.

(Changing code and failing to adjust the adjacent comment is even less
of an answer.)

regards, tom lane


From: Magnus Hagander <magnus(at)hagander(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Rajeev rastogi <rajeev(dot)rastogi(at)huawei(dot)com>
Subject: Re: datistemplate of pg_database does not behave as per description in documentation
Date: 2014-03-27 12:07:54
Message-ID: CABUevEwfrJ6v9Ju9mYLzDF3v9-d8=w3xbLWt=rtFma3ziy8f=w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mar 27, 2014 12:45 PM, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> Magnus Hagander <magnus(at)hagander(dot)net> writes:
> > On Thu, Mar 27, 2014 at 9:12 AM, Rajeev rastogi
> > <rajeev(dot)rastogi(at)huawei(dot)com>wrote:
> >> But current code does not behave in this manner. Even if dbistemplate
of
> >> database is false, still it allows to be used as template database.
>
> > AFAICT, the *only* thing datistemplate is used is to set parameters in
> > autovacuum.
>
> Huh? The code comment is perfectly clear:
>
> /*
> * Permission check: to copy a DB that's not marked datistemplate, you
> * must be superuser or the owner thereof.
> */
> if (!src_istemplate)
> {
> if (!pg_database_ownercheck(src_dboid, GetUserId()))
> ereport(ERROR,
> (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
> errmsg("permission denied to copy database \"%s\"",
> dbtemplate)));
> }
>
> I agree we need to make the docs match the code, but changing behavior
> that's been like that for ten or fifteen years isn't the answer.

Hah, I hadn't done more than git grep for datistemplate :-) that's what I
get for taking shortcuts...

But yes, fixing the documentation is what I advocate as well.

> (Changing code and failing to adjust the adjacent comment is even less
> of an answer.)

I didn't even look at the suggested patch..

/Magnus


From: Rajeev rastogi <rajeev(dot)rastogi(at)huawei(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>, "Magnus Hagander" <magnus(at)hagander(dot)net>
Subject: Re: datistemplate of pg_database does not behave as per description in documentation
Date: 2014-04-01 11:06:43
Message-ID: BF2827DCCE55594C8D7A8F7FFD3AB7713DDDD9F9@SZXEML508-MBX.china.huawei.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 27 March 2014 17:16, Tom Lane Wrote:

> I agree we need to make the docs match the code, but changing behavior
> that's been like that for ten or fifteen years isn't the answer.

Sounds good.

Please find the attached patch with only documentation change.

Thanks and Regards,
Kumar Rajeev Rastogi

Attachment Content-Type Size
datistemplate_issueV2.patch application/octet-stream 755 bytes

From: "MauMau" <maumau307(at)gmail(dot)com>
To: "Rajeev rastogi" <rajeev(dot)rastogi(at)huawei(dot)com>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: <pgsql-hackers(at)postgresql(dot)org>, "Magnus Hagander" <magnus(at)hagander(dot)net>
Subject: Re: datistemplate of pg_database does not behave as per description in documentation
Date: 2014-06-18 10:47:12
Message-ID: 4EE289C2D335454292E2221756472494@maumau
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

From: "Rajeev rastogi" <rajeev(dot)rastogi(at)huawei(dot)com>
> Please find the attached patch with only documentation change.

I marked this as ready for committer. The patch is good because it matches
the description in the following page:

http://www.postgresql.org/docs/devel/static/manage-ag-templatedbs.html

Regards
MauMau


From: Fujii Masao <masao(dot)fujii(at)gmail(dot)com>
To: MauMau <maumau307(at)gmail(dot)com>
Cc: Rajeev rastogi <rajeev(dot)rastogi(at)huawei(dot)com>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Magnus Hagander <magnus(at)hagander(dot)net>
Subject: Re: datistemplate of pg_database does not behave as per description in documentation
Date: 2014-06-18 12:31:26
Message-ID: CAHGQGwG+Yvo-cYsCMsvMkoEpD-utOQEGG86vg-5ddoS-a8E8Aw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Wed, Jun 18, 2014 at 7:47 PM, MauMau <maumau307(at)gmail(dot)com> wrote:
> From: "Rajeev rastogi" <rajeev(dot)rastogi(at)huawei(dot)com>
>
>> Please find the attached patch with only documentation change.
>
>
> I marked this as ready for committer. The patch is good because it matches
> the description in the following page:
>
> http://www.postgresql.org/docs/devel/static/manage-ag-templatedbs.html

ISTM that this patch has already been committed by Bruce. Please see
the commit 72590b3a69baaf24d1090a2c2ceb9181be34043e

Regards,

--
Fujii Masao


From: "MauMau" <maumau307(at)gmail(dot)com>
To: "Fujii Masao" <masao(dot)fujii(at)gmail(dot)com>
Cc: "Rajeev rastogi" <rajeev(dot)rastogi(at)huawei(dot)com>, "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "PostgreSQL-development" <pgsql-hackers(at)postgresql(dot)org>, "Magnus Hagander" <magnus(at)hagander(dot)net>
Subject: Re: datistemplate of pg_database does not behave as per description in documentation
Date: 2014-06-18 13:43:15
Message-ID: C822D6705EBA4519B6A1CC35596BC6D4@maumau
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

From: "Fujii Masao" <masao(dot)fujii(at)gmail(dot)com>
> On Wed, Jun 18, 2014 at 7:47 PM, MauMau <maumau307(at)gmail(dot)com> wrote:
>> I marked this as ready for committer. The patch is good because it
>> matches
>> the description in the following page:
>>
>> http://www.postgresql.org/docs/devel/static/manage-ag-templatedbs.html
>
> ISTM that this patch has already been committed by Bruce. Please see
> the commit 72590b3a69baaf24d1090a2c2ceb9181be34043e

Oh, the devel doc certainly reflects the change:

http://www.postgresql.org/docs/devel/static/catalog-pg-database.html

I marked this as committed.

I hope Magnus-san's enhancements to the CommitFest app will enable the
CommitFest entry to be automatically updated at git commit.

Regards
MauMau