How to stop postgres stats collector properly? - postgresql

I tried to profile plpgsql functions.
And I use pg_stat_statements to do that.
I can do profile, but after that I can not restart postgresql.
Result of ps -aux is below
postgres 22129 0.0 0.1 1276896 37276 ? Ss 07:13 0:00 postgres: dbname: checkpointer process
postgres 22134 0.0 0.0 178008 4360 ? Ss 07:13 0:00 postgres: dbname: stats collector process
postgres 23030 18.5 0.9 1436048 316336 ? Ss 07:17 4:53 postgres: dbname: postgres sakura 127.0.0.1(56496) idle waiting for D1E/CEA4D9C8
I think stats process may be lock the server to restart.
I think it's locking to stop server, because after that I have to wait long time to only shutdown the PC. May be shutdown process is waiting until stats process stop.
Is there any way to stop stats process properly?
Or, the way to force top stats process?
I tried kill -9, but it does not help.

Related

Unable to create database - psql: FATAL: role "dyadav" does not exist - Mac OS

I installed postgres using brew.
DYadav-MBP-2:~ dyadav$ brew install postgresql#9.6
Below is my .bash_profile file.
DYadav-MBP-2:~ dyadav$ cat .bash_profile
export JAVA_HOME=$(/usr/libexec/java_home)
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
export PATH=/usr/local/opt/postgresql#9.6/bin/:$PATH
I have also added 127.0.0.1 postgres to /etc/hosts.
Started postgres using below's command:
brew services start postgresql#9.6
But when I run psql postgres, I get "psql: FATAL: role "dyadav" does not exist"
I following many solutions provided in stack overflow, nothing really helped.
fyi: postgres is running on user dyadav.
DYadav-MBP-2:~ dyadav$ ps aux | grep postgres
dyadav 2338 0.0 0.0 4340528 896 ?? Ss 8:12AM 0:00.05 postgres: stats collector process
dyadav 2336 0.0 0.0 4485592 1968 ?? Ss 8:12AM 0:00.04 postgres: autovacuum launcher process
dyadav 2335 0.0 0.0 4485528 1000 ?? Ss 8:12AM 0:00.05 postgres: wal writer process
dyadav 2334 0.0 0.0 4485528 2020 ?? Ss 8:12AM 0:00.08 postgres: writer process
dyadav 2332 0.0 0.0 4485528 1072 ?? Ss 8:12AM 0:00.00 postgres: checkpointer process
dyadav 2302 0.0 0.1 4485808 17252 ?? S 8:12AM 0:00.06 /usr/local/opt/postgresql#9.6/bin/postgres -D /usr/local/var/postgresql#9.6
dyadav 4641 0.0 0.0 4268056 804 s000 S+ 9:41AM 0:00.00 grep postgres
psql assumes you want to connect to the database as the current operating system user. So psql postgres tries to connect to a database named postgres using the database user dyadav.
As you want to create a database you need to connect as the superuser postgres (a database user, not an operating system user):
psql -d postgres -U postgres
-d specifies the database to connect to, -U specifies the database user to use.
Then you can run the create database command to create another database.

Web2py Scheduler is creating "Idle in Transaction" process with PostgreSQL

