#include "access/tupdesc.h"
 #include "catalog/catalog.h"
 #include "catalog/heap.h"
+#include "catalog/dependency.h"
 #include "catalog/pg_attribute.h"
 #include "catalog/pg_class.h"
 #include "catalog/pg_inherits_fn.h"
    char        relkind = get_rel_relkind(relOid);
    char       *scontext = sepgsql_get_client_label();
    char       *tcontext;
+   char       *audit_name;
    Bitmapset  *columns;
    int         index;
    bool        result = true;
     * Check permissions on the relation
     */
    tcontext = sepgsql_get_label(RelationRelationId, relOid, 0);
+   audit_name = getObjectDescriptionOids(RelationRelationId, relOid);
    switch (relkind)
    {
        case RELKIND_RELATION:
                                         tcontext,
                                         SEPG_CLASS_DB_TABLE,
                                         required,
-                                        get_rel_name(relOid),
+                                        audit_name,
                                         abort);
-           if (!result)
-               return false;
            break;
 
        case RELKIND_SEQUENCE:
                                             tcontext,
                                             SEPG_CLASS_DB_SEQUENCE,
                                             SEPG_DB_SEQUENCE__GET_VALUE,
-                                            get_rel_name(relOid),
+                                            audit_name,
                                             abort);
-           return result;
+           break;
 
        case RELKIND_VIEW:
            result = sepgsql_check_perms(scontext,
                                         tcontext,
                                         SEPG_CLASS_DB_VIEW,
                                         SEPG_DB_VIEW__EXPAND,
-                                        get_rel_name(relOid),
+                                        audit_name,
                                         abort);
-           return result;
+           break;
 
        default:
            /* nothing to be checked */
-           return true;
+           break;
    }
+   pfree(tcontext);
+   pfree(audit_name);
+
+   /*
+    * Only columns owned by relations shall be checked
+    */
+   if (relkind != RELKIND_RELATION)
+       return true;
 
    /*
     * Check permissions on the columns
    {
        AttrNumber  attnum;
        uint32      column_perms = 0;
-       char        audit_name[NAMEDATALEN * 2 + 10];
+       ObjectAddress   object;
 
        if (bms_is_member(index, selected))
            column_perms |= SEPG_DB_COLUMN__SELECT;
        /* obtain column's permission */
        attnum = index + FirstLowInvalidHeapAttributeNumber;
        tcontext = sepgsql_get_label(RelationRelationId, relOid, attnum);
-       snprintf(audit_name, sizeof(audit_name), "%s.%s",
-                get_rel_name(relOid), get_attname(relOid, attnum));
+
+       object.classId = RelationRelationId;
+       object.objectId = relOid;
+       object.objectSubId = attnum;
+       audit_name = getObjectDescription(&object);
 
        result = sepgsql_check_perms(scontext,
                                     tcontext,
                                     column_perms,
                                     audit_name,
                                     abort);
+       pfree(tcontext);
+       pfree(audit_name);
+
        if (!result)
            return result;
    }
 
  table   | t3      | system_u:object_r:sepgsql_fixed_table_t:s0
  table   | t4      | system_u:object_r:sepgsql_secret_table_t:s0
  table   | t5      | system_u:object_r:sepgsql_table_t:s0
- column  | t5.g    | system_u:object_r:sepgsql_secret_table_t:s0
- column  | t5.f    | system_u:object_r:sepgsql_ro_table_t:s0
  column  | t5.e    | system_u:object_r:sepgsql_table_t:s0
+ column  | t5.f    | system_u:object_r:sepgsql_ro_table_t:s0
+ column  | t5.g    | system_u:object_r:sepgsql_secret_table_t:s0
 (8 rows)
 
 -- Hardwired Rules
 UPDATE pg_attribute SET attisdropped = true
     WHERE attrelid = 't5'::regclass AND attname = 'f'; -- failed
