pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
ALTER TABLE ... SET DEFAULT
8+ messages / 2 participants
[nested] [flat]

* ALTER TABLE ... SET DEFAULT
@ 2002-04-12 07:02 Brian McCane <bmccane@mccons.net>
  2002-04-12 07:21 ` Re: ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
  2002-04-12 14:00 ` Re: ALTER TABLE ... SET DEFAULT Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 8+ messages in thread

From: Brian McCane @ 2002-04-12 07:02 UTC (permalink / raw)
  To: pgsql-admin



I have a field called 'updated' of type 'integer' in a table.  I have
created a function called 'since_epoch' which is declared with iscacheable
and returns the value of 'extract(epoch from now())'.  I want to make
'updated' have a default of 'since_epoch()'.  I cannot get it to work with
the following command:

alter table foo alter column updated set default 'since_epoch()' ;

this gets the error:

ERROR:  pg_atoi: error in "since_epoch()": can't parse "since_epoch()"

I tried using 'since_epoch()::integer', but that failed as well.
Is it possible to make the default value of an integer field use a
function similar to how we use 'now()' for date and timestamp fields?

- brian

Wm. Brian McCane                    | Life is full of doors that won't open
Search http://recall.maxbaud.net/   | when you knock, equally spaced amid those
Usenet http://freenews.maxbaud.net/ | that open when you don't want them to.
Auction http://www.sellit-here.com/ | - Roger Zelazny "Blood of Amber"




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ALTER TABLE ... SET DEFAULT
  2002-04-12 07:02 ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
@ 2002-04-12 07:21 ` Brian McCane <bmccane@mccons.net>
  1 sibling, 0 replies; 8+ messages in thread

From: Brian McCane @ 2002-04-12 07:21 UTC (permalink / raw)
  To: pgsql-admin


Okay, I can't wait until morning for an answer, so I made one up ;-).  I
already have a function on this particular table which does processing
'BEFORE INSERT'.  As a temporary hack (or maybe permanent depending on
anyones response), I have added code to see if 'updated' IS NULL and
setting it to the return value of 'since_epoch()' if it is.  I suspect
that since I am already calling this function, it might actually be faster
than adding the 'set default' to the table anyway, especially since the
function 'iscacheable' and is being used in a transaction block.

-brian

On Fri, 12 Apr 2002, Brian McCane wrote:

>
>
>
> I have a field called 'updated' of type 'integer' in a table.  I have
> created a function called 'since_epoch' which is declared with iscacheable
> and returns the value of 'extract(epoch from now())'.  I want to make
> 'updated' have a default of 'since_epoch()'.  I cannot get it to work with
> the following command:
>
> alter table foo alter column updated set default 'since_epoch()' ;
>
> this gets the error:
>
> ERROR:  pg_atoi: error in "since_epoch()": can't parse "since_epoch()"
>
> I tried using 'since_epoch()::integer', but that failed as well.
> Is it possible to make the default value of an integer field use a
> function similar to how we use 'now()' for date and timestamp fields?
>
> - brian
>
> Wm. Brian McCane                    | Life is full of doors that won't open
> Search http://recall.maxbaud.net/   | when you knock, equally spaced amid those
> Usenet http://freenews.maxbaud.net/ | that open when you don't want them to.
> Auction http://www.sellit-here.com/ | - Roger Zelazny "Blood of Amber"
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ALTER TABLE ... SET DEFAULT
  2002-04-12 07:02 ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
@ 2002-04-12 14:00 ` Tom Lane <tgl@sss.pgh.pa.us>
  2002-04-13 00:19   ` Re: ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
  1 sibling, 1 reply; 8+ messages in thread

From: Tom Lane @ 2002-04-12 14:00 UTC (permalink / raw)
  To: Brian McCane <bmccane@mccons.net>; +Cc: pgsql-admin

Brian McCane <bmccane@mccons.net> writes:
> alter table foo alter column updated set default 'since_epoch()' ;
> this gets the error:
> ERROR:  pg_atoi: error in "since_epoch()": can't parse "since_epoch()"

Try it without the quotes.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ALTER TABLE ... SET DEFAULT
  2002-04-12 07:02 ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
  2002-04-12 14:00 ` Re: ALTER TABLE ... SET DEFAULT Tom Lane <tgl@sss.pgh.pa.us>
@ 2002-04-13 00:19   ` Brian McCane <bmccane@mccons.net>
  2002-04-13 00:30     ` Re: ALTER TABLE ... SET DEFAULT Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 8+ messages in thread

From: Brian McCane @ 2002-04-13 00:19 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-admin


Been there, done that :).  It put the current epoch value in as the
default value for updated.

- brian

On Fri, 12 Apr 2002, Tom Lane wrote:

>
> Brian McCane <bmccane@mccons.net> writes:
> > alter table foo alter column updated set default 'since_epoch()' ;
> > this gets the error:
> > ERROR:  pg_atoi: error in "since_epoch()": can't parse "since_epoch()"
>
> Try it without the quotes.
>
> 			regards, tom lane
>

Wm. Brian McCane                    | Life is full of doors that won't open
Search http://recall.maxbaud.net/   | when you knock, equally spaced amid those
Usenet http://freenews.maxbaud.net/ | that open when you don't want them to.
Auction http://www.sellit-here.com/ | - Roger Zelazny "Blood of Amber"




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ALTER TABLE ... SET DEFAULT
  2002-04-12 07:02 ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
  2002-04-12 14:00 ` Re: ALTER TABLE ... SET DEFAULT Tom Lane <tgl@sss.pgh.pa.us>
  2002-04-13 00:19   ` Re: ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
