Question

How can I tell specific Postgres processes not to consume all CPU?

Asked by: mahome

I have a Webapplication with Java and Hibernate using a Postgres DB.
On my server are two apps running:
1. The Webapplication
2. An Import for the Webapplication, also with Java and Hibernate
both are using the same Postgres DB.

The problem is that while the import is running, postgres consumes 100 % CPU and the Webapp is to slow. How can I specify that the Postgres activities of the import-app should not consume all the CPU, so that the web-app can work fast?

Thanks in advance.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-03-17 at 01:05:47ID24236744
Tags

postgres

,

cpu

,

java

,

process

,

linux

,

ubuntu

,

hibernate

Topics

PostgreSQL Database

,

Java Programming Language

,

Linux

Participating Experts
5
Points
500
Comments
23

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Postgres
    Hi, What is the sql statement to select all the tables name for POSTGRES database. For Oracle, eg. "SELECT table_name FROM all_tables where owner='HARMONY'" so, how about POSTGRES? Thank you
  2. Postgres
    Is there a way to initiate a connection to postgres. Do my query. Return ONLY 10 results from the database to to current page. Then on another page get the next 10 results. I want the physical data transfer to a bare minimum as this will be handling thousands of queries a sec...
  3. Vacuum Postgres DB
    We were doing regular vacuum of the site before and last week our admin deleted the cron for vacuum. We were running full vacuum every night using this command: vacuumdb -d db_final -f -z Also we were doing hourly vacuum also.. vacuumdb -t poll_responses -f -z >> thi...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: woolmilkporcPosted on 2009-03-17 at 01:59:12ID: 23905743

Hi,

here is an interesting thread on that theme.

Basically, it says that renicing a backend was "somewhere between useless and counterproductive for most query loads."


http://bytes.com/groups/postgresql/423435-change-query-priority

wmp

 

by: gheistPosted on 2009-03-17 at 03:23:47ID: 23906176

What is your operating system? Usermode apps do not cooperate in CPU scheduling.

 

by: hfraserPosted on 2009-03-17 at 04:57:58ID: 23906880