-ERROR:  selinux: hardwired security policy violation
+ERROR:  SELinux: hardwired security policy violation
 --
 -- Simple DML statements
 --
 
 SECURITY LABEL ON TABLE t1
     IS 'system_u:object_r:sepgsql_ro_table_t:s0';  -- ok
 SECURITY LABEL ON TABLE t2
-    IS 'invalid seuciryt context';         -- be failed
-ERROR:  invalid security label: "invalid seuciryt context"
+    IS 'invalid security context';         -- be failed
+ERROR:  SELinux: invalid security label: "invalid security context"
 SECURITY LABEL ON COLUMN t2
     IS 'system_u:object_r:sepgsql_ro_table_t:s0';  -- be failed
 ERROR:  improper relation name (too many dotted names): 
 
 -- Regression Test for Misc Permission Checks
 --
 LOAD '$libdir/sepgsql';        -- failed
-ERROR:  SELinux: LOAD is not allowed anyway.
+ERROR:  SELinux: LOAD is not permitted
 
    if (getpeercon_raw(port->sock, &context) < 0)
        ereport(FATAL,
                (errcode(ERRCODE_INTERNAL_ERROR),
-                errmsg("SELinux: unable to get peer label")));
+                errmsg("SELinux: unable to get peer label: %m")));
 
    sepgsql_set_client_label(context);
 
    if (getcon_raw(&context) < 0)
        ereport(ERROR,
                (errcode(ERRCODE_INTERNAL_ERROR),
-                errmsg("SELinux: failed to get server security label")));
+                errmsg("SELinux: failed to get server security label: %m")));
    sepgsql_set_client_label(context);
 
    /* Security label provider hook */
 
        if (security_get_initial_context_raw("unlabeled", &unlabeled) < 0)
            ereport(ERROR,
                    (errcode(ERRCODE_INTERNAL_ERROR),
-                    errmsg("SELinux: failed to get initial security label")));
+                    errmsg("SELinux: failed to get initial security label: %m")));
        PG_TRY();
        {
            label = pstrdup(unlabeled);
                                     &raw_label) < 0)
        ereport(ERROR,
                (errcode(ERRCODE_INTERNAL_ERROR),
-                errmsg("SELinux: could not translate security label")));
+                errmsg("SELinux: could not translate security label: %m")));
 
    PG_TRY();
    {
                                     &qual_label) < 0)
        ereport(ERROR,
                (errcode(ERRCODE_INTERNAL_ERROR),
-                errmsg("SELinux: could not translate security label")));
+                errmsg("SELinux: could not translate security label: %m")));
 
    PG_TRY();
    {
    PG_RETURN_TEXT_P(cstring_to_text(result));
 }
 
+/*
+ * quote_object_names
+ *
+ * It tries to quote the supplied identifiers
+ */
+static char *
+quote_object_name(const char *src1, const char *src2,
+                 const char *src3, const char *src4)
+{
+   StringInfoData  result;
+   const char     *temp;
+
+   initStringInfo(&result);
+
+   if (src1)
+   {
+       temp = quote_identifier(src1);
+       appendStringInfo(&result, "%s", temp);
+       if (src1 != temp)
+           pfree((void *)temp);
+   }
+   if (src2)
+   {
+       temp = quote_identifier(src2);
+       appendStringInfo(&result, ".%s", temp);
+       if (src2 != temp)
+           pfree((void *)temp);
+   }
+   if (src3)
+   {
+       temp = quote_identifier(src3);
+       appendStringInfo(&result, ".%s", temp);
+       if (src3 != temp)
+           pfree((void *)temp);
+   }
+   if (src4)
+   {
+       temp = quote_identifier(src4);
+       appendStringInfo(&result, ".%s", temp);
+       if (src4 != temp)
+           pfree((void *)temp);
+   }
+   return result.data;
+}
+
 /*
  * exec_object_restorecon
  *
        Form_pg_class       relForm;
        Form_pg_attribute   attForm;
        Form_pg_proc        proForm;
-       char                objname[NAMEDATALEN * 4 + 10];
+       char               *objname;
        int                 objtype = 1234;
        ObjectAddress       object;
        security_context_t  context;
                nspForm = (Form_pg_namespace) GETSTRUCT(tuple);
 
                objtype = SELABEL_DB_SCHEMA;
-               snprintf(objname, sizeof(objname), "%s.%s",
-                        database_name, NameStr(nspForm->nspname));
+
+               objname = quote_object_name(database_name,
+                                           NameStr(nspForm->nspname),
+                                           NULL, NULL);
 
                object.classId = NamespaceRelationId;
                object.objectId = HeapTupleGetOid(tuple);
                    continue;   /* no need to assign security label */
 
                namespace_name = get_namespace_name(relForm->relnamespace);
