Re: How to store fixed size images?

Lists: pgsql-general
From: sunpeng <bluevaley(at)gmail(dot)com>
To: PostgreSQL general <pgsql-general(at)postgresql(dot)org>
Subject: How to store fixed size images?
Date: 2014-06-17 01:10:57
Message-ID: CAOYKhLoy=VNitD35Gb0AuLNXpRVV8wSa7uPZ40Cz3CRmuKWFDg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

We have many small size(most fixed size) images, how to store them? There
are two options:
1. Store images in folders, managed by os file system, only store path in
postgresql
2. Store image as bytea in postgresql
How do you usually store images?
Thanks!

peng


From: Andreas Joseph Krogh <andreas(at)visena(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: How to store fixed size images?
Date: 2014-06-17 08:27:57
Message-ID: VisenaEmail.f7.6e526a63e1a0772d.146a8ef3eaa@tc7-on
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

På tirsdag 17. juni 2014 kl. 03:10:57, skrev sunpeng <bluevaley(at)gmail(dot)com
<mailto:bluevaley(at)gmail(dot)com>>: We have many small size(most fixed size) images,
how to store them? There are two options:1. Store images in folders, managed by
os file system, only store path in postgresql 2. Store image as bytea in
postgresql How do you usually store images? Thanks!   The images we store might
be quite large so we use OID (Large Objects) and use the java.sql.Blob API of
the pgjdbc-ng driver to avoid having to deal with large byte-arrays.   --
Andreas Jospeh Krogh CTO / Partner - Visena AS Mobile: +47 909 56 963
andreas(at)visena(dot)com <mailto:andreas(at)visena(dot)com> www.visena.com
<https://www.visena.com> <https://www.visena.com>  


From: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
To: sunpeng <bluevaley(at)gmail(dot)com>
Cc: PostgreSQL general <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to store fixed size images?
Date: 2014-06-17 16:15:49
Message-ID: CAMkU=1xT9prj_2Gh7deQ4mEoHMzETB_DV=hZrrSBRwnOEwauJA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Mon, Jun 16, 2014 at 6:10 PM, sunpeng <bluevaley(at)gmail(dot)com> wrote:
> We have many small size(most fixed size) images, how to store them? There
> are two options:
> 1. Store images in folders, managed by os file system, only store path in
> postgresql
> 2. Store image as bytea in postgresql
> How do you usually store images?

I use method 1, because the library/modules I made use of only
implemented that method. I'd prefer to use method 2, but not enough
to write the code for doing it when there was existing code. The
problem with 1 is now you have two streams of data to back up, and the
data itself is no longer transactional with its metadata. A potential
problem with 2 is that it will run into problems if any of the data is
more than a small fraction of RAM. So the images must be "always
small". If they are just "usually small", that isn't good enough.
Another problem with bytea is the encoding issues. Good up-to-date
drivers will handle that for you (mostly) transparently, but there are
lots of drivers that are not good, or not up-to-date.

Cheers,

Jeff


From: sunpeng <bluevaley(at)gmail(dot)com>
To: Jeff Janes <jeff(dot)janes(at)gmail(dot)com>
Cc: PostgreSQL general <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to store fixed size images?
Date: 2014-06-19 14:41:58
Message-ID: CAOYKhLo0+eeakp37g7mjjokN3qXo0jB9QLTVKcQFh+CLqVKv_Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Thank you, Jeff!
peng

On Wed, Jun 18, 2014 at 12:15 AM, Jeff Janes <jeff(dot)janes(at)gmail(dot)com> wrote:

> On Mon, Jun 16, 2014 at 6:10 PM, sunpeng <bluevaley(at)gmail(dot)com> wrote:
> > We have many small size(most fixed size) images, how to store them? There
> > are two options:
> > 1. Store images in folders, managed by os file system, only store path in
> > postgresql
> > 2. Store image as bytea in postgresql
> > How do you usually store images?
>
> I use method 1, because the library/modules I made use of only
> implemented that method. I'd prefer to use method 2, but not enough
> to write the code for doing it when there was existing code. The
> problem with 1 is now you have two streams of data to back up, and the
> data itself is no longer transactional with its metadata. A potential
> problem with 2 is that it will run into problems if any of the data is
> more than a small fraction of RAM. So the images must be "always
> small". If they are just "usually small", that isn't good enough.
> Another problem with bytea is the encoding issues. Good up-to-date
> drivers will handle that for you (mostly) transparently, but there are
> lots of drivers that are not good, or not up-to-date.
>
> Cheers,
>
> Jeff
>


From: Andy Colson <andy(at)squeakycode(dot)net>
To: sunpeng <bluevaley(at)gmail(dot)com>, PostgreSQL general <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to store fixed size images?
Date: 2014-06-19 15:21:56
Message-ID: 53A30014.5070708@squeakycode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 06/16/2014 08:10 PM, sunpeng wrote:
> We have many small size(most fixed size) images, how to store them? There are two options:
> 1. Store images in folders, managed by os file system, only store path in postgresql
> 2. Store image as bytea in postgresql
> How do you usually store images?
> Thanks!
>
> peng

I think it depends on how you are going to use them. I, for example, have lots of images that are served on a web page, after benchmarks I found it was faster to store them on filesystem and let apache serve them directly.

-Andy


From: Alan Hodgson <ahodgson(at)simkin(dot)ca>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: How to store fixed size images?
Date: 2014-06-19 16:16:40
Message-ID: 1587340.KArM26OA1M@skynet.simkin.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Thursday, June 19, 2014 10:21:56 AM Andy Colson wrote:
> On 06/16/2014 08:10 PM, sunpeng wrote:
> > We have many small size(most fixed size) images, how to store them? There
> > are two options: 1. Store images in folders, managed by os file system,
> > only store path in postgresql 2. Store image as bytea in postgresql
> > How do you usually store images?
> > Thanks!
> >
> > peng
>
> I think it depends on how you are going to use them. I, for example, have
> lots of images that are served on a web page, after benchmarks I found it
> was faster to store them on filesystem and let apache serve them directly.
>
> -Andy

That will always be the (much) faster option. There's basically no CPU
overhead, the web server can tell the kernel to copy the image from the
filesystem cache directly into a network buffer, and off it goes. Even apache can
serve line speed like that.

It's a lot easier to manage the images if they're in the database, though,
especially if you run off multiple web servers. If CPU overhead is actually an
issue, you can eliminate most of the speed hit by sticking a caching proxy
server like Varnish in front of your site, or by offloading the image serving to
a pass-through CDN. Just make sure images get a new URL path if they change
content.


From: Jonathan Vanasco <postgres(at)2xlp(dot)com>
To: PostgreSQL general <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to store fixed size images?
Date: 2014-06-20 19:10:48
Message-ID: 41829835-523B-478C-9BBB-E5CDA9ABACE1@2xlp.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


On Jun 19, 2014, at 11:21 AM, Andy Colson wrote:

> I think it depends on how you are going to use them. I, for example, have lots of images that are served on a web page, after benchmarks I found it was faster to store them on filesystem and let apache serve them directly.

I rarely store images like that locally now; I just toss them onto Amazon S3.

When I did have to store lots of images locally , I found this to be the best method:

1. The Postgres record for the image is given a unique and random hash as a hexdigest
2. The Image is saved onto a filesystem into a directory mapped by the hexdigest

for example, there might be something like this:

Postgres:
id | filename | hash
001 | image.jpg | abcdef123

Filesystem
abc/def/123/abcdef123-image.jpg

nginx/apache rewrite rule :
abcdef123-image.jpg -> abc/def/123/abcdef123-image.jpg

the reason for this has to do with the performance of various filesystems and issues with the distribution of digits in a sequence. it ties into Benford's Law ( http://en.wikipedia.org/wiki/Benford's_law ) as well.

a handful of filesystems exhibit decreased performance as the number of items in a directory increases. a few years ago, 1k-4k items was a safe max -- but at 10x that some filesystems really slowed. i think most modern filesystems are still quick at the 5-10k range.

a hash has more characters and a more normal distribution than a series of numbers or natural language filenames.

and if you group a hexdigest into triplets , you get 4096 max files/folders in a directory which is a decent sweet spot
16 * 16 * 16 = 4096

i haven't had to deal with this sort of stuff in almost 10 years now. but archiving content like this back then was a considerable improvement to filesystem performance and web serving.