Bit string help, please

Поиск
Список
Период
Сортировка
Искать

Bit string help, please

От:
tyrrill_ed@emc.com
Дата:
Hey PostgreSQL Gurus,

I am experimenting with PostgreSQL bit strings to see if they might help
with some performance issues I am having.  I added a "bit varying"
column to one of my tables.  I have a PL/pgSQL function with an insert
statement into this table.  For the bit varying column I would like to
insert a bit string of length n with the highest order bit being 1 and
all the other bits being 0.  Given a table definition:

create table table1 (  a int,  b bit varying
);

I hoped I could use the feature where casting right pads 0s to do
something like this:

insert into table1 values( DEFAULT, B'1'::bit( n ) );

Where n is one of the parameters to the PL/pgSQL function, but that
doesn't work.  PostgreSQL doesn't like having a variable for the bit
string length.  Does anyone have any ideas how I could achieve this?

Thanks,
Ed Tyrrill

Re: Bit string help, please

От:
Richard Huxton <dev@archonet.com>
Дата:
tyrrill_ed@emc.com wrote:
> 
> insert into table1 values( DEFAULT, B'1'::bit( n ) );
> 
> Where n is one of the parameters to the PL/pgSQL function, but that
> doesn't work.  PostgreSQL doesn't like having a variable for the bit
> string length.  Does anyone have any ideas how I could achieve this?

Try casting from a string:  SELECT ('1' || repeat('0', n-1))::bit varying;


--   Richard Huxton  Archonet Ltd

Re: Bit string help, please

От:
tyrrill_ed@emc.com
Дата:
I tried this real quick at the psql command prompt, and unfortunately it
doesn't work:

mydb=# select ('1' || repeat('0',7))::bit varying;
ERROR:  cannot cast type text to bit varying

I appreciate the try though.  Any other ideas?  I am using PostgreSQL
8.2.3.1.  I don't know if that matters too much.

Thanks,
Ed

-----Original Message-----
From: Richard Huxton [mailto:dev@archonet.com] 
Sent: Thursday, March 06, 2008 12:14 AM
To: Tyrrill, Ed
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Bit string help, please

tyrrill_ed@emc.com wrote:
> 
> insert into table1 values( DEFAULT, B'1'::bit( n ) );
> 
> Where n is one of the parameters to the PL/pgSQL function, but that
> doesn't work.  PostgreSQL doesn't like having a variable for the bit
> string length.  Does anyone have any ideas how I could achieve this?

Try casting from a string:  SELECT ('1' || repeat('0', n-1))::bit varying;


--   Richard Huxton  Archonet Ltd


Re: Bit string help, please

От:
Richard Huxton <dev@archonet.com>
Дата:
tyrrill_ed@emc.com wrote:
> I tried this real quick at the psql command prompt, and unfortunately it
> doesn't work:
> 
> mydb=# select ('1' || repeat('0',7))::bit varying;
> ERROR:  cannot cast type text to bit varying
> 
> I appreciate the try though.  Any other ideas?  I am using PostgreSQL
> 8.2.3.1.  I don't know if that matters too much.

Ah, sorry - that was an 8.3-only cast.

I think your only real option will be to build your query as a string 
and use EXECUTE to get the bit-varying you want in your function.

--   Richard Huxton  Archonet Ltd

Re: Bit string help, please

От:
tyrrill_ed@emc.com
Дата:
Thanks for the help again, Richard.  I will look into upgrading to 8.3
as I think that will be the best solution.

Ed Tyrrill

-----Original Message-----
From: Richard Huxton [mailto:dev@archonet.com] 
Sent: Thursday, March 06, 2008 10:36 AM
To: Tyrrill, Ed
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Bit string help, please

tyrrill_ed@emc.com wrote:
> I tried this real quick at the psql command prompt, and unfortunately
it
> doesn't work:
> 
> mydb=# select ('1' || repeat('0',7))::bit varying;
> ERROR:  cannot cast type text to bit varying
> 
> I appreciate the try though.  Any other ideas?  I am using PostgreSQL
> 8.2.3.1.  I don't know if that matters too much.

Ah, sorry - that was an 8.3-only cast.

I think your only real option will be to build your query as a string 
and use EXECUTE to get the bit-varying you want in your function.

--   Richard Huxton  Archonet Ltd


Re: Bit string help, please

От:
"Tena Sakai" <tsakai@gallo.ucsf.edu>
Дата:

Hi Ed,

I tried exactly what you did and it works for me.
My postgres is 8.3.0 running on redhat advanced
server.  This is what it told me:

prompt=# SELECT ('1' || repeat('0', 7))::bit varying;
  varbit 
----------
 10000000
(1 row)

prompt=#

Regards,

Tena Sakai
tsakai@gallo.ucsf.edu



-----Original Message-----
From: pgsql-sql-owner@postgresql.org on behalf of tyrrill_ed@emc.com
Sent: Thu 3/6/2008 10:24 AM
To: dev@archonet.com
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Bit string help, please

I tried this real quick at the psql command prompt, and unfortunately it
doesn't work:

mydb=# select ('1' || repeat('0',7))::bit varying;
ERROR:  cannot cast type text to bit varying

I appreciate the try though.  Any other ideas?  I am using PostgreSQL
8.2.3.1.  I don't know if that matters too much.

Thanks,
Ed


-----Original Message-----
From: Richard Huxton [mailto:dev@archonet.com]
Sent: Thursday, March 06, 2008 12:14 AM
To: Tyrrill, Ed
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] Bit string help, please

tyrrill_ed@emc.com wrote:
>
> insert into table1 values( DEFAULT, B'1'::bit( n ) );
>
> Where n is one of the parameters to the PL/pgSQL function, but that
> doesn't work.  PostgreSQL doesn't like having a variable for the bit
> string length.  Does anyone have any ideas how I could achieve this?

Try casting from a string:
   SELECT ('1' || repeat('0', n-1))::bit varying;


--
   Richard Huxton
   Archonet Ltd


--
Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
To make changes to your subscription:
http://mail.postgresql.org/mj/mj_wwwusr?domain=postgresql.org&extra=pgsql-sql

Re: Bit string help, please

От:
Richard Huxton <dev@archonet.com>
Дата:
tyrrill_ed@emc.com wrote:
> Thanks for the help again, Richard.  I will look into upgrading to 8.3
> as I think that will be the best solution.

I don't think you'll regret it. There are some excellent improvements in 8.3

--   Richard Huxton  Archonet Ltd

FAQ