This question is specific to scheduler in Web2py Framework.
I am using scheduler as a Linux Service.
I created a file /etc/systemd/system/web2py-sched.service
[Unit]
Description=Web2Py scheduler service
[Service]
ExecStart=/usr/bin/python /home/www-data/web2py/web2py.py -K myApp
Type=simple
Restart=always
[Install]
WantedBy=multi-user.target
Then install the service calling:
sudo systemctl enable /etc/systemd/system/web2py-sched.service
systemctl start /etc/systemd/system/web2py-sched
When I checked my Postgres status, I see there are processes with "Idle in Transaction" status.
may#myApp:/home/www-data/web2py/scmyAppts$ sudo systemctl status postgresql#9.5-main
● postgresql#9.5-main.service - PostgreSQL Cluster 9.5-main
Loaded: loaded (/lib/systemd/system/postgresql#.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2018-10-16 15:39:25 PDT; 1 weeks 1 days ago
Main PID: 28202 (postgres)
CGroup: /system.slice/system-postgresql.slice/postgresql#9.5-main.service
├─10956 postgres: may mydb ::1(60654) idle in transaction
├─10957 postgres: may mydb ::1(60656) idle in transaction
├─13421 postgres: may mydb 10.5.77.35(51998) idle
├─13422 postgres: may mydb 10.5.77.35(52004) idle
├─13423 postgres: may postgres 10.5.77.35(52005) idle
├─13425 postgres: may mydb 10.5.77.35(52007) idle
├─13433 postgres: may mydb 10.5.77.35(52332) idle
├─28202 /usr/lib/postgresql/9.5/bin/postgres -D /var/lib/postgresql/9.5/main -c config_file=/etc/postgresql/9.5/main/postgresql.conf
├─28204 postgres: checkpointer process
├─28205 postgres: writer process
├─28206 postgres: wal writer process
├─28207 postgres: autovacuum launcher process
├─28208 postgres: stats collector process
├─30475 postgres: may mydb ::1(59838) idle
├─30476 postgres: may mydb ::1(59840) idle in transaction
└─30500 postgres: may mydb ::1(59842) idle
When I observed few of process are idle and transaction and my DB Queries are timing out, I checked to see which table is acquiring locks.
SELECT relation, pid, mode, granted, relname
FROM pg_locks
INNER JOIN pg_stat_user_tables
ON pg_locks.relation = pg_stat_user_tables.relid
WHERE pg_locks.pid='30476';
relation pid mode granted relname
16553 30476 AccessShareLock TRUE scheduler_task
Please help me on this, I have thoroughly gone thru my scheduler code and there is db.commit() called after every transaction.
Version:
Version
2.17.2-stable+timestamp.2018.10.06.18.54.02
(Running on nginx/1.10.3, Python 2.7.12)
web2py is up to date
Is your app really called 'myApp' as you configured by the service start ExecStart=/usr/bin/python /home/www-data/web2py/web2py.py -K myApp?
You have to specify your application by name. 'myapp' is just a placeholder.
Best regards Clemens

How to start postgreSQL9.4 server installed by homebrew

I install postgresql9.4.5 by homebrew.
Before 9.4.5, I used 9.3.9 by macport, but I want to use only homebrew.
So I uninstall 9.3.9 and macport and install 9.4.5 by homebrew.
I could success "initdb /usr/local/var/postgres".
But, when I enter "postgres -D /usr/local/var/postgres", show error.
The error
LOG: could not bind IPv6 socket: Address already in use
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG: could not bind IPv4 socket: Address already in use
HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
WARNING: could not create listen socket for "localhost"
FATAL: could not create any TCP/IP sockets
I tyied to start server by manual
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
But, pg_ctl -D /usr/local/var/postgres status show pg_ctl: no server running.
I confirmed pg_hba.conf and it shows
# IPv4 local connections:
host all all 127.0.0.1/32 trust
Also, when I enter psql, shows the error psql: FATAL: could not open relation mapping file "global/pg_filenode.map": No such file or directory
What do I need to do? Please tell me.
Add a postscript
I also tried ps aux | grep postgres
postgres 276 0.0 0.0 2470236 452 ?? Ss 火12AM 0:03.18 postgres: stats collector process
postgres 275 0.0 0.0 2614684 1160 ?? Ss 火12AM 0:11.63 postgres: autovacuum launcher process
postgres 274 0.0 0.0 2614552 416 ?? Ss 火12AM 0:00.79 postgres: wal writer process
postgres 273 0.0 0.0 2614552 440 ?? Ss 火12AM 0:00.81 postgres: writer process
postgres 272 0.0 0.0 2614552 596 ?? Ss 火12AM 0:00.07 postgres: checkpointer process
postgres 242 0.0 0.0 2614552 1184 ?? S 火12AM 0:01.07 /opt/local/lib/postgresql93/bin/postgres -D /opt/local/var/db/postgresql93/defaultdb
root 75 0.0 0.0 2469228 884 ?? Ss 火12AM 0:00.03 /opt/local/bin/daemondo --label=postgresql93-server --start-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql93-server/postgresql93-server.wrapper start ; --stop-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql93-server/postgresql93-server.wrapper stop ; --restart-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql93-server/postgresql93-server.wrapper restart ; --pid=none
This shows some process is active. And, 2 lines show macport pass. But, I had deleted /opt/local folder.
I killed these process and ps aux | grep postgres did not show anything.
So, I tried postgres -D /usr/local/var/postgres. Next time, this shows
LOG: database system was shut down at 2015-11-19 18:45:38 JST
LOG: MultiXact member wraparound protections are now enabled
LOG: database system is ready to accept connections
LOG: autovacuum launcher started
And, the terminal stopped and did not show prompt. So I had to enter 'control + c'
But, I could start postgres manually. I did this command pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
And I could do psql commands.

Postgresql Logger Process

I'm trying to determine if Postgres 9.3 still has a logger process. It isn't referenced anywhere in the "PostgreSQL 9.3.4 Documentation". And I can't find it in my cluster's process list (see below). Also, does anyone know of a good general overview the memory structures in 9.3?
postgres 21397 1 0 20:51 pts/1 00:00:00 /opt/PostgreSQL/9.3/bin/postgres
postgres 21399 21397 0 20:51 ? 00:00:00 postgres: checkpointer process
postgres 21400 21397 0 20:51 ? 00:00:00 postgres: writer process
postgres 21401 21397 0 20:51 ? 00:00:00 postgres: wal writer process
postgres 21402 21397 0 20:51 ? 00:00:00 postgres: autovacuum launcher process
postgres 21403 21397 0 20:51 ? 00:00:00 postgres: archiver process last was 0001000004000092
postgres 21404 21397 0 20:51 ? 00:00:00 postgres: stats collector process
Thanks
Jim
Postgres has a logging collector process which is controlled through a config parameter,
logging_collector.
So in your postgresql.conf file, you would make sure this is set:
logging_collector = on
The blurb on this param from the postgres doc:
This parameter enables the logging collector, which is a background
process that captures log messages sent to stderr and redirects them
into log files. This approach is often more useful than logging to
syslog, since some types of messages might not appear in syslog
output. (One common example is dynamic-linker failure messages;
another is error messages produced by scripts such as
archive_command.) This parameter can only be set at server start.
It will show up in the process list with the following description:
postgres: logger process
For more info: http://www.postgresql.org/docs/current/static/runtime-config-logging.html
Regarding the memory structures, I'm not sure offhand, but would recommend you post that as a separate question.

Postgresql 9.0 is running in bg by default

I found that postgresql v9.0.7 is running in bg mode by default when it is started ( Command below ). Earlier when we were using postgresql v7.4.2 it used to run in foreground mode.
I am using freebsd 8.2 64 bit environment. Postgresql v9.0.7 is built with WITHOUT_XML, WITHOUT_GETTEXT and WITHOUT_GNUGEOPT options set.
c670e04:rkananth 16] /usr/local/bin/postgres --version
postgres (PostgreSQL) 9.0.7
c670e04:rkananth 7] setenv PGDATA /data/db/postgres/
c670e04:rkananth 11] /data/bin/runas pgsql /usr/local/bin/postgres
c670e04:rkananth 12] ps -A | grep post
65165 ?? Ss 0:00.01 /usr/local/bin/postgres
65167 ?? Ss 0:00.00 postgres: writer process (postgres)
65168 ?? Ss 0:00.00 postgres: wal writer process (postgres)
65169 ?? Ss 0:00.00 postgres: autovacuum launcher process (postgres)
65170 ?? Ss 0:00.00 postgres: stats collector process (postgres)
65224 0 S+ 0:00.00 grep post
c670e04:rkananth 14] ps -p 65165 -o ppid
PPID
1
c670e04:rkananth 15] ps 1
PID TT STAT TIME COMMAND
1 ?? SLs 0:00.17 /sbin/init --
c670e04:rkananth 16]
Are there any way where I can force it to come foreground?
Also looking at the above log, it shows that the server process's parent is set to 1 ( init) is it expected behavior when server runs in bg mode ?
However documentation for 9.0.7 says by default it should come in foreground. Why is this not happening in my case, may be a bug?
Update 1:
/data/bin/runas is not introducing any behavior change, tested without it. Same behavior is is also seen in v8.3 and on Freebsd 7.2 32-bit environment.
Server process becoming child of PID 1 ( init ) looks like a standard behavior of any application runs as a daemon.
My question now is - Is there a way where I can instruct Postgres not to run as Daemon ?
Update 2:
Got the solution it is the config "silent_mode = off" (By default it is on) will make it to run in foreground and process runs as child process of the shell. With this the question is answered.
( Source - http://postgresql.1045698.n5.nabble.com/BUG-4381-Postgresql-daemon-won-t-stay-in-the-foreground-td2127518.html )
Indeed, postgres should stay in the foreground, as stated in the manpage:
By default postgres starts in the foreground and prints log
messages to the standard error stream. In practical applications
postgres should be started as a background process, perhaps at
boot time.
Most likely the runas command is what daemonizes the postgres process. Try running postgres from a shell started as pgsql user:
root# su - pgsql
pgsql$ setenv PGDATA /data/db/postgres/
pgsql$ /usr/local/bin/postgres
You can also try with su(1) and sudo(8) if you don't like the idea of a shell as pgsql user.
Now I'm wondering why you need postgres to run in the foreground. Do you really have a good reason?