-               snprintf(objname, sizeof(objname), "%s.%s.%s",
-                        database_name, namespace_name,
-                        NameStr(relForm->relname));
+               objname = quote_object_name(database_name,
+                                           namespace_name,
+                                           NameStr(relForm->relname),
+                                           NULL);
                pfree(namespace_name);
 
                object.classId = RelationRelationId;
                namespace_id = get_rel_namespace(attForm->attrelid);
                namespace_name = get_namespace_name(namespace_id);
                relation_name = get_rel_name(attForm->attrelid);
-               snprintf(objname, sizeof(objname), "%s.%s.%s.%s",
-                        database_name, namespace_name,
-                        relation_name, NameStr(attForm->attname));
-               pfree(relation_name);
+               objname = quote_object_name(database_name,
+                                           namespace_name,
+                                           relation_name,
+                                           NameStr(attForm->attname));
                pfree(namespace_name);
+               pfree(relation_name);
 
                object.classId = RelationRelationId;
                object.objectId = attForm->attrelid;
                objtype = SELABEL_DB_PROCEDURE;
 
                namespace_name = get_namespace_name(proForm->pronamespace);
-               snprintf(objname, sizeof(objname), "%s.%s.%s",
-                        database_name, namespace_name,
-                        NameStr(proForm->proname));
+               objname = quote_object_name(database_name,
+                                           namespace_name,
+                                           NameStr(proForm->proname),
+                                           NULL);
                pfree(namespace_name);
 
                object.classId = ProcedureRelationId;
 
            default:
                elog(ERROR, "unexpected catalog id: %u", catalogId);