Are you sure the system is CPU bound? The most common problem with database performance is I/O, in which case you will probably see high I/O wait times (the %wa column if you're using top to watch system performance).

If the problem's I/O, there are many articles on tuning postgres for performance.

If the problem's truly CPU, you'll find fewer resources. However, check to ensure the import application is using prepared queries to eliminate the planning phase on the backend.

 

by: gheistPosted on 2009-03-17 at 06:44:59ID: 23907809

Usual problem scenario is that you are out of memory and both programms attempt to use swap and that slows down rest of IO in system as it goes at maximum priority.
Uptime 10..100 can be observed at that time.
Solution - restrict program's memory usage like -server -Xmx for Java or use some pooling proxy for Postgresql do that less connection serving processes run routinely.

 

by: stephenhoekstraPosted on 2009-03-17 at 14:35:48ID: 23913426

Hi,

You could make use of 'nice' which manipulates process priority.  The higher the nice value, the lower it's priority, the less nice it is (per se), the higher priority it will run with.

Simply give your postgresql a higher niceness than your webapp.

Man page:
http://linux.about.com/library/cmd/blcmdl1_nice.htm

Little article on using nice:
http://blogs.techrepublic.com.com/opensource/?p=140


HTH.

 

by: earthman2Posted on 2009-03-25 at 01:49:50ID: 23977037

hardware is so cheap separate the db from the appserver and have two separate servers.

 

by: gheistPosted on 2009-03-25 at 04:04:03ID: 23977910

It looks like memory shortage firsthand. There is no danger running all apps on same system if it has good CPU scheduler, like Windows Advanced or any Linux.

 

by: mahomePosted on 2009-03-26 at 04:53:26ID: 23989281

Sorry for answering so late. We had another urgent task.

@gheist
Our operationg system is Ubuntu 8.04

@hfraser
I couldn't recreate the same situation with 100% CPU, but I have attached a snippet of vmstat of the next import.
You were right, the high values come from wa.
>>If the problem's I/O, there are many articles on tuning postgres for performance.
The only effect of tuning postgres is that the import would be faster. But what I'm aiming at, is that the import app must not consume so much ressources.

vmstat 1 3600
 
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 0  1 738440  42440   8232 1201496    0    0    64  5476  141 3544 21  1 76  2
 1  0 738440  40520   8232 1203800    0    0    64   312   76 3091 23  2 73  2
 1  0 738440  38048   8252 1206056    0    0    16  4496  124 3051 21  1 76  2
 1  0 738440  35568   8276 1208444    0    0   200  3400  132 2890 23  1 73  3
 3  0 738440  32748   8300 1211268    0    0    64     0   85 3534 22  1 75  2
 1  0 738436  29956   8324 1213688   32    0    92  3540  137 3053 20  2 75  3
 1  0 738436  27724   8336 1215968    0    0    96  3528  102 3221 21  1 75  3
 1  0 738436  24728   8352 1218808    0    0   112  3608  134 3572 21  1 76  2
 1  0 749908  49468   7656 1190776    0 6352   184  9756  131 2939 20  2 75  3
 1  0 749908  46632   7660 1193968    0    0   328    96   74 3633 23  2 74  1
 0  1 749908  44092   7672 1196404    0    0    56  5916  111 3116 23  1 75  2
 1  2 749844  29368   7712 1210096   60    0    64  6252  430 1854  6  2 57 35
 0  3 749844  29192   7712 1210100    0    0     0  2376  312 1561  2  1 45 52
 1  3 749844  29216   7712 1210100    0    0     0  2328  376 1877  5  1 35 59
 1  3 749844  29280   7712 1210100    0    0     0  2604  364 1621  5  1 37 57
 0  2 749844  28804   7712 1211104    0    0    16  2912  345 2249 15  1 40 44
 0  2 749844  28628   7712 1211116    0    0     0  2860  375 1738  5  0 56 38
 0  2 749844  28668   7712 1211116    0    0     0  3132  363 1761  2  0 53 44
 0  2 749844  28504   7712 1211116    0    0     0  2384  398 2207  1  0 56 43
 0  6 749844  25988   7756 1211072    0    0    84  2112  338 1561  1  1 44 53
 0  2 749844  25440   7776 1211340    0    0   280  2840  308 1501  1  1 48 50
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 0  2 749844  25488   7776 1211424    0    0     0  4064  367 1388  1  0 51 47
 0  2 749844  25364   7776 1211424    0    0     0  2404  387 1906  1  0 55 43
 0  2 749844  25364   7776 1211424    0    0     0  2452  358 1432  1  1 65 34
 0  2 749844  25364   7776 1211424    0    0     0  3116  324 1420  1  0 54 45
 1  0 749844  26268   7836 1212468    0    0   156  2012  295 2742  7  1 70 22
 1  0 794192  54208   7804 1159104    0 9800    72 14060  201 2999 19  3 67 11
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:

Select allOpen in new window

 

by: gheistPosted on 2009-03-26 at 05:02:29ID: 23989348

It is swapping out in 13,33 and back in 13 get more RAM or limit app memory usage.
Ask if you need help.
You cannot force postgresql into RAM, nor it is advisable to do so with java, so limiting memory consumption to chips you have is only option.

 

by: mahomePosted on 2009-03-26 at 05:07:48ID: 23989391

@gheist
>>Usual problem scenario is that you are out of memory and both programms attempt to use swap and that slows down >>rest of IO in system as it goes at maximum priority.
>>Uptime 10..100 can be observed at that time.

I didn't figure out that point completely. How can I determine that the swaps are the problem?

@stephenhoeks&
If I would nice postgres I would also make my Wepapp slower, but I only want to nice the postgres processes which come from the import-app. Is there a possibility that postgres knows from which app the query comes and therefore consumes either more or less ressources. But looking at the posting of woolmilkporc, the nice-approach seems not to work anyway.

@earthman2
Yes hardware is cheap, but we are a small company and budget is low. But we've already planned that for the future.

@gheist
Here ist the memory situation. I know, that only 134 MB are left, but as I learned so far that the cached 918 MB is also available, so everything should be OK. Please correct me if I'm not right.

# free -m
            total       used       free     shared    buffers     cached
Mem:          3986       3851        134          0          1        918
-/+ buffers/cache:       2931       1055
Swap:         8197         85       8111





 

by: mahomePosted on 2009-03-26 at 05:34:40ID: 23989582

@gheist
I've attached the memory consumption on our server. Actually we have 3 wepapps, but that wasn't important so far.
So if I understand you right, I should reduce the Xmx values to say 600m, because I think I may not reduce the value under the RSS value. Looking at the VSZ value I wonder why that process may consume over 1 GB even if only 800 MB are allowed.

You said the problem is the swapping (line 13), but the the high wa-values are from line 17 to 25. Does that fit?

ps aux
 
root  9300  0.1  8.3 1092648 340020 ? Sl   Mar13  35:50 java -Xmx800m -server webapp1
root 28243  0.7 12.7 1103288 521024 ? Sl   Mar24  19:52 java -Xmx800m -server webapp2
root 32433  0.2 10.5 1099644 428740 ? Sl   Mar19  22:58 java -Xmx800m -server webapp3
 
root 31287 27.9 12.0 1290380 493676 pts/4  Sl+  13:04   4:41 java Import

                                              
1:
2:
3:
4:
5:
6:
7:

Select allOpen in new window

 

by: gheistPosted on 2009-03-26 at 09:36:07ID: 23992319

That fits perfectly. IO is counted and then swap pages are commited to disk and system is lazy while it waits for memory management operation to complete.
Java memory management is describet alongside -XX options. BTW is your system 32bit or 64bit?
Do you have any real need to exceed 256mb od -Xmx by overriding default value? Like OOM when running webapp.

What is your application server? Why you do not run all your "webapps" inside single server?

 

by: hfraserPosted on 2009-03-26 at 18:47:01ID: 23997456

The 4 java processes together only appear to be using about 40% of the memory, and each appears to consuming about the same amount of memory, which to me doesn't look like any of them were swapped out. It's possible some other apps on the system were swappped that caused the "so" spike when the import started.

There's definitely a lot of wait state, presumably after the import begins, which isn't unexpected. You might want to check how the import's done, especially concerning locks on the table that might be there to speed the import.

 

by: gheistPosted on 2009-03-26 at 22:57:36ID: 23998335

Apps that are operating are being swapped out.
what does ipcs -m say - you may have old postgresql shm segment with no users still taking memory.

Your system is actively swapping - java, postgresql, bash, and everything else you run.

 

by: hfraserPosted on 2009-03-27 at 03:51:46ID: 23999554

Sorry. I didn't mean to suggest there isn't swapping happening, or that more memory won't help (it always does). But there are only two major swap events:  at 13 (swap out) and 33 (swap in). The "ps aux" command doesn't seem to indicate either of them related to his java apps, and the high I/O wait activity would indicate the system is waiting on disk, which I'd presume is the import process.reading and postgres reading/writing. It's highly likely that everything unrelated has been swapped out, but they might be bash, X, gnome/kde, etc. which won't affect his web apps.

It's also possible that the import process does something very intrusive, like table locks, to improve the update process speed, but at the expense of db clients.

It's all speculation, of course, since I don't know what got swapped out. Checking for processes with a RSZ of 0 and a high VSZ will show what got swapped out.

My point is just that there might be more than swapping affecting the web app's performance.

Having said that, I have a couple of machines that are pure app servers (one is a db server), and I always disable X to conserve memory (start them at runlevel 3). Disabling them for a test is certainly easier and quicker than re-writing the import code, and would be my first thing to try.

 

by: gheistPosted on 2009-03-27 at 05:04:04ID: 23999904

1000 writes/sec are normal disk interface ability.
You really have the problem on your system identified in previous posts.
I propose solution, I do not wish to continue trading about opinions. Have a nice weekend!

 

by: hfraserPosted on 2009-03-27 at 08:46:33ID: 24002238

Perhaps. But 50% wait state is a lot of disk contention for active processes doing I/O.

 

by: earthman2Posted on 2009-03-27 at 11:27:02ID: 24003995

recommend that you make the database disk partition ext2 filesystem, otherwise you get buffering by both the database and the operating system, while multiplies up the  disk io.    How are your disks configured ?  Can you split indexes onto separate disks ?

 

by: gheistPosted on 2009-03-27 at 12:12:06ID: 24004404

Problem is that highest priority IO - swapper takes precedence over any balance of I and O needed by DB.
Actually one can use ext3 options to make it easier on IO and still keep avoiding fsck.

 

by: earthman2Posted on 2009-03-30 at 00:34:51ID: 24016638

I'll let the postgresql documentation elucidate those ext3 options...

Tip:  Because WAL restores database file contents after a crash, journaled filesystems are not necessary for reliable storage of the data files or WAL files. In fact, journaling overhead can reduce performance, especially if journaling causes file system data to be flushed to disk. Fortunately, data flushing during journaling can often be disabled with a filesystem mount option, e.g. data=writeback on a Linux ext3 file system. Journaled file systems do improve boot speed after a crash.

Another tip is:
It is of advantage if the write ahead log WAL is located on another disk than the main database files. This can be achieved by moving the directory pg_xlog to another location (while the server is shut down, of course) and creating a symbolic link from the original location in the main data directory to the new location.

 

by: gheistPosted on 2009-03-30 at 04:03:30ID: 24017618

Clarification to another tip:
By "other disk" and independent disk spindle or SSD is assumed, not another filesystem on same disk.

 

by: mahomePosted on 2009-04-23 at 23:59:35ID: 31558895

Sorry I forgot to close the question. Thx for the many answers. It was difficult for me to split the points as to whole discussion was valuable.

 

by: mahomePosted on 2009-04-24 at 00:04:09ID: 24222423

I find the modifications with the filesystem very interesting but to risky on our online server. I finally improved the import script so that the high don't last so long.
For me it was important to find out that the IO (wait value) was the problem and
that it makes no sense to make a postgres process nice.

P.S:
We have a ext3 filesystem and also a software RAID 1. That could possibly explain the high wa values.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...