Re: current version: Patch - Have psql show current values - Mailing list pgsql-patches

From Dhanaraj M
Subject Re: current version: Patch - Have psql show current values
Date
Msg-id 44607CC9.1020900@sun.com
Whole thread Raw
In response to Re: current version: Patch - Have psql show current values  (Bruce Momjian <pgman@candle.pha.pa.us>)
Responses Re: current version: Patch - Have psql show current values
List pgsql-patches
Bruce Momjian wrote:

>I am thinking we just add another column to the \d display for sequences
>showing the current value.
>
>---------------------------------------------------------------------------
>
>
>
As suggested in the previous mails, I tried to use the following to
display the seq. value.
select last_value from <seq>.

However, it was not possible to display the seq. value using this.
Hence, I made a small change in the currval() function, so that it
retrieves the last_value
even if the the value is not cached.

I hope this patch will be more suitable for this issue. Pl. look at the
patch.

Thanks
Dhanaraj

*** ./src/backend/commands/sequence.c.orig    Tue May  2 14:51:03 2006
--- ./src/backend/commands/sequence.c    Tue May  9 13:52:38 2006
***************
*** 605,610 ****
--- 605,612 ----
      int64        result;
      SeqTable    elm;
      Relation    seqrel;
+         Form_pg_sequence seq;
+         Buffer          buf;

      /* open and AccessShareLock sequence */
      init_sequence(relid, &elm, &seqrel);
***************
*** 616,632 ****
                   errmsg("permission denied for sequence %s",
                          RelationGetRelationName(seqrel))));

!     if (elm->increment == 0)    /* nextval/read_info were not called */
!         ereport(ERROR,
!                 (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
!                  errmsg("currval of sequence \"%s\" is not yet defined in this session",
!                         RelationGetRelationName(seqrel))));

!     result = elm->last;

!     relation_close(seqrel, NoLock);

!     PG_RETURN_INT64(result);
  }

  Datum
--- 618,641 ----
                   errmsg("permission denied for sequence %s",
                          RelationGetRelationName(seqrel))));

!         if ((elm->increment != 0) ||(elm->last != elm->cached)) /* some numbers were cached */
!         {
!                 result = elm->last;
!                 relation_close(seqrel, NoLock);
!                 PG_RETURN_INT64(result);
!         }

!         /* lock page' buffer and read tuple if not cached */
!         seq = read_info(elm, seqrel, &buf);
!         result = seq->last_value;

!     UnlockReleaseBuffer(buf);
!         relation_close(seqrel, NoLock);

!         seqtab = elm->next;
!         free(elm);
!
!         PG_RETURN_INT64(result);
  }

  Datum
*** ./src/bin/psql/describe.c.orig    Thu Apr 27 04:45:45 2006
--- ./src/bin/psql/describe.c    Tue May  9 16:26:10 2006
***************
*** 1480,1485 ****
--- 1480,1488 ----
                        _("table"), _("view"), _("index"), _("sequence"),
                        _("special"), _("Type"), _("Owner"));

+     if (showSeq && !showTables)
+         appendPQExpBuffer(&buf,",\n  currval(CAST(c.relname AS pg_catalog.text)) as \"%s\"",_("value"));
+
      if (showIndexes)
          appendPQExpBuffer(&buf,
                            ",\n c2.relname as \"%s\"",
No differences encountered

pgsql-patches by date:

Previous
From: "Peter Brant"
Date:
Subject: Re: pgstat: remove delayed destroy / pipe: socket
Next
From: Peter Eisentraut
Date:
Subject: Re: Cleaning up multiply-defined-symbol warnings on OS X