+               objname = NULL;     /* for compiler quiet */
                break;
        }
 
        else
            ereport(ERROR,
                    (errcode(ERRCODE_INTERNAL_ERROR),
-                    errmsg("SELinux: could not determine initial security label for %s (type=%d)", objname, objtype)));
+                    errmsg("SELinux: could not determine initial security label for %s (type=%d): %m", objname, objtype)));
+
+       pfree(objname);
    }
    systable_endscan(sscan);
 
    if (!sehnd)
        ereport(ERROR,
                (errcode(ERRCODE_INTERNAL_ERROR),
-                errmsg("SELinux: failed to initialize labeling handle")));
+                errmsg("SELinux: failed to initialize labeling handle: %m")));
    PG_TRY();
    {
        /*
 
 #include "access/genam.h"
 #include "access/heapam.h"
 #include "access/sysattr.h"
+#include "catalog/dependency.h"
 #include "catalog/indexing.h"
 #include "catalog/pg_namespace.h"
 #include "catalog/pg_proc.h"
    char       *tcontext;
    char       *audit_name;
 
-   audit_name = get_func_name(functionId);
+   audit_name = getObjectDescriptionOids(ProcedureRelationId, functionId);
 
    /*
     * check db_procedure:{setattr relabelfrom} permission
 
 #include "access/heapam.h"
 #include "access/sysattr.h"
 #include "catalog/indexing.h"
+#include "catalog/dependency.h"
 #include "catalog/pg_attribute.h"
 #include "catalog/pg_class.h"
 #include "catalog/pg_namespace.h"
 {
    char       *scontext = sepgsql_get_client_label();
    char       *tcontext;
-   char        audit_name[NAMEDATALEN * 2 + 10];
+   char       *audit_name;
+   ObjectAddress   object;
 
    if (get_rel_relkind(relOid) != RELKIND_RELATION)
        ereport(ERROR,
                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                 errmsg("cannot set security label on non-regular columns")));
 
-   snprintf(audit_name, sizeof(audit_name), "%s.%s",
-            get_rel_name(relOid), get_attname(relOid, attnum));
+   object.classId = RelationRelationId;
+   object.objectId = relOid;
+   object.objectSubId = attnum;
+   audit_name = getObjectDescription(&object);
 
    /*
     * check db_column:{setattr relabelfrom} permission
                        SEPG_DB_COLUMN__RELABELFROM,
                        audit_name,
                        true);
-   pfree(tcontext);
 
    /*
     * check db_column:{relabelto} permission
                        SEPG_DB_PROCEDURE__RELABELTO,
                        audit_name,
                        true);
+
+   pfree(tcontext);
+   pfree(audit_name);
 }
 
 /*
                 errmsg("cannot set security labels on relations except "
                        "for tables, sequences or views")));
 
-   audit_name = get_rel_name(relOid);
+   audit_name = getObjectDescriptionOids(RelationRelationId, relOid);
 
    /*
     * check db_xxx:{setattr relabelfrom} permission
                        SEPG_DB_TABLE__RELABELFROM,
                        audit_name,
                        true);
-   pfree(tcontext);
 
    /*
     * check db_xxx:{relabelto} permission
                        SEPG_DB_TABLE__RELABELTO,
                        audit_name,
                        true);
+
+   pfree(tcontext);
+   pfree(audit_name);
 }
 
  */
 #include "postgres.h"
 
+#include "catalog/dependency.h"
 #include "catalog/pg_namespace.h"
 #include "commands/seclabel.h"
 #include "utils/lsyscache.h"
    char       *tcontext;
    char       *audit_name;
 
-   audit_name = get_namespace_name(namespaceId);
+   audit_name = getObjectDescriptionOids(NamespaceRelationId, namespaceId);
 
    /*
     * check db_schema:{setattr relabelfrom} permission
 
    appendStringInfo(&buf, " scontext=%s tcontext=%s tclass=%s",
                     scontext, tcontext, class_name);
    if (audit_name)
-       appendStringInfo(&buf, " name=%s", audit_name);
+       appendStringInfo(&buf, " name=\"%s\"", audit_name);
 
    ereport(LOG, (errmsg("SELinux: %s", buf.data)));
 }
        ereport(ERROR,
                (errcode(ERRCODE_INTERNAL_ERROR),
                 errmsg("SELinux could not compute av_decision: "
-                       "scontext=%s tcontext=%s tclass=%s",
+                       "scontext=%s tcontext=%s tclass=%s: %m",
                        scontext, tcontext, tclass_name)));
 
    /*
        ereport(ERROR,
                (errcode(ERRCODE_INTERNAL_ERROR),
                 errmsg("SELinux could not compute a new context: "
-                       "scontext=%s tcontext=%s tclass=%s",
+                       "scontext=%s tcontext=%s tclass=%s: %m",
                        scontext, tcontext, tclass_name)));
 
    /*
 
 SECURITY LABEL ON TABLE t1
     IS 'system_u:object_r:sepgsql_ro_table_t:s0';  -- ok
 SECURITY LABEL ON TABLE t2
-    IS 'invalid seuciryt context';         -- be failed
+    IS 'invalid security context';         -- be failed
 SECURITY LABEL ON COLUMN t2
     IS 'system_u:object_r:sepgsql_ro_table_t:s0';  -- be failed
 SECURITY LABEL ON COLUMN t2.b