0%

执行建库脚本时需要drop数据库,那么问题来了,脚本出现如下错误提示:

1
2
3
4
5
6
ERROR: database "reis" is being accessed by other users
DETAIL: There are 5 other sessions using the database.
ERROR: tablespace "ts_reis" is not empty
ERROR: role "reis" cannot be dropped because some objects depend on it
DETAIL: owner of database reis
owner of tablespace ts_reis

也就是该库上还有会话存在,drop请求被拒绝。那么把活动连接drop掉就可以了吧。

可以使用pg_terminate_backend()函数和pg_stat_activity视图来终止数据库上的活动连接。
先查看下pg_stat_activity视图的详细信息:
[sql]
$ psql -U postgres -h localhost -d postgres
postgres=# \d pg_stat_activity ;

View “pg_catalog.pg_stat_activity”
Column Type Modifiers
——————+————————–+———–
datid oid
datname name
pid integer
usesysid oid
usename name
application_name text
client_addr inet
client_hostname text
client_port integer
backend_start timestamp with time zone
xact_start timestamp with time zone
query_start timestamp with time zone
state_change timestamp with time zone
waiting boolean
state text
query text

[/sql]

\d+ 命令可以获取表或视图更详细的信息。

然后就有了下面的sql语句来终止活动连接:

1
2
3
4
5
6
7
8
9
SELECT 
pg_terminate_backend(pid)
FROM
pg_stat_activity
WHERE
-- 不终止当前连接
pid <> pg_backend_pid()
-- 只终止target_database上的连接
AND datname = 'target_database';

世界一下子就清净了。

===
[erq]

升级yosemite后,启动postgesql时报以下错误:

1
2
3
4
...
FATAL: could not open directory "pg_twophase": No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

据说可能是因为yosemite删除了pg的一些空目录导致的,所以解决方案也十分简单:

如果/usr/local/var/postgres/目录下没有pg_tblspc,pg_twophase和pg_stat_tmp子目录,直接新建这几个目录即可。

然后可以正常启动postgresql

===
[erq]

debian配置默认文本编辑器sudo update-alternatives --config editor

有时候打开文件编辑完了才发现没有权限写当前文件,退出重新编辑?不用!vim里面调用外部命令写时使用sudo就可以了。

:w 命令如果不提供参数,则将当前缓冲区写到当前编辑的文件内,但是如果提供参数,比如
:w new_file 则将当前缓冲区内容写到新文件new_file中,其实:w命令有很多种形式
更进一步

1
2
:\[range\]w\[rite\] \[++opt\] !{cmd}
Execute {cmd} with \[range\] lines as standard input

上面的命令形式,将range范围内的缓冲区作为标准输入调用cmd命令。

而tee命令读取标准输入,然后将其写到文件和标准输出中,在vim中%代表当前编辑缓冲区的文件名,从而有了下面的命令:

1
:w !sudo tee %

但tee命令还会向standard output输出内容,可以将其重定向到/dev/null

1
:w !sudo tee % > /dev/null

还可以利用cat命令来达到同样的目的:

1
:w !sudo sh -c "cat > %"

===
[erq]

无需第三方工具,proc文件系统里面的/proc/cpuinfo提供了丰富的cpu信息。
其输出类似如下:

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
processor : 0
vendor_id : AuthenticAMD
cpu family : 16
model : 4
model name : Quad-Core AMD Opteron(tm) Processor 8382
stepping : 2
microcode : 0x1000086
cpu MHz : 2611.977
cache size : 512 KB
physical id : 0
siblings : 4
core id : 0
cpu cores : 4
apicid : 4
initial apicid : 0
fpu : yes
fpu_exception : yes
cpuid level : 5
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm 3dnowext 3dnow constant_tsc rep_good nopl nonstop_tsc extd_apicid pni monitor cx16 popcnt lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt hw_pstate npt lbrv svm_lock nrip_save
bogomips : 5223.95
TLB size : 1024 4K pages
clflush size : 64
cache_alignment : 64
address sizes : 48 bits physical, 48 bits virtual
power management: ts ttp tm stc 100mhzsteps hwpstate

型号和逻辑CPU个数

1
2
$ cat /proc/cpuinfo grep 'model name' cut -f2 -d: uniq -c
32 Quad-Core AMD Opteron(tm) Processor 8382

可以看到有32个逻辑CPU,后面是型号

物理CPU个数

1
2
3
4
5
6
7
8
9
$ cat /proc/cpuinfo grep 'physical id' uniq -d
physical id : 0
physical id : 1
physical id : 2
physical id : 3
physical id : 4
physical id : 5
physical id : 6
physical id : 7

或者

1
2
$ cat /proc/cpuinfo grep 'physical id' uniq -d cut -f1 -d: uniq -c
8 physical id

可以看到有8颗物理CPU

每颗CPU核心数

1
2
$ cat /proc/cpuinfo grep 'cpu cores' uniq
cpu cores : 4

或者

1
2
3
4
5
$ cat /proc/cpuinfo grep 'core id' sort uniq
core id : 0
core id : 1
core id : 2
core id : 3

8*4刚好是32颗逻辑CPU,如果有超线程技术则不是简单的相乘就可以,还要乘以每个核心的线程数。

还有一个命令lscpu,可以总览系统cpu概况:

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
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 32
On-line CPU(s) list: 0-31
Thread(s) per core: 1
Core(s) per socket: 4
Socket(s): 8
NUMA node(s): 4
Vendor ID: AuthenticAMD
CPU family: 16
Model: 4
Stepping: 2
CPU MHz: 2611.977
BogoMIPS: 5224.55
Virtualization: AMD-V
L1d cache: 64K
L1i cache: 64K
L2 cache: 512K
L3 cache: 6144K
NUMA node0 CPU(s): 0-3
NUMA node1 CPU(s): 4-7
NUMA node2 CPU(s): 8-11
NUMA node3 CPU(s): 12-31

===
[erq]

gnome自带的菜单编辑工具包名字叫alacarte,这单词来自法语,意思是“照菜单点菜”。

有兽因为依赖问题有些包会无法正常卸载,提示一堆错误。这时候可以强制卸载。

apt包管理器的数据库位于/var/lib/dpkg目录。假设需要强制卸载的包名字为foo,执行以下命令:

1
2
3
# cd /var/lib/dpkg/info
# rm foo.*
# aptitude purge foo (或许# dpkg purge foo亦可,未尝试)

包所属的文件可以直接从硬盘上删除

===
[erq]

windows 2008 r2默认是开启网络防火墙的,因此oracle的服务默认端口1521也是被阻止的。费了好大劲连不上才发现这个问题,可以这样解决,用命令行:C:\Users\Administrator>netsh firewall set portopening TCP 1521 "ORACLE",或者图形化设置:服务器管理器->配置->高级安全windows防火墙->入站规则->新建规则来配置TCP 1521端口

windows 2008 R2上安装oracle 10g 10.2.0.4时会提示”检查操作系统版本:必须是5.0,5.1,5.2 or 6.0。实际为6.1。未通过。”,修改安装目录下的文件install/oraparam.ini,其中Windows=5.0,5.1,5.2,6.0一行最后添加”,6.1”,安装过程中的其他依赖检查错误通过勾选都设置为”用户已验证”即可。