@ 2002-04-13 00:30     ` Tom Lane <tgl@sss.pgh.pa.us>
  2002-04-13 00:43       ` Re: ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
  0 siblings, 1 reply; 8+ messages in thread

From: Tom Lane @ 2002-04-13 00:30 UTC (permalink / raw)
  To: Brian McCane <bmccane@mccons.net>; +Cc: pgsql-admin

Brian McCane <bmccane@mccons.net> writes:
> Been there, done that :).  It put the current epoch value in as the
> default value for updated.

That would suggest that you marked since_epoch() as cachable.  Wrong
thing to do, if you want it evaluated again every time the default
is used.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ALTER TABLE ... SET DEFAULT
  2002-04-12 07:02 ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
  2002-04-12 14:00 ` Re: ALTER TABLE ... SET DEFAULT Tom Lane <tgl@sss.pgh.pa.us>
  2002-04-13 00:19   ` Re: ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
  2002-04-13 00:30     ` Re: ALTER TABLE ... SET DEFAULT Tom Lane <tgl@sss.pgh.pa.us>
@ 2002-04-13 00:43       ` Brian McCane <bmccane@mccons.net>
  2002-04-13 02:53         ` Re: ALTER TABLE ... SET DEFAULT Tom Lane <tgl@sss.pgh.pa.us>
  2002-04-13 03:56         ` Re: ALTER TABLE ... SET DEFAULT Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 8+ messages in thread

From: Brian McCane @ 2002-04-13 00:43 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-admin


Okay, so if I create a function as 'iscachable', it assumes that the value
will never change, calls the function and places the value in as the
default?  This doesn't make sense to me since the 'iscacheable' only works
inside of a transaction block.  If I call my function a hundred times, I
get a hundred different answers.  Once I do a 'BEGIN', the result never
changes again until I 'COMMIT', which is what I would expect according to
the docs I have found.

- brian



On Fri, 12 Apr 2002, Tom Lane wrote:

>
> Brian McCane <bmccane@mccons.net> writes:
> > Been there, done that :).  It put the current epoch value in as the
> > default value for updated.
>
> That would suggest that you marked since_epoch() as cachable.  Wrong
> thing to do, if you want it evaluated again every time the default
> is used.
>
> 			regards, tom lane
>

Wm. Brian McCane                    | Life is full of doors that won't open
Search http://recall.maxbaud.net/   | when you knock, equally spaced amid those
Usenet http://freenews.maxbaud.net/ | that open when you don't want them to.
Auction http://www.sellit-here.com/ | - Roger Zelazny "Blood of Amber"




^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ALTER TABLE ... SET DEFAULT
  2002-04-12 07:02 ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
  2002-04-12 14:00 ` Re: ALTER TABLE ... SET DEFAULT Tom Lane <tgl@sss.pgh.pa.us>
  2002-04-13 00:19   ` Re: ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
  2002-04-13 00:30     ` Re: ALTER TABLE ... SET DEFAULT Tom Lane <tgl@sss.pgh.pa.us>
  2002-04-13 00:43       ` Re: ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
@ 2002-04-13 02:53         ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 8+ messages in thread

From: Tom Lane @ 2002-04-13 02:53 UTC (permalink / raw)
  To: Brian McCane <bmccane@mccons.net>; +Cc: pgsql-admin

Brian McCane <bmccane@mccons.net> writes:
> This doesn't make sense to me since the 'iscacheable' only works
> inside of a transaction block.

Nope, wrong definition.

This has been refined for 7.3, but in existing releases "iscachable"
really means "result never changes, *ever*".

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 8+ messages in thread

* Re: ALTER TABLE ... SET DEFAULT
  2002-04-12 07:02 ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
  2002-04-12 14:00 ` Re: ALTER TABLE ... SET DEFAULT Tom Lane <tgl@sss.pgh.pa.us>
  2002-04-13 00:19   ` Re: ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
  2002-04-13 00:30     ` Re: ALTER TABLE ... SET DEFAULT Tom Lane <tgl@sss.pgh.pa.us>
  2002-04-13 00:43       ` Re: ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
@ 2002-04-13 03:56         ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 8+ messages in thread

From: Tom Lane @ 2002-04-13 03:56 UTC (permalink / raw)
  To: Brian McCane <bmccane@mccons.net>; +Cc: pgsql-admin

Brian McCane <bmccane@mccons.net> writes:
> Okay, so if I create a function as 'iscachable', it assumes that the value
> will never change, calls the function and places the value in as the
> default?  This doesn't make sense to me since the 'iscacheable' only works
> inside of a transaction block.

Um ... I already told you, that is *not* the meaning of 'isCachable'.

7.3 will split this term into 'isImmutable' and 'isStable', where the
latter means approximately what you seem to expect.  But the practical
behavior of existing releases is that 'isCachable' means 'fixed forever
given fixed input arguments'.  If you don't like it, argue with the
code.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 8+ messages in thread


end of thread, other threads:[~2002-04-13 03:56 UTC | newest]

Thread overview: 8+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-04-12 07:02 ALTER TABLE ... SET DEFAULT Brian McCane <bmccane@mccons.net>
2002-04-12 07:21 ` Brian McCane <bmccane@mccons.net>
2002-04-12 14:00 ` Tom Lane <tgl@sss.pgh.pa.us>
2002-04-13 00:19   ` Brian McCane <bmccane@mccons.net>
2002-04-13 00:30     ` Tom Lane <tgl@sss.pgh.pa.us>
2002-04-13 00:43       ` Brian McCane <bmccane@mccons.net>
2002-04-13 02:53         ` Tom Lane <tgl@sss.pgh.pa.us>
2002-04-13 03:56         ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox